This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
313 lines (252 loc) · 6.63 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import tapeCss_ from './module';
const test = require('tape-catch');
const clone = require('clone');
const {freeze} = Object;
const {jsdom} = require('jsdom');
const tape = require('tape');
test('Doesn’t modify the `tape` instance', (is) => {
const tapeClone = clone(tape);
freeze(tapeClone.Test);
freeze(tapeClone);
is.doesNotThrow(
() => tapeCss_(tapeClone),
'doesn’t attempt to change any property'
);
is.end();
});
test('Doesn’t change the `tape` API', (is) => {
const localTape = tape.createHarness({exit: false});
const localTapeCss = tapeCss_(localTape);
const tapStream = localTape.createStream({objectMode: true});
is.plan(13);
localTapeCss('1', (localIs) => {
is.fail('`tape.only` works as before');
localIs.end();
});
localTapeCss.only('2', (localIs) => {
localIs.pass();
localIs.ok(true);
localIs.notOk(false);
localIs.equal(1, 1);
localIs.deepEqual([1], [1]);
localIs.throws(() => { throw new Error('whatever'); });
localIs.comment('Yeeaah!');
localIs.skip('Whooah!');
localIs.fail();
localIs.ok(false);
localIs.deepEqual([1], [2]);
localIs.end();
});
const registerStreams = [
({type}) => is.equal(
type, 'test',
'`tape` works as before'
),
({operator, ok}) => is.deepEqual(
[operator, ok],
['pass', true],
'`t.pass` succeeds as before'
),
({operator, ok}) => is.deepEqual(
[operator, ok],
['ok', true],
'`t.ok` succeeds as before'
),
({operator, ok}) => is.deepEqual(
[operator, ok],
['notOk', true],
'`t.notOk` succeeds as before'
),
({operator, ok}) => is.deepEqual(
[operator, ok],
['equal', true],
'`t.equal` succeeds as before'
),
({operator, ok}) => is.deepEqual(
[operator, ok],
['deepEqual', true],
'`t.deepEqual` succeeds as before'
),
({operator, ok}) => is.deepEqual(
[operator, ok],
['throws', true],
'`t.throws` succeeds as before'
),
(data) => is.equal(
data,
'Yeeaah!',
'`t.comment` outputs as before'
),
({operator, skip}) => is.deepEqual(
[operator, skip],
['skip', true],
'`t.skip` skips as before'
),
({operator, ok}) => is.deepEqual(
[operator, ok],
['fail', false],
'`t.fail` fails as before'
),
({operator, ok}) => is.deepEqual(
[operator, ok],
['ok', false],
'`t.ok` fails as before'
),
({operator, ok}) => is.deepEqual(
[operator, ok],
['deepEqual', false],
'`t.deepEqual` fails as before'
),
({type}) => is.deepEqual(
type,
'end',
'`t.end` ends as before'
),
].reduceRight(
(callback, assertion) => () => tapStream.once('data', (data) => {
assertion(data);
callback();
}),
() => {}
);
registerStreams();
});
const doc = (typeof window !== 'undefined' && window.window === window ?
window.document :
jsdom('<!DOCTYPE html>').defaultView.document
);
test('Adds and removes DOM', (is) => {
const blindTape = tape.createHarness({exit: false});
const tapeCss = tapeCss_(blindTape);
blindTape.createStream({objectMode: true});
// No need to test the output of `tapeCss` unless our tests fail.
is.plan(7);
const span = doc.createElement('span');
tapeCss('Whatever', {
dom: span,
document: doc,
}, (localIs) => {
is.equal(
span.parentNode,
doc.body,
'inserts a single element to the <body>'
);
localIs.end();
is.notOk(
doc.body.contains(span),
'removes the single element from the <body>'
);
});
const tree = doc.createDocumentFragment();
const button = doc.createElement('button');
const p = doc.createElement('p');
const div = doc.createElement('div');
tree.appendChild(button);
p.appendChild(div);
tree.appendChild(p);
tapeCss('Whatever', {
dom: tree,
document: doc,
}, (localIs) => {
is.ok(
doc.body.contains(button) &&
doc.body.contains(p) &&
doc.body.contains(div),
'inserts a whole DOM tree to the <body>'
);
is.equal(
button.parentNode,
doc.body,
'puts every element of the tree directly in the <body>'
);
is.equal(
div.parentNode,
p,
'keeps the internal DOM structure'
);
localIs.end();
is.notOk(
doc.body.contains(button) ||
doc.body.contains(p) ||
doc.body.contains(div),
'removes the whole tree from the <body> after the test'
);
is.equal(
div.parentNode,
p,
'doesn’t screw up the DOM structure while at it'
);
});
});
test('Adds and removes styles', (is) => {
const blindTape = tape.createHarness({exit: false});
const tapeCss = tapeCss_(blindTape);
blindTape.createStream({objectMode: true});
// No need to test the output of `tapeCss` unless our tests fail.
is.plan(2);
const styles = 'span {font-size: 99px}';
tapeCss('Whatever', {
styles,
document: doc,
}, (localIs) => {
const styleElements = doc.head.getElementsByTagName('style');
const styleElement = styleElements[styleElements.length - 1];
is.equal(
styleElement && styleElement.textContent,
styles,
'inserts our styles into the <head>'
);
localIs.end();
is.notOk(
doc.head.contains(styleElement),
'removes them after the test'
);
});
});
test('`test.only` works as they say in the ads', (is) => {
const blindTape = tape.createHarness({exit: false});
const tapeCss = tapeCss_(blindTape);
blindTape.createStream({objectMode: true});
// No need to test the output of `tapeCss` unless our tests fail.
is.plan(4);
const styles = 'span {font-size: 73px}';
const span = doc.createElement('span');
tapeCss('Shouldn’t run', () => {
is.fail(
'test before `test.only` doesn’t run'
);
});
tapeCss.only('Whatever', {
styles,
dom: span,
document: doc,
}, (localIs) => {
const styleElements = doc.head.getElementsByTagName('style');
const styleElement = styleElements[styleElements.length - 1];
is.equal(
styleElement && styleElement.textContent,
styles,
'inserts our styles into the <head>'
);
is.equal(
span.parentNode,
doc.body,
'inserts our DOM to the <body>'
);
localIs.end();
is.ok(
doc.head.contains(styleElement),
'doesn’t remove styles after the test'
);
is.ok(
doc.body.contains(span),
'doesn’t remove the DOM after the test'
);
is.end();
});
tapeCss('Shouldn’t run', () => {
is.fail(
'test after `test.only` doesn’t run'
);
});
});