@@ -8,36 +8,32 @@ describe('TypeaheadComponent', () => {
88 let component : TypeaheadComponent ;
99 let fixture : ComponentFixture < TypeaheadComponent > ;
1010
11- const testSearchResults = [ {
11+ const testSearchResults = [
12+ {
1213 displayName : 'option1' ,
13- value : { key : 1 }
14+ value : { key : 1 } ,
1415 } ,
1516 {
1617 displayName : 'option2' ,
17- value : { key : 2 }
18+ value : { key : 2 } ,
1819 } ,
1920 {
2021 displayName : 'option3' ,
21- value : { id : '1234' , key : 3 }
22- } ] ;
22+ value : { id : '1234' , key : 3 } ,
23+ } ,
24+ ] ;
2325
2426 beforeEach ( async ( ( ) => {
2527 TestBed . configureTestingModule ( {
26- declarations : [
27- TypeaheadComponent ,
28- OutsideClickListenerDirective
29- ] ,
30- imports : [
31- FormsModule ,
32- ReactiveFormsModule
33- ]
34- } )
35- . compileComponents ( ) ;
28+ declarations : [ TypeaheadComponent , OutsideClickListenerDirective ] ,
29+ imports : [ FormsModule , ReactiveFormsModule ] ,
30+ } ) . compileComponents ( ) ;
3631 } ) ) ;
3732
3833 beforeEach ( ( ) => {
3934 fixture = TestBed . createComponent ( TypeaheadComponent ) ;
4035 component = fixture . componentInstance ;
36+ component . displayProp = 'displayName' ;
4137 fixture . detectChanges ( ) ;
4238 } ) ;
4339
@@ -48,7 +44,10 @@ describe('TypeaheadComponent', () => {
4844 it ( '#should populate with search results on end of user input' , ( ) => {
4945 component . searchResults = testSearchResults ;
5046
51- expect ( component . searchResults . length > 0 ) . toBe ( true , 'search results are properly populated' ) ;
47+ expect ( component . searchResults . length > 0 ) . toBe (
48+ true ,
49+ 'search results are properly populated' ,
50+ ) ;
5251 } ) ;
5352
5453 it ( '#should choose one item when [multiple] = false' , ( ) => {
@@ -61,11 +60,19 @@ describe('TypeaheadComponent', () => {
6160 component . selectSearchResult ( component . searchResults [ 0 ] ) ;
6261 component . selectSearchResult ( component . searchResults [ 1 ] ) ;
6362
63+ console . log (
64+ ! Object . is (
65+ component . selectedItems . get ( 0 ) . value ,
66+ component . value [ 0 ] . value ,
67+ ) ,
68+ ) ;
69+
6470 // the typeahead should return a new array with the copied search items
65- expect ( component . value . length === 1 &&
66- ! Object . is ( component . selectedItems . get ( 0 ) . value , component . value [ 0 ] . value ) &&
67- component . value [ 0 ] . value . id === component . selectedItems . get ( 0 ) . value . id )
68- . toBe ( true , 'single choice is correctly populated' ) ;
71+ expect (
72+ component . value . length === 1 &&
73+ ! Object . is ( component . selectedItems . get ( 0 ) , component . value [ 0 ] ) &&
74+ component . value [ 0 ] . value . id === component . selectedItems . get ( 0 ) . value . id ,
75+ ) . toBe ( true , 'single choice is correctly populated' ) ;
6976 } ) ;
7077
7178 it ( '#should be able to choose more than one result when [multiple] = true' , ( ) => {
@@ -76,9 +83,10 @@ describe('TypeaheadComponent', () => {
7683 component . selectSearchResult ( component . searchResults [ i ] ) ;
7784 }
7885
79- expect ( component . value . length === testSearchResults . length &&
80- ! Object . is ( component . selectedItems , component . value ) )
81- . toBe ( true , 'multiple choice is correctly populated' ) ;
86+ expect (
87+ component . value . length === testSearchResults . length &&
88+ ! Object . is ( component . selectedItems , component . value ) ,
89+ ) . toBe ( true , 'multiple choice is correctly populated' ) ;
8290 } ) ;
8391
8492 it ( '#should remove selected item by using the remove button' , ( ) => {
@@ -89,27 +97,54 @@ describe('TypeaheadComponent', () => {
8997
9098 component . removeSearchResult ( component . selectedItems . size - 1 ) ;
9199
92- expect ( component . value . length === 0 )
93- . toBe ( true , 'selected item successfully removed' ) ;
100+ expect ( component . value . length === 0 ) . toBe (
101+ true ,
102+ 'selected item successfully removed' ,
103+ ) ;
94104 } ) ;
95105
96106 it ( '#should be able to populate correctly with a pre-defined SearchResult[] when [multiple] = true' , ( ) => {
97107 component . multiple = true ;
98108 component . value = testSearchResults ;
99- expect ( component . selectedItems . toArray ( ) )
100- . toEqual ( component . value , 'component value and immutable list have the same items and length' ) ;
109+ expect ( component . selectedItems . toArray ( ) ) . toEqual (
110+ component . value ,
111+ 'component value and immutable list have the same items and length' ,
112+ ) ;
101113 } ) ;
102114
103115 it ( '#should be able to populate correctly with a pre-defined SearchResult[] when [multiple] = false' , ( ) => {
104116 component . multiple = false ;
105117 component . value = testSearchResults ;
106118 const itemsToArray = component . selectedItems . toArray ( ) ;
107- expect ( itemsToArray . length === 1 )
108- . toBe ( true , 'immutable list has only one item' ) ;
109-
110- expect ( Object . is ( itemsToArray [ 0 ] , component . value [ 0 ] ) )
111- . toBe ( true , 'the only item is the first one from the value array' ) ;
119+ expect ( itemsToArray . length === 1 ) . toBe (
120+ true ,
121+ 'immutable list has only one item' ,
122+ ) ;
123+
124+ expect ( Object . is ( itemsToArray [ 0 ] , component . value [ 0 ] ) ) . toBe (
125+ true ,
126+ 'the only item is the first one from the value array' ,
127+ ) ;
112128 } ) ;
113129
130+ it ( '#should be working with plain strings' , ( ) => {
131+ const stringSearchResults = [ 'option1' , 'option2' , 'option3' ] ;
114132
133+ component . displayProp = '' ;
134+ component . multiple = false ;
135+ component . value = stringSearchResults ;
136+ component . selectSearchResult ( testSearchResults [ 0 ] ) ;
137+ component . selectSearchResult ( testSearchResults [ 1 ] ) ;
138+
139+ const itemsToArray = component . selectedItems . toArray ( ) ;
140+ expect ( itemsToArray . length === 1 ) . toBe (
141+ true ,
142+ 'immutable list has only one item' ,
143+ ) ;
144+
145+ expect ( itemsToArray [ 0 ] === component . value [ 0 ] ) . toBe (
146+ true ,
147+ 'the only item is the first one from the value array' ,
148+ ) ;
149+ } ) ;
115150} ) ;
0 commit comments