Repository navigation
Expand file tree
/
Copy pathtests.js
More file actions
214 lines (183 loc) · 6.82 KB
/
Copy pathtests.js
File metadata and controls
214 lines (183 loc) · 6.82 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*globals describe, it, require, before, global, $*/
var expect = require('chai').expect;
var jsdom = require('jsdom');
var jq = require('jquery');
var result = require('../tasks/task-1')();
describe('Task #1 Tests', function () {
before(function (done) {
jsdom.env({
html: '',
done: function (errors, window) {
global.window = window;
global.document = window.document;
global.$ = jq(window);
Object.keys(window)
.filter(function (prop) {
return prop.toLowerCase().indexOf('html') >= 0;
}).forEach(function (prop) {
global[prop] = window[prop];
});
done();
}
});
});
describe('Valid Tests', function () {
it('expect nothing to change, whenm no elements with class `button` or `content`', function () {
var html = '<div id="root"><div></div><ul><li><a href="#">link</a></li></ul></div>';
document.body.innerHTML = html;
result('root');
expect(document.body.innerHTML).to.equal(html);
});
it('expect to change the content of all `.button` to "hide"', function () {
var container = document.createElement('div'),
count = 15,
possibleTags = ['a', 'button', 'p', 'div'],
i,
len,
buttonNode,
contentNode,
tag;
container.id = 'root';
for (i = 0; i < count; i += 1) {
tag = possibleTags[(Math.random() * possibleTags.length) | 0];
buttonNode = document.createElement(tag);
buttonNode.className = 'button';
container.appendChild(buttonNode);
possibleTags[(Math.random() * possibleTags.length) | 0];
contentNode = document.createElement(tag);
contentNode.className = 'content';
container.appendChild(contentNode);
}
document.body.innerHTML = container.outerHTML;
result('root');
var btns = document.getElementsByClassName('button');
expect(btns).to.have.length(count);
for (i = 0, len = btns.length; i < len; i += 1) {
expect(btns[i].innerHTML).to.equal('hide');
}
});
it('expect to hide the next content on button click, and then show it again, when there are other elements', function () {
function createDummyNode(){
var tags = ['a', 'button', 'p', 'div', 'ul', 'li', 'ol', 'input', 'table','tr','br','hr', 'span'];
var node = document.createElement(tags[(Math.random*tags.length) | 0]);
node.innerHTML = 'Dummy Element: ' + Math.random();
return node;
}
var container = document.createElement('div'),
count = 150,
possibleTags = ['a', 'button', 'p', 'div'],
i,
buttonNode,
contentNode,
tag;
container.id = 'root';
var dymmyObjectChance = 70;
for (i = 0; i < count; i += 1) {
if(Math.random * 100 < dymmyObjectChance){
container.appendChild(createDummyNode());
}
tag = possibleTags[(Math.random() * possibleTags.length) | 0];
buttonNode = document.createElement(tag);
buttonNode.className = 'button';
if (i === ((count / 2) | 0 )) {
buttonNode.id = 'the-button';
}
container.appendChild(buttonNode);
possibleTags[(Math.random() * possibleTags.length) | 0];
contentNode = document.createElement(tag);
contentNode.className = 'content';
if(Math.random * 100 < dymmyObjectChance){
container.appendChild(createDummyNode());
}
container.appendChild(contentNode);
if(Math.random * 100 < dymmyObjectChance){
container.appendChild(createDummyNode());
}
}
document.body.innerHTML = container.outerHTML;
result('root');
var theButton = document.getElementById('the-button');
var theContent = theButton.nextElementSibling;
while (theContent && theContent.className.indexOf('content') < 0) {
theContent = theContent.nextElementSibling;
}
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, true);
theButton.dispatchEvent(event);
expect(theButton).to.exist;
expect(theContent).to.exist;
expect(theButton.innerHTML).to.equal('show');
expect(theContent.style.display).to.equal('none');
theButton.dispatchEvent(event);
expect(theButton.innerHTML).to.equal('hide');
expect(theContent.style.display).to.equal('');
});
it('expect to hide the next content on button click, and then show it again', function () {
var container = document.createElement('div'),
count = 15,
possibleTags = ['a', 'button', 'p', 'div'],
i,
buttonNode,
contentNode,
tag;
container.id = 'root';
for (i = 0; i < count; i += 1) {
tag = possibleTags[(Math.random() * possibleTags.length) | 0];
buttonNode = document.createElement(tag);
buttonNode.className = 'button';
if (i === ((count / 2) | 0 )) {
buttonNode.id = 'the-button';
}
container.appendChild(buttonNode);
possibleTags[(Math.random() * possibleTags.length) | 0];
contentNode = document.createElement(tag);
contentNode.className = 'content';
container.appendChild(contentNode);
}
document.body.innerHTML = container.outerHTML;
result('root');
var theButton = document.getElementById('the-button');
var theContent = theButton.nextElementSibling;
while (theContent && theContent.className.indexOf('content') < 0) {
theContent = theContent.nextElementSibling;
}
var event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, true);
theButton.dispatchEvent(event);
expect(theButton).to.exist;
expect(theContent).to.exist;
expect(theButton.innerHTML).to.equal('show');
expect(theContent.style.display).to.equal('none');
theButton.dispatchEvent(event);
expect(theButton.innerHTML).to.equal('hide');
expect(theContent.style.display).to.equal('');
});
});
describe('Invalid', function () {
it('Expect to throw, when no params are provided', function () {
function test() {
result();
}
expect(test).to.throw();
});
/* Selector validation */
it('Expect to throw, when undefined is provided as selector', function () {
function test() {
result(undefined, []);
}
expect(test).to.throw();
});
it('Expect to throw, when null is provided as selector', function () {
function test() {
result(null, []);
}
expect(test).to.throw();
});
it('Expect to throw, when selector that selects nothing is provided', function () {
function test() {
result('TEST_NO_SUCH_SELECTOR', []);
}
expect(test).to.throw();
});
});
});