This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
/
chips.spec.js
executable file
·1816 lines (1376 loc) · 62 KB
/
chips.spec.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
describe('<md-chips>', function() {
var attachedElements = [];
var scope, $exceptionHandler, $timeout;
var BASIC_CHIP_TEMPLATE =
'<md-chips ng-model="items"></md-chips>';
var CHIP_TRANSFORM_TEMPLATE =
'<md-chips ng-model="items" md-transform-chip="transformChip($chip)"></md-chips>';
var CHIP_ADD_TEMPLATE =
'<md-chips ng-model="items" md-on-add="addChip($chip, $index)"></md-chips>';
var CHIP_REMOVE_TEMPLATE =
'<md-chips ng-model="items" md-on-remove="removeChip($chip, $index, $event)"></md-chips>';
var CHIP_SELECT_TEMPLATE =
'<md-chips ng-model="items" md-on-select="selectChip($chip)"></md-chips>';
var CHIP_NG_CHANGE_TEMPLATE =
'<md-chips ng-model="items" ng-change="onModelChange(items)"></md-chips>';
var CHIP_READONLY_TEMPLATE =
'<md-chips ng-model="items" readonly="isReadonly"></md-chips>';
var CHIP_READONLY_AUTOCOMPLETE_TEMPLATE =
'<md-chips ng-model="items" readonly="true">' +
' <md-autocomplete md-items="item in [\'hi\', \'ho\', \'he\']"></md-autocomplete>' +
'</md-chips>';
var CHIP_NOT_REMOVABLE_TEMPLATE =
'<md-chips ng-model="items" readonly="true" md-removable="false"></md-chips>';
var CHIP_APPEND_DELAY_TEMPLATE =
'<md-chips ng-model="items" md-chip-append-delay="800"></md-chips>';
afterEach(function() {
attachedElements.forEach(function(element) {
var scope = element.scope();
scope && scope.$destroy();
element.remove();
});
attachedElements = [];
});
describe('with no overrides', function() {
beforeEach(module('material.components.chips', 'material.components.autocomplete'));
beforeEach(inject(function($rootScope, _$exceptionHandler_, _$timeout_) {
scope = $rootScope.$new(false);
scope.items = ['Apple', 'Banana', 'Orange'];
$exceptionHandler = _$exceptionHandler_;
$timeout = _$timeout_;
}));
describe('basic functionality', function() {
it('should render a default input element', function() {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
var input = element.find('input');
expect(input.length).toBe(1);
});
it('should render a list of chips', function() {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var chips = getChipElements(element);
expect(chips.length).toBe(3);
expect(chips[0].innerHTML).toContain('Apple');
expect(chips[1].innerHTML).toContain('Banana');
expect(chips[2].innerHTML).toContain('Orange');
});
it('should render a user-provided chip template', function() {
var template =
'<md-chips ng-model="items">' +
' <md-chip-template><div class="mychiptemplate">{$chip}</div></md-chip-template>' +
'</md-chips>';
var element = buildChips(template);
var chip = element.find('md-chip');
expect(chip[0].querySelector('div.mychiptemplate')).not.toBeNull();
});
it('should add a chip', function() {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
element.scope().$apply(function() {
ctrl.chipBuffer = 'Grape';
simulateInputEnterKey(ctrl);
});
expect(scope.items.length).toBe(4);
var chips = getChipElements(element);
expect(chips.length).toBe(4);
expect(chips[0].innerHTML).toContain('Apple');
expect(chips[1].innerHTML).toContain('Banana');
expect(chips[2].innerHTML).toContain('Orange');
expect(chips[3].innerHTML).toContain('Grape');
});
it('should not add a blank chip', function() {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
element.scope().$apply(function() {
ctrl.chipBuffer = '';
simulateInputEnterKey(ctrl);
});
expect(scope.items.length).toBe(3);
});
it('should remove a chip', function() {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
element.scope().$apply(function() {
// Remove "Banana"
ctrl.removeChip(1);
});
var chips = getChipElements(element);
expect(chips.length).toBe(2);
expect(chips[0].innerHTML).toContain('Apple');
expect(chips[1].innerHTML).toContain('Orange');
});
it('should call the transform method when adding a chip', function() {
var element = buildChips(CHIP_TRANSFORM_TEMPLATE);
var ctrl = element.controller('mdChips');
var doubleText = function(text) {
return "" + text + text;
};
scope.transformChip = jasmine.createSpy('transformChip').and.callFake(doubleText);
element.scope().$apply(function() {
ctrl.chipBuffer = 'Grape';
simulateInputEnterKey(ctrl);
});
expect(scope.transformChip).toHaveBeenCalled();
expect(scope.transformChip.calls.mostRecent().args[0]).toBe('Grape');
expect(scope.items.length).toBe(4);
expect(scope.items[3]).toBe('GrapeGrape');
});
it('should not add the chip if md-transform-chip returns null', function() {
var element = buildChips(CHIP_TRANSFORM_TEMPLATE);
var ctrl = element.controller('mdChips');
var nullChip = function(text) {
return null;
};
scope.transformChip = jasmine.createSpy('transformChip').and.callFake(nullChip);
element.scope().$apply(function() {
ctrl.chipBuffer = 'Grape';
simulateInputEnterKey(ctrl);
});
expect(scope.transformChip).toHaveBeenCalled();
expect(scope.transformChip.calls.mostRecent().args[0]).toBe('Grape');
expect(scope.items.length).toBe(3);
});
it('should call the add method when adding a chip', function() {
var element = buildChips(CHIP_ADD_TEMPLATE);
var ctrl = element.controller('mdChips');
scope.addChip = jasmine.createSpy('addChip');
element.scope().$apply(function() {
ctrl.chipBuffer = 'Grape';
simulateInputEnterKey(ctrl);
});
expect(scope.addChip).toHaveBeenCalled();
expect(scope.addChip.calls.mostRecent().args[0]).toBe('Grape'); // Chip
expect(scope.addChip.calls.mostRecent().args[1]).toBe(3); // Index
});
it('should update the view if the add method changes or removes the chip', function() {
var element = buildChips(CHIP_ADD_TEMPLATE);
var ctrl = element.controller('mdChips');
scope.addChip = function ($chip, $index) {
if ($chip === 'Grape') {
var grape = scope.items.pop();
grape += '[' + $index + ']';
scope.items.push(grape);
}
if ($chip === 'Broccoli') {
scope.items.pop();
}
};
element.scope().$apply(function() {
ctrl.chipBuffer = 'Broccoli';
simulateInputEnterKey(ctrl);
ctrl.chipBuffer = 'Grape';
simulateInputEnterKey(ctrl);
});
expect(scope.items[3]).toBe('Grape[3]');
});
it('should call the remove method when removing a chip', function() {
var element = buildChips(CHIP_REMOVE_TEMPLATE);
var ctrl = element.controller('mdChips');
scope.removeChip = jasmine.createSpy('removeChip');
element.scope().$apply(function() {
ctrl.items = ['Grape'];
ctrl.removeChip(0);
});
expect(scope.removeChip).toHaveBeenCalled();
expect(scope.removeChip.calls.mostRecent().args[0]).toBe('Grape'); // Chip
expect(scope.removeChip.calls.mostRecent().args[1]).toBe(0); // Index
});
it('should make the event available when removing a chip', function() {
var element = buildChips(CHIP_REMOVE_TEMPLATE);
var chips = getChipElements(element);
scope.removeChip = jasmine.createSpy('removeChip');
var chipButton = angular.element(chips[1]).find('button');
chipButton[0].click();
expect(scope.removeChip).toHaveBeenCalled();
expect(scope.removeChip.calls.mostRecent().args[2].type).toBe('click');
});
it('should trigger ng-change on chip addition/removal', function() {
var element = buildChips(CHIP_NG_CHANGE_TEMPLATE);
var ctrl = element.controller('mdChips');
scope.onModelChange = jasmine.createSpy('onModelChange');
element.scope().$apply(function() {
ctrl.chipBuffer = 'Melon';
simulateInputEnterKey(ctrl);
});
expect(scope.onModelChange).toHaveBeenCalled();
expect(scope.onModelChange.calls.count()).toBe(1);
expect(scope.onModelChange.calls.mostRecent().args[0].length).toBe(4);
element.scope().$apply(function() {
ctrl.removeChip(0);
});
expect(scope.onModelChange).toHaveBeenCalled();
expect(scope.onModelChange.calls.count()).toBe(2);
expect(scope.onModelChange.calls.mostRecent().args[0].length).toBe(3);
});
it('should call the select method when selecting a chip', function() {
var element = buildChips(CHIP_SELECT_TEMPLATE);
var ctrl = element.controller('mdChips');
scope.selectChip = jasmine.createSpy('selectChip');
element.scope().$apply(function() {
ctrl.items = ['Grape'];
ctrl.selectChip(0);
});
expect(scope.selectChip).toHaveBeenCalled();
expect(scope.selectChip.calls.mostRecent().args[0]).toBe('Grape');
});
describe('when adding chips on blur', function() {
it('should append a new chip for the remaining text', function() {
var element = buildChips(
'<md-chips ng-model="items" md-add-on-blur="true">' +
'</md-chips>'
);
var input = element.find('input');
expect(scope.items.length).toBe(3);
input.val('Remaining');
input.triggerHandler('change');
// Trigger a blur event, to check if the text was converted properly.
input.triggerHandler('blur');
expect(scope.items.length).toBe(4);
});
it('should update form state when a chip is added', inject(function($mdConstant) {
scope.items = [];
var template =
'<form name="form">' +
' <md-chips name="chips" ng-model="items"></md-chips>' +
'</form>';
var element = buildChips(template);
var ctrl = element.controller('mdChips');
var chips = getChipElements(element);
var input = element.find('input');
expect(scope.form.$pristine).toBeTruthy();
expect(scope.form.$dirty).toBeFalsy();
// Add 'Banana'
input.val('Banana');
// IE11 does not support the `input` event to update the ngModel. An alternative for
// `input` is to use the `change` event.
input.triggerHandler('change');
var enterEvent = {
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.ENTER,
which: $mdConstant.KEY_CODE.ENTER
};
input.triggerHandler(enterEvent);
scope.$digest();
expect(scope.form.$pristine).toBeFalsy();
expect(scope.form.$dirty).toBeTruthy();
expect(scope.items).toEqual(['Banana']);
}));
it('should allow adding the first chip on blur when required exists', function() {
scope.items = [];
var template =
'<form name="form">' +
' <md-chips name="chips" ng-required="true" ng-model="items" md-add-on-blur="true"></md-chips>' +
'</form>';
var element = buildChips(template);
var ctrl = element.find('md-chips').controller('mdChips');
element.scope().$apply(function() {
ctrl.chipBuffer = 'Test';
});
element.find('input').triggerHandler('blur');
expect(scope.form.chips.$error['required']).toBeUndefined();
expect(scope.items).toEqual(['Test']);
});
it('should not append a new chip if the limit has reached', function() {
var element = buildChips(
'<md-chips ng-model="items" md-add-on-blur="true" md-max-chips="3">' +
'</md-chips>'
);
var input = element.find('input');
expect(scope.items.length).toBe(3);
input.val('Remaining');
input.triggerHandler('change');
// Trigger a blur event, to check if the text was converted properly.
input.triggerHandler('blur');
expect(scope.items.length).toBe(3);
});
it('should not append a new chip when the chips model is invalid', function() {
var element = buildChips(
'<md-chips ng-model="items" md-add-on-blur="true">'
);
var input = element.find('input');
var ngModelCtrl = element.controller('ngModel');
expect(scope.items.length).toBe(3);
input.val('Remaining');
input.triggerHandler('change');
input.triggerHandler('blur');
$timeout.flush();
expect(scope.items.length).toBe(4);
input.val('Second');
ngModelCtrl.$setValidity('is-valid', false);
input.triggerHandler('change');
input.triggerHandler('blur');
expect(scope.items.length).toBe(4);
});
it('should not append a new chip when the custom input model is invalid', function() {
var element = buildChips(
'<md-chips ng-model="items" md-add-on-blur="true">' +
'<input ng-model="subModel" ng-maxlength="2">' +
'</md-chips>'
);
$timeout.flush();
var input = element.find('input');
expect(scope.items.length).toBe(3);
input.val('EN');
input.triggerHandler('change');
input.triggerHandler('blur');
// Flush the timeout after each blur, because custom inputs have listeners running
// in an AngularJS digest.
$timeout.flush();
expect(scope.items.length).toBe(4);
input.val('Another');
input.triggerHandler('change');
input.triggerHandler('blur');
// Flush the timeout after each blur, because custom inputs have listeners running
// in an AngularJS digest.
$timeout.flush();
expect(scope.items.length).toBe(4);
});
it('should not append a new chip when requireMatch is enabled', function() {
var template =
'<md-chips ng-model="items" md-add-on-blur="true" md-require-match="true">' +
'<md-autocomplete ' +
'md-selected-item="selectedItem" ' +
'md-search-text="searchText" ' +
'md-items="item in querySearch(searchText)" ' +
'md-item-text="item">' +
'<span md-highlight-text="searchText">{{item}}</span>' +
'</md-autocomplete>' +
'</md-chips>';
setupScopeForAutocomplete();
var element = buildChips(template);
var ctrl = element.controller('mdChips');
var input = element.find('input');
expect(ctrl.shouldAddOnBlur()).toBeFalsy();
// Flush the initial timeout of the md-autocomplete.
$timeout.flush();
scope.$apply('searchText = "Hello"');
expect(ctrl.shouldAddOnBlur()).toBeFalsy();
});
it('should not append a new chip on blur when the autocomplete is showing', function() {
var template =
'<md-chips ng-model="items" md-add-on-blur="true">' +
'<md-autocomplete ' +
'md-selected-item="selectedItem" ' +
'md-search-text="searchText" ' +
'md-items="item in querySearch(searchText)" ' +
'md-item-text="item">' +
'<span md-highlight-text="searchText">{{item}}</span>' +
'</md-autocomplete>' +
'</md-chips>';
setupScopeForAutocomplete();
var element = buildChips(template);
var ctrl = element.controller('mdChips');
var input = element.find('input');
expect(ctrl.shouldAddOnBlur()).toBeFalsy();
// Flush the initial timeout of the md-autocomplete.
$timeout.flush();
var autocompleteCtrl = element.find('md-autocomplete').controller('mdAutocomplete');
// Open the dropdown by searching for a possible item and focusing the input.
scope.$apply('searchText = "Ki"');
autocompleteCtrl.focus();
expect(ctrl.shouldAddOnBlur()).toBeFalsy();
});
});
describe('when removable', function() {
it('should not append the input div when not removable and readonly is enabled', function() {
var element = buildChips(CHIP_NOT_REMOVABLE_TEMPLATE);
var wrap = element.children();
var controller = element.controller("mdChips");
expect(wrap.hasClass("md-removable")).toBe(false);
expect(controller.removable).toBe(false);
var containers = wrap[0].querySelectorAll(".md-chip-input-container");
expect(containers.length).toBe(0);
var removeContainer = wrap[0].querySelector('.md-chip-remove-container');
expect(removeContainer).not.toBeTruthy();
});
it('should not remove chip through the backspace/delete key when removable is set to false', inject(function($mdConstant) {
var element = buildChips(CHIP_NOT_REMOVABLE_TEMPLATE);
var wrap = element.find('md-chips-wrap');
var controller = element.controller("mdChips");
var chips = getChipElements(element);
expect(wrap.hasClass("md-removable")).toBe(false);
expect(controller.removable).toBe(false);
controller.selectChip(0);
wrap.triggerHandler({
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.BACKSPACE
});
var updatedChips = getChipElements(element);
expect(chips.length).toBe(updatedChips.length);
}));
it('should remove a chip by default through the backspace/delete key', inject(function($mdConstant) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var wrap = element.find('md-chips-wrap');
var controller = element.controller("mdChips");
var chips = getChipElements(element);
controller.selectChip(0);
wrap.triggerHandler({
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.BACKSPACE
});
var updatedChips = getChipElements(element);
expect(updatedChips.length).toBe(chips.length - 1);
}));
it('should set removable to true by default', function() {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var wrap = element.children();
var controller = element.controller('mdChips');
expect(wrap.hasClass('md-removable')).toBe(true);
// The controller variable is kept undefined by default, to allow us to difference between the default value
// and a user-provided value.
expect(controller.removable).toBe(undefined);
var containers = wrap[0].querySelectorAll(".md-chip-input-container");
expect(containers.length).not.toBe(0);
var removeContainer = wrap[0].querySelector('.md-chip-remove-container');
expect(removeContainer).toBeTruthy();
});
it('should append dynamically the remove button', function() {
var template = '<md-chips ng-model="items" readonly="true" md-removable="removable"></md-chips>';
scope.removable = false;
var element = buildChips(template);
var wrap = element.children();
var controller = element.controller("mdChips");
expect(wrap.hasClass("md-removable")).toBe(false);
expect(controller.removable).toBe(false);
var containers = wrap[0].querySelectorAll(".md-chip-remove-container");
expect(containers.length).toBe(0);
scope.$apply('removable = true');
expect(wrap.hasClass("md-removable")).toBe(true);
expect(controller.removable).toBe(true);
containers = wrap[0].querySelector(".md-chip-remove-container");
expect(containers).toBeTruthy();
});
});
describe('when readonly', function() {
var element, ctrl;
it("properly toggles the controller's readonly property", function() {
element = buildChips(CHIP_READONLY_TEMPLATE);
ctrl = element.controller('mdChips');
expect(ctrl.readonly).toBeFalsy();
scope.$apply('isReadonly = true');
expect(ctrl.readonly).toBeTruthy();
});
it("properly toggles the wrapper's .md-readonly class", function() {
element = buildChips(CHIP_READONLY_TEMPLATE);
ctrl = element.controller('mdChips');
expect(element.find('md-chips-wrap')).not.toHaveClass('md-readonly');
scope.$apply('isReadonly = true');
expect(element.find('md-chips-wrap')).toHaveClass('md-readonly');
});
it('is false with empty items should not hide the chips wrapper', function() {
scope.isReadonly = false;
scope.items = [];
element = buildChips(CHIP_READONLY_TEMPLATE);
expect(element.find('md-chips-wrap').length).toBe(1);
});
it('is true with empty items should not hide the chips wrapper', function() {
scope.isReadonly = true;
scope.items = [];
element = buildChips(CHIP_READONLY_TEMPLATE);
expect(element.find('md-chips-wrap').length).toBe(1);
});
it('is true should not throw an error when used with an autocomplete', function() {
element = buildChips(CHIP_READONLY_AUTOCOMPLETE_TEMPLATE);
$timeout.flush();
expect($exceptionHandler.errors).toEqual([]);
});
it('should disable removing when `md-removable` is not defined', function() {
element = buildChips(
'<md-chips ng-model="items" readonly="isReadonly" md-removable="isRemovable"></md-chips>'
);
var wrap = element.find('md-chips-wrap');
ctrl = element.controller('mdChips');
expect(element.find('md-chips-wrap')).not.toHaveClass('md-readonly');
scope.$apply('isReadonly = true');
expect(element.find('md-chips-wrap')).toHaveClass('md-readonly');
expect(ctrl.removable).toBeUndefined();
var removeContainer = wrap[0].querySelector('.md-chip-remove-container');
expect(removeContainer).toBeFalsy();
scope.$apply('isRemovable = true');
removeContainer = wrap[0].querySelector('.md-chip-remove-container');
expect(removeContainer).toBeTruthy();
});
});
it('should disallow duplicate object chips', function() {
var element = buildChips(CHIP_TRANSFORM_TEMPLATE);
var ctrl = element.controller('mdChips');
// Manually set the items
ctrl.items = [{name: 'Apple', uppername: 'APPLE'}];
// Make our custom transformChip function return our existing item
var chipObj = function(chip) {
return ctrl.items[0];
};
scope.transformChip = jasmine.createSpy('transformChip').and.callFake(chipObj);
element.scope().$apply(function() {
ctrl.chipBuffer = 'Apple';
simulateInputEnterKey(ctrl);
});
expect(ctrl.items.length).toBe(1);
expect(scope.transformChip).toHaveBeenCalled();
expect(scope.transformChip.calls.mostRecent().args[0]).toBe('Apple');
});
it('should disallow identical object chips', function() {
var element = buildChips(CHIP_TRANSFORM_TEMPLATE);
var ctrl = element.controller('mdChips');
ctrl.items = [{name: 'Apple', uppername: 'APPLE'}];
var chipObj = function(chip) {
return {
name: chip,
uppername: chip.toUpperCase()
};
};
scope.transformChip = jasmine.createSpy('transformChip').and.callFake(chipObj);
element.scope().$apply(function() {
ctrl.chipBuffer = 'Apple';
simulateInputEnterKey(ctrl);
});
expect(ctrl.items.length).toBe(1);
expect(scope.transformChip).toHaveBeenCalled();
expect(scope.transformChip.calls.mostRecent().args[0]).toBe('Apple');
});
it('should prevent the default when backspace is pressed', inject(function($mdConstant) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
var backspaceEvent = {
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.BACKSPACE,
which: $mdConstant.KEY_CODE.BACKSPACE,
preventDefault: jasmine.createSpy('preventDefault')
};
element.find('input').triggerHandler(backspaceEvent);
expect(backspaceEvent.preventDefault).toHaveBeenCalled();
}));
describe('with input text', function() {
it('should prevent the default when enter is pressed', inject(function($mdConstant) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
var enterEvent = {
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.ENTER,
which: $mdConstant.KEY_CODE.ENTER,
preventDefault: jasmine.createSpy('preventDefault')
};
ctrl.chipBuffer = 'Test';
element.find('input').triggerHandler(enterEvent);
expect(enterEvent.preventDefault).toHaveBeenCalled();
}));
it('should trim the buffer when a chip will be added', inject(function($mdConstant) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
var input = element.find('input');
// This string contains a lot of spaces, which should be trimmed.
input.val(' Test ');
// We have to trigger the `change` event, because IE11 does not support
// the `input` event to update the ngModel. An alternative for `input` is to use the
// `change` event.
input.triggerHandler('change');
expect(ctrl.chipBuffer).toBeTruthy();
var enterEvent = {
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.ENTER,
which: $mdConstant.KEY_CODE.ENTER
};
input.triggerHandler(enterEvent);
expect(scope.items).toEqual(['Apple', 'Banana', 'Orange', 'Test']);
}));
describe('with backspace event', function() {
var backspaceEvent, element, input, ctrl, isValidInput;
beforeEach(inject(function($mdConstant) {
backspaceEvent = {
type: 'keydown',
keyCode: $mdConstant.KEY_CODE.BACKSPACE,
which: $mdConstant.KEY_CODE.BACKSPACE,
preventDefault: jasmine.createSpy('preventDefault')
};
}));
afterEach(function() {
element && element.remove();
});
function createChips(template) {
element = buildChips(template);
input = element.find('input');
ctrl = element.controller('mdChips');
$timeout.flush();
// Add the element to the document's body, because otherwise we won't be able
// to set the selection of the chip input.
document.body.appendChild(element[0]);
/** Detect whether the current input is supporting the `selectionStart` property */
var oldInputValue = input.val();
input.val('2');
isValidInput = angular.isDefined(ctrl.getCursorPosition(input[0]));
input.val(oldInputValue);
}
/**
* Updates the cursor position of the input.
* This is necessary to test the cursor position.
*/
function updateInputCursor() {
if (isValidInput) {
var inputLength = input[0].value.length;
try {
input[0].selectionStart = input[0].selectionEnd = inputLength;
} catch (e) {
// Chrome does not allow setting a selection for number inputs and just throws
// a DOMException as soon as something tries to set a selection programmatically.
// Faking the selection properties for the ChipsController works for our tests.
var selectionDescriptor = { writable: true, value: inputLength };
Object.defineProperty(input[0], 'selectionStart', selectionDescriptor);
Object.defineProperty(input[0], 'selectionEnd', selectionDescriptor);
}
}
}
it('should properly cancel the backspace event to select the chip before', function() {
createChips(BASIC_CHIP_TEMPLATE);
input.val(' ');
updateInputCursor();
input.triggerHandler('change');
input.triggerHandler(backspaceEvent);
expect(backspaceEvent.preventDefault).not.toHaveBeenCalled();
input.val('');
updateInputCursor();
input.triggerHandler('change');
input.triggerHandler(backspaceEvent);
expect(backspaceEvent.preventDefault).toHaveBeenCalledTimes(1);
});
it('should properly cancel the backspace event to select the chip before', function() {
createChips(BASIC_CHIP_TEMPLATE);
input.val(' ');
updateInputCursor();
input.triggerHandler('change');
input.triggerHandler(backspaceEvent);
expect(backspaceEvent.preventDefault).not.toHaveBeenCalled();
input.val('');
updateInputCursor();
input.triggerHandler('change');
input.triggerHandler(backspaceEvent);
expect(backspaceEvent.preventDefault).toHaveBeenCalledTimes(1);
});
it('should properly handle the cursor position when using a number input', function() {
createChips(
'<md-chips ng-model="items">' +
'<input type="number" placeholder="Enter a number">' +
'</md-chips>'
);
input.val('2');
updateInputCursor();
input.triggerHandler('change');
input.triggerHandler(backspaceEvent);
$timeout.flush();
expect(backspaceEvent.preventDefault).not.toHaveBeenCalled();
input.val('');
updateInputCursor();
input.triggerHandler('change');
input.triggerHandler(backspaceEvent);
$timeout.flush();
expect(backspaceEvent.preventDefault).toHaveBeenCalledTimes(1);
});
});
});
it('focuses/blurs the component when focusing/blurring the input', inject(function() {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
// Focus the input and check
element.find('input').triggerHandler('focus');
expect(ctrl.inputHasFocus).toBe(true);
expect(element.find('md-chips-wrap').hasClass('md-focused')).toBe(true);
// Blur the input and check
element.find('input').triggerHandler('blur');
expect(ctrl.inputHasFocus).toBe(false);
expect(element.find('md-chips-wrap').hasClass('md-focused')).toBe(false);
}));
describe('placeholder', function() {
it('should put placeholder text in the input element when chips exist but there is no secondary-placeholder text', inject(function() {
var template =
'<md-chips ng-model="items" placeholder="placeholder text"></md-chips>';
var element = buildChips(template);
var ctrl = element.controller('mdChips');
var input = element.find('input')[0];
expect(scope.items.length).toBeGreaterThan(0);
expect(input.placeholder).toBe('placeholder text');
}));
it('should put placeholder text in the input element when there are no chips', inject(function() {
var ctrl, element, input, template;
scope.items = [];
template =
'<md-chips ng-model="items" placeholder="placeholder text" ' +
'secondary-placeholder="secondary-placeholder text"></md-chips>';
element = buildChips(template);
ctrl = element.controller('mdChips');
input = element.find('input')[0];
expect(scope.items.length).toBe(0);
expect(input.placeholder).toBe('placeholder text');
}));
it('should put secondary-placeholder text in the input element when there is at least one chip', inject(function() {
var template =
'<md-chips ng-model="items" placeholder="placeholder text" ' +
'secondary-placeholder="secondary-placeholder text"></md-chips>';
var element = buildChips(template);
var ctrl = element.controller('mdChips');
var input = element.find('input')[0];
expect(scope.items.length).toBeGreaterThan(0);
expect(input.placeholder).toBe('secondary-placeholder text');
}));
});
it('utilizes the default chip append delay of 300ms', inject(function($timeout) {
var element = buildChips(BASIC_CHIP_TEMPLATE);
var ctrl = element.controller('mdChips');
// Append element to body
angular.element(document.body).append(element);
// Append a new chips which will fire the delay
ctrl.appendChip('test');
// Before 300ms timeout, focus should be on the chip (i.e. the chip content)
$timeout.flush(299);
expect(document.activeElement).toHaveClass('md-chip-content');
// At/after 300ms timeout, focus should be on the input
$timeout.flush();
expect(document.activeElement.tagName.toUpperCase()).toEqual('INPUT');
// cleanup
element.remove();
}));
it('utilizes a custom chip append delay', inject(function($timeout) {
var element = buildChips(CHIP_APPEND_DELAY_TEMPLATE);
var ctrl = element.controller('mdChips');
// Append element to body
angular.element(document.body).append(element);
// Append a new chips which will fire the delay
ctrl.appendChip('test');
// Before custom timeout, focus should be on the chip (i.e. the chip content)
$timeout.flush(ctrl.chipAppendDelay - 1);
expect(document.activeElement).toHaveClass('md-chip-content');
// At/after custom timeout, focus should be on the input
$timeout.flush();
expect(document.activeElement.tagName.toUpperCase()).toEqual('INPUT');
// cleanup
element.remove();
}));