Repository navigation
Expand file tree
/
Copy pathopt.test.js
More file actions
382 lines (303 loc) · 14.4 KB
/
Copy pathopt.test.js
File metadata and controls
382 lines (303 loc) · 14.4 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
/**
* opt() - Source Mangling Tests
*
* Tests the opt() function for fine-grained reactivity optimization.
*/
import { describe, assert } from './test-runner.js';
import { opt, convertArrowToFunction, extractExpressions, shouldSkipWrapping, mangleTemplateSource } from '../../lib/opt.js';
import { html, defineComponent, flushRenders } from '../../lib/framework.js';
describe('opt() - Arrow Function Conversion', function(it) {
it('converts expression arrow function to regular function', async () => {
const result = convertArrowToFunction('() => html`<div></div>`');
assert.equal(result, 'function() { return html`<div></div>`; }');
});
it('converts block arrow function to regular function', async () => {
const result = convertArrowToFunction('() => { return html`<div></div>`; }');
assert.equal(result, 'function() { return html`<div></div>`; }');
});
it('preserves regular function syntax', async () => {
const result = convertArrowToFunction('function() { return html`<div></div>`; }');
assert.equal(result, 'function() { return html`<div></div>`; }');
});
it('handles whitespace in arrow functions', async () => {
const result = convertArrowToFunction('( ) => html`<div></div>`');
assert.equal(result, 'function() { return html`<div></div>`; }');
});
});
describe('opt() - Expression Extraction', function(it) {
it('extracts simple expression', async () => {
const source = 'html`<div>${this.state.count}</div>`';
const expressions = extractExpressions(source);
assert.equal(expressions.length, 1);
assert.equal(expressions[0].expr, 'this.state.count');
});
it('extracts multiple expressions', async () => {
const source = 'html`<div>${this.state.a} and ${this.state.b}</div>`';
const expressions = extractExpressions(source);
assert.equal(expressions.length, 2);
assert.equal(expressions[0].expr, 'this.state.a');
assert.equal(expressions[1].expr, 'this.state.b');
});
it('handles nested braces in expressions', async () => {
const source = 'html`<div>${this.state.items.filter(x => x.active)}</div>`';
const expressions = extractExpressions(source);
assert.equal(expressions.length, 1);
assert.equal(expressions[0].expr, 'this.state.items.filter(x => x.active)');
});
it('handles object literals in expressions', async () => {
const source = 'html`<div>${{ a: 1, b: 2 }}</div>`';
const expressions = extractExpressions(source);
assert.equal(expressions.length, 1);
assert.equal(expressions[0].expr, '{ a: 1, b: 2 }');
});
it('handles string literals in expressions', async () => {
const source = 'html`<div>${this.state.msg || "default"}</div>`';
const expressions = extractExpressions(source);
assert.equal(expressions.length, 1);
assert.equal(expressions[0].expr, 'this.state.msg || "default"');
});
it('handles nested template literals', async () => {
const source = 'html`<div>${this.state.items.map(x => `${x.name}`)}</div>`';
const expressions = extractExpressions(source);
assert.equal(expressions.length, 1);
// Should extract the entire map expression including nested template
assert.ok(expressions[0].expr.includes('map'));
assert.ok(expressions[0].expr.includes('x.name'));
});
it('handles expressions with single quotes', async () => {
const source = "html`<div>${this.state.msg || 'fallback'}</div>`";
const expressions = extractExpressions(source);
assert.equal(expressions.length, 1);
assert.equal(expressions[0].expr, "this.state.msg || 'fallback'");
});
});
describe('opt() - Skip Wrapping Rules', function(it) {
it('skips contain() expressions', async () => {
assert.equal(shouldSkipWrapping('contain(() => this.state.x)'), true);
});
it('skips raw() expressions', async () => {
assert.equal(shouldSkipWrapping('raw(this.state.html)'), true);
});
it('skips html.contain() expressions', async () => {
assert.equal(shouldSkipWrapping('html.contain(() => this.state.x)'), true);
});
it('skips arrow functions with no params', async () => {
assert.equal(shouldSkipWrapping('() => this.state.count'), true);
});
it('skips arrow functions with params', async () => {
assert.equal(shouldSkipWrapping('(item) => item.name'), true);
assert.equal(shouldSkipWrapping('item => item.name'), true);
});
it('skips arrow functions with multiple params', async () => {
assert.equal(shouldSkipWrapping('(a, b) => a + b'), true);
});
it('skips function expressions', async () => {
assert.equal(shouldSkipWrapping('function() { return x; }'), true);
assert.equal(shouldSkipWrapping('function foo() { return x; }'), true);
});
it('skips async arrow functions', async () => {
assert.equal(shouldSkipWrapping('async () => await fetch()'), true);
assert.equal(shouldSkipWrapping('async x => await x.load()'), true);
});
it('skips async function expressions', async () => {
assert.equal(shouldSkipWrapping('async function() { return await x; }'), true);
});
it('does NOT skip simple expressions', async () => {
assert.equal(shouldSkipWrapping('this.state.count'), false);
assert.equal(shouldSkipWrapping('this.state.a + this.state.b'), false);
});
it('DOES skip when/each/memoEach (evaluated before callback)', async () => {
// These return vnodes that are processed specially by the renderer
// Wrapping them in contain() breaks because condition/array is evaluated early
assert.equal(shouldSkipWrapping('when(this.state.loading, html`...`)'), true);
assert.equal(shouldSkipWrapping('each(this.state.items, item => html`...`)'), true);
assert.equal(shouldSkipWrapping('memoEach(this.state.items, item => html`...`)'), true);
});
});
describe('opt() - Source Mangling', function(it) {
it('wraps simple expression in html.contain', async () => {
const source = 'function() { return html`<div>${this.state.count}</div>`; }';
const result = mangleTemplateSource(source);
assert.ok(result.includes('html.contain(() => (this.state.count))'));
});
it('wraps multiple expressions', async () => {
const source = 'function() { return html`<div>${this.state.a} and ${this.state.b}</div>`; }';
const result = mangleTemplateSource(source);
assert.ok(result.includes('html.contain(() => (this.state.a))'));
assert.ok(result.includes('html.contain(() => (this.state.b))'));
});
it('does NOT wrap when() (condition evaluated early)', async () => {
const source = 'function() { return html`<div>${when(this.state.loading, html`...`)}</div>`; }';
const result = mangleTemplateSource(source);
// when() should NOT be wrapped - condition is evaluated before callback runs
assert.ok(!result.includes('html.contain(() => (when('));
});
it('does NOT wrap each() (array evaluated early)', async () => {
const source = 'function() { return html`<div>${each(this.state.items, item => html`...`)}</div>`; }';
const result = mangleTemplateSource(source);
// each() should NOT be wrapped - array is evaluated before callback runs
assert.ok(!result.includes('html.contain(() => (each('));
});
it('does NOT wrap contain()', async () => {
const source = 'function() { return html`<div>${contain(() => this.state.x)}</div>`; }';
const result = mangleTemplateSource(source);
// Should NOT have double-wrapping
assert.ok(!result.includes('html.contain(() => (contain('));
// Original contain should remain
assert.ok(result.includes('contain(() => this.state.x)'));
});
it('does NOT wrap arrow functions', async () => {
const source = 'function() { return html`<div>${() => this.state.x}</div>`; }';
const result = mangleTemplateSource(source);
// Should NOT wrap the arrow function
assert.ok(!result.includes('html.contain(() => (()'));
});
it('handles complex nested expressions', async () => {
const source = 'function() { return html`<div>${this.state.items.filter(x => x.active).length}</div>`; }';
const result = mangleTemplateSource(source);
assert.ok(result.includes('html.contain(() => (this.state.items.filter(x => x.active).length))'));
});
});
describe('opt() - Full Function', function(it) {
it('returns a string', async () => {
const result = opt(() => html`<div>test</div>`);
assert.equal(typeof result, 'string');
});
it('returns parenthesized function string', async () => {
const result = opt(() => html`<div>test</div>`);
assert.ok(result.startsWith('('));
assert.ok(result.endsWith(')'));
});
it('converts arrow to regular function', async () => {
const result = opt(() => html`<div>test</div>`);
assert.ok(result.includes('function()'));
assert.ok(!result.includes('=>'));
});
it('wraps expressions in html.contain', async () => {
const result = opt(function() {
return html`<div>${this.state.count}</div>`;
});
assert.ok(result.includes('html.contain(() => (this.state.count))'));
});
it('can be evaled to a function', async () => {
const result = opt(() => html`<div>test</div>`);
const fn = eval(result);
assert.equal(typeof fn, 'function');
});
});
describe('opt() - Integration with Components', function(it) {
it('works with attribute expressions', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
try {
defineComponent('opt-test-attr', {
data() {
return { className: 'initial', href: '/test' };
},
template: eval(opt(function() {
return html`<a class="${this.state.className}" href="${this.state.href}">Link</a>`;
}))
});
const el = document.createElement('opt-test-attr');
container.appendChild(el);
flushRenders();
const link = el.querySelector('a');
assert.ok(link, 'Should find link element');
assert.equal(link.className, 'initial');
assert.equal(link.getAttribute('href'), '/test');
// Update state
el.state.className = 'updated';
el.state.href = '/new-path';
flushRenders();
assert.equal(link.className, 'updated');
assert.equal(link.getAttribute('href'), '/new-path');
} finally {
container.remove();
}
});
it('works with custom element prop expressions', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
try {
// Create a simple custom element to receive props
defineComponent('opt-test-receiver', {
props: { value: 0, label: '' },
template() {
return html`<span class="value">${this.props.value}</span><span class="label">${this.props.label}</span>`;
}
});
defineComponent('opt-test-custom-props', {
data() {
return { count: 42, text: 'Hello' };
},
template: eval(opt(function() {
return html`<opt-test-receiver value="${this.state.count}" label="${this.state.text}"></opt-test-receiver>`;
}))
});
const el = document.createElement('opt-test-custom-props');
container.appendChild(el);
flushRenders();
const receiver = el.querySelector('opt-test-receiver');
assert.ok(receiver, 'Should find receiver element');
assert.equal(receiver.props.value, 42);
assert.equal(receiver.props.label, 'Hello');
// Update state
el.state.count = 100;
el.state.text = 'World';
flushRenders();
assert.equal(receiver.props.value, 100);
assert.equal(receiver.props.label, 'World');
} finally {
container.remove();
}
});
it('works with defineComponent template', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
try {
// Verify html.contain exists
assert.ok(typeof html.contain === 'function', 'html.contain should be a function');
defineComponent('opt-test-simple', {
data() {
return { count: 0 };
},
template: eval(opt(function() {
return html`<div class="count">${this.state.count}</div>`;
}))
});
const el = document.createElement('opt-test-simple');
container.appendChild(el);
flushRenders();
const countEl = el.querySelector('.count');
assert.ok(countEl, 'Should find .count element');
assert.equal(countEl.textContent, '0');
// Update state
el.state.count = 42;
flushRenders();
assert.equal(countEl.textContent, '42');
} finally {
container.remove();
}
});
it('works with arrow function syntax', async () => {
const container = document.createElement('div');
document.body.appendChild(container);
try {
defineComponent('opt-test-arrow', {
data() {
return { name: 'World' };
},
template: eval(opt(() => html`<div class="greeting">Hello ${this.state.name}</div>`))
});
const el = document.createElement('opt-test-arrow');
container.appendChild(el);
flushRenders();
const greetingEl = el.querySelector('.greeting');
assert.ok(greetingEl, 'Should find .greeting element');
assert.ok(greetingEl.textContent.includes('Hello'), 'Should contain Hello');
assert.ok(greetingEl.textContent.includes('World'), 'Should contain World');
} finally {
container.remove();
}
});
});