-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
286 lines (236 loc) · 5.97 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
import './module';
// This registers the <parametric-svg> thing on the actual `document`.
import register from './module/register';
const spec = require('tape-catch');
const repeat = require('repeat-element');
spec('Registers the <parametric-svg> element', (test) => {
test.plan(6);
const HTMLElement = () => {};
const registerElement = (
name, {prototype, extends: extendsArg = null}
) => {
test.pass(
'taking a custom implementation of `document`'
);
test.equal(
name,
'parametric-svg',
'under the name <parametric-svg>'
);
test.equal(
extendsArg,
null,
'as a standard HTML element – no bells and whistles'
);
test.equal(
prototype.constructor,
HTMLElement,
'inheriting from the given `HTMLElement`'
);
test.equal(
typeof prototype.createdCallback,
'function',
'attaching stuff to the createdCallback…'
);
test.deepEqual(
[
prototype.attachedCallback,
prototype.detachedCallback,
prototype.atributeChangedCallback,
],
repeat(undefined, 3),
'…and to no other lifecycle callback'
);
};
register({document: {registerElement}, HTMLElement});
});
spec('Works in a DOM structure created in one go', (test) => {
test.plan(1);
document.body.innerHTML = `
<parametric-svg>
<svg>
<rect parametric:x="50" />
</svg>
</parametric-svg>
`;
const rect = document.body.querySelector('rect');
test.equal(
rect.getAttribute('x'),
'50',
'synchronously'
);
});
spec('Works in a DOM structure built up programatically', (test) => {
test.plan(2);
setTimeout(() => {
// For reliable, consistent results this spec must fire after the initial
// render.
const parametricSvg = document.createElement('parametric-svg');
const svg = document.createElement('svg');
const circle = document.createElement('circle');
svg.appendChild(circle);
parametricSvg.appendChild(svg);
circle.setAttribute('r', '5');
circle.setAttribute('parametric:r', '2 * (3 + 5)');
test.equal(
circle.getAttribute('r'),
'5',
'updates asynchronously'
);
requestAnimationFrame(() => {
test.equal(
circle.getAttribute('r'),
String(2 * (3 + 5)),
'within a single animation frame'
);
});
});
});
spec('Only affects the first child SVG', (test) => {
test.plan(2);
document.body.innerHTML = `
<parametric-svg>
<svg>
</svg>
<svg>
<rect parametric:x="50" />
</svg>
<div><svg>
<circle parametric:r="70" r="5" />
</svg></div>
</parametric-svg>
`;
const rect = document.body.querySelector('rect');
test.equal(
rect.getAttribute('x'),
null,
'leaves a second child untouched'
);
const circle = document.body.querySelector('circle');
test.equal(
circle.getAttribute('r'),
'5',
'leaves another nested child untouched'
);
});
spec('Works with a nested SVG', (test) => {
test.plan(1);
document.body.innerHTML = `
<parametric-svg>
<div>
<svg>
<rect parametric:x="5 / 5" />
</svg>
</div>
</parametric-svg>
`;
const rect = document.body.querySelector('rect');
test.equal(
rect.getAttribute('x'),
String(5 / 5),
'nested one level deep'
);
});
spec('Warns when no <svg> is inside', (test) => {
test.plan(1);
const HTMLElement = {prototype: {
querySelector: (selector) => (selector === 'svg' ? null : {}),
}};
const document = {
registerElement: (_, {prototype}) => prototype.createdCallback(),
};
const logger = {warn(message) {
test.ok(
/couldn’t find/i.test(message),
'prints a helpful warning'
);
}};
register({document, HTMLElement, logger});
});
spec('Supports variables', (test) => {
test.plan(1);
document.body.innerHTML = `
<parametric-svg
a="2"
b="50"
>
<svg>
<rect parametric:x="b / a" />
</svg>
</parametric-svg>
`;
const rect = document.body.querySelector('rect');
test.equal(
rect.getAttribute('x'),
String(50 / 2),
'passes multiple variables in the right order'
);
});
spec('Only updates a parameter when all variables are defined', (test) => {
test.plan(3);
document.body.innerHTML = `
<parametric-svg
a="2"
>
<svg>
<circle parametric:r="a + b" r="5" />
<ellipse parametric:fill="c" />
<rect parametric:x="a" />
</svg>
</parametric-svg>
`;
const circle = document.body.querySelector('circle');
test.equal(
circle.getAttribute('r'),
'5',
'doesn’t update an attribute when its dependencies aren’t satisfied'
);
const ellipse = document.body.querySelector('ellipse');
test.equal(
ellipse.getAttribute('fill'),
null,
'doesn’t create an attribute when its dependencies aren’t satisfied'
);
const rect = document.body.querySelector('rect');
test.equal(
rect.getAttribute('x'),
'2',
'updates another attribute with satisfied dependencies'
);
});
spec('Updates variables dynamically', (test) => {
test.plan(4);
document.body.innerHTML = `
<parametric-svg>
<svg>
<rect parametric:x="a" parametric:y="b" />
</svg>
</parametric-svg>
`;
const rect = document.body.querySelector('rect');
const parametricSvg = document.body.querySelector('parametric-svg');
parametricSvg.setAttribute('a', '5');
test.equal(
rect.getAttribute('x'),
'5',
'updates parametric attributes synchronously'
);
parametricSvg.setAttribute('a', '15');
test.equal(
rect.getAttribute('x'),
'15',
'does it repeatedly'
);
parametricSvg.removeAttribute('a');
parametricSvg.setAttribute('b', '8');
test.equal(
rect.getAttribute('x'),
'15',
'leaves an attribute as it when a dependency is removed'
);
test.equal(
rect.getAttribute('y'),
'8',
'but updates other attributes even so'
);
});