/
target-action-test.js
785 lines (670 loc) · 20.9 KB
/
target-action-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
import {
moduleFor,
RenderingTestCase,
ApplicationTestCase,
strip,
runTask,
} from 'internal-test-helpers';
import { set, Mixin } from '@ember/-internals/metal';
import Controller from '@ember/controller';
import { Object as EmberObject } from '@ember/-internals/runtime';
import { Route } from '@ember/-internals/routing';
import { Component } from '../../utils/helpers';
function expectSendActionDeprecation(fn) {
expectDeprecation(
fn,
/You called (.*).sendAction\((.*)\) but Component#sendAction is deprecated. Please use closure actions instead./
);
}
moduleFor(
'Components test: sendAction',
class extends RenderingTestCase {
constructor() {
super(...arguments);
this.actionCounts = {};
this.sendCount = 0;
this.actionArguments = null;
let self = this;
this.registerComponent('action-delegate', {
ComponentClass: Component.extend({
init() {
this._super();
self.delegate = this;
this.name = 'action-delegate';
},
}),
});
}
renderDelegate(template = '{{action-delegate}}', context = {}) {
let root = this;
context = Object.assign(context, {
send(actionName, ...args) {
root.sendCount++;
root.actionCounts[actionName] = root.actionCounts[actionName] || 0;
root.actionCounts[actionName]++;
root.actionArguments = args;
},
});
this.render(template, context);
}
assertSendCount(count) {
this.assert.equal(this.sendCount, count, `Send was called ${count} time(s)`);
}
assertNamedSendCount(actionName, count) {
this.assert.equal(
this.actionCounts[actionName],
count,
`An action named '${actionName}' was sent ${count} times`
);
}
assertSentWithArgs(expected, message = 'arguments were sent with the action') {
this.assert.deepEqual(this.actionArguments, expected, message);
}
['@test Calling sendAction on a component without an action defined does nothing']() {
this.renderDelegate();
expectSendActionDeprecation(() => {
runTask(() => this.delegate.sendAction());
});
this.assertSendCount(0);
}
['@test Calling sendAction on a component with an action defined calls send on the controller']() {
this.renderDelegate();
expectSendActionDeprecation(() => {
runTask(() => {
set(this.delegate, 'action', 'addItem');
this.delegate.sendAction();
});
});
this.assertSendCount(1);
this.assertNamedSendCount('addItem', 1);
}
['@test Calling sendAction on a component with a function calls the function']() {
this.assert.expect(2);
this.renderDelegate();
expectSendActionDeprecation(() => {
runTask(() => {
set(this.delegate, 'action', () => this.assert.ok(true, 'function is called'));
this.delegate.sendAction();
});
});
}
['@test Calling sendAction on a component with a function calls the function with arguments']() {
this.assert.expect(2);
let argument = {};
this.renderDelegate();
expectSendActionDeprecation(() => {
runTask(() => {
set(this.delegate, 'action', (actualArgument) => {
this.assert.deepEqual(argument, actualArgument, 'argument is passed');
});
this.delegate.sendAction('action', argument);
});
});
}
// TODO consolidate these next 2 tests
['@test Calling sendAction on a component with a reference attr calls the function with arguments']() {
this.renderDelegate('{{action-delegate playing=this.playing}}', {
playing: null,
});
expectSendActionDeprecation(() => {
runTask(() => this.delegate.sendAction());
});
this.assertSendCount(0);
runTask(() => {
set(this.context, 'playing', 'didStartPlaying');
});
expectSendActionDeprecation(() => {
runTask(() => {
this.delegate.sendAction('playing');
});
});
this.assertSendCount(1);
this.assertNamedSendCount('didStartPlaying', 1);
}
['@test Calling sendAction on a component with a {{mut}} attr calls the function with arguments']() {
this.renderDelegate('{{action-delegate playing=(mut this.playing)}}', {
playing: null,
});
expectSendActionDeprecation(() => {
runTask(() => this.delegate.sendAction('playing'));
});
this.assertSendCount(0);
runTask(() => this.delegate.attrs.playing.update('didStartPlaying'));
expectSendActionDeprecation(() => {
runTask(() => this.delegate.sendAction('playing'));
});
this.assertSendCount(1);
this.assertNamedSendCount('didStartPlaying', 1);
}
["@test Calling sendAction with a named action uses the component's property as the action name"]() {
this.renderDelegate();
let component = this.delegate;
expectSendActionDeprecation(() => {
runTask(() => {
set(this.delegate, 'playing', 'didStartPlaying');
component.sendAction('playing');
});
});
this.assertSendCount(1);
this.assertNamedSendCount('didStartPlaying', 1);
expectSendActionDeprecation(() => {
runTask(() => component.sendAction('playing'));
});
this.assertSendCount(2);
this.assertNamedSendCount('didStartPlaying', 2);
expectSendActionDeprecation(() => {
runTask(() => {
set(component, 'action', 'didDoSomeBusiness');
component.sendAction();
});
});
this.assertSendCount(3);
this.assertNamedSendCount('didDoSomeBusiness', 1);
}
['@test Calling sendAction when the action name is not a string raises an exception']() {
this.renderDelegate();
runTask(() => {
set(this.delegate, 'action', {});
set(this.delegate, 'playing', {});
});
expectSendActionDeprecation(() => {
expectAssertion(() => this.delegate.sendAction());
});
expectSendActionDeprecation(() => {
expectAssertion(() => this.delegate.sendAction('playing'));
});
}
['@test Calling sendAction on a component with contexts']() {
this.renderDelegate();
let testContext = { song: 'She Broke My Ember' };
let firstContext = { song: 'She Broke My Ember' };
let secondContext = { song: 'My Achey Breaky Ember' };
expectSendActionDeprecation(() => {
runTask(() => {
set(this.delegate, 'playing', 'didStartPlaying');
this.delegate.sendAction('playing', testContext);
});
});
this.assertSendCount(1);
this.assertNamedSendCount('didStartPlaying', 1);
this.assertSentWithArgs([testContext], 'context was sent with the action');
expectSendActionDeprecation(() => {
runTask(() => {
this.delegate.sendAction('playing', firstContext, secondContext);
});
});
this.assertSendCount(2);
this.assertNamedSendCount('didStartPlaying', 2);
this.assertSentWithArgs(
[firstContext, secondContext],
'multiple contexts were sent to the action'
);
}
['@test calling sendAction on a component within a block sends to the outer scope GH#14216'](
assert
) {
let testContext = this;
// overrides default action-delegate so actions can be added
this.registerComponent('action-delegate', {
ComponentClass: Component.extend({
init() {
this._super();
testContext.delegate = this;
this.name = 'action-delegate';
},
actions: {
derp(arg1) {
assert.ok(true, 'action called on action-delgate');
assert.equal(arg1, 'something special', 'argument passed through properly');
},
},
}),
template: strip`
{{#component-a}}
{{component-b bar="derp"}}
{{/component-a}}
`,
});
this.registerComponent('component-a', {
ComponentClass: Component.extend({
init() {
this._super(...arguments);
this.name = 'component-a';
},
actions: {
derp() {
assert.ok(false, 'no! bad scoping!');
},
},
}),
});
let innerChild;
this.registerComponent('component-b', {
ComponentClass: Component.extend({
init() {
this._super(...arguments);
innerChild = this;
this.name = 'component-b';
},
}),
});
this.renderDelegate();
expectSendActionDeprecation(() => {
runTask(() => innerChild.sendAction('bar', 'something special'));
});
}
['@test asserts if called on a destroyed component']() {
let component;
this.registerComponent('rip-alley', {
ComponentClass: Component.extend({
init() {
this._super();
component = this;
},
toString() {
return 'component:rip-alley';
},
}),
});
this.render('{{#if this.shouldRender}}{{rip-alley}}{{/if}}', {
shouldRender: true,
});
runTask(() => {
set(this.context, 'shouldRender', false);
});
expectAssertion(() => {
component.sendAction('trigger-me-dead');
}, "Attempted to call .sendAction() with the action 'trigger-me-dead' on the destroyed object 'component:rip-alley'.");
}
}
);
moduleFor(
'Components test: sendAction to a controller',
class extends ApplicationTestCase {
["@test sendAction should trigger an action on the parent component's controller if it exists"](
assert
) {
assert.expect(20);
let component;
this.router.map(function () {
this.route('a');
this.route('b');
this.route('c', function () {
this.route('d');
this.route('e');
});
});
this.addComponent('foo-bar', {
ComponentClass: Component.extend({
init() {
this._super(...arguments);
component = this;
},
}),
template: `{{this.val}}`,
});
this.add(
'controller:a',
Controller.extend({
send(actionName, actionContext) {
assert.equal(
actionName,
'poke',
'send() method was invoked from a top level controller'
);
assert.equal(
actionContext,
'top',
'action arguments were passed into the top level controller'
);
},
})
);
this.addTemplate('a', '{{foo-bar val="a" poke="poke"}}');
this.add(
'route:b',
Route.extend({
actions: {
poke(actionContext) {
assert.ok(true, 'Unhandled action sent to route');
assert.equal(actionContext, 'top no controller');
},
},
})
);
this.addTemplate('b', '{{foo-bar val="b" poke="poke"}}');
this.add(
'route:c',
Route.extend({
actions: {
poke(actionContext) {
assert.ok(true, 'Unhandled action sent to route');
assert.equal(actionContext, 'top with nested no controller');
},
},
})
);
this.addTemplate('c', '{{foo-bar val="c" poke="poke"}}{{outlet}}');
this.add('route:c.d', Route.extend({}));
this.add(
'controller:c.d',
Controller.extend({
send(actionName, actionContext) {
assert.equal(actionName, 'poke', 'send() method was invoked from a nested controller');
assert.equal(
actionContext,
'nested',
'action arguments were passed into the nested controller'
);
},
})
);
this.addTemplate('c.d', '{{foo-bar val=".d" poke="poke"}}');
this.add(
'route:c.e',
Route.extend({
actions: {
poke(actionContext) {
assert.ok(true, 'Unhandled action sent to route');
assert.equal(actionContext, 'nested no controller');
},
},
})
);
this.addTemplate('c.e', '{{foo-bar val=".e" poke="poke"}}');
return this.visit('/a')
.then(() => {
expectSendActionDeprecation(() => component.sendAction('poke', 'top'));
})
.then(() => {
this.assertText('a');
return this.visit('/b');
})
.then(() => {
expectSendActionDeprecation(() => component.sendAction('poke', 'top no controller'));
})
.then(() => {
this.assertText('b');
return this.visit('/c');
})
.then(() => {
expectSendActionDeprecation(() => {
component.sendAction('poke', 'top with nested no controller');
});
})
.then(() => {
this.assertText('c');
return this.visit('/c/d');
})
.then(() => {
expectSendActionDeprecation(() => component.sendAction('poke', 'nested'));
})
.then(() => {
this.assertText('c.d');
return this.visit('/c/e');
})
.then(() => {
expectSendActionDeprecation(() => component.sendAction('poke', 'nested no controller'));
})
.then(() => this.assertText('c.e'));
}
["@test sendAction should not trigger an action in an outlet's controller if a parent component handles it"](
assert
) {
assert.expect(2);
let component;
this.addComponent('x-parent', {
ComponentClass: Component.extend({
actions: {
poke() {
assert.ok(true, 'parent component handled the aciton');
},
},
}),
template: '{{x-child poke="poke"}}',
});
this.addComponent('x-child', {
ComponentClass: Component.extend({
init() {
this._super(...arguments);
component = this;
},
}),
});
this.addTemplate('application', '{{x-parent}}');
this.add(
'controller:application',
Controller.extend({
send() {
throw new Error('controller action should not be called');
},
})
);
return this.visit('/').then(() => {
expectSendActionDeprecation(() => component.sendAction('poke'));
});
}
}
);
moduleFor(
'Components test: sendAction of a closure action',
class extends RenderingTestCase {
['@test action should be called'](assert) {
assert.expect(2);
let component;
this.registerComponent('inner-component', {
ComponentClass: Component.extend({
init() {
this._super(...arguments);
component = this;
},
}),
template: 'inner',
});
this.registerComponent('outer-component', {
ComponentClass: Component.extend({
outerSubmit() {
assert.ok(true, 'outerSubmit called');
},
}),
template: '{{inner-component submitAction=(action this.outerSubmit)}}',
});
this.render('{{outer-component}}');
expectSendActionDeprecation(() => {
runTask(() => component.sendAction('submitAction'));
});
}
['@test contexts passed to sendAction are appended to the bound arguments on a closure action']() {
let first = 'mitch';
let second = 'martin';
let third = 'matt';
let fourth = 'wacky wycats';
let innerComponent;
let actualArgs;
this.registerComponent('inner-component', {
ComponentClass: Component.extend({
init() {
this._super(...arguments);
innerComponent = this;
},
}),
template: 'inner',
});
this.registerComponent('outer-component', {
ComponentClass: Component.extend({
third,
actions: {
outerSubmit() {
actualArgs = [...arguments];
},
},
}),
template: `{{inner-component innerSubmit=(action (action "outerSubmit" "${first}") "${second}" this.third)}}`,
});
this.render('{{outer-component}}');
expectSendActionDeprecation(() => {
runTask(() => innerComponent.sendAction('innerSubmit', fourth));
});
this.assert.deepEqual(
actualArgs,
[first, second, third, fourth],
'action has the correct args'
);
}
}
);
moduleFor(
'Components test: send',
class extends RenderingTestCase {
['@test sending to undefined actions triggers an error'](assert) {
assert.expect(2);
let component;
this.registerComponent('foo-bar', {
ComponentClass: Component.extend({
init() {
this._super();
component = this;
},
actions: {
foo(message) {
assert.equal('bar', message);
},
},
}),
});
this.render('{{foo-bar}}');
runTask(() => component.send('foo', 'bar'));
expectAssertion(() => {
return component.send('baz', 'bar');
}, /had no action handler for: baz/);
}
['@test `send` will call send from a target if it is defined']() {
let component;
let target = {
send: (message, payload) => {
this.assert.equal('foo', message);
this.assert.equal('baz', payload);
},
};
this.registerComponent('foo-bar', {
ComponentClass: Component.extend({
init() {
this._super();
component = this;
},
target,
}),
});
this.render('{{foo-bar}}');
runTask(() => component.send('foo', 'baz'));
}
['@test a handled action can be bubbled to the target for continued processing']() {
this.assert.expect(2);
let component;
this.registerComponent('foo-bar', {
ComponentClass: Component.extend({
init() {
this._super(...arguments);
component = this;
},
actions: {
poke: () => {
this.assert.ok(true, 'component action called');
return true;
},
},
target: Controller.extend({
actions: {
poke: () => {
this.assert.ok(true, 'action bubbled to controller');
},
},
}).create(),
}),
});
this.render('{{foo-bar poke="poke"}}');
runTask(() => component.send('poke'));
}
["@test action can be handled by a superclass' actions object"](assert) {
this.assert.expect(4);
let component;
let SuperComponent = Component.extend({
actions: {
foo() {
assert.ok(true, 'foo');
},
bar(msg) {
assert.equal(msg, 'HELLO');
},
},
});
let BarViewMixin = Mixin.create({
actions: {
bar(msg) {
assert.equal(msg, 'HELLO');
this._super(msg);
},
},
});
this.registerComponent('x-index', {
ComponentClass: SuperComponent.extend(BarViewMixin, {
init() {
this._super(...arguments);
component = this;
},
actions: {
baz() {
assert.ok(true, 'baz');
},
},
}),
});
this.render('{{x-index}}');
runTask(() => {
component.send('foo');
component.send('bar', 'HELLO');
component.send('baz');
});
}
['@test actions cannot be provided at create time'](assert) {
this.registerComponent('foo-bar', Component.extend());
let ComponentFactory = this.owner.factoryFor('component:foo-bar');
expectAssertion(() => {
ComponentFactory.create({
actions: {
foo() {
assert.ok(true, 'foo');
},
},
});
}, /`actions` must be provided at extend time, not at create time/);
// but should be OK on an object that doesn't mix in Ember.ActionHandler
EmberObject.create({
actions: ['foo'],
});
}
['@test asserts if called on a destroyed component']() {
let component;
this.registerComponent('rip-alley', {
ComponentClass: Component.extend({
init() {
this._super();
component = this;
},
toString() {
return 'component:rip-alley';
},
}),
});
this.render('{{#if this.shouldRender}}{{rip-alley}}{{/if}}', {
shouldRender: true,
});
runTask(() => {
set(this.context, 'shouldRender', false);
});
expectAssertion(() => {
component.send('trigger-me-dead');
}, "Attempted to call .send() with the action 'trigger-me-dead' on the destroyed object 'component:rip-alley'.");
}
}
);