forked from rollbar/rollbar.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.domUtility.test.js
192 lines (179 loc) · 6.36 KB
/
browser.domUtility.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* globals expect */
/* globals describe */
/* globals it */
/* globals sinon */
var d = require('../src/browser/domUtility');
function fullElement() {
return {
tagName: 'DIV',
id: 'myId',
className: 'a b c',
getAttribute: function(t) {
return {
type: 'theType',
name: 'someName',
other: 'otherAttr'
}[t];
}
};
}
function genElement(tag, id, classes, type, name) {
var elem = {
tagName: tag,
getAttribute: function(t) {
return {
type: type,
name: name,
other: 'otherAttr'
}[t];
}
};
if (id) {
elem.id = id;
}
if (classes) {
elem.className = classes;
}
return elem;
}
describe('isDescribedElement', function() {
it('should match the type without subtypes', function() {
var e = genElement('div', null, null, 'text');
expect(d.isDescribedElement(e, 'div')).to.be.ok();
expect(d.isDescribedElement(e, 'DIV')).to.be.ok();
expect(d.isDescribedElement(e, 'span')).to.not.be.ok();
});
it('should work with subtypes', function() {
var e = genElement('div', null, null, 'text');
expect(d.isDescribedElement(e, 'div', ['input', 'text'])).to.be.ok();
expect(d.isDescribedElement(e, 'div', ['input', 'nottext'])).to.not.be.ok();
expect(d.isDescribedElement(e, 'div', [])).to.not.be.ok();
});
it('should work if element has no type', function() {
var e = genElement('div');
expect(d.isDescribedElement(e, 'div', ['input', 'text'])).to.not.be.ok();
expect(d.isDescribedElement(e, 'div')).to.be.ok();
});
});
describe('describeElement', function() {
it('should include the id', function() {
var elem = fullElement();
var description = d.describeElement(elem);
expect(description.id).to.eql('myId');
});
it('should have the right tag name', function() {
var elem = fullElement();
var description = d.describeElement(elem);
expect(description.tagName).to.eql('div');
});
});
describe('descriptionToString', function() {
it('should be right', function() {
var elem = fullElement();
var desc = d.describeElement(elem);
var str = d.descriptionToString(desc);
expect(str).to.eql('div#myId.a.b.c[type="theType"][name="someName"]');
});
});
describe('treeToArray', function() {
it('should follow parent pointers', function() {
var base = genElement('span', 'cool');
base.parentNode = genElement('div', 'parent');
var arr = d.treeToArray(base);
expect(arr.length).to.eql(2);
});
it('should not stop before html tag', function() {
var e1 = genElement('div', 'cool');
var e2 = genElement('div', null, 'a b');
var h = genElement('html');
e1.parentNode = e2;
e2.parentNode = h;
var arr = d.treeToArray(e1);
expect(arr.length).to.eql(2);
});
it('should cap out at 5 elements', function() {
var e1 = genElement('div', 'cool');
var e2 = genElement('div', null, 'a b');
var e3 = genElement('div', null, 'a b');
var e4 = genElement('div', null, 'a b');
var e5 = genElement('div', null, 'a b');
var e6 = genElement('div', null, 'a b');
e1.parentNode = e2;
e2.parentNode = e3;
e3.parentNode = e4;
e4.parentNode = e5;
e5.parentNode = e6;
var arr = d.treeToArray(e1);
expect(arr.length).to.eql(5);
});
it('should put the innermost element last', function() {
var e1 = genElement('div', 'id1');
var e2 = genElement('div', 'id2', 'a b');
var e3 = genElement('div', 'id3', 'a b');
var e4 = genElement('div', 'id4', 'a b');
var e5 = genElement('div', 'id5', 'a b');
var e6 = genElement('div', 'id6', 'a b');
e1.parentNode = e2;
e2.parentNode = e3;
e3.parentNode = e4;
e4.parentNode = e5;
e5.parentNode = e6;
var arr = d.treeToArray(e1);
expect(arr[4].id).to.eql('id1');
expect(arr[0].id).to.eql('id5');
});
});
describe('elementArrayToString', function() {
it('should work with one element', function() {
var e1 = {tagName: 'div', id: 'id1', classes: ['a', 'b'], attributes: []};
var arr = [e1];
var res = d.elementArrayToString(arr);
expect(res).to.eql('div#id1.a.b');
});
it('should work with two elements', function() {
var e1 = {tagName: 'div', id: 'id1', classes: ['a', 'b'], attributes: []};
var e2 = {tagName: 'div', id: 'id2', classes: ['a', 'b', 'c'], attributes: [{key: 'name', value: 'thing'}]};
var arr = [e1, e2];
var res = d.elementArrayToString(arr);
expect(res).to.eql('div#id1.a.b > div#id2.a.b.c[name="thing"]');
});
it('should truncate at 80 characters max without breaking within a element', function() {
var e1 = {tagName: 'div', id: 'id1', classes: ['a', 'b'], attributes: []};
var e2 = {tagName: 'div', id: 'id2', classes: ['a', 'b', 'c'], attributes: [{key: 'name', value: 'thing2'}]};
var e3 = {tagName: 'div', id: 'id3', classes: ['a', 'b'], attributes: []};
var e4 = {tagName: 'div', id: 'id4', classes: ['a', 'b', 'c'], attributes: [{key: 'name', value: 'thing4'}]};
var arr = [e1, e2, e3, e4];
var res = d.elementArrayToString(arr);
expect(res).to.eql('... > div#id2.a.b.c[name="thing2"] > div#id3.a.b > div#id4.a.b.c[name="thing4"]');
});
});
describe('everything', function() {
it('should work with one element', function() {
var e = genElement('div', 'id1');
var description = d.descriptionToString(d.describeElement(e));
var result = d.elementArrayToString(d.treeToArray(e));
expect(description).to.eql(result);
});
it('should work with many elements', function() {
var e1 = genElement('div', 'id1');
var e2 = genElement('div', 'id2', 'a b', 'input');
var e3 = genElement('div', 'id3', 'a b', null, 'thing');
var e4 = genElement('div', 'id4', 'a b');
var e5 = genElement('div', 'id5', 'a b');
var e6 = genElement('div', 'id6', 'a b');
e1.parentNode = e2;
e2.parentNode = e3;
e3.parentNode = e4;
e4.parentNode = e5;
e5.parentNode = e6;
var d1 = d.descriptionToString(d.describeElement(e1));
var d2 = d.descriptionToString(d.describeElement(e2));
var d3 = d.descriptionToString(d.describeElement(e3));
var d4 = d.descriptionToString(d.describeElement(e4));
var d5 = d.descriptionToString(d.describeElement(e5));
var d6 = d.descriptionToString(d.describeElement(e6));
var description = ['...', d4, d3, d2, d1].join(' > ');
var result = d.elementArrayToString(d.treeToArray(e1));
expect(description).to.eql(result);
});
});