-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathselect-with-autocomplete.spec.ts
1333 lines (1096 loc) · 45 KB
/
select-with-autocomplete.spec.ts
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
/*
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import { Component, ElementRef, EventEmitter, Input, Output, QueryList, ViewChild, ViewChildren } from '@angular/core';
import { ComponentFixture, fakeAsync, flush, TestBed } from '@angular/core/testing';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { RouterTestingModule } from '@angular/router/testing';
import { from, zip, Subject } from 'rxjs';
import createSpy = jasmine.createSpy;
import {
NbSelectWithAutocompleteModule as NbSelectModule,
NbThemeModule,
NbOverlayContainerAdapter,
NB_DOCUMENT,
NbSelectWithAutocompleteComponent as NbSelectComponent,
NbLayoutModule,
NbOptionComponent,
NbOptionGroupComponent,
NbTriggerStrategyBuilderService,
NbFocusKeyManagerFactoryService,
} from '@nebular/theme';
import { NbActiveDescendantKeyManagerFactoryService } from '../cdk/a11y/descendant-key-manager';
const eventMock = { preventDefault() {} } as Event;
const TEST_GROUPS = [
{
title: 'Group 1',
options: [
{ title: 'Option 1', value: 'Option 1' },
{ title: 'Option 2', value: 'Option 2' },
{ title: 'Option 3', value: 'Option 3' },
],
},
{
title: 'Group 2',
options: [
{ title: 'Option 21', value: 'Option 21' },
{ title: 'Option 22', value: 'Option 22' },
{ title: 'Option 23', value: 'Option 23' },
],
},
{
title: 'Group 3',
options: [
{ title: 'Option 31', value: 'Option 31' },
{ title: 'Option 32', value: 'Option 32' },
{ title: 'Option 33', value: 'Option 33' },
],
},
{
title: 'Group 4',
options: [
{ title: 'Option 41', value: '' },
{ title: 'Option 42', value: '0' },
{ title: 'Option 43', value: 0 },
{ title: 'Option 44' },
],
},
];
@Component({
selector: 'nb-select-test',
template: `
<nb-layout>
<nb-layout-column>
<nb-select-with-autocomplete
placeholder="This is test select component"
[multiple]="multiple"
[selected]="selected"
(selectedChange)="selectedChange.emit($event)"
(selectOpen)="opened = true"
(selectClose)="opened = false"
>
<nb-select-label *ngIf="!multiple && customLabel">
{{ selected.split('').reverse().join('') }}
</nb-select-label>
<nb-option>None</nb-option>
<nb-option-group *ngFor="let group of groups" [title]="group.title">
<nb-option *ngFor="let option of group.options" [value]="option.value">{{ option.title }}</nb-option>
</nb-option-group>
</nb-select-with-autocomplete>
</nb-layout-column>
</nb-layout>
`,
})
export class NbSelectTestComponent {
@Input() selected: any = null;
@Input() multiple: boolean;
@Input() customLabel: boolean;
@Output() selectedChange: EventEmitter<any> = new EventEmitter();
@ViewChildren(NbOptionComponent) options: QueryList<NbOptionComponent<any>>;
groups = TEST_GROUPS;
opened = false;
}
@Component({
template: `
<nb-layout>
<nb-layout-column>
<nb-select-with-autocomplete>
<nb-option value="a">a</nb-option>
<nb-option value="b">b</nb-option>
<nb-option value="c">c</nb-option>
</nb-select-with-autocomplete>
</nb-layout-column>
</nb-layout>
`,
})
export class BasicSelectTestComponent {}
@Component({
template: `
<nb-layout>
<nb-layout-column>
<nb-select-with-autocomplete [selected]="selected" [compareWith]="compareFn">
<nb-option *ngFor="let option of options" [value]="option">{{ option }}</nb-option>
</nb-select-with-autocomplete>
</nb-layout-column>
</nb-layout>
`,
})
export class NbSelectWithOptionsObjectsComponent {
@Input() compareFn = (o1: any, o2: any) => JSON.stringify(o1) === JSON.stringify(o2);
@Input() selected = { id: 2 };
@Input() options = [{ id: 1 }, { id: 2 }, { id: 3 }];
@ViewChildren(NbOptionComponent) optionComponents: QueryList<NbOptionComponent>;
}
@Component({
template: `
<nb-layout>
<nb-layout-column>
<nb-select-with-autocomplete [selected]="selected">
<nb-option *ngFor="let option of options" [value]="option">{{ option }}</nb-option>
</nb-select-with-autocomplete>
</nb-layout-column>
</nb-layout>
`,
})
export class NbSelectWithInitiallySelectedOptionComponent {
@Input() selected = 1;
@Input() options = [1, 2, 3];
}
@Component({
template: `
<nb-layout>
<nb-layout-column>
<nb-select-with-autocomplete *ngIf="showSelect" [formControl]="formControl">
<nb-option *ngFor="let option of options" [value]="option">{{ option }}</nb-option>
</nb-select-with-autocomplete>
</nb-layout-column>
</nb-layout>
`,
})
export class NbReactiveFormSelectComponent {
options: number[] = [1];
showSelect: boolean = true;
formControl: FormControl = new FormControl();
@ViewChild(NbSelectComponent) selectComponent: NbSelectComponent;
@ViewChildren(NbOptionComponent) optionComponents: QueryList<NbOptionComponent<number>>;
}
@Component({
template: `
<nb-layout>
<nb-layout-column>
<nb-select-with-autocomplete [(ngModel)]="selectedValue">
<nb-option *ngFor="let option of options" [value]="option">{{ option }}</nb-option>
</nb-select-with-autocomplete>
</nb-layout-column>
</nb-layout>
`,
})
export class NbNgModelSelectComponent {
options: number[] = [1];
selectedValue: number = null;
@ViewChild(NbOptionComponent) optionComponent: NbOptionComponent<number>;
}
@Component({
template: `
<nb-layout>
<nb-layout-column>
<nb-select-with-autocomplete>
<nb-option>No value option</nb-option>
<nb-option [value]="null">undefined value</nb-option>
<nb-option [value]="undefined">undefined value</nb-option>
<nb-option [value]="false">false value</nb-option>
<nb-option [value]="0">0 value</nb-option>
<nb-option [value]="''">empty string value</nb-option>
<nb-option [value]="nanValue">NaN value</nb-option>
<nb-option value="1">truthy value</nb-option>
</nb-select-with-autocomplete>
</nb-layout-column>
</nb-layout>
`,
})
export class NbSelectWithFalsyOptionValuesComponent {
nanValue = NaN;
@ViewChildren(NbOptionComponent) options: QueryList<NbOptionComponent<any>>;
@ViewChildren(NbOptionComponent, { read: ElementRef }) optionElements: QueryList<ElementRef<HTMLElement>>;
get noValueOption(): NbOptionComponent<any> {
return this.options.toArray()[0];
}
get noValueOptionElement(): ElementRef<HTMLElement> {
return this.optionElements.toArray()[0];
}
get nullOption(): NbOptionComponent<any> {
return this.options.toArray()[1];
}
get nullOptionElement(): ElementRef<HTMLElement> {
return this.optionElements.toArray()[1];
}
get undefinedOption(): NbOptionComponent<any> {
return this.options.toArray()[2];
}
get undefinedOptionElement(): ElementRef<HTMLElement> {
return this.optionElements.toArray()[2];
}
get falseOption(): NbOptionComponent<any> {
return this.options.toArray()[3];
}
get falseOptionElement(): ElementRef<HTMLElement> {
return this.optionElements.toArray()[3];
}
get zeroOption(): NbOptionComponent<any> {
return this.options.toArray()[4];
}
get zeroOptionElement(): ElementRef<HTMLElement> {
return this.optionElements.toArray()[4];
}
get emptyStringOption(): NbOptionComponent<any> {
return this.options.toArray()[5];
}
get emptyStringOptionElement(): ElementRef<HTMLElement> {
return this.optionElements.toArray()[5];
}
get nanOption(): NbOptionComponent<any> {
return this.options.toArray()[6];
}
get nanOptionElement(): ElementRef<HTMLElement> {
return this.optionElements.toArray()[6];
}
get truthyOption(): NbOptionComponent<any> {
return this.options.toArray()[7];
}
get truthyOptionElement(): ElementRef<HTMLElement> {
return this.optionElements.toArray()[7];
}
}
@Component({
template: `
<nb-layout>
<nb-layout-column>
<nb-select-with-autocomplete multiple>
<nb-option>No value option</nb-option>
<nb-option [value]="null">undefined value</nb-option>
<nb-option [value]="undefined">undefined value</nb-option>
<nb-option [value]="false">false value</nb-option>
<nb-option [value]="0">0 value</nb-option>
<nb-option [value]="''">empty string value</nb-option>
<nb-option [value]="nanValue">NaN value</nb-option>
<nb-option value="1">truthy value</nb-option>
</nb-select-with-autocomplete>
</nb-layout-column>
</nb-layout>
`,
})
export class NbMultipleSelectWithFalsyOptionValuesComponent extends NbSelectWithFalsyOptionValuesComponent {}
@Component({
template: `
<nb-layout>
<nb-layout-column>
<nb-select-with-autocomplete>
<nb-option-group [disabled]="optionGroupDisabled">
<nb-option [value]="1" [disabled]="optionDisabled">1</nb-option>
</nb-option-group>
</nb-select-with-autocomplete>
</nb-layout-column>
</nb-layout>
`,
})
export class NbOptionDisabledTestComponent {
optionGroupDisabled = false;
optionDisabled = false;
@ViewChild(NbSelectComponent) selectComponent: NbSelectComponent;
@ViewChild(NbOptionGroupComponent) optionGroupComponent: NbOptionGroupComponent;
@ViewChild(NbOptionComponent) optionComponent: NbOptionComponent<number>;
}
describe('Component: NbSelectComponent', () => {
let fixture: ComponentFixture<NbSelectTestComponent>;
let overlayContainerService: NbOverlayContainerAdapter;
let overlayContainer: HTMLElement;
let document: Document;
let select: NbSelectComponent;
const setSelectedAndOpen = (selected) => {
fixture.componentInstance.selected = selected;
fixture.detectChanges();
select.show();
};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([]),
FormsModule,
ReactiveFormsModule,
NbThemeModule.forRoot(),
NbLayoutModule,
NbSelectModule,
],
declarations: [
NbSelectTestComponent,
NbSelectWithOptionsObjectsComponent,
NbSelectWithInitiallySelectedOptionComponent,
NbReactiveFormSelectComponent,
NbNgModelSelectComponent,
],
});
fixture = TestBed.createComponent(NbSelectTestComponent);
overlayContainerService = TestBed.inject(NbOverlayContainerAdapter);
document = TestBed.inject(NB_DOCUMENT);
select = fixture.debugElement.query(By.directive(NbSelectComponent)).componentInstance;
overlayContainer = document.createElement('div');
overlayContainerService.setContainer(overlayContainer);
fixture.detectChanges();
});
afterEach(() => {
select.hide();
overlayContainerService.clearContainer();
});
it('should render passed item as selected', () => {
setSelectedAndOpen('Option 23');
const selected = overlayContainer.querySelector('nb-option.selected');
expect(selected).toBeTruthy();
expect(selected.textContent).toContain('Option 23');
});
it('should render passed items as selected', () => {
select.multiple = true;
setSelectedAndOpen(['Option 1', 'Option 21', 'Option 31']);
const selected = overlayContainer.querySelectorAll('nb-option.selected');
expect(selected.length).toBe(3);
expect(selected[0].textContent).toContain('Option 1');
expect(selected[1].textContent).toContain('Option 21');
expect(selected[2].textContent).toContain('Option 31');
});
it('should fire selectedChange item when selection changes', (done) => {
setSelectedAndOpen('Option 1');
fixture.componentInstance.selectedChange.subscribe((selection) => {
expect(selection).toBe('Option 21');
done();
});
const option = overlayContainer.querySelectorAll('nb-option')[4];
option.dispatchEvent(new Event('click'));
});
it('should fire selectedChange items when selecting multiple one by one', (done) => {
select.multiple = true;
setSelectedAndOpen([]);
zip(
from([['Option 2'], ['Option 2', 'Option 21'], ['Option 2', 'Option 21', 'Option 23']]),
fixture.componentInstance.selectedChange,
).subscribe(([expected, real]) => expect(real).toEqual(expected), null, done);
const option1 = overlayContainer.querySelectorAll('nb-option')[2];
const option2 = overlayContainer.querySelectorAll('nb-option')[4];
const option3 = overlayContainer.querySelectorAll('nb-option')[6];
option1.dispatchEvent(new Event('click'));
option2.dispatchEvent(new Event('click'));
option3.dispatchEvent(new Event('click'));
});
it('should deselect item when clicking on reselect item', () => {
setSelectedAndOpen('Option 1');
const option = overlayContainer.querySelector('nb-option');
option.dispatchEvent(new Event('click'));
expect(overlayContainer.querySelectorAll('nb-option.selected').length).toBe(0);
});
it('should deselect all items when clicking on reset item in multiple select', () => {
select.multiple = true;
setSelectedAndOpen(['Option 1', 'Option 2']);
const option = overlayContainer.querySelector('nb-option');
option.dispatchEvent(new Event('click'));
expect(overlayContainer.querySelectorAll('nb-option.selected').length).toBe(0);
});
it('should emit selectionChange with empty array when reset option selected in multiple select', () => {
select.multiple = true;
setSelectedAndOpen(['Option 1', 'Option 2']);
const selectionChangeSpy = createSpy('selectionChangeSpy');
select.selectedChange.subscribe(selectionChangeSpy);
const option = overlayContainer.querySelector('nb-option');
option.dispatchEvent(new Event('click'));
expect(selectionChangeSpy).toHaveBeenCalledWith([]);
});
it('should emit selectionChange with null when reset option selected in single select', () => {
setSelectedAndOpen('Option 1');
const selectionChangeSpy = createSpy('selectionChangeSpy');
select.selectedChange.subscribe(selectionChangeSpy);
const option = overlayContainer.querySelector('nb-option');
option.dispatchEvent(new Event('click'));
expect(selectionChangeSpy).toHaveBeenCalledWith(null);
});
it('should deselect only clicked item in multiple select', () => {
select.multiple = true;
setSelectedAndOpen(['Option 1', 'Option 2']);
const option = overlayContainer.querySelectorAll('nb-option')[1];
option.dispatchEvent(new Event('click'));
fixture.detectChanges();
const selected = overlayContainer.querySelectorAll('nb-option.selected');
expect(selected.length).toBe(1);
expect(selected[0].textContent).toContain('Option 2');
});
it('should render placeholder when nothing selected', () => {
select.multiple = true;
setSelectedAndOpen([]);
const button = fixture.nativeElement.querySelector('button');
expect(button.textContent).toContain('This is test select component');
});
it('should render default label when something selected', () => {
setSelectedAndOpen('Option 1');
const button = fixture.nativeElement.querySelector('button');
expect(button.textContent).toContain('Option 1');
});
it('should render custom label when something selected and custom label provided', fakeAsync(() => {
fixture.componentInstance.customLabel = true;
fixture.componentInstance.selected = 'Option 1';
fixture.detectChanges();
flush();
fixture.detectChanges();
const button = fixture.nativeElement.querySelector('button');
expect(button.textContent).toContain('1 noitpO');
}));
it('should select initially specified value without errors', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbSelectWithInitiallySelectedOptionComponent);
selectFixture.detectChanges();
flush();
selectFixture.detectChanges();
const selectedOption = selectFixture.debugElement
.query(By.directive(NbSelectComponent))
.componentInstance.options.find((o) => o.selected);
expect(selectedOption.value).toEqual(selectFixture.componentInstance.selected);
const selectButton = selectFixture.nativeElement.querySelector('nb-select-with-autocomplete button') as HTMLElement;
expect(selectButton.textContent).toEqual(selectedOption.value.toString());
}));
it('should use compareWith function to compare values', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbSelectWithOptionsObjectsComponent);
const testComponent = selectFixture.componentInstance;
selectFixture.detectChanges();
flush();
selectFixture.detectChanges();
const selectedOption = testComponent.optionComponents.find((o) => o.selected);
expect(selectedOption.value).toEqual({ id: 2 });
}));
it('should ignore selection change if destroyed', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbReactiveFormSelectComponent);
const testSelectComponent = selectFixture.componentInstance;
selectFixture.detectChanges();
flush();
const setSelectionSpy = spyOn(testSelectComponent.selectComponent as any, 'setSelection').and.callThrough();
testSelectComponent.showSelect = false;
selectFixture.detectChanges();
expect(() => testSelectComponent.formControl.setValue(1)).not.toThrow();
expect(setSelectionSpy).not.toHaveBeenCalled();
}));
it('should select option set through formControl binding', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbReactiveFormSelectComponent);
const testComponent = selectFixture.componentInstance;
selectFixture.detectChanges();
flush();
const optionSelectSpy = spyOn(testComponent.optionComponents.first, 'select').and.callThrough();
expect(testComponent.optionComponents.first.selected).toEqual(false);
testComponent.formControl.setValue(1);
selectFixture.detectChanges();
expect(testComponent.optionComponents.first.selected).toEqual(true);
expect(optionSelectSpy).toHaveBeenCalledTimes(1);
}));
it('should select option set through select "selected" binding', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbSelectTestComponent);
const testComponent = selectFixture.componentInstance;
selectFixture.detectChanges();
flush();
const optionToSelect = testComponent.options.find((o) => o.value != null);
const optionSelectSpy = spyOn(optionToSelect, 'select').and.callThrough();
expect(optionToSelect.selected).toEqual(false);
testComponent.selected = optionToSelect.value;
selectFixture.detectChanges();
expect(optionToSelect.selected).toEqual(true);
expect(optionSelectSpy).toHaveBeenCalledTimes(1);
}));
it('should select option set through ngModel binding', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbNgModelSelectComponent);
const testComponent = selectFixture.componentInstance;
selectFixture.detectChanges();
const optionToSelect = testComponent.optionComponent;
const optionSelectSpy = spyOn(optionToSelect, 'select').and.callThrough();
expect(optionToSelect.selected).toEqual(false);
testComponent.selectedValue = optionToSelect.value;
selectFixture.detectChanges();
// need to call flush because NgModelDirective updates value on
// resolvedPromise.then
flush();
selectFixture.detectChanges();
expect(optionToSelect.selected).toEqual(true);
expect(optionSelectSpy).toHaveBeenCalledTimes(1);
}));
it('should unselect previously selected option', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbSelectTestComponent);
const testSelectComponent = selectFixture.componentInstance;
testSelectComponent.selected = TEST_GROUPS[0].options[0].value;
selectFixture.detectChanges();
flush();
selectFixture.detectChanges();
const selectedOption: NbOptionComponent<any> = testSelectComponent.options.find((o) => o.selected);
const selectionChangeSpy = createSpy('selectionChangeSpy');
selectedOption.selectionChange.subscribe(selectionChangeSpy);
testSelectComponent.selected = TEST_GROUPS[0].options[1].value;
selectFixture.detectChanges();
expect(selectionChangeSpy).toHaveBeenCalledTimes(1);
expect(selectedOption.selected).toEqual(false);
}));
it('should not deselect option if option stays selected', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbSelectTestComponent);
const testSelectComponent = selectFixture.componentInstance;
testSelectComponent.selected = TEST_GROUPS[0].options[0].value;
selectFixture.detectChanges();
flush();
selectFixture.detectChanges();
const selectedOption: NbOptionComponent<any> = testSelectComponent.options.find((o) => o.selected);
const selectionChangeSpy = spyOn(selectedOption, 'deselect');
testSelectComponent.selected = selectedOption.value;
selectFixture.detectChanges();
expect(selectionChangeSpy).not.toHaveBeenCalled();
}));
it(`should not call dispose on uninitialized resources`, () => {
const selectFixture = new NbSelectComponent(null, null, null, null, null, null, null, null, null, null, null, null);
expect(() => selectFixture.ngOnDestroy()).not.toThrow();
});
it(`should has 'empty' class when has no placeholder and text`, () => {
const selectFixture = TestBed.createComponent(NbSelectComponent);
selectFixture.detectChanges();
const button = selectFixture.debugElement.query(By.css('button'));
expect(button.classes.empty).toEqual(true);
});
it(`should set overlay width same as button inside select`, () => {
const selectFixture = TestBed.createComponent(NbSelectComponent);
const selectComponent = selectFixture.componentInstance;
selectFixture.detectChanges();
const selectElement: HTMLElement = selectFixture.nativeElement;
const buttonElement: HTMLElement = selectElement.querySelector('button');
selectElement.style.padding = '1px';
expect(selectComponent.hostWidth).not.toEqual(selectElement.offsetWidth);
expect(selectComponent.hostWidth).toEqual(buttonElement.offsetWidth);
});
it('should not open when disabled and button clicked', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbSelectComponent);
selectFixture.componentInstance.disabled = true;
selectFixture.detectChanges();
const selectButton: HTMLElement = selectFixture.debugElement.query(By.css('button')).nativeElement;
selectButton.click();
flush();
fixture.detectChanges();
expect(selectFixture.componentInstance.isOpen).toBeFalsy();
}));
it('should not open when disabled and toggle icon clicked', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbSelectComponent);
selectFixture.componentInstance.disabled = true;
selectFixture.detectChanges();
const selectToggleIcon: HTMLElement = selectFixture.debugElement.query(By.css('nb-icon')).nativeElement;
selectToggleIcon.click();
flush();
fixture.detectChanges();
expect(selectFixture.componentInstance.isOpen).toBeFalsy();
}));
it('should mark touched when select button loose focus and select closed', fakeAsync(() => {
const touchedSpy = jasmine.createSpy('touched spy');
const selectFixture = TestBed.createComponent(NbSelectComponent);
const selectComponent: NbSelectComponent = selectFixture.componentInstance;
selectFixture.detectChanges();
flush();
selectComponent.registerOnTouched(touchedSpy);
selectFixture.debugElement.query(By.css('.select-button')).triggerEventHandler('blur', {});
expect(touchedSpy).toHaveBeenCalledTimes(1);
}));
it('should not mark touched when select button loose focus and select open', fakeAsync(() => {
const touchedSpy = jasmine.createSpy('touched spy');
const selectFixture = TestBed.createComponent(NbSelectTestComponent);
select = selectFixture.debugElement.query(By.directive(NbSelectComponent)).componentInstance;
selectFixture.detectChanges();
flush();
select.registerOnTouched(touchedSpy);
select.show();
selectFixture.debugElement.query(By.css('.select-button')).triggerEventHandler('blur', {});
expect(touchedSpy).not.toHaveBeenCalled();
}));
it('should emit open event after opening and close event after closing', fakeAsync(() => {
const selectFixture = TestBed.createComponent(NbSelectTestComponent);
select = selectFixture.debugElement.query(By.directive(NbSelectComponent)).componentInstance;
selectFixture.detectChanges();
expect(selectFixture.componentInstance.opened).toBe(false);
select.show();
selectFixture.detectChanges();
flush();
expect(selectFixture.componentInstance.opened).toBe(true);
select.hide();
selectFixture.detectChanges();
flush();
expect(selectFixture.componentInstance.opened).toBe(false);
}));
});
describe('NbSelectComponent - falsy values', () => {
let fixture: ComponentFixture<NbSelectWithFalsyOptionValuesComponent>;
let testComponent: NbSelectWithFalsyOptionValuesComponent;
let select: NbSelectComponent;
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule.withRoutes([]), NbThemeModule.forRoot(), NbLayoutModule, NbSelectModule],
declarations: [NbSelectWithFalsyOptionValuesComponent, NbMultipleSelectWithFalsyOptionValuesComponent],
});
fixture = TestBed.createComponent(NbSelectWithFalsyOptionValuesComponent);
testComponent = fixture.componentInstance;
select = fixture.debugElement.query(By.directive(NbSelectComponent)).componentInstance;
fixture.detectChanges();
flush();
}));
it('should clean selection when selected option does not have a value', fakeAsync(() => {
select.selected = testComponent.truthyOption.value;
fixture.detectChanges();
testComponent.noValueOption.onClick(eventMock);
fixture.detectChanges();
expect(select.selectionModel.length).toEqual(0);
}));
it('should clean selection when selected option has null value', fakeAsync(() => {
select.selected = testComponent.truthyOption.value;
fixture.detectChanges();
testComponent.nullOption.onClick(eventMock);
fixture.detectChanges();
expect(select.selectionModel.length).toEqual(0);
}));
it('should clean selection when selected option has undefined value', fakeAsync(() => {
select.selected = testComponent.truthyOption.value;
fixture.detectChanges();
testComponent.undefinedOption.onClick(eventMock);
fixture.detectChanges();
expect(select.selectionModel.length).toEqual(0);
}));
it('should not reset selection when selected option has false value', fakeAsync(() => {
select.selected = testComponent.truthyOption.value;
fixture.detectChanges();
testComponent.falseOption.onClick(eventMock);
fixture.detectChanges();
expect(select.selectionModel.length).toEqual(1);
}));
it('should not reset selection when selected option has zero value', fakeAsync(() => {
select.selected = testComponent.truthyOption.value;
fixture.detectChanges();
testComponent.zeroOption.onClick(eventMock);
fixture.detectChanges();
expect(select.selectionModel.length).toEqual(1);
}));
it('should not reset selection when selected option has empty string value', fakeAsync(() => {
select.selected = testComponent.truthyOption.value;
fixture.detectChanges();
testComponent.emptyStringOption.onClick(eventMock);
fixture.detectChanges();
expect(select.selectionModel.length).toEqual(1);
}));
it('should not reset selection when selected option has NaN value', fakeAsync(() => {
select.selected = testComponent.truthyOption.value;
fixture.detectChanges();
testComponent.nanOption.onClick(eventMock);
fixture.detectChanges();
expect(select.selectionModel.length).toEqual(1);
}));
it('should set class if fullWidth input set to true', () => {
select.fullWidth = true;
fixture.detectChanges();
const button = fixture.debugElement.query(By.directive(NbSelectComponent));
expect(button.classes['full-width']).toEqual(true);
});
describe('multiple', () => {
beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(NbMultipleSelectWithFalsyOptionValuesComponent);
testComponent = fixture.componentInstance;
select = fixture.debugElement.query(By.directive(NbSelectComponent)).componentInstance;
fixture.detectChanges();
flush();
select.show();
fixture.detectChanges();
}));
it('should not render checkbox on options with reset values', () => {
expect(testComponent.noValueOptionElement.nativeElement.querySelector('nb-checkbox')).toEqual(null);
expect(testComponent.nullOptionElement.nativeElement.querySelector('nb-checkbox')).toEqual(null);
expect(testComponent.undefinedOptionElement.nativeElement.querySelector('nb-checkbox')).toEqual(null);
});
it('should render checkbox on options with falsy non-reset values', () => {
expect(testComponent.falseOptionElement.nativeElement.querySelector('nb-checkbox')).not.toEqual(null);
expect(testComponent.zeroOptionElement.nativeElement.querySelector('nb-checkbox')).not.toEqual(null);
expect(testComponent.emptyStringOptionElement.nativeElement.querySelector('nb-checkbox')).not.toEqual(null);
expect(testComponent.nanOptionElement.nativeElement.querySelector('nb-checkbox')).not.toEqual(null);
expect(testComponent.truthyOptionElement.nativeElement.querySelector('nb-checkbox')).not.toEqual(null);
});
});
it('should select initial falsy value', fakeAsync(() => {
fixture = TestBed.createComponent(NbSelectWithFalsyOptionValuesComponent);
testComponent = fixture.componentInstance;
select = fixture.debugElement.query(By.directive(NbSelectComponent)).componentInstance;
select.selected = '';
fixture.detectChanges();
flush();
expect(select.selectionModel[0]).toEqual(testComponent.emptyStringOption);
expect(testComponent.emptyStringOption.selected).toEqual(true);
}));
});
describe('NbSelectComponent - Triggers', () => {
let fixture: ComponentFixture<BasicSelectTestComponent>;
let selectComponent: NbSelectComponent;
let triggerBuilderStub;
let showTriggerStub: Subject<Event>;
let hideTriggerStub: Subject<Event>;
beforeEach(fakeAsync(() => {
showTriggerStub = new Subject<Event>();
hideTriggerStub = new Subject<Event>();
triggerBuilderStub = {
trigger() {
return this;
},
host() {
return this;
},
container() {
return this;
},
build() {
return { show$: showTriggerStub, hide$: hideTriggerStub, destroy() {} };
},
};
TestBed.configureTestingModule({
imports: [RouterTestingModule.withRoutes([]), NbThemeModule.forRoot(), NbLayoutModule, NbSelectModule],
declarations: [BasicSelectTestComponent],
});
TestBed.overrideProvider(NbTriggerStrategyBuilderService, { useValue: triggerBuilderStub });
fixture = TestBed.createComponent(BasicSelectTestComponent);
fixture.detectChanges();
flush();
selectComponent = fixture.debugElement.query(By.directive(NbSelectComponent)).componentInstance;
}));
it('should mark touched if clicked outside of overlay and select', fakeAsync(() => {
const touchedSpy = jasmine.createSpy('touched spy');
selectComponent.registerOnTouched(touchedSpy);
const elementOutsideSelect = fixture.debugElement.query(By.css('nb-layout')).nativeElement;
selectComponent.show();
fixture.detectChanges();
hideTriggerStub.next({ target: elementOutsideSelect } as unknown as Event);
expect(touchedSpy).toHaveBeenCalledTimes(1);
}));
it('should not mark touched if clicked on the select button', fakeAsync(() => {
const touchedSpy = jasmine.createSpy('touched spy');
selectComponent.registerOnTouched(touchedSpy);
const selectButton = fixture.debugElement.query(By.css('.select-button')).nativeElement;
selectComponent.show();
fixture.detectChanges();
hideTriggerStub.next({ target: selectButton } as unknown as Event);
expect(touchedSpy).not.toHaveBeenCalled();
}));
});
describe('NbSelectComponent - Key manager', () => {
let fixture: ComponentFixture<BasicSelectTestComponent>;
let selectComponent: NbSelectComponent;
let tabOutStub: Subject<void>;
let keyManagerFactoryStub;
let keyManagerStub;
beforeEach(fakeAsync(() => {
tabOutStub = new Subject<void>();
keyManagerStub = {
withTypeAhead() {
return this;
},
setActiveItem() {},
setFirstItemActive() {},
onKeydown() {},
skipPredicate() {
return this;
},
tabOut: tabOutStub,
};
keyManagerFactoryStub = {
create() {
return keyManagerStub;
},
};
TestBed.configureTestingModule({
imports: [RouterTestingModule.withRoutes([]), NbThemeModule.forRoot(), NbLayoutModule, NbSelectModule],
declarations: [BasicSelectTestComponent],
});
TestBed.overrideProvider(NbFocusKeyManagerFactoryService, { useValue: keyManagerFactoryStub });
TestBed.overrideProvider(NbActiveDescendantKeyManagerFactoryService, { useValue: keyManagerFactoryStub });
fixture = TestBed.createComponent(BasicSelectTestComponent);
fixture.detectChanges();
flush();
selectComponent = fixture.debugElement.query(By.directive(NbSelectComponent)).componentInstance;
}));
it('should mark touched when tabbing out from options list', fakeAsync(() => {
selectComponent.show();
fixture.detectChanges();
const touchedSpy = jasmine.createSpy('touched spy');
selectComponent.registerOnTouched(touchedSpy);
tabOutStub.next();
flush();
expect(touchedSpy).toHaveBeenCalledTimes(1);
}));
});
describe('NbOptionComponent', () => {
let fixture: ComponentFixture<NbReactiveFormSelectComponent>;
let testSelectComponent: NbReactiveFormSelectComponent;
let option: NbOptionComponent<number>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([]),
FormsModule,
ReactiveFormsModule,
NbThemeModule.forRoot(),
NbLayoutModule,
NbSelectModule,
],
declarations: [NbNgModelSelectComponent, NbSelectTestComponent, NbReactiveFormSelectComponent],
});
fixture = TestBed.createComponent(NbReactiveFormSelectComponent);
testSelectComponent = fixture.componentInstance;
fixture.detectChanges();
option = testSelectComponent.optionComponents.first;
});
it('should ignore selection change if destroyed', fakeAsync(() => {
const selectionChangeSpy = createSpy('selectionChangeSpy');
option.selectionChange.subscribe(selectionChangeSpy);
expect(option.selected).toEqual(false);
testSelectComponent.showSelect = false;
fixture.detectChanges();