Repository navigation
Expand file tree
/
Copy pathclass-component.test.js
More file actions
389 lines (323 loc) · 15.1 KB
/
Copy pathclass-component.test.js
File metadata and controls
389 lines (323 loc) · 15.1 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* Tests for class-authored components (defineComponent with a class)
*/
import { describe, assert } from './test-runner.js';
import { defineComponent, Component, html, flushSync, createStore } from '../../lib/framework.js';
/** Capture console.warn calls while fn runs, returns the messages */
function captureWarns(fn) {
const warns = [];
const original = console.warn;
console.warn = (...args) => { warns.push(args.join(' ')); };
try {
fn();
} finally {
console.warn = original;
}
return warns;
}
describe('Class components: basics', function(it) {
it('renders template, binds methods, and reacts to state changes', () => {
class TcBasic extends Component {
static props = { label: 'World' };
constructor(props) {
super(props);
this.state = { count: 0 };
}
increment() { this.state.count++; }
template() {
return html`<div><span id="l">${this.props.label}</span><span id="c">${this.state.count}</span></div>`;
}
}
defineComponent('tc-basic', TcBasic);
const el = document.createElement('tc-basic');
document.body.appendChild(el);
assert.equal(el.querySelector('#l').textContent, 'World', 'Should render default prop');
assert.equal(el.querySelector('#c').textContent, '0', 'Should render initial state');
// Methods are bound - callable detached from the element
const inc = el.increment;
flushSync(() => inc());
assert.equal(el.querySelector('#c').textContent, '1', 'Bound method should update state and DOM');
document.body.removeChild(el);
});
it('constructor receives real prop values (attribute set before mount)', () => {
let seenInCtor = null;
class TcCtorProps extends Component {
static props = { label: 'default' };
constructor(props) {
super(props);
seenInCtor = props.label;
this.state = { copied: props.label };
}
template() { return html`<div id="v">${this.state.copied}</div>`; }
}
defineComponent('tc-ctor-props', TcCtorProps);
const el = document.createElement('tc-ctor-props');
el.setAttribute('label', 'from-attribute');
document.body.appendChild(el);
assert.equal(seenInCtor, 'from-attribute', 'Constructor should see attribute value');
assert.equal(el.querySelector('#v').textContent, 'from-attribute', 'State copied from props should render');
document.body.removeChild(el);
// Same for props set as properties before mount
const el2 = document.createElement('tc-ctor-props');
el2.label = 'from-property';
document.body.appendChild(el2);
assert.equal(seenInCtor, 'from-property', 'Constructor should see property value');
document.body.removeChild(el2);
});
it('supports field-style state reading this.props', () => {
class TcField extends Component {
static props = { start: '5' };
state = { n: Number(this.props.start) * 2 };
template() { return html`<div id="n">${this.state.n}</div>`; }
}
defineComponent('tc-field', TcField);
const el = document.createElement('tc-field');
el.setAttribute('start', '21');
document.body.appendChild(el);
assert.equal(el.querySelector('#n').textContent, '42', 'Field initializer should see real props');
flushSync(() => { el.state.n = 7; });
assert.equal(el.querySelector('#n').textContent, '7', 'Field-style state should be reactive');
document.body.removeChild(el);
});
it('binds arrow-function fields to the element, not a throwaway instance', () => {
class TcArrow extends Component {
state = { hits: 0 };
hit = () => { this.state.hits++; };
template() { return html`<div id="h">${this.state.hits}</div>`; }
}
defineComponent('tc-arrow', TcArrow);
const el = document.createElement('tc-arrow');
document.body.appendChild(el);
const detached = el.hit;
flushSync(() => detached());
assert.equal(el.state.hits, 1, 'Arrow field should close over the element state');
assert.equal(el.querySelector('#h').textContent, '1', 'DOM should reflect the update');
document.body.removeChild(el);
});
it('runs mounted and unmounted hooks with element as this', (done) => {
let mountedTag = null;
let unmountedCalled = false;
class TcLifecycle extends Component {
state = { ready: false };
mounted() {
mountedTag = this.tagName.toLowerCase();
this.state.ready = true;
}
unmounted() { unmountedCalled = true; }
template() { return html`<div id="r">${this.state.ready}</div>`; }
}
defineComponent('tc-lifecycle', TcLifecycle);
const el = document.createElement('tc-lifecycle');
document.body.appendChild(el);
setTimeout(() => {
assert.equal(mountedTag, 'tc-lifecycle', 'mounted() should run with element as this');
assert.equal(el.querySelector('#r').textContent, 'true', 'State set in mounted should render');
document.body.removeChild(el);
assert.ok(unmountedCalled, 'unmounted() should run on removal');
done();
}, 50);
});
});
describe('Class components: getters and inheritance', function(it) {
it('exposes getters as cached computed properties', () => {
let calls = 0;
class TcGetter extends Component {
state = { a: 2, b: 3 };
get sum() {
calls++;
return this.state.a + this.state.b;
}
template() { return html`<div id="s">${this.sum}</div>`; }
}
defineComponent('tc-getter', TcGetter);
const el = document.createElement('tc-getter');
document.body.appendChild(el);
assert.equal(el.querySelector('#s').textContent, '5', 'Should render computed value');
const callsAfterMount = calls;
assert.equal(el.sum, 5, 'Getter readable on the element');
assert.equal(el.sum, 5, 'Getter readable repeatedly');
assert.equal(calls, callsAfterMount, 'Repeated reads should hit the cache');
flushSync(() => { el.state.a = 10; });
assert.equal(el.querySelector('#s').textContent, '13', 'DOM should update when computed deps change');
document.body.removeChild(el);
});
it('re-evaluates dependency-free getters on every read (no stale cache)', () => {
let externalValue = 1; // not reactive, not props
class TcVolatile extends Component {
get external() { return externalValue; }
template() { return html`<div></div>`; }
}
defineComponent('tc-volatile', TcVolatile);
const el = document.createElement('tc-volatile');
document.body.appendChild(el);
assert.equal(el.external, 1, 'Should read initial value');
externalValue = 2;
assert.equal(el.external, 2, 'Dependency-free getter should not cache');
document.body.removeChild(el);
});
it('caches props-only getters and invalidates them on prop change', () => {
let calls = 0;
class TcPropGetter extends Component {
static props = { word: 'hi' };
get shouted() {
calls++;
return this.props.word.toUpperCase();
}
template() { return html`<div id="w">${this.shouted}</div>`; }
}
defineComponent('tc-prop-getter', TcPropGetter);
const el = document.createElement('tc-prop-getter');
document.body.appendChild(el);
assert.equal(el.querySelector('#w').textContent, 'HI', 'Should render derived value');
const callsAfterMount = calls;
assert.equal(el.shouted, 'HI', 'Getter readable');
assert.equal(el.shouted, 'HI', 'Getter readable repeatedly');
assert.equal(calls, callsAfterMount, 'Props-only getter should stay cached across reads');
flushSync(() => { el.word = 'bye'; });
assert.equal(el.querySelector('#w').textContent, 'BYE', 'Prop change should invalidate the cache');
document.body.removeChild(el);
});
it('supports inheritance: merged static props and super method calls', () => {
class TcGreeterBase extends Component {
static props = { punctuation: '!' };
greeting() { return 'hello'; }
template() { return html`<div id="g">${this.greeting()}${this.props.punctuation}</div>`; }
}
class TcGreeterSub extends TcGreeterBase {
static props = { loud: 'yes' };
greeting() {
const base = super.greeting();
return this.props.loud === 'yes' ? base.toUpperCase() : base;
}
}
// Only the subclass is registered - the base is just a class
defineComponent('tc-greeter-sub', TcGreeterSub);
const el = document.createElement('tc-greeter-sub');
document.body.appendChild(el);
assert.equal(el.props.punctuation, '!', 'Parent static props should merge in');
assert.equal(el.props.loud, 'yes', 'Child static props should merge in');
assert.equal(el.querySelector('#g').textContent, 'HELLO!', 'super.method() should work on the element');
document.body.removeChild(el);
});
it('wires static stores', () => {
const tcStore = createStore({ msg: 'hello' });
class TcStore extends Component {
static stores = { box: tcStore };
template() { return html`<div id="m">${this.stores.box.msg}</div>`; }
}
defineComponent('tc-store', TcStore);
const el = document.createElement('tc-store');
document.body.appendChild(el);
assert.equal(el.querySelector('#m').textContent, 'hello', 'Should render store state');
flushSync(() => { tcStore.state.msg = 'bye'; });
assert.equal(el.querySelector('#m').textContent, 'bye', 'Should react to store changes');
document.body.removeChild(el);
});
});
describe('Class components: guardrails', function(it) {
it('removes prop-named class fields and warns (accessor keeps working)', () => {
class TcShadow extends Component {
static props = { title: 'proper-default' };
title = 'field-value';
template() { return html`<div id="t">${this.props.title}</div>`; }
}
defineComponent('tc-shadow', TcShadow);
const el = document.createElement('tc-shadow');
let warns;
try {
warns = captureWarns(() => document.body.appendChild(el));
assert.ok(warns.some(w => w.includes('"title"')), 'Should warn about the shadowing field');
assert.ok(!Object.prototype.hasOwnProperty.call(el, 'title'),
'Own field should be removed so the prototype accessor works');
assert.equal(el.title, 'proper-default', 'Accessor should read the prop, not the field');
let observed = null;
el.propsChanged = (prop, val) => { observed = `${prop}=${val}`; };
el.title = 'via-accessor';
assert.equal(observed, 'title=via-accessor', 'Prop setter should still notify propsChanged');
} finally {
document.body.removeChild(el);
}
});
it('does not re-run the constructor on reconnection', () => {
let ctorRuns = 0;
class TcReconnect extends Component {
constructor(props) {
super(props);
ctorRuns++;
this.state = { v: 1 };
}
template() { return html`<div></div>`; }
}
defineComponent('tc-reconnect', TcReconnect);
const el = document.createElement('tc-reconnect');
document.body.appendChild(el);
el.state.v = 42;
// Moving an element disconnects and reconnects it synchronously
const other = document.createElement('div');
document.body.appendChild(other);
other.appendChild(el);
assert.equal(ctorRuns, 1, 'Constructor should run exactly once');
assert.equal(el.state.v, 42, 'State should survive the move');
document.body.removeChild(other);
});
it('warns on tag name collision and returns the registered class', () => {
const First = defineComponent('tc-collision', class extends Component {
template() { return html`<i>first</i>`; }
});
let Second;
const warns = captureWarns(() => {
Second = defineComponent('tc-collision', class extends Component {
template() { return html`<i>second</i>`; }
});
});
assert.ok(warns.some(w => w.includes('tc-collision')), 'Should warn about the collision');
assert.equal(Second, First, 'Should return the class that is actually registered');
const el = document.createElement('tc-collision');
document.body.appendChild(el);
assert.equal(el.querySelector('i').textContent, 'first', 'First registration should win');
document.body.removeChild(el);
});
it('re-registering the same definition is silent and idempotent', () => {
class TcIdem extends Component {
template() { return html`<div></div>`; }
}
const First = defineComponent('tc-idem', TcIdem);
let Second;
const warns = captureWarns(() => {
Second = defineComponent('tc-idem', TcIdem);
});
assert.equal(warns.length, 0, 'Same class re-registered should not warn');
assert.equal(Second, First, 'Should return the registered class');
// Same for a re-passed options object
const opts = { template() { return html`<div></div>`; } };
const OptFirst = defineComponent('tc-idem-opts', opts);
let OptSecond;
const optWarns = captureWarns(() => {
OptSecond = defineComponent('tc-idem-opts', opts);
});
assert.equal(optWarns.length, 0, 'Same options object re-registered should not warn');
assert.equal(OptSecond, OptFirst, 'Should return the registered class');
});
it('rejects direct instantiation and non-Component classes', () => {
class TcDirect extends Component {
template() { return html`<div></div>`; }
}
defineComponent('tc-direct', TcDirect);
let directError = null;
try {
new TcDirect();
} catch (e) {
directError = e;
}
assert.ok(directError && directError.message.includes('defineComponent'),
'Direct instantiation should throw with guidance');
let plainError = null;
try {
defineComponent('tc-plain-class', class { template() { return html`<div></div>`; } });
} catch (e) {
plainError = e;
}
assert.ok(plainError && plainError.message.includes('extend Component'),
'Classes not extending Component should be rejected');
});
});