@@ -2,6 +2,7 @@ import { fakeAsync, flush } from '@angular/core/testing';
22import { IconType } from '@hypertrace/assets-library' ;
33import { createHostFactory , SpectatorHost } from '@ngneat/spectator/jest' ;
44import { MockComponent } from 'ng-mocks' ;
5+ import { DividerComponent } from '../divider/divider.component' ;
56import { LabelComponent } from '../label/label.component' ;
67import { LetAsyncModule } from '../let-async/let-async.module' ;
78import { SelectJustify } from '../select/select-justify' ;
@@ -13,7 +14,7 @@ describe('Multi Select Component', () => {
1314 component : MultiSelectComponent ,
1415 imports : [ LetAsyncModule ] ,
1516 entryComponents : [ SelectOptionComponent ] ,
16- declarations : [ MockComponent ( LabelComponent ) ] ,
17+ declarations : [ MockComponent ( LabelComponent ) , MockComponent ( DividerComponent ) ] ,
1718 shallow : true
1819 } ) ;
1920
@@ -75,7 +76,7 @@ describe('Multi Select Component', () => {
7576 spectator . tick ( ) ;
7677
7778 spectator . click ( '.trigger-content' ) ;
78- const optionElements = spectator . queryAll ( '.multi-select-option' , { root : true } ) ;
79+ const optionElements = spectator . queryAll ( '.multi-select-option:not(.all-options) ' , { root : true } ) ;
7980 expect ( spectator . query ( '.multi-select-content' , { root : true } ) ) . toExist ( ) ;
8081 expect ( optionElements . length ) . toBe ( 3 ) ;
8182
@@ -109,15 +110,79 @@ describe('Multi Select Component', () => {
109110 spectator . tick ( ) ;
110111 spectator . click ( '.trigger-content' ) ;
111112
112- const optionElements = spectator . queryAll ( '.multi-select-option' , { root : true } ) ;
113+ const optionElements = spectator . queryAll ( '.multi-select-option:not(.all-options) ' , { root : true } ) ;
113114 spectator . click ( optionElements [ 2 ] ) ;
115+ spectator . tick ( ) ;
114116
115117 expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
116118 expect ( onChange ) . toHaveBeenCalledWith ( [ selectionOptions [ 1 ] . value , selectionOptions [ 2 ] . value ] ) ;
117119 expect ( spectator . query ( LabelComponent ) ?. label ) . toEqual ( 'second and 1 more' ) ;
118120 flush ( ) ;
119121 } ) ) ;
120122
123+ test ( 'should show all checkbox only when enabled by input property' , fakeAsync ( ( ) => {
124+ spectator = hostFactory (
125+ `
126+ <ht-multi-select>
127+ <ht-select-option *ngFor="let option of options" [label]="option.label" [value]="option.value">
128+ </ht-select-option>
129+ </ht-multi-select>` ,
130+ {
131+ hostProps : {
132+ options : selectionOptions
133+ }
134+ }
135+ ) ;
136+
137+ spectator . tick ( ) ;
138+ spectator . click ( '.trigger-content' ) ;
139+
140+ const allOptionElement = spectator . query ( '.all-options' , { root : true } ) ;
141+ expect ( allOptionElement ) . not . toExist ( ) ;
142+
143+ flush ( ) ;
144+ } ) ) ;
145+
146+ test ( 'should notify and update selection when all checkbox is selected' , fakeAsync ( ( ) => {
147+ const onChange = jest . fn ( ) ;
148+
149+ spectator = hostFactory (
150+ `
151+ <ht-multi-select [selected]="selected" (selectedChange)="onChange($event)" [placeholder]="placeholder" [showAllOptionControl]="showAllOptionControl">
152+ <ht-select-option *ngFor="let option of options" [label]="option.label" [value]="option.value">
153+ </ht-select-option>
154+ </ht-multi-select>` ,
155+ {
156+ hostProps : {
157+ options : selectionOptions ,
158+ selected : [ selectionOptions [ 1 ] . value ] ,
159+ placeholder : 'Select options' ,
160+ showAllOptionControl : true ,
161+ onChange : onChange
162+ }
163+ }
164+ ) ;
165+
166+ spectator . tick ( ) ;
167+ spectator . click ( '.trigger-content' ) ;
168+
169+ const allOptionElement = spectator . query ( '.all-options' , { root : true } ) ;
170+ expect ( allOptionElement ) . toExist ( ) ;
171+ spectator . click ( allOptionElement ! ) ;
172+
173+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
174+ expect ( onChange ) . toHaveBeenCalledWith ( selectionOptions . map ( option => option . value ) ) ;
175+ expect ( spectator . query ( LabelComponent ) ?. label ) . toEqual ( 'first and 2 more' ) ;
176+
177+ // De select all
178+ spectator . click ( allOptionElement ! ) ;
179+ expect ( onChange ) . toHaveBeenCalledTimes ( 2 ) ;
180+ expect ( onChange ) . toHaveBeenLastCalledWith ( [ ] ) ;
181+ expect ( spectator . query ( LabelComponent ) ?. label ) . toEqual ( 'Select options' ) ;
182+
183+ flush ( ) ;
184+ } ) ) ;
185+
121186 test ( 'should notify but not change trigger label if triggerLabelDisplayMode is placeholder' , fakeAsync ( ( ) => {
122187 const onChange = jest . fn ( ) ;
123188
@@ -141,7 +206,7 @@ describe('Multi Select Component', () => {
141206 expect ( spectator . query ( LabelComponent ) ?. label ) . toEqual ( 'Placeholder' ) ;
142207 spectator . click ( '.trigger-content' ) ;
143208
144- const optionElements = spectator . queryAll ( '.multi-select-option' , { root : true } ) ;
209+ const optionElements = spectator . queryAll ( '.multi-select-option:not(.all-options) ' , { root : true } ) ;
145210 spectator . click ( optionElements [ 2 ] ) ;
146211
147212 expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments