-
Notifications
You must be signed in to change notification settings - Fork 280
/
toasterContainerSpec.js
948 lines (645 loc) · 28.3 KB
/
toasterContainerSpec.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
/* global describe global it global beforeEach global angular global jasmine global inject global expect global spyOn */
'use strict';
var rootScope, toaster, $compile, $sanitize;
describe('toasterContainer', function () {
beforeEach(function () {
module('toaster');
// inject the toaster service
inject(function (_toaster_, _$rootScope_, _$compile_) {
toaster = _toaster_;
rootScope = _$rootScope_;
$compile = _$compile_;
});
});
it('should pop a toast via individual parameters', function () {
var container = compileContainer();
var scope = container.scope();
toaster.pop('info', 'test', 'test');
expect(scope.toasters.length).toBe(1);
});
it('should unsubscribe events on $destroy if handlers exist', function () {
var toasterEventRegistry;
inject(function (_toasterEventRegistry_) {
toasterEventRegistry = _toasterEventRegistry_;
});
var container = compileContainer();
var scope = container.scope();
spyOn(toasterEventRegistry, 'unsubscribeToNewToastEvent').and.callThrough();
spyOn(toasterEventRegistry, 'unsubscribeToClearToastsEvent').and.callThrough();
scope.$destroy();
expect(toasterEventRegistry.unsubscribeToNewToastEvent).toHaveBeenCalled();
expect(toasterEventRegistry.unsubscribeToClearToastsEvent).toHaveBeenCalled();
});
describe('addToast', function () {
it('should default to icon-class config value if toast.type not found in icon-classes', function () {
var toasterConfig;
inject(function (_toasterConfig_) {
toasterConfig = _toasterConfig_;
});
compileContainer();
expect(toasterConfig['icon-class']).toBe('toast-info');
toaster.pop({ type: 'invalid' });
rootScope.$digest();
expect(toaster.toast.type).toBe('toast-info');
});
it('should allow subsequent duplicates if prevent-duplicates is not set', function () {
var container = compileContainer();
var scope = container.scope();
expect(scope.toasters.length).toBe(0);
toaster.pop({ type: 'info', title: 'title', body: 'body' });
toaster.pop({ type: 'info', title: 'title', body: 'body' });
rootScope.$digest();
expect(scope.toasters.length).toBe(2);
});
it('should not allow subsequent duplicates if prevent-duplicates is true and body matches', function () {
var container = angular.element(
'<toaster-container toaster-options="{\'prevent-duplicates\': true}"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
var scope = container.scope();
expect(scope.toasters.length).toBe(0);
toaster.pop({ type: 'info', title: 'title', body: 'body' });
toaster.pop({ type: 'info', title: 'title', body: 'body' });
expect(scope.toasters.length).toBe(1);
});
it('should not allow subsequent duplicates if prevent-duplicates is true and id matches with unique bodies', function () {
var container = angular.element(
'<toaster-container toaster-options="{\'prevent-duplicates\': true}"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
var scope = container.scope();
expect(scope.toasters.length).toBe(0);
var toastWrapper = toaster.pop({ type: 'info', title: 'title', body: 'body' });
toaster.pop({ type: 'info', title: 'title', body: 'body2', toastId: toastWrapper.toastId });
expect(scope.toasters.length).toBe(1);
});
it('should allow subsequent duplicates if prevent-duplicates is true with unique toastId and body params', function () {
var container = angular.element(
'<toaster-container toaster-options="{\'prevent-duplicates\': true}"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
var scope = container.scope();
expect(scope.toasters.length).toBe(0);
toaster.pop({ type: 'info', title: 'title', body: 'body', toastId: 1 });
toaster.pop({ type: 'info', title: 'title', body: 'body2', toastId: 2 });
rootScope.$digest();
expect(scope.toasters.length).toBe(2);
});
it('should not allow subsequent duplicates if prevent-duplicates is true with identical toastId params', function () {
var container = angular.element(
'<toaster-container toaster-options="{\'prevent-duplicates\': true}"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
var scope = container.scope();
expect(scope.toasters.length).toBe(0);
toaster.pop({ type: 'info', title: 'title', body: 'body', toastId: 1 });
toaster.pop({ type: 'info', title: 'title', body: 'body', toastId: 1 });
rootScope.$digest();
expect(scope.toasters.length).toBe(1);
});
it('should not render the close button if showCloseButton is false', function () {
var container = compileContainer();
toaster.pop({ type: 'info', body: 'With a close button' });
rootScope.$digest();
expect(container.find('button')[0]).toBeUndefined();
});
it('should use the default close html if toast.closeHtml is undefined', function () {
var container = compileContainer();
toaster.pop({ type: 'info', body: 'With a close button', showCloseButton: true });
rootScope.$digest();
var buttons = container.find('button');
expect(buttons.length).toBe(1);
expect(buttons[0].outerHTML).toBe('<button class="toast-close-button" type="button">×</button>');
});
it('should use the toast.closeHtml argument if passed', function () {
var container = compileContainer();
toaster.pop({ type: 'info', body: 'With a close button', showCloseButton: true,
closeHtml: '<button>Close</button>'
});
rootScope.$digest();
var buttons = container.find('button');
expect(buttons.length).toBe(1);
expect(buttons[0].outerHTML).toBe('<button>Close</button>');
});
it('should render toast.closeHtml argument if not a button element', function () {
var container = compileContainer();
toaster.pop({ type: 'info', body: 'With close text', showCloseButton: true,
closeHtml: '<span>Close</span>'
});
rootScope.$digest();
var spans = container.find('span');
expect(spans.length).toBe(1);
expect(spans[0].outerHTML).toBe('<span>Close</span>');
});
it('should show the close button if mergedConfig close-button is an object set to true for toast-info', function () {
var container = angular.element(
'<toaster-container toaster-options="{\'close-button\': {\'toast-info\': true}}"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
toaster.pop({ type: 'info' });
rootScope.$digest();
var buttons = container.find('button');
expect(buttons.length).toBe(1);
expect(buttons[0].outerHTML).toBe('<button class="toast-close-button" type="button">×</button>');
});
it('should not render the close button if mergedConfig close-button type cannot be found', function () {
var container = angular.element(
'<toaster-container toaster-options="{\'close-button\': {\'toast-invalid\': true}}"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
toaster.pop({ type: 'info' });
rootScope.$digest();
var buttons = container.find('button');
expect(buttons.length).toBe(0);
expect(buttons[0]).toBeUndefined();
});
it('should not render the close button if mergedConfig close-button is not an object', function () {
var container = angular.element(
'<toaster-container toaster-options="{\'close-button\': 1 }"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
toaster.pop({ type: 'info' });
rootScope.$digest();
var buttons = container.find('button');
expect(buttons.length).toBe(0);
expect(buttons[0]).toBeUndefined();
});
it('should render \'trustedHtml\' bodyOutputType', function () {
var container = compileContainer();
toaster.pop({ bodyOutputType: 'trustedHtml', body: '<section>Body</section>' });
rootScope.$digest();
var body = container.find('section');
expect(body.length).toBe(1);
expect(body[0].outerHTML).toBe('<section>Body</section>');
});
it('default toast template exists', function () {
inject(function($templateCache) {
var template = $templateCache.get('angularjs-toaster/toast.html');
expect(template.length).toBeGreaterThan(0);
});
});
it('should render template bodyOutputType when body is passed', function () {
inject(function($templateCache) {
$templateCache.put('/templatepath/template.html', '<section>Template</section>');
});
var container = compileContainer();
toaster.pop({ bodyOutputType: 'template', body: '/templatepath/template.html' });
rootScope.$digest();
expect(toaster.toast.body).toBe('/templatepath/template.html');
var body = container.find('section');
expect(body.length).toBe(1);
expect(body[0].outerHTML).toBe('<section class="ng-scope">Template</section>');
});
it('should render default template bodyOutputType when body is not passed', function () {
inject(function($templateCache) {
$templateCache.put('toasterBodyTmpl.html', '<section>Template</section>');
});
var container = compileContainer();
toaster.pop({ bodyOutputType: 'template' });
rootScope.$digest();
expect(toaster.toast.bodyTemplate).toBe('toasterBodyTmpl.html');
var body = container.find('section');
expect(body.length).toBe(1);
expect(body[0].outerHTML).toBe('<section class="ng-scope">Template</section>');
});
it('should render templateWithData bodyOutputType when body is passed', function () {
inject(function($templateCache) {
$templateCache.put('template.html', '<section>Template {{toaster.data}}</section>');
});
var container = compileContainer();
toaster.pop({ bodyOutputType: 'templateWithData', body: "{template: 'template.html', data: 123 }" });
rootScope.$digest();
var body = container.find('section');
expect(body.length).toBe(1);
expect(body[0].outerHTML).toBe('<section class="ng-binding ng-scope">Template 123</section>');
});
it('should throw exception for default templateWithData bodyOutputType when body is not passed', function () {
// TODO: If the default fallback template cannot be parsed to an object
// composed of template and data, an exception is thrown. This seems to
// be undesirable behavior. A clearer exception should be thrown, or better
// handling should be handled, or the fallback option should be removed.
inject(function($templateCache) {
$templateCache.put('template.html', '<section>Template {{toaster.data}}</section>');
});
compileContainer();
var hasException = false;
try {
toaster.pop({ bodyOutputType: 'templateWithData' });
} catch (e) {
expect(e.message).toBe("Cannot read property 'template' of undefined");
hasException = true;
}
expect(hasException).toBe(true);
});
it('should remove first in toast if limit is met and newest-on-top is true', function () {
var container = angular.element(
'<toaster-container toaster-options="{\'limit\': 2, \'newest-on-top\': true }"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
var scope = container.scope();
toaster.pop({ type: 'info', body: 'first' });
toaster.pop({ type: 'info', body: 'second' });
rootScope.$digest();
expect(scope.toasters.length).toBe(2);
expect(scope.toasters[0].body).toBe('second');
expect(scope.toasters[1].body).toBe('first');
toaster.pop({ type: 'info', body: 'third' });
rootScope.$digest();
expect(scope.toasters.length).toBe(2);
expect(scope.toasters[0].body).toBe('third');
expect(scope.toasters[1].body).toBe('second');
});
it('should remove last in toast if limit is met and newest-on-top is false', function () {
var container = angular.element(
'<toaster-container toaster-options="{\'limit\': 2, \'newest-on-top\': false }"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
var scope = container.scope();
toaster.pop({ type: 'info', body: 'first' });
toaster.pop({ type: 'info', body: 'second' });
rootScope.$digest();
expect(scope.toasters.length).toBe(2);
expect(scope.toasters[0].body).toBe('first');
expect(scope.toasters[1].body).toBe('second');
toaster.pop({ type: 'info', body: 'third' });
rootScope.$digest();
expect(scope.toasters.length).toBe(2);
expect(scope.toasters[0].body).toBe('second');
expect(scope.toasters[1].body).toBe('third');
});
it('should invoke onShowCallback if it exists when toast is added', function () {
compileContainer();
var mock = {
callback : function () { }
};
spyOn(mock, 'callback');
toaster.pop({ type: 'info', body: 'toast 1', onShowCallback: mock.callback });
rootScope.$digest();
expect(mock.callback).toHaveBeenCalled();
});
it('should not invoke onShowCallback if it does not exist when toast is added', function () {
compileContainer();
var mock = {
callback : function () { }
};
spyOn(mock, 'callback');
toaster.pop({ type: 'info', body: 'toast 1' });
rootScope.$digest();
expect(mock.callback).not.toHaveBeenCalled();
});
it('should invoke pass toast instance to onShowCallback', function () {
compileContainer();
var toastSetByCallback = null;
function callback(t) {
toastSetByCallback = t;
}
toaster.pop({ type: 'info', body: 'toast 1', onShowCallback: callback });
rootScope.$digest();
expect(toastSetByCallback).not.toBeNull();
});
});
describe('removeToast', function () {
it('should not remove toast if toastId does not match a toastId', function () {
var container = compileContainer();
var scope = container.scope();
var toast1 = toaster.pop({ type: 'info', body: 'toast 1' });
var toast2 = toaster.pop({ type: 'info', body: 'toast 2' });
rootScope.$digest();
expect(scope.toasters.length).toBe(2);
expect(scope.toasters[1].toastId).toBe(toast1.toastId)
expect(scope.toasters[0].toastId).toBe(toast2.toastId)
scope.removeToast(3);
rootScope.$digest();
expect(scope.toasters.length).toBe(2);
});
it('should invoke onHideCallback if it exists when toast is removed', function () {
var container = compileContainer();
var scope = container.scope();
var mock = {
callback : function () { }
};
spyOn(mock, 'callback');
var toast = toaster.pop({ type: 'info', body: 'toast 1', onHideCallback: mock.callback });
rootScope.$digest();
scope.removeToast(toast.toastId);
rootScope.$digest();
expect(mock.callback).toHaveBeenCalled();
});
it('should invoke pass toast instance to onHideCallback', function () {
var container = compileContainer();
var scope = container.scope();
var toastSetByCallback = null;
function callback(t) {
toastSetByCallback = t;
}
var toast = toaster.pop({ type: 'info', body: 'toast 1', onHideCallback: callback });
rootScope.$digest();
scope.removeToast(toast.toastId);
rootScope.$digest();
expect(toastSetByCallback).not.toBeNull();
});
it('should invoke onHideCallback if toast is removed by limit', function () {
var container = angular.element(
'<toaster-container toaster-options="{\'limit\': 2, \'newest-on-top\': true }"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
var scope = container.scope();
var mock = {
callback : function () { }
};
spyOn(mock, 'callback');
toaster.pop({ type: 'info', body: 'first', onHideCallback: mock.callback });
toaster.pop({ type: 'info', body: 'second' });
rootScope.$digest();
expect(scope.toasters.length).toBe(2);
expect(scope.toasters[0].body).toBe('second');
expect(scope.toasters[1].body).toBe('first');
toaster.pop({ type: 'info', body: 'third' });
rootScope.$digest();
expect(scope.toasters.length).toBe(2);
expect(scope.toasters[0].body).toBe('third');
expect(scope.toasters[1].body).toBe('second');
expect(mock.callback).toHaveBeenCalled();
});
});
describe('scope._onNewTest', function () {
it('should not add toast if toasterId is passed to scope._onNewToast but toasterId is not set via config', function () {
var container = compileContainer();
var scope = container.scope();
expect(scope.config.toasterId).toBeUndefined();
toaster.pop({ type: 'info', body: 'toast 1', toasterId: 1 });
rootScope.$digest();
expect(scope.toasters.length).toBe(0);
});
it('should add toast if toasterId is passed to scope._onNewToast and toasterId is set via config', function () {
var container = angular.element(
'<toaster-container toaster-options="{ \'toaster-id\': 1 }"></toaster-container>');
$compile(container)(rootScope);
rootScope.$digest();
var scope = container.scope();
expect(scope.config.toasterId).toBe(1);
toaster.pop({ type: 'info', body: 'toast 1', toasterId: 1 });
rootScope.$digest();
expect(scope.toasters.length).toBe(1);
});
it('should add toasts to their respective container based on toasterId', function () {
var container1 = angular.element(
'<toaster-container toaster-options="{ \'toaster-id\': 1 }"></toaster-container>');
var container2 = angular.element(
'<toaster-container toaster-options="{ \'toaster-id\': 2 }"></toaster-container>');
$compile(container1)(rootScope);
$compile(container2)(rootScope);
rootScope.$digest();
var scope1 = container1.scope();
var scope2 = container2.scope();
toaster.pop({ type: 'info', body: 'toast 1', toasterId: 1 });
toaster.pop({ type: 'info', body: 'toast 2', toasterId: 2 });
rootScope.$digest();
expect(scope1.toasters.length).toBe(1);
expect(scope2.toasters.length).toBe(1);
});
});
describe('scope._onClearToasts', function (){
it('should remove all toasts from all containers if toasterId is *', function () {
var container1 = angular.element(
'<toaster-container toaster-options="{ \'toaster-id\': 1 }"></toaster-container>');
var container2 = angular.element(
'<toaster-container toaster-options="{ \'toaster-id\': 2 }"></toaster-container>');
$compile(container1)(rootScope);
$compile(container2)(rootScope);
rootScope.$digest();
var scope1 = container1.scope();
var scope2 = container2.scope();
toaster.pop({ type: 'info', body: 'toast 1', toasterId: 1 });
toaster.pop({ type: 'info', body: 'toast 2', toasterId: 2 });
rootScope.$digest();
expect(scope1.toasters.length).toBe(1);
expect(scope2.toasters.length).toBe(1);
toaster.clear('*');
rootScope.$digest();
expect(scope1.toasters.length).toBe(0);
expect(scope2.toasters.length).toBe(0);
});
it('should remove all toasts from all containers if config.toasterId and toastId are undefined', function () {
var container1 = angular.element(
'<toaster-container toaster-options="{ \'close-button\': false }"></toaster-container>');
var container2 = angular.element(
'<toaster-container toaster-options="{ \'close-button\': true }" ></toaster-container>');
$compile(container1)(rootScope);
$compile(container2)(rootScope);
rootScope.$digest();
var scope1 = container1.scope();
var scope2 = container2.scope();
toaster.pop({ type: 'info', body: 'toast 1' });
toaster.pop({ type: 'info', body: 'toast 2' });
rootScope.$digest();
// since there are two separate instances of the container
// without a toasterId, both receive the newToast event
expect(scope1.toasters.length).toBe(2);
expect(scope2.toasters.length).toBe(2);
toaster.clear();
rootScope.$digest();
expect(scope1.toasters.length).toBe(0);
expect(scope2.toasters.length).toBe(0);
});
it('should not remove by toasterId / toastId from the correct container if toast.toasterId is defined and toast.toastId is undefined', function () {
var container1 = angular.element(
'<toaster-container toaster-options="{ \'toaster-id\': 1 }"></toaster-container>');
var container2 = angular.element(
'<toaster-container toaster-options="{ \'toaster-id\': 2 }"></toaster-container>');
$compile(container1)(rootScope);
$compile(container2)(rootScope);
rootScope.$digest();
var scope1 = container1.scope();
var scope2 = container2.scope();
// removeAllToasts explicitly looks for toast.uid, which is only set
// if toastId is passed as a parameter
toaster.pop({ type: 'info', body: 'toast 1', toasterId: 1 });
toaster.pop({ type: 'info', body: 'toast 2', toasterId: 2 });
toaster.pop({ type: 'info', body: 'toast 3', toasterId: 2 });
rootScope.$digest();
expect(scope1.toasters.length).toBe(1);
expect(scope2.toasters.length).toBe(2);
toaster.clear(2, 1);
rootScope.$digest();
expect(scope1.toasters.length).toBe(1);
expect(scope2.toasters.length).toBe(2);
});
it('should remove by toasterId / toastId from the correct container if toasterId is defined and toastId is defined', function () {
var container1 = angular.element(
'<toaster-container toaster-options="{ \'toaster-id\': 1 }"></toaster-container>');
var container2 = angular.element(
'<toaster-container toaster-options="{ \'toaster-id\': 2 }"></toaster-container>');
$compile(container1)(rootScope);
$compile(container2)(rootScope);
rootScope.$digest();
var scope1 = container1.scope();
var scope2 = container2.scope();
// removeAllToasts explicitly looks for toast.uid, which is only set
// if toastId is passed as a parameter
var toast1 = toaster.pop({ type: 'info', body: 'toast 1', toasterId: 1, toastId: 1 });
var toast2 = toaster.pop({ type: 'info', body: 'toast 2', toasterId: 2, toastId: 1 });
var toast3 = toaster.pop({ type: 'info', body: 'toast 3', toasterId: 2, toastId: 2 });
rootScope.$digest();
expect(scope1.toasters.length).toBe(1);
expect(scope2.toasters.length).toBe(2);
toaster.clear(2, toast2.toastId);
rootScope.$digest();
expect(scope1.toasters.length).toBe(1);
expect(scope2.toasters.length).toBe(1);
});
});
});
describe('toastContainer -> addToast -> bodyOutputType.html with ngSanitize', function () {
beforeEach(function () {
module('ngSanitize');
module('toaster');
// inject the toaster service
inject(function (_toaster_, _$rootScope_, _$compile_, _$sanitize_) {
toaster = _toaster_;
rootScope = _$rootScope_;
$compile = _$compile_;
$sanitize = _$sanitize_;
});
});
it('should render \'html\' bodyOutputType if content is safe', function () {
var container = compileContainer();
toaster.pop({ bodyOutputType: 'html', body: '<div>Body</div>' });
rootScope.$digest();
var body = container[0].querySelector('.toast-message div');
expect(body.innerHTML).toBe('<div>Body</div>');
});
it('should render \'html\' bodyOutputType removing cusotm element tags', function () {
var container = compileContainer();
toaster.pop({ bodyOutputType: 'html', body: '<custom>Body</custom>' });
rootScope.$digest();
var body = container[0].querySelector('.toast-message div');
expect(body.innerHTML).toBe('Body');
});
it('should render \'html\' bodyOutputType removing ids', function () {
var container = compileContainer();
toaster.pop({ bodyOutputType: 'html', body: '<div id="test">Body</div>' });
rootScope.$digest();
var body = container[0].querySelector('.toast-message div');
expect(body.innerHTML).toBe('<div>Body</div>');
});
it('should remove unsafe attributes if \'html\' bodyOutputType', function () {
var container = compileContainer();
toaster.pop({ bodyOutputType: 'html', body: '<p style="color:blue">an html\n' +
'<em onmouseover="this.textContent=\'PWN3D!\'"><a href="www.google.com">click here</a></em>\n' +
'snippet</p>' });
rootScope.$digest();
var body = container[0].querySelector('em');
expect(body.outerHTML).toEqual('<em><a href="www.google.com">click here</a></em>');
});
});
describe('toastContainer -> addToast -> bodyOutputType.html without ngSanitize', function () {
beforeEach(function () {
module('toaster');
// inject the toaster service
inject(function (_toaster_, _$rootScope_, _$compile_) {
toaster = _toaster_;
rootScope = _$rootScope_;
$compile = _$compile_;
});
});
it('should throw exception \'html\' bodyOutputType if content is safe', function () {
var container = compileContainer();
toaster.pop({ bodyOutputType: 'html', body: '<div>Body</div>' });
var wasError = false;
try {
rootScope.$digest();
} catch (e) {
expect(e.message.includes('Attempting to use an unsafe value in a safe context')).toBeTruthy();
wasError = true;
}
expect(wasError).toEqual(true);
expect(container[0].querySelector('div[ng-switch-when="html"]').innerHTML).toEqual('');
});
it('should throw exception \'html\' bodyOutputType if content is unsafe', function () {
var container = compileContainer();
toaster.pop({ bodyOutputType: 'html', body: '<p style="color:blue">an html\n' +
'<em onmouseover="this.textContent=\'PWN3D!\'"><a href="www.google.com">click here</a></em>\n' +
'snippet</p>' });
var wasError = false;
try {
rootScope.$digest();
} catch (e) {
expect(e.message.includes('Attempting to use an unsafe value in a safe context')).toBeTruthy();
wasError = true;
}
expect(wasError).toEqual(true);
expect(container[0].querySelector('div[ng-switch-when="html"]').innerHTML).toEqual('');
});
});
describe('toasterContainer', function () {
var $interval, $intervalSpy;
inject(function (_$interval_) {
$interval = _$interval_;
});
beforeEach(function () {
$intervalSpy = jasmine.createSpy('$interval', $interval);
module('toaster', function ($provide) {
$provide.value('$interval', $intervalSpy);
});
// inject the toaster service
inject(function (_toaster_, _$rootScope_, _$compile_) {
toaster = _toaster_;
rootScope = _$rootScope_;
$compile = _$compile_;
});
});
it('should use the toast.timeout argument if it is a valid number', function () {
var container = compileContainer();
var scope = container.scope();
spyOn(scope, 'configureTimer').and.callThrough();
toaster.pop({ timeout: 2 });
expect(scope.configureTimer).toHaveBeenCalled();
expect(scope.configureTimer.calls.allArgs()[0][0].timeout).toBe(2);
expect($intervalSpy.calls.first().args[1]).toBe(2)
});
it('should not use the toast.timeout argument if not a valid number', function () {
var container = compileContainer();
var scope = container.scope();
spyOn(scope, 'configureTimer').and.callThrough();
toaster.pop({ timeout: "2" });
expect(scope.configureTimer).toHaveBeenCalled();
expect(scope.configureTimer.calls.allArgs()[0][0].timeout).toBe("2");
expect($intervalSpy.calls.first().args[1]).toBe(5000);
});
it('should call scope.removeToast when toast.timeoutPromise expires', function () {
var container = compileContainer();
var scope = container.scope();
spyOn(scope, 'removeToast').and.callThrough();
toaster.pop({ timeout: 2 });
$intervalSpy.calls.first().args[0]();
rootScope.$digest();
expect(scope.removeToast).toHaveBeenCalled();
});
it('should retrieve timeout by toast type if mergedConfig toast-timeout is an object', function () {
var element = angular.element(
'<toaster-container toaster-options="{\'time-out\': {\'toast-info\': 5}}"></toaster-container>');
$compile(element)(rootScope);
rootScope.$digest();
toaster.pop({ type: 'info' });
expect($intervalSpy.calls.first().args[1]).toBe(5);
});
it('should not set a timeout if mergedConfig toast-timeout is an object and does not match toast type', function () {
// TODO: this seems to be a bug in the toast-timeout configuration option.
// It should fall back to a default value if the toast type configuration
// does not match the target toast type or throw an exception to warn of an
// invalid configuration.
var element = angular.element(
'<toaster-container toaster-options="{\'time-out\': {\'toast-info\': 5}}"></toaster-container>');
$compile(element)(rootScope);
rootScope.$digest();
toaster.pop({ type: 'warning' });
expect($intervalSpy.calls.all().length).toBe(0);
});
});
function compileContainer() {
var element = angular.element('<toaster-container></toaster-container>');
$compile(element)(rootScope);
rootScope.$digest();
return element;
}