1
1
import { HttpClientTestingModule } from '@angular/common/http/testing' ;
2
2
import { fakeAsync , flush } from '@angular/core/testing' ;
3
+ import { By } from '@angular/platform-browser' ;
3
4
import { IconLibraryTestingModule , IconType } from '@hypertrace/assets-library' ;
4
5
import { NavigationService } from '@hypertrace/common' ;
6
+ import { IconComponent } from '@hypertrace/components' ;
5
7
import { createHostFactory , mockProvider , SpectatorHost } from '@ngneat/spectator/jest' ;
8
+ import { MockComponent } from 'ng-mocks' ;
6
9
import { EMPTY } from 'rxjs' ;
7
10
import { SelectControlOptionPosition } from './select-control-option.component' ;
8
11
import { SelectJustify } from './select-justify' ;
@@ -14,6 +17,7 @@ describe('Select Component', () => {
14
17
component : SelectComponent ,
15
18
imports : [ SelectModule , HttpClientTestingModule , IconLibraryTestingModule ] ,
16
19
declareComponent : false ,
20
+ declarations : [ MockComponent ( IconComponent ) ] ,
17
21
providers : [
18
22
mockProvider ( NavigationService , {
19
23
navigation$ : EMPTY ,
@@ -27,7 +31,7 @@ describe('Select Component', () => {
27
31
const selectionOptions = [
28
32
{ label : 'first' , value : 'first-value' } ,
29
33
{ label : 'second' , value : 'second-value' } ,
30
- { label : 'third' , value : 'third-value' , selectedLabel : 'Third Value!!!' }
34
+ { label : 'third' , value : 'third-value' , selectedLabel : 'Third Value!!!' , icon : 'test-icon' , iconColor : 'red' }
31
35
] ;
32
36
33
37
test ( 'should display initial selection' , fakeAsync ( ( ) => {
@@ -146,12 +150,56 @@ describe('Select Component', () => {
146
150
const optionElements = spectator . queryAll ( '.select-option' , { root : true } ) ;
147
151
spectator . click ( optionElements [ 2 ] ) ;
148
152
149
- expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
150
153
expect ( onChange ) . toHaveBeenCalledWith ( selectionOptions [ 2 ] . value ) ;
151
154
expect ( spectator . query ( '.trigger-content' ) ) . toHaveText ( selectionOptions [ 2 ] . selectedLabel ! ) ;
152
155
flush ( ) ;
153
156
} ) ) ;
154
157
158
+ test ( 'should set trigger-prefix-icon correctly' , fakeAsync ( ( ) => {
159
+ spectator = hostFactory (
160
+ `
161
+ <ht-select [icon]="icon">
162
+ <ht-select-option *ngFor="let option of options" [label]="option.label" [value]="option.value" [icon]="option.icon" [iconColor]="option.iconColor">
163
+ </ht-select-option>
164
+ </ht-select>` ,
165
+ {
166
+ hostProps : {
167
+ options : selectionOptions ,
168
+ icon : 'select-level-icon'
169
+ }
170
+ }
171
+ ) ;
172
+ spectator . tick ( ) ;
173
+
174
+ // No selection -> select component level icon and no color
175
+ expect ( spectator . debugElement . query ( By . css ( '.trigger-prefix-icon' ) ) . componentInstance . icon ) . toBe (
176
+ 'select-level-icon'
177
+ ) ;
178
+ // tslint:disable-next-line:no-null-keyword
179
+ expect ( spectator . debugElement . query ( By . css ( '.trigger-prefix-icon' ) ) . componentInstance . color ) . toBe ( null ) ;
180
+
181
+ // Selection with no icon -> default icon and no color
182
+ spectator . click ( '.trigger-content' ) ;
183
+ let optionElements = spectator . queryAll ( '.select-option' , { root : true } ) ;
184
+ spectator . click ( optionElements [ 1 ] ) ;
185
+ spectator . tick ( ) ;
186
+ expect ( spectator . debugElement . query ( By . css ( '.trigger-prefix-icon' ) ) . componentInstance . icon ) . toBe (
187
+ 'select-level-icon'
188
+ ) ;
189
+ expect ( spectator . debugElement . query ( By . css ( '.trigger-prefix-icon' ) ) . componentInstance . color ) . toBe ( undefined ) ;
190
+
191
+ // Selection with icon and color
192
+ spectator . click ( '.trigger-content' ) ;
193
+ optionElements = spectator . queryAll ( '.select-option' , { root : true } ) ;
194
+ spectator . click ( optionElements [ 2 ] ) ;
195
+ spectator . tick ( ) ;
196
+
197
+ expect ( spectator . debugElement . query ( By . css ( '.trigger-prefix-icon' ) ) . componentInstance . icon ) . toBe ( 'test-icon' ) ;
198
+ expect ( spectator . debugElement . query ( By . css ( '.trigger-prefix-icon' ) ) . componentInstance . color ) . toBe ( 'red' ) ;
199
+
200
+ flush ( ) ;
201
+ } ) ) ;
202
+
155
203
test ( 'should set correct label alignment' , fakeAsync ( ( ) => {
156
204
spectator = hostFactory (
157
205
`
0 commit comments