Repository navigation
Expand file tree
/
Copy pathcomponent.test.js
More file actions
397 lines (327 loc) · 13.6 KB
/
Copy pathcomponent.test.js
File metadata and controls
397 lines (327 loc) · 13.6 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
390
391
392
393
394
395
396
397
/**
* Tests for Component System (State Management & Event Binding)
*/
import { describe, assert } from './test-runner.js';
import { defineComponent } from '../../lib/framework.js';
import { createStore } from '../../lib/framework.js';
import { html } from '../../lib/framework.js';
describe('Component State Management', function(it) {
it('initializes component state', () => {
const TestComponent = defineComponent('test-state-init', {
data() {
return { count: 0, name: 'test' };
},
template() {
return html`<div>${this.state.count} ${this.state.name}</div>`;
}
});
const el = document.createElement('test-state-init');
document.body.appendChild(el);
assert.equal(el.state.count, 0, 'Should initialize count to 0');
assert.equal(el.state.name, 'test', 'Should initialize name to test');
document.body.removeChild(el);
});
it('updates component when state changes', (done) => {
const TestComponent = defineComponent('test-state-update', {
data() {
return { count: 0 };
},
template() {
return html`<div id="counter">${this.state.count}</div>`;
}
});
const el = document.createElement('test-state-update');
document.body.appendChild(el);
// Wait for initial render
setTimeout(() => {
const counter = el.querySelector('#counter');
assert.ok(counter.textContent.includes('0'), 'Should show initial count');
// Update state
el.state.count = 5;
// Wait for re-render
setTimeout(() => {
const updated = el.querySelector('#counter');
assert.ok(updated.textContent.includes('5'), 'Should show updated count');
document.body.removeChild(el);
done();
}, 100);
}, 100);
});
it('subscribes to external stores', (done) => {
const externalStore = createStore({ value: 'initial' });
const TestComponent = defineComponent('test-store-sub', {
data() {
return { storeValue: null };
},
mounted() {
this.unsubscribe = externalStore.subscribe(state => {
this.state.storeValue = state.value;
});
},
unmounted() {
if (this.unsubscribe) this.unsubscribe();
},
template() {
return html`<div id="value">${this.state.storeValue || 'none'}</div>`;
}
});
const el = document.createElement('test-store-sub');
document.body.appendChild(el);
setTimeout(() => {
const valueEl = el.querySelector('#value');
assert.ok(valueEl.textContent.includes('initial'), 'Should show initial store value');
// Update store
externalStore.set({ value: 'updated' });
setTimeout(() => {
const updated = el.querySelector('#value');
assert.ok(updated.textContent.includes('updated'), 'Should show updated store value');
document.body.removeChild(el);
done();
}, 100);
}, 100);
});
});
describe('Component Event Binding', function(it) {
it('binds click events with on-click', (done) => {
let clicked = false;
const TestComponent = defineComponent('test-click-event', {
methods: {
handleClick() {
clicked = true;
}
},
template() {
return html`<button id="btn" on-click="handleClick">Click me</button>`;
}
});
const el = document.createElement('test-click-event');
document.body.appendChild(el);
// Wait for component to mount and render
setTimeout(() => {
const btn = el.querySelector('#btn');
assert.ok(btn, 'Should find button');
btn.click();
assert.ok(clicked, 'Should call handleClick on click');
document.body.removeChild(el);
done();
}, 100);
});
it('binds submit events with on-submit', (done) => {
let submitted = false;
let defaultPrevented = false;
const TestComponent = defineComponent('test-submit-event', {
methods: {
handleSubmit(e) {
submitted = true;
defaultPrevented = e.defaultPrevented;
}
},
template() {
return html`<form id="form" on-submit-prevent="handleSubmit">
<input type="text" value="test">
<button type="submit">Submit</button>
</form>`;
}
});
const el = document.createElement('test-submit-event');
document.body.appendChild(el);
setTimeout(() => {
const form = el.querySelector('#form');
assert.ok(form, 'Should find form');
const submitBtn = form.querySelector('button[type="submit"]');
submitBtn.click();
setTimeout(() => {
assert.ok(submitted, 'Should call handleSubmit on submit');
assert.ok(defaultPrevented, 'Should prevent default with -prevent modifier');
document.body.removeChild(el);
done();
}, 50);
}, 100);
});
it('binds change events with on-change', (done) => {
let changed = false;
let changeValue = null;
const TestComponent = defineComponent('test-change-event', {
methods: {
handleChange(e) {
changed = true;
changeValue = e.target.checked;
}
},
template() {
return html`<input type="checkbox" id="cb" on-change="handleChange">`;
}
});
const el = document.createElement('test-change-event');
document.body.appendChild(el);
setTimeout(() => {
const checkbox = el.querySelector('#cb');
assert.ok(checkbox, 'Should find checkbox');
checkbox.checked = true;
checkbox.dispatchEvent(new Event('change', { bubbles: true }));
setTimeout(() => {
assert.ok(changed, 'Should call handleChange on change');
assert.equal(changeValue, true, 'Should pass correct checked value');
document.body.removeChild(el);
done();
}, 50);
}, 100);
});
});
describe('Component Re-rendering', function(it) {
it('conditionally renders based on state', (done) => {
const TestComponent = defineComponent('test-conditional', {
data() {
return { showForm: false };
},
template() {
if (this.state.showForm) {
return html`<form id="form">Form is visible</form>`;
} else {
return html`<div id="message">Click to show form</div>`;
}
}
});
const el = document.createElement('test-conditional');
document.body.appendChild(el);
setTimeout(() => {
assert.ok(el.querySelector('#message'), 'Should show message initially');
assert.ok(!el.querySelector('#form'), 'Should not show form initially');
// Change state
el.state.showForm = true;
setTimeout(() => {
assert.ok(!el.querySelector('#message'), 'Should hide message after state change');
assert.ok(el.querySelector('#form'), 'Should show form after state change');
document.body.removeChild(el);
done();
}, 100);
}, 100);
});
it('re-renders when an attribute changes after mount', (done) => {
let propsChangedCalls = [];
defineComponent('test-attr-change', {
props: { label: 'default' },
propsChanged(prop, newValue, oldValue) {
propsChangedCalls.push({ prop, newValue, oldValue });
},
template() {
return html`<span id="label">${this.props.label}</span>`;
}
});
const el = document.createElement('test-attr-change');
el.setAttribute('label', 'initial');
document.body.appendChild(el);
setTimeout(() => {
assert.equal(el.querySelector('#label').textContent, 'initial', 'Initial attribute rendered');
// Change via setAttribute (not the property setter)
el.setAttribute('label', 'updated');
setTimeout(() => {
assert.equal(el.querySelector('#label').textContent, 'updated', 'Attribute change re-renders');
assert.equal(propsChangedCalls.length, 1, 'propsChanged called once for the update');
assert.equal(propsChangedCalls[0].prop, 'label', 'propsChanged receives prop name');
assert.equal(propsChangedCalls[0].newValue, 'updated', 'propsChanged receives new value');
assert.equal(propsChangedCalls[0].oldValue, 'initial', 'propsChanged receives old value');
document.body.removeChild(el);
done();
}, 100);
}, 100);
});
it('setProps batches: sibling props are current inside propsChanged', (done) => {
const observed = [];
defineComponent('test-batched-props', {
props: { params: null, query: null },
propsChanged(prop, newValue) {
// Record what the SIBLING prop looks like when this fires —
// with batched delivery both backing values must already be set
observed.push({
prop,
params: this.props.params,
query: this.props.query
});
},
template() {
return html`<span>batched</span>`;
}
});
const el = document.createElement('test-batched-props');
el.params = { id: 'old' };
el.query = { q: 'old' };
document.body.appendChild(el);
setTimeout(() => {
observed.length = 0;
const newParams = { id: 'new' };
const newQuery = { q: 'new' };
el.setProps({ params: newParams, query: newQuery });
assert.equal(observed.length, 2, 'propsChanged fires for both props');
for (const o of observed) {
assert.equal(o.params, newParams, `sibling params current when ${o.prop} fired`);
assert.equal(o.query, newQuery, `sibling query current when ${o.prop} fired`);
}
// Unchanged values are skipped
observed.length = 0;
el.setProps({ params: newParams, query: { q: 'newer' } });
assert.equal(observed.length, 1, 'unchanged prop skipped in batch');
assert.equal(observed[0].prop, 'query', 'only changed prop fires');
document.body.removeChild(el);
done();
}, 100);
});
});
describe('Component Event Isolation', function(it) {
it('does not bind child component events to parent (light DOM child)', (done) => {
let parentClicked = false;
let childClicked = false;
// Define child component WITHOUT Shadow DOM
const ChildComponent = defineComponent('test-child-light', {
methods: {
handleChildClick() {
childClicked = true;
}
},
template() {
return html`<button id="child-btn" on-click="handleChildClick">Child Button</button>`;
}
});
// Define parent component
const ParentComponent = defineComponent('test-parent-light', {
methods: {
handleParentClick() {
parentClicked = true;
},
handleChildClick() {
// This should NOT be called when child button is clicked
throw new Error('Parent handleChildClick should not be called');
}
},
template() {
return html`
<div>
<button id="parent-btn" on-click="handleParentClick">Parent Button</button>
<test-child-light></test-child-light>
</div>
`;
}
});
const el = document.createElement('test-parent-light');
document.body.appendChild(el);
setTimeout(() => {
const parentBtn = el.querySelector('#parent-btn');
const childBtn = el.querySelector('#child-btn');
assert.ok(parentBtn, 'Should find parent button');
assert.ok(childBtn, 'Should find child button');
// Click parent button
parentBtn.click();
assert.ok(parentClicked, 'Should call parent handleParentClick');
assert.ok(!childClicked, 'Should not call child handler yet');
// Reset
parentClicked = false;
// Click child button - should only call child handler
childBtn.click();
assert.ok(!parentClicked, 'Should not call parent handler');
assert.ok(childClicked, 'Should call child handleChildClick');
document.body.removeChild(el);
done();
}, 150);
});
// Removed: Shadow DOM test - not supported in current architecture
});