-
Notifications
You must be signed in to change notification settings - Fork 3
/
converter.test.ts
783 lines (770 loc) · 26.5 KB
/
converter.test.ts
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
import {
expect,
test,
describe,
beforeAll,
beforeEach,
afterEach,
} from 'vitest';
import { preprocess } from '@glimmer/syntax';
import { ComplexJSType, convert } from './converter';
import { ASTv1 } from '@glimmer/syntax';
import {
HBSControlExpression,
HBSNode,
serializeNode,
resetContextCounter,
} from './utils';
import { EVENT_TYPE, SYMBOLS } from './symbols';
import { defaultFlags } from './flags';
const flags = defaultFlags();
// flags.WITH_HELPER_MANAGER = false;
// Maybe modifier
function $mm(name: string, params: string = '', hash: string = '{}') {
if (flags.WITH_MODIFIER_MANAGER) {
return `$:($n) => $_maybeModifier($:${name},$n,[${params}],${hash})`;
} else {
return `$:($n) => $:${name}($n,${params.trim()})`;
}
}
// Maybe helper
function $mh(name: string, params: string = '', hash: string = '{}') {
if (flags.WITH_HELPER_MANAGER) {
return `$:$_maybeHelper(${name},[${params}],${hash})`;
} else {
return `$:${name}(${params})`;
}
}
function $args(str: string) {
if (flags.IS_GLIMMER_COMPAT_MODE) {
if (str === '{}') {
return '{}';
}
return `${SYMBOLS.ARGS}(${str})`;
} else {
return str;
}
}
function $glimmerCompat(str: string) {
if (flags.IS_GLIMMER_COMPAT_MODE) {
return `() => ` + str.replace('$:', '');
} else {
return str.replace('$:', '');
}
}
function $s<T extends ComplexJSType>(node: T): string | null | undefined {
return serializeNode(node);
}
function $t<T extends ASTv1.Node>(tpl: string): ComplexJSType {
const seenNodes: Set<ASTv1.Node> = new Set();
const { ToJSType } = convert(seenNodes, flags);
const ast = preprocess(tpl);
const node = ast.body[0] as T;
return ToJSType(node);
}
function $control(
partial: Partial<HBSControlExpression>,
): HBSControlExpression {
return {
type: 'if',
isControl: true,
condition: '',
blockParams: [],
isSync: false,
children: [],
inverse: null,
key: null,
...partial,
};
}
function $node(partial: Partial<HBSNode>): HBSNode {
return {
...partial,
events: partial.events ?? [],
children: partial.children ?? [],
attributes: partial.attributes ?? [],
properties: partial.properties ?? [],
blockParams: partial.blockParams ?? [],
selfClosing: partial.selfClosing ?? false,
hasStableChild: partial.hasStableChild ?? true,
tag: partial.tag ?? '',
};
}
describe.each([
{ glimmerCompat: true, name: 'glimmer compat mode' },
{ glimmerCompat: false, name: 'glimmer non-compat mode' },
{
helperManager: true,
glimmerCompat: true,
name: 'glimmer compat mode [hm]',
},
{
helperManager: false,
glimmerCompat: false,
name: 'glimmer non-compat mode [hm]',
},
{ modifierManager: false, name: 'without modifier manager' },
{ modifierManager: true, name: 'with modifier manager' },
])('$name', ({ glimmerCompat, helperManager, modifierManager }) => {
beforeAll(() => {
if (glimmerCompat !== undefined) {
flags.IS_GLIMMER_COMPAT_MODE = glimmerCompat;
}
if (helperManager !== undefined) {
flags.WITH_HELPER_MANAGER = helperManager;
}
if (modifierManager !== undefined) {
flags.WITH_MODIFIER_MANAGER = modifierManager;
}
});
beforeEach(() => {
resetContextCounter();
});
describe('convert function builder', () => {
describe('mustache helper usage - optional chaining', () => {
test('it has proper chains', () => {
expect(
$t<ASTv1.MustacheStatement>(
`{{toInitials @name @initialLength @initials}}`,
),
).toEqual(
`$:() => ` +
$mh(
'toInitials',
'$:this[$args].name,$:this[$args].initialLength,$:this[$args].initials',
),
);
expect(
$t<ASTv1.MustacheStatement>(
`{{toInitials @name @initialLength.a @initials}}`,
),
).toEqual(
`$:() => ` +
$mh(
'toInitials',
'$:this[$args].name,$:this[$args].initialLength?.a,$:this[$args].initials',
),
);
});
});
describe('path expressions are optional chained', () => {
test('works for template paths', () => {
expect($t<ASTv1.BlockStatement>(`{{this.foo.bar.baz}}`)).toEqual(
`$:this.foo?.bar?.baz`,
);
expect($t<ASTv1.BlockStatement>(`{{this.foo}}`)).toEqual(`$:this.foo`);
expect($t<ASTv1.BlockStatement>(`{{this.foo.bar}}`)).toEqual(
`$:this.foo?.bar`,
);
expect($t<ASTv1.BlockStatement>(`{{foo.bar.baz}}`)).toEqual(
`$:foo?.bar?.baz`,
);
expect($t<ASTv1.BlockStatement>(`{{foo.bar}}`)).toEqual(`$:foo.bar`);
expect($t<ASTv1.BlockStatement>(`{{@foo.bar.baz}}`)).toEqual(
`$:this[$args].foo?.bar?.baz`,
);
expect($t<ASTv1.BlockStatement>(`{{@foo.bar}}`)).toEqual(
`$:this[$args].foo.bar`,
);
});
test('it works for sub expression paths in mustache', () => {
expect(
$t<ASTv1.ElementNode>(
`<div class={{maybeClass (if @arrowProps.className @arrowProps.className)}}></div>`,
),
).toEqual(
$node({
tag: 'div',
properties: [
[
'',
`$:() => ${$mh(
'maybeClass',
`$:$__if($:this[$args].arrowProps?.className,$:this[$args].arrowProps?.className)`,
)}`,
],
],
}),
);
});
test('works for sub-expression paths', () => {
expect(
$t<ASTv1.BlockStatement>(`{{and (or this.foo.bar.baz)}}`),
).toEqual(`$:() => ${$mh('and', $mh('or', `$:this.foo?.bar?.baz`))}`);
expect($t<ASTv1.BlockStatement>(`{{and (or this.foo)}}`)).toEqual(
`$:() => ${$mh('and', $mh('or', '$:this.foo'))}`,
);
expect($t<ASTv1.BlockStatement>(`{{and (or this.foo.bar)}}`)).toEqual(
`$:() => ${$mh('and', $mh('or', '$:this.foo?.bar'))}`,
);
expect($t<ASTv1.BlockStatement>(`{{and (or foo.bar.baz)}}`)).toEqual(
`$:() => ${$mh('and', $mh('or', '$:foo?.bar?.baz'))}`,
);
expect($t<ASTv1.BlockStatement>(`{{and (or foo.bar)}}`)).toEqual(
`$:() => ${$mh('and', $mh('or', `$:foo.bar`))}`,
);
expect($t<ASTv1.BlockStatement>(`{{and (or @foo.bar.baz)}}`)).toEqual(
`$:() => ${$mh('and', $mh('or', `$:this[$args].foo?.bar?.baz`))}`,
);
expect($t<ASTv1.BlockStatement>(`{{and (or @foo.bar)}}`)).toEqual(
`$:() => ${$mh('and', $mh('or', `$:this[$args].foo?.bar`))}`,
);
});
test('works as hash params', () => {
// maybeWrapped
const mW = (str: string) => {
if (flags.IS_GLIMMER_COMPAT_MODE) {
return `$:() => $:$__hash({a: () => ${str}})`;
} else {
return `$:() => $:$__hash({a: ${str}})`;
}
};
expect($t<ASTv1.BlockStatement>(`{{hash a=this.foo.bar.baz}}`)).toEqual(
mW('this.foo?.bar?.baz'),
);
expect($t<ASTv1.BlockStatement>(`{{hash a=this.foo}}`)).toEqual(
mW('this.foo'),
);
expect($t<ASTv1.BlockStatement>(`{{hash a=this.foo.bar}}`)).toEqual(
mW('this.foo?.bar'),
);
expect($t<ASTv1.BlockStatement>(`{{hash a=foo.bar.baz}}`)).toEqual(
mW('foo?.bar?.baz'),
);
expect($t<ASTv1.BlockStatement>(`{{hash a=foo.bar}}`)).toEqual(
mW('foo.bar'),
);
expect($t<ASTv1.BlockStatement>(`{{hash a=@foo.bar.baz}}`)).toEqual(
mW('this[$args].foo?.bar?.baz'),
);
expect($t<ASTv1.BlockStatement>(`{{hash a=@foo.bar}}`)).toEqual(
mW('this[$args].foo?.bar'),
);
});
});
describe('basic element helper support', () => {
test('it return kinda valid component-like code', () => {
expect($t<ASTv1.BlockStatement>(`{{(element "tag")}}`)).toEqual(
`$:() => $:function(args){const $fw = $_GET_FW(this, arguments);const $slots = $_GET_SLOTS(this, arguments);return{[$nodes]:[$_tag("tag", $fw,[()=>$_slot('default',()=>[],$slots)], this)], ctx: this};}`,
);
});
});
describe('Builtin helpers in MustacheStatements', () => {
test('fn helper properly mapped', () => {
expect($t<ASTv1.MustacheStatement>(`{{fn a "b" "c"}}`)).toEqual(
`$:() => $:$__fn($:a,"b","c")`,
);
});
test('if helper properly mapped', () => {
expect($t<ASTv1.MustacheStatement>(`{{if foo "bar" "baz"}}`)).toEqual(
`$:() => $:$__if($:foo,"bar","baz")`,
);
});
test('unless helper properly mapped', () => {
expect(
$t<ASTv1.MustacheStatement>(`{{unless foo "bar" "baz"}}`),
).toEqual(`$:() => $:$__if($:foo,"baz","bar")`);
});
test('eq helper properly mapped', () => {
expect($t<ASTv1.MustacheStatement>(`{{eq foo "bar" "baz"}}`)).toEqual(
`$:() => $:$__eq($:foo,"bar","baz")`,
);
});
test('debugger helper properly mapped', () => {
expect(
$t<ASTv1.MustacheStatement>(`{{debugger foo "bar" "baz"}}`),
).toEqual(`$:() => $:$__debugger.call($:this,$:foo,"bar","baz")`);
});
test('log helper properly mapped', () => {
expect($t<ASTv1.MustacheStatement>(`{{log foo "bar" "baz"}}`)).toEqual(
`$:() => $:$__log($:foo,"bar","baz")`,
);
});
test('array helper properly mapped', () => {
expect(
$t<ASTv1.MustacheStatement>(`{{array foo "bar" "baz"}}`),
).toEqual(`$:() => $:$__array($:foo,"bar","baz")`);
});
test('hash helper properly mapped', () => {
expect(
$t<ASTv1.MustacheStatement>(`{{hash foo="bar" boo="baz"}}`),
).toEqual(`$:() => $:$__hash({foo: "bar", boo: "baz"})`);
});
});
describe('special ember composition helpers', () => {
test('its properly converted', () => {
expect($t<ASTv1.MustacheStatement>(`{{component @cmp 123 name=hash}}`)).toEqual(
`$:() => $:${SYMBOLS.COMPONENT_HELPER}([$:this[$args].cmp,123],{name: ${$glimmerCompat('hash')}})`,
);
expect($t<ASTv1.MustacheStatement>(`{{helper @cmp 123 name=hash}}`)).toEqual(
`$:() => $:${SYMBOLS.HELPER_HELPER}([$:this[$args].cmp,123],{name: ${$glimmerCompat('hash')}})`,
);
expect($t<ASTv1.MustacheStatement>(`{{modifier @cmp 123 name=hash}}`)).toEqual(
`$:() => $:${SYMBOLS.MODIFIER_HELPER}([$:this[$args].cmp,123],{name: ${$glimmerCompat('hash')}})`,
);
});
});
describe('Builtin helpers in SubExpression', () => {
test('fn helper properly mapped', () => {
expect($t<ASTv1.MustacheStatement>(`{{q (fn a b (if c d))}}`)).toEqual(
`$:() => ${$mh('q', `$:$__fn($:a,$:b,$:$__if($:c,$:d))`)}`,
);
});
test('if helper properly mapped', () => {
expect($t<ASTv1.MustacheStatement>(`{{q (if a b (if c d))}}`)).toEqual(
`$:() => ${$mh('q', `$:$__if($:a,$:b,$:$__if($:c,$:d))`)}`,
);
});
test('unless helper properly mapped', () => {
expect(
$t<ASTv1.MustacheStatement>(`{{q (unless a b (if c d))}}`),
).toEqual(`$:() => ${$mh('q', `$:$__if($:a,$:$__if($:c,$:d),$:b)`)}`);
});
test('eq helper properly mapped', () => {
expect($t<ASTv1.MustacheStatement>(`{{q (eq a b)}}`)).toEqual(
`$:() => ${$mh('q', `$:$__eq($:a,$:b)`)}`,
);
});
test('debugger helper properly mapped', () => {
expect($t<ASTv1.MustacheStatement>(`{{q (debugger a)}}`)).toEqual(
`$:() => ${$mh('q', `$:$__debugger.call($:this,$:a)`)}`,
);
});
test('log helper properly mapped', () => {
expect($t<ASTv1.MustacheStatement>(`{{q (log a b)}}`)).toEqual(
`$:() => ${$mh('q', `$:$__log($:a,$:b)`)}`,
);
});
test('array helper properly mapped', () => {
expect(
$t<ASTv1.MustacheStatement>(`{{q (array foo "bar" "baz")}}`),
).toEqual(`$:() => ${$mh('q', `$:$__array($:foo,"bar","baz")`)}`);
});
test('hash helper properly mapped', () => {
expect(
$t<ASTv1.MustacheStatement>(`{{q (hash foo="bar" boo="baz")}}`),
).toEqual(`$:() => ${$mh('q', `$:$__hash({foo: "bar", boo: "baz"})`)}`);
});
});
describe('TextNode', () => {
test('converts a simple string', () => {
expect($t<ASTv1.TextNode>(`"Hello World"`)).toEqual(`"Hello World"`);
});
test('non empty text nodes not trimmed', () => {
expect($t<ASTv1.TextNode>(` foo`)).toEqual(` foo`);
});
test('empty non multiline text nodes preserved', () => {
expect($t<ASTv1.TextNode>(` `)).toEqual(' ');
});
test(' is preserved', () => {
expect($t<ASTv1.TextNode>(` `)).not.toEqual(null);
});
test('multispace lines are removed', () => {
expect($t<ASTv1.TextNode>(` `)).toEqual(null);
});
test('empty multiline text nodes trimmed', () => {
expect($t<ASTv1.TextNode>(` \n `)).toEqual(null);
});
});
describe('MustacheStatement', () => {
test('converts a args-less path', () => {
expect($t<ASTv1.MustacheStatement>(`{{foo-bar}}`)).toEqual(`$:foo-bar`);
});
test('converts a path with args', () => {
expect($t<ASTv1.MustacheStatement>(`{{foo-bar bas boo}}`)).toEqual(
`$:() => ${$mh('foo-bar', '$:bas,$:boo')}`,
);
});
test('converts sub-expression without args', () => {
expect($t<ASTv1.MustacheStatement>(`{{(foo-bar)}}`)).toEqual(
`$:() => ${$mh('foo-bar')}`,
);
});
test('supports helper composition', () => {
expect($t<ASTv1.MustacheStatement>(`{{(foo-bar (baz-bat))}}`)).toEqual(
`$:() => ${$mh('foo-bar', $mh('baz-bat'))}`,
);
});
test('support boolean literals', () => {
expect($t<ASTv1.MustacheStatement>(`{{true}}`)).toEqual(true);
expect($t<ASTv1.MustacheStatement>(`{{false}}`)).toEqual(false);
});
test('support null literals', () => {
expect($t<ASTv1.MustacheStatement>(`{{null}}`)).toEqual(null);
});
test('support undefined literals', () => {
expect($t<ASTv1.MustacheStatement>(`{{undefined}}`)).toEqual(undefined);
});
test('support bool, null, undefined as helper args', () => {
expect(
$t<ASTv1.MustacheStatement>(`{{foo true null undefined}}`),
).toEqual(`$:() => ${$mh('foo', 'true,null,undefined')}`);
});
});
describe('ElementNode', () => {
test('converts a simple element', () => {
expect($t<ASTv1.ElementNode>(`<div></div>`)).toEqual(
$node({ tag: 'div' }),
);
});
test('converts a simple element with string attribute', () => {
expect($t<ASTv1.ElementNode>(`<div class="foo"></div>`)).toEqual(
$node({
tag: 'div',
properties: [['', 'foo']],
}),
);
});
test('converts a simple element with string child', () => {
expect($t<ASTv1.ElementNode>(`<div> sd</div>`)).toEqual(
$node({
tag: 'div',
events: [[EVENT_TYPE.TEXT_CONTENT, ' sd']],
}),
);
});
test('converts a simple element with complex child', () => {
expect($t<ASTv1.ElementNode>(`<div> sd<span></span></div>`)).toEqual(
$node({
tag: 'div',
children: [' sd', $node({ tag: 'span' })],
}),
);
});
test('converts a simple element with concat string attribute', () => {
expect(
$t<ASTv1.ElementNode>(`<div class="{{foo}} bar {{boo baks}}"></div>`),
).toEqual(
$node({
tag: 'div',
properties: [
['', `$:() => [$:foo," bar ",${$mh('boo', '$:baks')}].join('')`],
],
}),
);
});
test('converts a simple element with path attribute', () => {
expect($t<ASTv1.ElementNode>(`<div class={{foo}}></div>`)).toEqual(
$node({
tag: 'div',
properties: [['', '$:foo']],
}),
);
});
test('converts a simple element with path attribute with string literal', () => {
expect(
$t<ASTv1.ElementNode>(`<div class={{foo "bar"}}></div>`),
).toEqual(
$node({
tag: 'div',
properties: [['', `$:() => ${$mh('foo', '"bar"')}`]],
}),
);
});
test('converts a simple element with path attribute with path literal', () => {
expect($t<ASTv1.ElementNode>(`<div class={{foo bar}}></div>`)).toEqual(
$node({
tag: 'div',
properties: [['', `$:() => ${$mh('foo', '$:bar')}`]],
}),
);
});
test('converts a simple element with `on` modifier', () => {
// @todo - likely need to return proper closure here (arrow function)
expect($t<ASTv1.ElementNode>(`<div {{on "click" foo}}></div>`)).toEqual(
$node({
tag: 'div',
events: [['click', '$:($e, $n) => $:foo($e, $n, )']],
}),
);
});
test('converts a simple element with `on` modifier, with composed args', () => {
// @todo - likely need to return proper closure here (arrow function)
expect(
$t<ASTv1.ElementNode>(`<div {{on "click" (foo bar baz)}}></div>`),
).toEqual(
$node({
tag: 'div',
events: [
['click', `$:($e, $n) => ${$mh('foo', '$:bar,$:baz')}($e, $n, )`],
],
}),
);
});
test('support custom modifiers', () => {
expect($t<ASTv1.ElementNode>(`<div {{foo-bar}}></div>`)).toEqual(
$node({
tag: 'div',
events: [['0', $mm('foo-bar')]],
}),
);
});
test('support helper as on modifier argument', () => {
expect($t<ASTv1.ElementNode>(`<div {{on "click" (optional tab.onClick)}}></div>`)).toEqual(
$node({
tag: 'div',
events: [['click', $mm('foo-bar')]],
}),
);
});
test('support custom modifiers with params ', () => {
expect(
$t<ASTv1.ElementNode>(
`<div {{foo-bar foo 1 true null undefined}}></div>`,
),
).toEqual(
$node({
tag: 'div',
events: [['0', $mm('foo-bar', '$:foo,1,true,null,undefined')]],
}),
);
});
test('support custom modifiers with hash params ', () => {
expect(
$t<ASTv1.ElementNode>(
`<div {{foo-bar a=1 b=true c=null d=undefined b="a" }}></div>`,
),
).toEqual(
$node({
tag: 'div',
events: [
[
'0',
$mm(
'foo-bar',
'',
'{a: 1, b: true, c: null, d: undefined, b: "a"}',
),
],
],
}),
);
});
});
describe('if condition', () => {
test('only true part', () => {
expect(
$t<ASTv1.BlockStatement>(`{{#if foo}}123{{/if}}`),
).toEqual<HBSControlExpression>(
$control({
condition: $glimmerCompat('$:foo'),
children: ['123'],
}),
);
});
test('both parts', () => {
expect(
$t<ASTv1.BlockStatement>(`{{#if foo}}123{{else}}456{{/if}}`),
).toEqual<HBSControlExpression>(
$control({
condition: $glimmerCompat('$:foo'),
children: ['123'],
inverse: ['456'],
}),
);
});
test('helper in condition', () => {
expect(
$t<ASTv1.BlockStatement>(`{{#if (foo bar)}}123{{else}}456{{/if}}`),
).toEqual<HBSControlExpression>(
$control({
type: 'if',
condition: $glimmerCompat($mh('foo', '$:bar')),
children: ['123'],
inverse: ['456'],
}),
);
expect(
$t<ASTv1.BlockStatement>(
`{{#unless (foo bar)}}123{{else}}456{{/unless}}`,
),
).toEqual<HBSControlExpression>(
$control({
type: 'if',
condition: $glimmerCompat($mh('foo', '$:bar')),
children: ['456'],
inverse: ['123'],
}),
);
});
});
describe('let condition', () => {
const mathRandom = Math.random;
beforeEach(() => {
Math.random = () => 0.001;
});
afterEach(() => {
Math.random = mathRandom;
});
test('it works', () => {
expect(
$t<ASTv1.BlockStatement>(
`{{#let foo "name" as |bar k|}}p{{bar}}{{k}}{{/let}}`,
),
).toEqual(
`$:...(() => {let self = this;let Let_bar_6c3gez6 = $:() => $:foo;let Let_k_6c3gez6 = "name";return [$_text("p"), ${
flags.IS_GLIMMER_COMPAT_MODE
? '() => Let_bar_6c3gez6'
: 'Let_bar_6c3gez6'
}, ${
flags.IS_GLIMMER_COMPAT_MODE
? '() => Let_k_6c3gez6'
: 'Let_k_6c3gez6'
}]})()`,
);
});
test('it not override arg assign case', () => {
const result = $t<ASTv1.BlockStatement>(
`{{#let foo "name" as |bar k|}}<Div @bar={{bar}} bar={{if bar bar}} />{{/let}}`,
);
if (flags.IS_GLIMMER_COMPAT_MODE) {
expect(result).toEqual(
`$:...(() => {let self = this;let Let_bar_6c3gez6 = $:() => $:foo;let Let_k_6c3gez6 = "name";return [$_c(Div,$_args({bar: () => Let_bar_6c3gez6},{},[[],[['bar', () => $:$__if($:Let_bar_6c3gez6,$:Let_bar_6c3gez6)]],[]]), this)]})()`,
);
} else {
expect(result).toEqual(
`$:...(() => {let self = this;let Let_bar_6c3gez6 = $:() => $:foo;let Let_k_6c3gez6 = "name";return [$_c(Div,{bar: Let_bar_6c3gez6, "$:[$PROPS_SYMBOL]": [[],[['bar', () => $:$__if($:Let_bar_6c3gez6,$:Let_bar_6c3gez6)]],[]]}, this)]})()`,
);
}
});
});
describe('each condition', () => {
test('it adds unstable child wrapper for simple multi-nodes', () => {
const converted = $t<ASTv1.BlockStatement>(
`{{#each foo as |bar|}}<div></div><span></span>{{/each}}`,
);
expect(converted).toEqual<HBSControlExpression>(
$control({
type: 'each',
condition: $glimmerCompat('$:foo'),
blockParams: ['bar'],
children: [$node({ tag: 'div' }), $node({ tag: 'span' })],
}),
);
expect($s(converted)).toEqual(
`$_each(${$glimmerCompat(
'foo',
)}, (bar,$index,ctx0) => [$_ucw((ctx1) => [$_tag('div', $_edp, [], ctx1), $_tag('span', $_edp, [], ctx1)], ctx0)], null, this)`,
);
});
test('it not add unstable child wrapper for simple node', () => {
const converted = $t<ASTv1.BlockStatement>(
`{{#each foo as |bar|}}<div></div>{{/each}}`,
);
expect(converted).toEqual<HBSControlExpression>(
$control({
type: 'each',
condition: $glimmerCompat('$:foo'),
blockParams: ['bar'],
children: [$node({ tag: 'div' })],
}),
);
expect($s(converted)).toEqual(
`$_each(${$glimmerCompat(
'foo',
)}, (bar,$index,ctx0) => [$_tag('div', $_edp, [], ctx0)], null, this)`,
);
});
test('it do not add UnstableChildWrapper if we have component surrounded by empty text', () => {
const converted = $t<ASTv1.BlockStatement>(
`{{#each foo as |bar|}} <Smile /> {{/each}}`,
);
expect($s(converted)).toEqual(
`$_each(${$glimmerCompat(
'foo',
)}, (bar,$index,ctx0) => [$_c(Smile,${$args(
'{}',
)}, ctx0)], null, this)`,
);
});
test('it add UnstableChildWrapper if component surrounded my meaningful text', () => {
const converted = $t<ASTv1.BlockStatement>(
`{{#each foo as |bar|}}1<Smile />{{/each}}`,
);
expect($s(converted)).toEqual(
`$_each(${$glimmerCompat(
'foo',
)}, (bar,$index,ctx0) => [$_ucw((ctx1) => [$_text("1"), $_c(Smile,${$args(
'{}',
)}, ctx1)], ctx0)], null, this)`,
);
});
test('it works', () => {
expect(
$t<ASTv1.BlockStatement>(`{{#each foo as |bar index|}}123{{/each}}`),
).toEqual<HBSControlExpression>(
$control({
type: 'each',
condition: $glimmerCompat('$:foo'),
blockParams: ['bar', 'index'],
children: ['123'],
}),
);
});
test('it could provide keys', () => {
expect(
$t<ASTv1.BlockStatement>(
`{{#each foo key="id" as |bar index|}}123{{/each}}`,
),
).toEqual<HBSControlExpression>(
$control({
type: 'each',
condition: $glimmerCompat('$:foo'),
blockParams: ['bar', 'index'],
children: ['123'],
key: 'id',
}),
);
});
});
describe('stableChildDetection', () => {
test('detects stable child', () => {
expect($t<ASTv1.ElementNode>(`<div>foo</div>`)).toEqual(
$node({
tag: 'div',
hasStableChild: true,
events: [[EVENT_TYPE.TEXT_CONTENT, 'foo']],
}),
);
expect($t<ASTv1.ElementNode>(`<div><p></p></div>`)).toEqual(
$node({
tag: 'div',
hasStableChild: true,
children: [$node({ tag: 'p' })],
}),
);
expect($t<ASTv1.ElementNode>(`<div><:slot></:slot></div>`)).toEqual(
$node({
tag: 'div',
hasStableChild: false,
children: [$node({ tag: ':slot' })],
}),
);
expect(
$t<ASTv1.ElementNode>(`<div>{{#if foo}}123{{/if}}</div>`),
).toEqual(
$node({
tag: 'div',
hasStableChild: false,
children: [
$control({
condition: $glimmerCompat('$:foo'),
children: ['123'],
}),
],
}),
);
});
});
describe('components', () => {
test('it forwarding props', () => {
expect($t<ASTv1.ElementNode>(`<DIV @name={{1}}></DIV>`)).toEqual(
$node({
tag: 'DIV',
attributes: [['@name', 1]],
}),
);
});
});
});
});