11import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
22
3- import { AeSelectComponent } from './ae-select.component' ;
3+ import { AeSelectComponent , SelectOption } from './ae-select.component' ;
4+ import { By } from '@angular/platform-browser' ;
45
56describe ( 'AeSelectComponent' , ( ) => {
67 let component : AeSelectComponent ;
78 let fixture : ComponentFixture < AeSelectComponent > ;
89
10+ const testOptions : SelectOption [ ] = [
11+ {
12+ label : 'test label1' ,
13+ value : 'test value1'
14+ } ,
15+ {
16+ label : 'test label2' ,
17+ value : 'test value2'
18+ }
19+ ] ;
20+
921 beforeEach ( async ( ( ) => {
1022 TestBed . configureTestingModule ( {
1123 declarations : [ AeSelectComponent ]
1224 } )
13- . compileComponents ( ) ;
25+ . compileComponents ( ) ;
1426 } ) ) ;
1527
1628 beforeEach ( ( ) => {
@@ -22,4 +34,72 @@ describe('AeSelectComponent', () => {
2234 it ( 'should create' , ( ) => {
2335 expect ( component ) . toBeTruthy ( ) ;
2436 } ) ;
37+
38+ it ( 'should be visible after initialized' , ( ) => {
39+ const hide = spyOn ( component , 'hide' ) ;
40+ component . ngOnInit ( ) ;
41+ expect ( component . hidden ) . toBe ( 'inline-block' ) ;
42+ expect ( hide ) . not . toHaveBeenCalled ( ) ;
43+ } ) ;
44+
45+ it ( 'should select first option after initialized' , ( ) => {
46+ component . options = testOptions ;
47+ component . ngOnInit ( ) ;
48+ expect ( component . selectedOption ) . toBe ( testOptions [ 0 ] ) ;
49+ } ) ;
50+
51+ it ( 'should call hide method after initialized when passed isHidden: true' , ( ) => {
52+ const hide = spyOn ( component , 'hide' ) ;
53+ component . isHidden = true ;
54+ component . ngOnInit ( ) ;
55+ expect ( hide ) . toHaveBeenCalled ( ) ;
56+ } ) ;
57+
58+ it ( 'should be hidden after called hide method' , ( ) => {
59+ component . hide ( ) ;
60+ expect ( component . hidden ) . toBe ( 'none' ) ;
61+ } ) ;
62+
63+ it ( 'should render options' , ( ) => {
64+ component . options = testOptions ;
65+ component . selectedOption = testOptions [ 0 ] ;
66+ fixture . detectChanges ( ) ;
67+
68+ const options = fixture . debugElement . queryAll ( By . css ( '.ae-picker-item' ) ) ;
69+ expect ( options . length ) . toBe ( 2 ) ;
70+ } ) ;
71+
72+ it ( 'should select option by click' , ( ) => {
73+ component . options = testOptions ;
74+ component . selectedOption = testOptions [ 0 ] ;
75+ fixture . detectChanges ( ) ;
76+
77+ const options = fixture . debugElement . queryAll ( By . css ( '.ae-picker-item' ) ) ;
78+ const optionSelect = spyOn ( component , 'optionSelect' ) ;
79+ options [ 1 ] . triggerEventHandler ( 'click' , { } ) ;
80+ expect ( optionSelect ) . toHaveBeenCalledWith ( testOptions [ 1 ] , { } as MouseEvent ) ;
81+ } ) ;
82+
83+ it ( 'should select option and close after' , ( ) => {
84+ const event = new MouseEvent ( 'click' ) ;
85+ const stopPropagation = spyOn ( event , 'stopPropagation' ) ;
86+ const setValue = spyOn ( component , 'setValue' ) . and . callFake ( ( ) => { } ) ;
87+ const onChange = spyOn ( component , 'onChange' ) . and . callFake ( ( ) => { } ) ;
88+ const onTouched = spyOn ( component , 'onTouched' ) ;
89+ const changeEvent = spyOn ( component . changeEvent , 'emit' ) . and . callFake ( ( ) => { } ) ;
90+
91+ component . opened = true ;
92+
93+ component . selectedOption = testOptions [ 1 ] ;
94+
95+ component . optionSelect ( testOptions [ 1 ] , event ) ;
96+
97+ expect ( stopPropagation ) . toHaveBeenCalled ( ) ;
98+ expect ( setValue ) . toHaveBeenCalledWith ( testOptions [ 1 ] . value ) ;
99+ expect ( onChange ) . toHaveBeenCalledWith ( testOptions [ 1 ] . value ) ;
100+ expect ( onTouched ) . toHaveBeenCalled ( ) ;
101+ expect ( changeEvent ) . toHaveBeenCalledWith ( testOptions [ 1 ] . value ) ;
102+ expect ( component . opened ) . toBe ( false ) ;
103+ } ) ;
104+
25105} ) ;
0 commit comments