-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
128 lines (102 loc) · 3.89 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
describe('bearhug', function() {
it('should throw an error if node is not set', function() {
expect(init).toThrow();
function init() { bearhug({ pattern: 'abc' }); }
});
it('should throw an error if pattern is not set', function() {
expect(init).toThrow();
function init() { bearhug({ node: document.createElement('div') }); }
});
it('should allow tagName to be specified', function() {
var before = 'abcde',
after = 'a<span>bcd</span>e',
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: 'bcd', tagName: 'span' });
expect(testNode.innerHTML).toEqual(after);
});
it('should allow className to be specified', function() {
var before = 'abcde',
after = 'a<strong class="one two">bcd</strong>e',
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: 'bcd', className: 'one two' });
expect(testNode.innerHTML).toEqual(after);
});
it('should be case insensitive by default', function() {
var before = 'ABCDE',
after = 'A<strong>BCD</strong>E',
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: 'bcd' });
expect(testNode.innerHTML).toEqual(after);
});
it('should support case sensitivity', function() {
var before = 'ABCDE',
after = 'ABCDE',
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: 'bcd', caseSensitive: true });
expect(testNode.innerHTML).toEqual(after);
});
it('should support words only matching', function() {
var before = 'tone one phone',
after = 'tone <strong>one</strong> phone',
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: 'one', wordsOnly: true });
expect(testNode.innerHTML).toEqual(after);
});
it('should support matching multiple patterns', function() {
var before = 'tone one phone',
after = '<strong>tone</strong> one <strong>phone</strong>',
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: ['tone', 'phone'] });
expect(testNode.innerHTML).toEqual(after);
});
it('should support regex chars in the pattern', function() {
var before = '*.js when?',
after = '<strong>*.</strong>js when<strong>?</strong>',
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: ['*.', '?'] });
expect(testNode.innerHTML).toEqual(after);
});
it('should work on complex html structures', function() {
var before = [
'<div>abcde',
'<span>abcde</span>',
'<div><p>abcde</p></div>',
'</div>'
].join(''),
after = [
'<div><strong>abc</strong>de',
'<span><strong>abc</strong>de</span>',
'<div><p><strong>abc</strong>de</p></div>',
'</div>'
].join(''),
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: 'abc' });
expect(testNode.innerHTML).toEqual(after);
});
it('should ignore html tags and attributes', function() {
var before = '<span class="class"></span>',
after = '<span class="class"></span>',
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: ['span', 'class'] });
expect(testNode.innerHTML).toEqual(after);
});
it('should not match across tags', function() {
var before = 'a<span>b</span>c',
after = 'a<span>b</span>c',
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: 'abc' });
expect(testNode.innerHTML).toEqual(after);
});
it('should ignore html comments', function() {
var before = '<!-- abc -->',
after = '<!-- abc -->',
testNode = buildTestNode(before);
bearhug({ node: testNode, pattern: 'abc' });
expect(testNode.innerHTML).toEqual(after);
});
function buildTestNode(content) {
var node = document.createElement('div');
node.innerHTML = content;
return node;
}
});