/
custom-helper-test.js
967 lines (714 loc) · 27.1 KB
/
custom-helper-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
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
import { DEBUG } from '@glimmer/env';
import {
RenderingTestCase,
moduleFor,
runDestroy,
runTask,
defineSimpleHelper,
} from 'internal-test-helpers';
import { Helper, Component } from '@ember/-internals/glimmer';
import { set } from '@ember/-internals/metal';
moduleFor(
'Helpers test: custom helpers',
class extends RenderingTestCase {
['@test it cannot override built-in syntax']() {
this.registerHelper('array', () => 'Nope');
expectAssertion(() => {
this.render(`{{array this.foo 'LOL'}}`, { foo: true });
}, /You attempted to overwrite the built-in helper "array" which is not allowed. Please rename the helper./);
}
['@test it can resolve custom simple helpers with or without dashes']() {
this.registerHelper('hello', () => 'hello');
this.registerHelper('hello-world', () => 'hello world');
this.render('{{hello}} | {{hello-world}}');
this.assertText('hello | hello world');
runTask(() => this.rerender());
this.assertText('hello | hello world');
}
['@test it does not resolve helpers with a `.` (period)']() {
expectDeprecation(
/The `[^`]+` property(?: path)? was used in the `[^`]+` template without using `this`. This fallback behavior has been deprecated, all properties must be looked up on `this` when used in the template: {{[^}]+}}/
);
this.registerHelper('hello.world', () => 'hello world');
this.render('{{hello.world}}', {
hello: {
world: '',
},
});
this.assertText('');
this.assertStableRerender();
this.assertText('');
runTask(() => set(this.context, 'hello', { world: 'hello world!' }));
this.assertText('hello world!');
runTask(() => {
set(this.context, 'hello', {
world: '',
});
});
this.assertText('');
}
['@test it can resolve custom class-based helpers with or without dashes']() {
this.registerHelper('hello', {
compute() {
return 'hello';
},
});
this.registerHelper('hello-world', {
compute() {
return 'hello world';
},
});
this.render('{{hello}} | {{hello-world}}');
this.assertText('hello | hello world');
runTask(() => this.rerender());
this.assertText('hello | hello world');
}
['@test throws if `this._super` is not called from `init`']() {
this.registerHelper('hello-world', {
init() {},
});
expectAssertion(() => {
this.render('{{hello-world}}');
}, /You must call `super.init\(...arguments\);` or `this._super\(...arguments\)` when overriding `init` on a framework object. Please update .*/);
}
['@test class-based helper can recompute a new value'](assert) {
let destroyCount = 0;
let computeCount = 0;
let helper;
this.registerHelper('hello-world', {
init() {
this._super(...arguments);
helper = this;
},
compute() {
return ++computeCount;
},
destroy() {
destroyCount++;
this._super();
},
});
this.render('{{hello-world}}');
this.assertText('1');
runTask(() => this.rerender());
this.assertText('1');
runTask(() => helper.recompute());
this.assertText('2');
assert.strictEqual(destroyCount, 0, 'destroy is not called on recomputation');
}
['@test class-based helper lifecycle'](assert) {
let hooks = [];
let helper;
this.registerHelper('hello-world', {
init() {
this._super(...arguments);
hooks.push('init');
helper = this;
},
compute() {
hooks.push('compute');
},
willDestroy() {
hooks.push('willDestroy');
this._super();
},
destroy() {
hooks.push('destroy');
this._super();
},
});
this.render('{{#if this.show}}{{hello-world}}{{/if}}', {
show: true,
});
assert.deepEqual(hooks, ['init', 'compute']);
runTask(() => this.rerender());
assert.deepEqual(hooks, ['init', 'compute']);
runTask(() => helper.recompute());
assert.deepEqual(hooks, ['init', 'compute', 'compute']);
runTask(() => set(this.context, 'show', false));
assert.deepEqual(hooks, ['init', 'compute', 'compute', 'destroy', 'willDestroy']);
}
['@test class-based helper with static arguments can recompute a new value'](assert) {
let destroyCount = 0;
let computeCount = 0;
let helper;
this.registerHelper('hello-world', {
init() {
this._super(...arguments);
helper = this;
},
compute() {
return ++computeCount;
},
destroy() {
destroyCount++;
this._super();
},
});
this.render('{{hello-world "whut"}}');
this.assertText('1');
runTask(() => this.rerender());
this.assertText('1');
runTask(() => helper.recompute());
this.assertText('2');
assert.strictEqual(destroyCount, 0, 'destroy is not called on recomputation');
}
// https://github.com/emberjs/ember.js/issues/14774
['@test class-based helper with static arguments can recompute a new value without a runloop'](
assert
) {
let destroyCount = 0;
let computeCount = 0;
let helper;
this.registerHelper('hello-world', {
init() {
this._super(...arguments);
helper = this;
},
compute() {
return ++computeCount;
},
destroy() {
destroyCount++;
this._super();
},
});
this.render('{{hello-world "whut"}}');
this.assertText('1');
runTask(() => this.rerender());
this.assertText('1');
helper.recompute();
this.assertText('2');
assert.strictEqual(destroyCount, 0, 'destroy is not called on recomputation');
}
['@test helper params can be returned']() {
this.registerHelper('hello-world', (values) => {
return values;
});
this.render('{{#each (hello-world this.model) as |item|}}({{item}}){{/each}}', {
model: ['bob'],
});
this.assertText('(bob)');
}
['@test helper hash can be returned']() {
this.registerHelper('hello-world', (_, hash) => {
return hash.model;
});
this.render(`{{get (hello-world model=this.model) 'name'}}`, {
model: { name: 'bob' },
});
this.assertText('bob');
}
['@test simple helper is called for param changes'](assert) {
let computeCount = 0;
this.registerHelper('hello-world', ([value]) => {
computeCount++;
return `${value}-value`;
});
this.render('{{hello-world this.model.name}}', {
model: { name: 'bob' },
});
this.assertText('bob-value');
assert.strictEqual(computeCount, 1, 'compute is called exactly 1 time');
runTask(() => this.rerender());
this.assertText('bob-value');
assert.strictEqual(computeCount, 1, 'compute is called exactly 1 time');
runTask(() => set(this.context, 'model.name', 'sal'));
this.assertText('sal-value');
assert.strictEqual(computeCount, 2, 'compute is called exactly 2 times');
runTask(() => set(this.context, 'model', { name: 'bob' }));
this.assertText('bob-value');
assert.strictEqual(computeCount, 3, 'compute is called exactly 3 times');
}
['@test class-based helper compute is called for param changes'](assert) {
let createCount = 0;
let computeCount = 0;
this.registerHelper('hello-world', {
init() {
this._super(...arguments);
createCount++;
},
compute([value]) {
computeCount++;
return `${value}-value`;
},
});
this.render('{{hello-world this.model.name}}', {
model: { name: 'bob' },
});
this.assertText('bob-value');
assert.strictEqual(computeCount, 1, 'compute is called exactly 1 time');
runTask(() => this.rerender());
this.assertText('bob-value');
assert.strictEqual(computeCount, 1, 'compute is called exactly 1 time');
runTask(() => set(this.context, 'model.name', 'sal'));
this.assertText('sal-value');
assert.strictEqual(computeCount, 2, 'compute is called exactly 2 times');
runTask(() => set(this.context, 'model', { name: 'bob' }));
this.assertText('bob-value');
assert.strictEqual(computeCount, 3, 'compute is called exactly 3 times');
assert.strictEqual(createCount, 1, 'helper is only created once');
}
['@test simple helper receives params, hash']() {
this.registerHelper('hello-world', (_params, _hash) => {
return `params: ${JSON.stringify(_params)}, hash: ${JSON.stringify(_hash)}`;
});
this.render('{{hello-world this.model.name "rich" first=this.model.age last="sam"}}', {
model: {
name: 'bob',
age: 42,
},
});
this.assertText('params: ["bob","rich"], hash: {"first":42,"last":"sam"}');
runTask(() => this.rerender());
this.assertText('params: ["bob","rich"], hash: {"first":42,"last":"sam"}');
runTask(() => set(this.context, 'model.name', 'sal'));
this.assertText('params: ["sal","rich"], hash: {"first":42,"last":"sam"}');
runTask(() => set(this.context, 'model.age', 28));
this.assertText('params: ["sal","rich"], hash: {"first":28,"last":"sam"}');
runTask(() => set(this.context, 'model', { name: 'bob', age: 42 }));
this.assertText('params: ["bob","rich"], hash: {"first":42,"last":"sam"}');
}
['@test class-based helper receives params, hash']() {
this.registerHelper('hello-world', {
compute(_params, _hash) {
return `params: ${JSON.stringify(_params)}, hash: ${JSON.stringify(_hash)}`;
},
});
this.render('{{hello-world this.model.name "rich" first=this.model.age last="sam"}}', {
model: {
name: 'bob',
age: 42,
},
});
this.assertText('params: ["bob","rich"], hash: {"first":42,"last":"sam"}');
runTask(() => this.rerender());
this.assertText('params: ["bob","rich"], hash: {"first":42,"last":"sam"}');
runTask(() => set(this.context, 'model.name', 'sal'));
this.assertText('params: ["sal","rich"], hash: {"first":42,"last":"sam"}');
runTask(() => set(this.context, 'model.age', 28));
this.assertText('params: ["sal","rich"], hash: {"first":28,"last":"sam"}');
runTask(() => set(this.context, 'model', { name: 'bob', age: 42 }));
this.assertText('params: ["bob","rich"], hash: {"first":42,"last":"sam"}');
}
['@test class-based helper usable in subexpressions']() {
this.registerHelper('join-words', {
compute(params) {
return params.join(' ');
},
});
this.render(
`{{join-words "Who"
(join-words "overcomes" "by")
this.model.reason
(join-words (join-words "hath overcome but" "half"))
(join-words "his" (join-words "foe"))}}`,
{ model: { reason: 'force' } }
);
this.assertText('Who overcomes by force hath overcome but half his foe');
runTask(() => this.rerender());
this.assertText('Who overcomes by force hath overcome but half his foe');
runTask(() => set(this.context, 'model.reason', 'Nickleback'));
this.assertText('Who overcomes by Nickleback hath overcome but half his foe');
runTask(() => set(this.context, 'model', { reason: 'force' }));
this.assertText('Who overcomes by force hath overcome but half his foe');
}
['@test parameterless helper is usable in subexpressions']() {
this.registerHelper('should-show', () => {
return true;
});
this.render(`{{#if (should-show)}}true{{/if}}`);
this.assertText('true');
runTask(() => this.rerender());
this.assertText('true');
}
['@test parameterless helper is usable in attributes']() {
this.registerHelper('foo-bar', () => {
return 'baz';
});
this.render(`<div data-foo-bar="{{foo-bar}}"></div>`);
this.assertHTML('<div data-foo-bar="baz"></div>');
runTask(() => this.rerender());
this.assertHTML('<div data-foo-bar="baz"></div>');
}
['@test simple helper not usable with a block'](assert) {
if (!DEBUG) {
assert.expect(0);
return;
}
this.registerHelper('some-helper', () => {});
assert.throws(() => {
this.render(`{{#some-helper}}{{/some-helper}}`);
}, /Attempted to resolve `some-helper`, which was expected to be a component, but nothing was found./);
}
['@test class-based helper not usable with a block'](assert) {
if (!DEBUG) {
assert.expect(0);
return;
}
this.registerHelper('some-helper', {
compute() {},
});
assert.throws(() => {
this.render(`{{#some-helper}}{{/some-helper}}`);
}, /Attempted to resolve `some-helper`, which was expected to be a component, but nothing was found./);
}
['@test simple helper not usable within element'](assert) {
if (!DEBUG) {
assert.expect(0);
return;
}
this.registerHelper('some-helper', () => {});
assert.throws(() => {
this.render(`<div {{some-helper}}></div>`);
}, /Attempted to resolve `some-helper`, which was expected to be a modifier, but nothing was found./);
}
['@test class-based helper not usable within element'](assert) {
if (!DEBUG) {
assert.expect(0);
return;
}
this.registerHelper('some-helper', {
compute() {},
});
assert.throws(() => {
this.render(`<div {{some-helper}}></div>`);
}, /Attempted to resolve `some-helper`, which was expected to be a modifier, but nothing was found./);
}
['@test class-based helper is torn down'](assert) {
let destroyCalled = 0;
this.registerHelper('some-helper', {
destroy() {
destroyCalled++;
this._super(...arguments);
},
compute() {
return 'must define a compute';
},
});
this.render(`{{some-helper}}`);
runDestroy(this.component);
assert.strictEqual(destroyCalled, 1, 'destroy called once');
}
['@test class-based helper used in subexpression can recompute']() {
let helper;
let phrase = 'overcomes by';
this.registerHelper('dynamic-segment', {
init() {
this._super(...arguments);
helper = this;
},
compute() {
return phrase;
},
});
this.registerHelper('join-words', {
compute(params) {
return params.join(' ');
},
});
this.render(
`{{join-words "Who"
(dynamic-segment)
"force"
(join-words (join-words "hath overcome but" "half"))
(join-words "his" (join-words "foe"))}}`
);
this.assertText('Who overcomes by force hath overcome but half his foe');
runTask(() => this.rerender());
this.assertText('Who overcomes by force hath overcome but half his foe');
phrase = 'believes his';
runTask(() => helper.recompute());
this.assertText('Who believes his force hath overcome but half his foe');
phrase = 'overcomes by';
runTask(() => helper.recompute());
this.assertText('Who overcomes by force hath overcome but half his foe');
}
['@test class-based helper used in subexpression can recompute component']() {
let helper;
let phrase = 'overcomes by';
this.registerHelper('dynamic-segment', {
init() {
this._super(...arguments);
helper = this;
},
compute() {
return phrase;
},
});
this.registerHelper('join-words', {
compute(params) {
return params.join(' ');
},
});
this.registerComponent('some-component', {
template: '{{@first}} {{@second}} {{@third}} {{@fourth}} {{@fifth}}',
});
this.render(
`{{some-component first="Who"
second=(dynamic-segment)
third="force"
fourth=(join-words (join-words "hath overcome but" "half"))
fifth=(join-words "his" (join-words "foe"))}}`
);
this.assertText('Who overcomes by force hath overcome but half his foe');
runTask(() => this.rerender());
this.assertText('Who overcomes by force hath overcome but half his foe');
phrase = 'believes his';
runTask(() => helper.recompute());
this.assertText('Who believes his force hath overcome but half his foe');
phrase = 'overcomes by';
runTask(() => helper.recompute());
this.assertText('Who overcomes by force hath overcome but half his foe');
}
['@test class-based helper used in subexpression is destroyed'](assert) {
let destroyCount = 0;
this.registerHelper('dynamic-segment', {
phrase: 'overcomes by',
init() {
this._super(...arguments);
},
compute() {
return this.phrase;
},
destroy() {
destroyCount++;
this._super(...arguments);
},
});
this.registerHelper('join-words', {
compute(params) {
return params.join(' ');
},
});
this.render(
`{{join-words "Who"
(dynamic-segment)
"force"
(join-words (join-words "hath overcome but" "half"))
(join-words "his" (join-words "foe"))}}`
);
runDestroy(this.component);
assert.equal(destroyCount, 1, 'destroy is called after a view is destroyed');
}
['@test simple helper can be invoked manually via `owner.factoryFor(...).create().compute()'](
assert
) {
this.registerHelper('some-helper', () => {
assert.ok(true, 'some-helper helper invoked');
return 'lolol';
});
let instance = this.owner.factoryFor('helper:some-helper').create();
assert.equal(typeof instance.compute, 'function', 'expected instance.compute to be present');
assert.equal(instance.compute(), 'lolol', 'can invoke `.compute`');
}
['@test class-based helper can be invoked manually via `owner.factoryFor(...).create().compute()'](
assert
) {
this.registerHelper('some-helper', {
compute() {
assert.ok(true, 'some-helper helper invoked');
return 'lolol';
},
});
let instance = this.owner.factoryFor('helper:some-helper').create();
assert.equal(typeof instance.compute, 'function', 'expected instance.compute to be present');
assert.equal(instance.compute(), 'lolol', 'can invoke `.compute`');
}
['@test class-based helper in native ES syntax receives owner'](assert) {
let testContext = this;
this.add(
'helper:hello-world',
class extends Helper {
constructor(owner) {
super(owner);
assert.equal(owner, testContext.owner, 'owner was passed as a constructor argument');
}
compute() {
return 'huzza!';
}
}
);
this.render('{{hello-world}}');
this.assertText('huzza!');
}
'@feature(EMBER_DYNAMIC_HELPERS_AND_MODIFIERS) Can resolve a helper'() {
this.registerHelper('hello-world', ([text]) => text ?? 'Hello, world!');
this.render('[{{helper "hello-world"}}][{{helper (helper "hello-world") "wow"}}]');
this.assertText('[Hello, world!][wow]');
this.assertStableRerender();
}
'@feature(EMBER_DYNAMIC_HELPERS_AND_MODIFIERS) Cannot dynamically resolve a helper'(assert) {
this.registerHelper('hello-world', () => 'Hello, world!');
if (DEBUG) {
expectAssertion(
() => this.render('{{helper this.name}}', { name: 'hello-world' }),
/Passing a dynamic string to the `\(helper\)` keyword is disallowed\./
);
} else {
assert.expect(0);
}
}
'@feature(EMBER_DYNAMIC_HELPERS_AND_MODIFIERS) Can use a curried dynamic helper'() {
let val = defineSimpleHelper((value) => value);
this.registerComponent('foo', {
template: '{{@value}}',
});
this.registerComponent('bar', {
template: '<Foo @value={{helper this.val "Hello, world!"}}/>',
ComponentClass: Component.extend({ val }),
});
this.render('<Bar/>');
this.assertText('Hello, world!');
this.assertStableRerender();
}
'@feature(!EMBER_DYNAMIC_HELPERS_AND_MODIFIERS) Can use a curried dynamic helper'() {
expectAssertion(() => {
this.registerComponent('bar', {
template: '<Foo @value={{helper this.val "Hello, world!"}}/>',
});
}, /Cannot use the \(helper\) keyword yet, as it has not been implemented/);
}
'@feature(EMBER_DYNAMIC_HELPERS_AND_MODIFIERS) Can use a dynamic helper with nested helpers'() {
let foo = defineSimpleHelper(() => 'world!');
let bar = defineSimpleHelper((value) => 'Hello, ' + value);
this.registerComponent('baz', {
template: '{{this.bar (this.foo)}}',
ComponentClass: Component.extend({ foo, bar }),
});
this.render('<Baz/>');
this.assertText('Hello, world!');
this.assertStableRerender();
}
['@test helpers are not computed eagerly when used with if expressions'](assert) {
this.registerHelper('is-ok', () => 'hello');
this.registerHelper('throws-error', () => assert.ok(false, 'helper was computed eagerly'));
this.render('{{if true (is-ok) (throws-error)}}');
this.assertText('hello');
runTask(() => this.rerender());
this.assertText('hello');
}
}
);
if (DEBUG) {
class HelperMutatingArgsTests extends RenderingTestCase {
buildCompute() {
return (params, hash) => {
this.assert.throws(() => {
params.push('foo');
// cannot assert error message as it varies by platform
});
this.assert.throws(() => {
hash.foo = 'bar';
// cannot assert error message as it varies by platform
});
this.assert.throws(() => {
hash.someUnusedHashProperty = 'bar';
// cannot assert error message as it varies by platform
});
};
}
['@test cannot mutate params - no positional specified / named specified']() {
this.render('{{test-helper foo=this.bar}}', { bar: 'derp' });
}
['@test cannot mutate params - positional specified / no named specified']() {
this.render('{{test-helper this.bar}}', { bar: 'derp' });
}
['@test cannot mutate params - positional specified / named specified']() {
this.render('{{test-helper this.bar foo=this.qux}}', { bar: 'derp', qux: 'baz' });
}
['@test cannot mutate params - no positional specified / no named specified']() {
this.render('{{test-helper}}', { bar: 'derp', qux: 'baz' });
}
}
moduleFor(
'Helpers test: mutation triggers errors - class based helper',
class extends HelperMutatingArgsTests {
constructor() {
super(...arguments);
let compute = this.buildCompute();
this.registerHelper('test-helper', {
compute,
});
}
}
);
moduleFor(
'Helpers test: mutation triggers errors - simple helper',
class extends HelperMutatingArgsTests {
constructor() {
super(...arguments);
let compute = this.buildCompute();
this.registerHelper('test-helper', compute);
}
}
);
moduleFor(
'Helpers test: argument-less helper invocation in named arguments position',
class extends RenderingTestCase {
constructor() {
super(...arguments);
this.registerComponent('bar', {
template: '[{{is-string @content}}][{{@content}}]',
});
this.registerHelper('is-string', ([value]) => typeof value === 'string');
}
['@test invoking an argument-less helper without parens in named argument position is deprecated']() {
this.registerHelper('foo', () => 'Hello, world!');
expectDeprecation(
() => this.render('<Bar @content={{foo}} />', { foo: 'Not it!' }),
new RegExp(
/The `foo` helper was used in the `-top-level` template as /.source +
/`@content={{foo}}`\. This is ambigious between wanting the `@content` /.source +
/argument to be the `foo` helper itself, or the result of invoking the /.source +
/`foo` helper \(current behavior\)\. This implicit invocation behavior /.source +
/has been deprecated\./.source
)
);
this.assertText('[true][Hello, world!]');
this.assertStableRerender();
}
['@test invoking an argument-less helper with parens in named argument position is not deprecated']() {
this.registerHelper('foo', () => 'Hello, world!');
expectNoDeprecation(() => this.render('<Bar @content={{(foo)}} />', { foo: 'Not it!' }));
this.assertText('[true][Hello, world!]');
this.assertStableRerender();
}
['@test invoking an argument-less helper with quotes in named argument position is not deprecated']() {
this.registerHelper('foo', () => 'Hello, world!');
expectNoDeprecation(() => this.render('<Bar @content="{{foo}}" />', { foo: 'Not it!' }));
this.assertText('[true][Hello, world!]');
this.assertStableRerender();
}
['@test passing a local helper in named argument position is not deprecated']() {
let foo = defineSimpleHelper(() => 'Hello, world!');
expectNoDeprecation(() =>
this.render(`{{#let this.foo as |foo|}}<Bar @content={{foo}} />{{/let}}`, { foo })
);
this.assertText('[false][Hello, world!]');
this.assertStableRerender();
}
// TODO: this one really should work, and there is a passing test in glimmer-vm,
// but somehow it doesn't work here. This is almost certainly a VM bug as something
// is trying to call `block.compile()` but `block` is the reference for `this.foo`.
// So the execution stack is probably off-by-one or something.
['@test invoking a local helper with parens in named argument position is not deprecated']() {
let foo = defineSimpleHelper(() => 'Hello, world!');
expectNoDeprecation(() =>
this.render(`{{#let this.foo as |foo|}}<Bar @content={{(foo)}} />{{/let}}`, { foo })
);
this.assertText('[true][Hello, world!]');
this.assertStableRerender();
}
// TODO: this one doesn't work yet, and there is a failing test in glimmer-vm
['@skip invoking a helper with quotes in named argument position is not deprecated']() {
let foo = defineSimpleHelper(() => 'Hello, world!');
expectNoDeprecation(() =>
this.render(`{{#let this.foo as |foo|}}<Bar @content="{{foo}}" />{{/let}}`, { foo })
);
this.assertText('[true][Hello, world!]');
this.assertStableRerender();
}
}
);
}