-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtest-action.js
1847 lines (1651 loc) · 55.9 KB
/
test-action.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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
ActionInvocation,
ActionService,
DeferredEvent,
dereferenceArgsVariables,
parseActionMap,
} from '../../src/service/action-impl';
import {
ActionTrust,
DEFAULT_ACTION,
RAW_OBJECT_ARGS_KEY,
} from '../../src/action-constants';
import {AmpDocSingle} from '../../src/service/ampdoc-impl';
import {Keys} from '../../src/utils/key-codes';
import {createCustomEvent} from '../../src/event-helper';
import {createElementWithAttributes} from '../../src/dom';
import {htmlFor} from '../../src/static-template';
import {setParentWindow} from '../../src/service';
import {whenCalled} from '../../testing/test-helper.js';
/**
* @return {!ActionService}
*/
function actionService() {
const win = {
document: {body: {}},
__AMP_SERVICES: {
vsync: {obj: {}},
},
};
return new ActionService(new AmpDocSingle(win), document);
}
function createExecElement(id, enqueAction, defaultActionAlias) {
const execElement = document.createElement('amp-element');
execElement.setAttribute('id', id);
execElement.enqueAction = enqueAction;
execElement.getDefaultActionAlias = defaultActionAlias;
return execElement;
}
function assertInvocation(
inv,
node,
method,
source,
caller,
opt_event,
child,
opt_args
) {
expect(inv.node).to.equal(node);
expect(inv.method).to.equal(method);
expect(inv.caller).to.equal(caller);
if (opt_event !== undefined) {
expect(inv.event).to.equal(opt_event);
}
if (opt_args !== undefined) {
expect(inv.args).to.deep.equal(opt_args);
}
}
describe('ActionService parseAction', () => {
function parseMultipleActions(s) {
const actionMap = parseActionMap(s);
if (actionMap == null) {
return null;
}
const keys = Object.keys(actionMap);
expect(keys).to.have.length(1);
return actionMap[keys[0]];
}
function parseAction(s) {
const actions = parseMultipleActions(s);
if (actions == null) {
return null;
}
expect(actions).to.have.length(1);
return actions[0];
}
it('should parse full form', () => {
const a = parseAction('event1:target1.method1');
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal('method1');
});
it('should parse full form with two actions', () => {
const a = parseMultipleActions('event1:target1.methodA,target2.methodB');
expect(a[0].event).to.equal('event1');
expect(a[0].target).to.equal('target1');
expect(a[0].method).to.equal('methodA');
expect(a[1].event).to.equal('event1');
expect(a[1].target).to.equal('target2');
expect(a[1].method).to.equal('methodB');
});
it('should parse with default method', () => {
const a = parseAction('event1:target1');
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal(DEFAULT_ACTION);
});
it('should parse with default method for two different targets', () => {
const a = parseMultipleActions('event1:target1,target2');
expect(a[0].event).to.equal('event1');
expect(a[0].target).to.equal('target1');
expect(a[0].method).to.equal(DEFAULT_ACTION);
expect(a[1].event).to.equal('event1');
expect(a[1].target).to.equal('target2');
expect(a[1].method).to.equal(DEFAULT_ACTION);
});
it('should parse with numeric target', () => {
const a = parseAction('event1:1234.method1');
expect(a.event).to.equal('event1');
expect(a.target).to.equal('1234');
expect(a.method).to.equal('method1');
});
it('should parse with two numeric targets', () => {
const a = parseMultipleActions('event1:1234.method1,9876.method2');
expect(a[0].event).to.equal('event1');
expect(a[0].target).to.equal('1234');
expect(a[0].method).to.equal('method1');
expect(a[1].event).to.equal('event1');
expect(a[1].target).to.equal('9876');
expect(a[1].method).to.equal('method2');
});
it('should parse with lots of whitespace', () => {
const a = parseAction(' event1 : target1 . method1 ');
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal('method1');
});
it('should parse with an arg', () => {
const a = parseAction('event1:target1.method1(key1=value1)');
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal('method1');
expect(a.args['key1']).to.equal('value1');
});
it('should parse args in more than one action', () => {
const a = parseMultipleActions(
'event1:target1.methodA(key1=value1),target2.methodB(keyA=valueA)'
);
expect(a[0].event).to.equal('event1');
expect(a[0].target).to.equal('target1');
expect(a[0].method).to.equal('methodA');
expect(a[0].args['key1']).to.equal('value1');
expect(a[1].event).to.equal('event1');
expect(a[1].target).to.equal('target2');
expect(a[1].method).to.equal('methodB');
expect(a[1].args['keyA']).to.equal('valueA');
});
it('should parse multiple event types with multiple actions', () => {
const a = parseActionMap(
'event1:foo, baz.firstMethod, corge.secondMethod(keyA=valueA);' +
'event2:bar, qux.thirdMethod, grault.fourthMethod(keyB=valueB)'
);
expect(Object.keys(a)).to.have.length(2);
expect(a['event1']).to.have.length(3);
expect(a['event2']).to.have.length(3);
// action definitions for event1
expect(a['event1'][0].event).to.equal('event1');
expect(a['event1'][0].target).to.equal('foo');
expect(a['event1'][0].method).to.equal(DEFAULT_ACTION);
expect(a['event1'][0].args).to.be.null;
expect(a['event1'][1].event).to.equal('event1');
expect(a['event1'][1].target).to.equal('baz');
expect(a['event1'][1].method).to.equal('firstMethod');
expect(a['event1'][1].args).to.be.null;
expect(a['event1'][2].event).to.equal('event1');
expect(a['event1'][2].target).to.equal('corge');
expect(a['event1'][2].method).to.equal('secondMethod');
expect(a['event1'][2].args['keyA']).to.equal('valueA');
// action definitions for event2
expect(a['event2'][0].event).to.equal('event2');
expect(a['event2'][0].target).to.equal('bar');
expect(a['event2'][0].method).to.equal(DEFAULT_ACTION);
expect(a['event2'][0].args).to.be.null;
expect(a['event2'][1].event).to.equal('event2');
expect(a['event2'][1].target).to.equal('qux');
expect(a['event2'][1].method).to.equal('thirdMethod');
expect(a['event2'][1].args).to.be.null;
expect(a['event2'][2].event).to.equal('event2');
expect(a['event2'][2].target).to.equal('grault');
expect(a['event2'][2].method).to.equal('fourthMethod');
expect(a['event2'][2].args['keyB']).to.equal('valueB');
});
it('should parse with multiple args', () => {
const a = parseAction('event1:target1.method1(key1=value1, key2 = value2)');
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal('method1');
expect(a.args['key1']).to.equal('value1');
expect(a.args['key2']).to.equal('value2');
});
it('should parse with multiple args with whitespace', () => {
const a = parseAction(
'event1:target1.method1 ( key1 = value1 , key2 = value2 ) '
);
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal('method1');
expect(a.args['key1']).to.equal('value1');
expect(a.args['key2']).to.equal('value2');
});
it('should parse with no args', () => {
const a = parseAction('event1:target1.method1()');
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal('method1');
expect(a.args).to.be.null;
});
it('should parse with no args with whitespace', () => {
const a = parseAction('event1:target1.method1 ( ) ');
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal('method1');
expect(a.args).to.be.null;
});
it('should parse with double-quoted args', () => {
const a = parseAction(
'event1:target1.method1(key1=value1, key2 = "value (2)\'")'
);
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal('method1');
expect(a.args['key1']).to.equal('value1');
expect(a.args['key2']).to.equal("value (2)'");
});
it('should parse with single-quoted args', () => {
const a = parseAction(
"event1:target1.method1(key1=value1, key2 = 'value (2)\"')"
);
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal('method1');
expect(a.args['key1']).to.equal('value1');
expect(a.args['key2']).to.equal('value (2)"');
});
it('should parse with args with trailing comma', () => {
const a = parseAction('event1:target1.method1(key1=value1, )');
expect(a.event).to.equal('event1');
expect(a.target).to.equal('target1');
expect(a.method).to.equal('method1');
expect(a.args['key1']).to.equal('value1');
expect(Object.keys(a.args)).to.have.length(1);
});
it('should parse with boolean args', () => {
expect(parseAction('e:t.m(k=true)').args['k']).to.equal(true);
expect(parseAction('e:t.m(k=false)').args['k']).to.equal(false);
});
it('should parse with numeric args', () => {
expect(parseAction('e:t.m(k=123)').args['k']).to.equal(123);
expect(parseAction('e:t.m(k=1.23)').args['k']).to.equal(1.23);
expect(parseAction('e:t.m(k=.123)').args['k']).to.equal(0.123);
});
it('should parse with term semicolon', () => {
expect(parseAction('e:t.m(k=1); ').args['k']).to.equal(1);
});
it('should parse with args as a proto-less object', () => {
expect(parseAction('e:t.m(k=1)').args.__proto__).to.be.undefined;
});
it('should interprete key always as string', () => {
expect(parseAction('true:t.m').event).to.equal('true');
expect(parseAction('e:true.m').target).to.equal('true');
expect(parseAction('e:t.true').method).to.equal('true');
expect(parseAction('e:t.m(true=1)').args['true']).to.equal(1);
expect(parseAction('e:t.m(01=1)').args['01']).to.equal(1);
});
it('should parse with object literal args', () => {
const a = parseAction('e:t.m({"foo": {"bar": "qux"}})');
expect(a.args[RAW_OBJECT_ARGS_KEY]).to.equal('{"foo": {"bar": "qux"}}');
});
it('should parse with expression args', () => {
const a = parseAction('e:t.m(key1=foo.bar)');
expect(a.args['key1']).to.deep.equal({expression: 'foo.bar'});
});
it('should return null for undefined references in dereferenced arg', () => {
const a = parseAction('e:t.m(key1=foo.bar)');
expect(dereferenceArgsVariables(a.args, null)).to.deep.equal({key1: null});
expect(dereferenceArgsVariables(a.args, {})).to.deep.equal({key1: null});
expect(dereferenceArgsVariables(a.args, {foo: null})).to.deep.equal({
key1: null,
});
});
it('should return null for non-primitives in dereferenced args', () => {
const a = parseAction('e:t.m(key1=foo.bar)');
expect(
dereferenceArgsVariables(a.args, {foo: {bar: undefined}})
).to.deep.equal({key1: null});
expect(dereferenceArgsVariables(a.args, {foo: {bar: {}}})).to.deep.equal({
key1: null,
});
expect(dereferenceArgsVariables(a.args, {foo: {bar: []}})).to.deep.equal({
key1: null,
});
expect(
dereferenceArgsVariables(a.args, {foo: {bar: () => {}}})
).to.deep.equal({key1: null});
});
it('should support event data and opt_args', () => {
const a = parseAction('e:t.m(key1=foo,key2=x)');
const event = createCustomEvent(window, 'MyEvent');
expect(dereferenceArgsVariables(a.args, event, {x: 'bar'})).to.deep.equal({
key1: 'foo',
key2: 'bar',
});
});
it('evaluated args should be proto-less objects', () => {
const a = parseAction('e:t.m(key1=foo)');
expect(dereferenceArgsVariables(a.args, null).__proto__).to.be.undefined;
expect(dereferenceArgsVariables(a.args, null).constructor).to.be.undefined;
});
it('should dereference arg expressions', () => {
const a = parseAction('e:t.m(key1=foo)');
expect(dereferenceArgsVariables(a.args, null)).to.deep.equal({key1: 'foo'});
});
it('should dereference arg expressions with an event without data', () => {
const a = parseAction('e:t.m(key1=foo)');
const event = createCustomEvent(window, 'MyEvent');
expect(dereferenceArgsVariables(a.args, {event})).to.deep.equal({
key1: 'foo',
});
});
it('should dereference arg expressions with an event with data', () => {
const a = parseAction('e:t.m(key1=event.foo)');
const event = createCustomEvent(window, 'MyEvent', {foo: 'bar'});
expect(dereferenceArgsVariables(a.args, event)).to.deep.equal({
key1: 'bar',
});
});
it('should parse empty to null', () => {
const a = parseAction('');
expect(a).to.equal(null);
});
it('should fail parse without event', () => {
allowConsoleError(() => {
expect(() => {
parseAction('target1.method1');
}).to.throw(/expected \[\:\]/);
});
});
it('should fail parse without target', () => {
allowConsoleError(() => {
expect(() => {
parseAction('event1:');
}).to.throw(/Invalid action/);
expect(() => {
parseAction('.method1');
}).to.throw(/Invalid action/);
expect(() => {
parseAction('event1:.method1');
}).to.throw(/Invalid action/);
});
});
it('should fail parse with period in event or method', () => {
allowConsoleError(() => {
expect(() => {
parseAction('event.1:target1.method');
}).to.throw(/Invalid action/);
expect(() => {
parseAction('event:target1.method.1');
}).to.throw(/Invalid action/);
});
});
it('should fail parse with invalid args', () => {
allowConsoleError(() => {
// No args allowed without explicit action.
expect(() => {
parseAction('event:target1()');
}).to.throw(/Invalid action/);
// Missing parens
expect(() => {
parseAction('event:target1.method(');
}).to.throw(/Invalid action/);
expect(() => {
parseAction('event:target1.method(key = value');
}).to.throw(/Invalid action/);
// No arg value.
expect(() => {
parseAction('event:target1.method(key)');
}).to.throw(/Invalid action/);
expect(() => {
parseAction('event:target1.method(key=)');
}).to.throw(/Invalid action/);
// Missing quote
expect(() => {
parseAction('event:target1.method(key = "value)');
}).to.throw(/Invalid action/);
// Broken quotes.
expect(() => {
parseAction('event:target1.method(key = "value"")');
}).to.throw(/Invalid action/);
// Missing comma.
expect(() => {
parseAction('event:target1.method(key = value key2 = value2)');
}).to.throw(/Invalid action/);
// Empty (2...n)nd target
expect(() => {
parseAction('event:target1,');
}).to.throw(/Invalid action/);
});
});
});
describe('ActionService setActions', () => {
it('should set actions', () => {
const action = actionService();
const element = document.createElement('div');
element.setAttribute('on', 'event1:action1');
// Invoke findAction_ once at the start to cache the actions.
action.findAction_(element, 'event1');
// Reset the actions with a new actions string.
action.setActions(element, 'event2:action2');
// `on` attribute should reflect the new action defs.
expect(element.getAttribute('on')).to.equal('event2:action2');
// Action cache should have been cleared.
expect(action.findAction_(element, 'event1')).to.be.null;
expect(action.findAction_(element, 'event2')).to.be.not.null;
});
});
describe('Action parseActionMap', () => {
it('should parse with a single action', () => {
const m = parseActionMap('event1:action1');
expect(m['event1'][0].target).to.equal('action1');
});
it('should parse with two actions', () => {
const m = parseActionMap('event1:action1; event2: action2');
expect(m['event1'][0].target).to.equal('action1');
expect(m['event2'][0].target).to.equal('action2');
});
it('should parse with dupe actions by overriding with last', () => {
const m = parseActionMap('event1:action1; event1: action2');
expect(m['event1']).to.have.length(1);
// Currently, we overwrite the events.
expect(m['event1'][0].target).to.equal('action2');
});
it('should parse empty forms to null', () => {
expect(parseActionMap('')).to.equal(null);
expect(parseActionMap(' ')).to.equal(null);
expect(parseActionMap(';;;')).to.equal(null);
});
});
describes.sandboxed('Action adoptEmbedWindow', {}, () => {
let action;
let embedWin;
beforeEach(() => {
action = actionService();
embedWin = {
frameElement: document.createElement('div'),
document: document.implementation.createHTMLDocument(''),
};
setParentWindow(embedWin, action.ampdoc.win);
});
it('should create embedded action service', () => {
ActionService.installInEmbedWindow(embedWin, action.ampdoc);
const embedService =
embedWin.__AMP_SERVICES.action && embedWin.__AMP_SERVICES.action.obj;
expect(embedService).to.exist;
expect(embedService.ampdoc).to.equal(action.ampdoc);
expect(embedService.root_).to.equal(embedWin.document);
});
});
describes.sandboxed('Action findAction', {}, () => {
let action;
beforeEach(() => {
action = actionService();
});
it('should create action map in getActionMap_', () => {
const element = document.createElement('div');
element.setAttribute('on', 'event1:action1');
const m = action.getActionMap_(element);
expect(m['event1']).to.have.length(1);
expect(m['event1'][0].target).to.equal('action1');
});
it('should cache action map', () => {
const element = document.createElement('div');
element.setAttribute('on', 'event1:action1');
const m1 = action.getActionMap_(element);
const m2 = action.getActionMap_(element);
expect(m1).to.equal(m2);
});
it('should find action on the same element', () => {
const element = document.createElement('div');
element.setAttribute('on', 'event1:action1');
const a = action.findAction_(element, 'event1');
expect(a.node).to.equal(element);
expect(a.actionInfos).to.have.length(1);
expect(a.actionInfos[0].target).to.equal('action1');
expect(action.findAction_(element, 'event3')).to.equal(null);
});
it('should find action in subtree', () => {
const parent = document.createElement('div');
parent.setAttribute('on', 'event1:action1');
const element = document.createElement('div');
element.setAttribute('on', 'event2:action2');
parent.appendChild(element);
let a = action.findAction_(element, 'event1');
expect(a.node).to.equal(parent);
expect(a.actionInfos).to.have.length(1);
expect(a.actionInfos[0].target).to.equal('action1');
a = action.findAction_(element, 'event2');
expect(a.node).to.equal(element);
expect(a.actionInfos).to.have.length(1);
expect(a.actionInfos[0].target).to.equal('action2');
expect(action.findAction_(element, 'event3')).to.equal(null);
});
it('should skip action on disabled elements', () => {
const parent = document.createElement('button');
parent.setAttribute('on', 'event1:action1');
parent.disabled = true;
expect(action.findAction_(parent, 'event1')).to.equal(null);
});
it('should skip parent action on descendants of disabled elements', () => {
const parent = document.createElement('button');
parent.setAttribute('on', 'event1:action1');
parent.disabled = true;
const element = document.createElement('div');
parent.appendChild(element);
expect(action.findAction_(element, 'event1')).to.equal(null);
});
it('should skip action on form control in a disabled fieldset', () => {
const parent = document.createElement('fieldset');
parent.setAttribute('on', 'event1:action1');
parent.disabled = true;
const element = document.createElement('button');
element.setAttribute('on', 'event2:action2');
parent.appendChild(element);
expect(action.findAction_(element, 'event1')).to.equal(null);
expect(action.findAction_(element, 'event2')).to.equal(null);
});
});
describes.sandboxed('Action hasAction', {}, () => {
let action;
beforeEach(() => {
action = actionService();
});
it('returns true if the target element has the target action', () => {
const element = document.createElement('div');
element.setAttribute('on', 'event1:action1');
expect(action.hasAction(element, 'event1')).to.equal(true);
});
it('returns true if an intermediate element has target action', () => {
const child = document.createElement('div');
const element = document.createElement('div');
element.appendChild(child);
element.setAttribute('on', 'event1:action1');
const parent = document.createElement('div');
parent.appendChild(element);
parent.setAttribute('on', 'event2:action2');
expect(action.hasAction(element, 'event1', parent)).to.equal(true);
expect(action.hasAction(element, 'event2', parent)).to.equal(false);
});
it('returns false if the target element does not have the target action', () => {
const element = document.createElement('div');
element.setAttribute('on', 'event1:action1');
expect(action.hasAction(element, 'event2')).to.equal(false);
});
});
describes.fakeWin('Action hasResolvableAction', {amp: true}, env => {
let action;
let html;
beforeEach(() => {
html = htmlFor(env.win.document);
action = new ActionService(env.ampdoc, env.win.document);
// Insert element for valid actions to be resolved.
env.win.document.body.appendChild(
html`
<div id="valid-target"></div>
`
);
});
it('returns true if the target element exists (single)', () => {
const element = html`
<div on="event1: valid-target"></div>
`;
expect(action.hasResolvableAction(element, 'event1')).to.equal(true);
});
it('returns true if the target element exists (action up the tree)', () => {
const wrapper = html`
<div on="event1: valid-target"></div>
`;
const child = html`
<div></div>
`;
wrapper.appendChild(child);
expect(action.hasResolvableAction(child, 'event1')).to.equal(true);
});
it('returns true if the target element exists (one amongst many)', () => {
const element = html`
<div on="event1: i-dont-exist, valid-target, i-dont-exist-either"></div>
`;
expect(action.hasResolvableAction(element, 'event1')).to.equal(true);
});
it('returns false if the target element does not exist (one)', () => {
const element = html`
<div on="event1: i-do-not-exist"></div>
`;
expect(action.hasResolvableAction(element, 'event1')).to.equal(false);
});
it('returns false if the target element does not exist (multiple)', () => {
const element = html`
<div on="event1: i-do-not-exist, i-dont-exist-either"></div>
`;
expect(action.hasResolvableAction(element, 'event1')).to.equal(false);
});
it('returns false if target element does not have the target action', () => {
const element = html`
<div on="event1: valid-target"></div>
`;
expect(action.hasResolvableAction(element, 'event2')).to.equal(false);
});
});
describes.sandboxed('Action method', {}, () => {
let action;
let getDefaultActionAlias;
let id;
let onEnqueue;
let targetElement, parent, child, execElement;
beforeEach(() => {
action = actionService();
onEnqueue = sandbox.spy();
getDefaultActionAlias = sandbox.spy();
targetElement = document.createElement('target');
id = ('E' + Math.random()).replace('.', '');
targetElement.setAttribute('on', 'tap:' + id + '.method1');
parent = document.createElement('parent');
child = document.createElement('child');
parent.appendChild(targetElement);
targetElement.appendChild(child);
document.body.appendChild(parent);
execElement = createExecElement(id, onEnqueue, getDefaultActionAlias);
parent.appendChild(execElement);
});
afterEach(() => {
document.body.removeChild(parent);
});
it('should invoke on the AMP element', () => {
action.invoke_(
new ActionInvocation(
execElement,
'method1',
/* args */ null,
'source1',
'caller1',
'event1'
)
);
expect(onEnqueue).to.be.calledOnce;
const inv = onEnqueue.getCall(0).args[0];
expect(inv.node).to.equal(execElement);
expect(inv.method).to.equal('method1');
expect(inv.source).to.equal('source1');
expect(inv.caller).to.equal('caller1');
expect(inv.event).to.equal('event1');
expect(inv.args).to.be.null;
});
it('should invoke on the AMP element with args', () => {
action.invoke_(
new ActionInvocation(
execElement,
'method1',
{'key1': 11},
'source1',
'caller1',
'event1'
)
);
expect(onEnqueue).to.be.calledOnce;
const inv = onEnqueue.getCall(0).args[0];
expect(inv.node).to.equal(execElement);
expect(inv.method).to.equal('method1');
expect(inv.source).to.equal('source1');
expect(inv.caller).to.equal('caller1');
expect(inv.event).to.equal('event1');
expect(inv.args['key1']).to.equal(11);
});
it('should not allow invoke on non-AMP and non-whitelisted element', () => {
allowConsoleError(() => {
expect(() => {
action.invoke_(
new ActionInvocation(
document.createElement('img'),
'method1',
/* args */ null,
'source1',
'event1'
)
);
}).to.throw(/doesn't support "method1" action/);
});
expect(onEnqueue).to.have.not.been.called;
});
it('should not allow invoke on unresolved AMP element', () => {
allowConsoleError(() => {
expect(() => {
action.invoke_(
new ActionInvocation(
document.createElement('amp-foo'),
'method1',
/* args */ null,
'source1',
'event1'
)
);
}).to.throw(/Unrecognized AMP element/);
});
expect(onEnqueue).to.have.not.been.called;
});
it('should trigger event', () => {
action.trigger(child, 'tap', null);
expect(onEnqueue).to.be.calledOnce;
const inv = onEnqueue.getCall(0).args[0];
expect(inv.node).to.equal(execElement);
expect(inv.method).to.equal('method1');
expect(inv.source).to.equal(child);
expect(inv.caller).to.equal(targetElement);
});
it('should execute method', () => {
action.execute(execElement, 'method1', {'key1': 11}, child, null);
expect(onEnqueue).to.be.calledOnce;
const inv = onEnqueue.getCall(0).args[0];
expect(inv.node).to.equal(execElement);
expect(inv.method).to.equal('method1');
expect(inv.args['key1']).to.equal(11);
expect(inv.source).to.equal(child);
});
describe('macros', () => {
let ampActionMacro;
beforeEach(() => {
// A caller that references an action macro.
targetElement.setAttribute('on', 'tap:action-macro-id.execute(arg1=2)');
ampActionMacro = document.createElement('amp-action-macro');
ampActionMacro.setAttribute('id', id);
ampActionMacro.setAttribute(
'execute',
'action-macro-id.method(realArgName=arg1)'
);
ampActionMacro.setAttribute('arguments', 'arg1');
document.body.appendChild(ampActionMacro);
});
it('should invoke proper action', () => {
// Given that an amp action macro is triggered.
const invoke_ = sandbox.stub(action, 'invoke_');
sandbox
.stub(action.root_, 'getElementById')
.withArgs('action-macro-id')
.returns(ampActionMacro);
action.trigger(
ampActionMacro,
'tap',
null,
ActionTrust.HIGH,
/* opt_args */ {arg1: 'realArgValue'}
);
return whenCalled(invoke_).then(() => {
expect(action.invoke_).to.have.been.calledOnce;
const invocation = action.invoke_.getCall(0).args[0];
const {
actionEventType,
args,
method,
node,
source,
caller,
event,
tagOrTarget,
trust,
} = invocation;
expect(node).to.equal(ampActionMacro);
expect(caller).to.equal(ampActionMacro);
expect(event).to.be.null;
expect(method).to.equal('method');
expect(actionEventType).to.equal('tap');
expect(args).to.deep.equal({realArgName: 'realArgValue'});
expect(trust).to.equal(100);
expect(tagOrTarget).to.equal('AMP-ACTION-MACRO');
expect(actionEventType).to.equal('tap');
expect(source).to.equal(ampActionMacro);
});
});
});
});
describes.sandboxed('installActionHandler', {}, () => {
let action;
beforeEach(() => {
action = actionService();
});
it('should invoke on non-AMP but whitelisted element', () => {
const handlerSpy = sandbox.spy();
const target = document.createElement('form');
action.installActionHandler(target, handlerSpy);
action.invoke_(
new ActionInvocation(
target,
'submit',
/* args */ null,
'button',
'button',
'tap',
ActionTrust.HIGH
)
);
expect(handlerSpy).to.be.calledOnce;
const callArgs = handlerSpy.getCall(0).args[0];
expect(callArgs.node).to.be.equal(target);
expect(callArgs.method).to.be.equal('submit');
expect(callArgs.args).to.be.equal(null);
expect(callArgs.source).to.be.equal('button');
expect(callArgs.caller).to.be.equal('button');
expect(callArgs.event).to.be.equal('tap');
expect(callArgs.trust).to.be.equal(ActionTrust.HIGH);
});
it('should not check trust level (handler should check)', () => {
const handlerSpy = sandbox.spy();
const target = document.createElement('form');
action.installActionHandler(target, handlerSpy);
const invocation = new ActionInvocation(
target,
'submit',
/* args */ null,
'button',
'button',
'tapEvent',
ActionTrust.HIGH
);
action.invoke_(invocation);
expect(handlerSpy).to.be.calledOnce;
invocation.trust = ActionTrust.LOW;
action.invoke_(invocation);
expect(handlerSpy).to.be.calledTwice;
});
});
describes.sandboxed('Multiple handlers action method', {}, () => {
let action;
let getDefaultActionAlias;
let onEnqueue1, onEnqueue2;
let targetElement, parent, child, execElement1, execElement2;
beforeEach(() => {
action = actionService();
onEnqueue1 = sandbox.spy();
onEnqueue2 = sandbox.spy();
getDefaultActionAlias = sandbox.spy();
targetElement = document.createElement('target');
targetElement.setAttribute('on', 'tap:foo.method1,bar.method2');
parent = document.createElement('parent');
child = document.createElement('child');
parent.appendChild(targetElement);
targetElement.appendChild(child);
document.body.appendChild(parent);
execElement1 = createExecElement('foo', onEnqueue1, getDefaultActionAlias);
execElement2 = createExecElement('bar', onEnqueue2, getDefaultActionAlias);
parent.appendChild(execElement1);
parent.appendChild(execElement2);
});
afterEach(() => {
document.body.removeChild(parent);
});
it('should trigger event', () => {
action.trigger(child, 'tap', null);
expect(onEnqueue1).to.be.calledOnce;
assertInvocation(
onEnqueue1.getCall(0).args[0],
execElement1,
'method1',
child,
targetElement
);
assertInvocation(
onEnqueue2.getCall(0).args[0],
execElement2,