@@ -2168,34 +2168,27 @@ describe('MdSelect', () => {
2168
2168
beforeEach ( async ( ( ) => {
2169
2169
fixture = TestBed . createComponent ( NgModelCompareWithSelect ) ;
2170
2170
instance = fixture . componentInstance ;
2171
- spyOn ( instance , 'compareByReference' ) . and . callThrough ( ) ;
2172
2171
fixture . detectChanges ( ) ;
2173
2172
} ) ) ;
2174
2173
2175
- const testCompareByReferenceBehavior = ( ) => {
2176
- it ( 'should initialize with no selection despite having a value' , ( ) => {
2177
- expect ( instance . selectedFood . value ) . toBe ( 'pizza-1' ) ;
2178
- expect ( instance . select . selected ) . toBeUndefined ( ) ;
2179
- } ) ;
2180
-
2181
- it ( 'should not update the selection when changing the value' , async ( ( ) => {
2182
- instance . options . first . _selectViaInteraction ( ) ;
2183
- fixture . detectChanges ( ) ;
2184
- fixture . whenStable ( ) . then ( ( ) => {
2185
- expect ( instance . selectedFood . value ) . toEqual ( 'steak-0' ) ;
2186
- expect ( instance . select . selected ) . toBeUndefined ( ) ;
2187
- } ) ;
2188
- } ) ) ;
2189
- } ;
2190
-
2191
- it ( 'should not use the comparator' , ( ) => {
2192
- expect ( instance . compareByReference ) . not . toHaveBeenCalled ( ) ;
2174
+ it ( 'should have a selection' , ( ) => {
2175
+ const selectedOption = instance . select . selected as MdOption ;
2176
+ expect ( selectedOption . value . value ) . toEqual ( 'pizza-1' ) ;
2193
2177
} ) ;
2194
2178
2195
- testCompareByReferenceBehavior ( ) ;
2179
+ it ( 'should update when making a new selection' , async ( ( ) => {
2180
+ instance . options . last . _selectViaInteraction ( ) ;
2181
+ fixture . detectChanges ( ) ;
2182
+ fixture . whenStable ( ) . then ( ( ) => {
2183
+ const selectedOption = instance . select . selected as MdOption ;
2184
+ expect ( instance . selectedFood . value ) . toEqual ( 'tacos-2' ) ;
2185
+ expect ( selectedOption . value . value ) . toEqual ( 'tacos-2' ) ;
2186
+ } ) ;
2187
+ } ) ) ;
2196
2188
2197
2189
describe ( 'when comparing by reference' , ( ) => {
2198
2190
beforeEach ( async ( ( ) => {
2191
+ spyOn ( instance , 'compareByReference' ) . and . callThrough ( ) ;
2199
2192
instance . useCompareByReference ( ) ;
2200
2193
fixture . detectChanges ( ) ;
2201
2194
} ) ) ;
@@ -2204,30 +2197,32 @@ describe('MdSelect', () => {
2204
2197
expect ( instance . compareByReference ) . toHaveBeenCalled ( ) ;
2205
2198
} ) ;
2206
2199
2207
- testCompareByReferenceBehavior ( ) ;
2208
- } ) ;
2209
-
2210
- describe ( 'when comparing by value' , ( ) => {
2211
- beforeEach ( async ( ( ) => {
2212
- instance . useCompareByValue ( ) ;
2213
- fixture . detectChanges ( ) ;
2214
- } ) ) ;
2215
-
2216
- it ( 'should have a selection' , ( ) => {
2217
- const selectedOption = instance . select . selected as MdOption ;
2218
- expect ( selectedOption . value . value ) . toEqual ( 'pizza-1' ) ;
2200
+ it ( 'should initialize with no selection despite having a value' , ( ) => {
2201
+ expect ( instance . selectedFood . value ) . toBe ( 'pizza-1' ) ;
2202
+ expect ( instance . select . selected ) . toBeUndefined ( ) ;
2219
2203
} ) ;
2220
2204
2221
- it ( 'should update when making a new selection ' , async ( ( ) => {
2222
- instance . options . last . _selectViaInteraction ( ) ;
2205
+ it ( 'should not update the selection when changing the value ' , async ( ( ) => {
2206
+ instance . options . first . _selectViaInteraction ( ) ;
2223
2207
fixture . detectChanges ( ) ;
2224
2208
fixture . whenStable ( ) . then ( ( ) => {
2225
- const selectedOption = instance . select . selected as MdOption ;
2226
- expect ( instance . selectedFood . value ) . toEqual ( 'tacos-2' ) ;
2227
- expect ( selectedOption . value . value ) . toEqual ( 'tacos-2' ) ;
2209
+ expect ( instance . selectedFood . value ) . toEqual ( 'steak-0' ) ;
2210
+ expect ( instance . select . selected ) . toBeUndefined ( ) ;
2228
2211
} ) ;
2229
2212
} ) ) ;
2230
2213
} ) ;
2214
+
2215
+ describe ( 'when using a non-function comparator' , ( ) => {
2216
+ beforeEach ( ( ) => {
2217
+ instance . useNullComparator ( ) ;
2218
+ } ) ;
2219
+
2220
+ it ( 'should throw an error' , ( ) => {
2221
+ expect ( ( ) => {
2222
+ fixture . detectChanges ( ) ;
2223
+ } ) . toThrowError ( 'compareWith must be a function, but received null' ) ;
2224
+ } ) ;
2225
+ } ) ;
2231
2226
} ) ;
2232
2227
} ) ;
2233
2228
@@ -2636,7 +2631,7 @@ class NgModelCompareWithSelect {
2636
2631
{ value : 'tacos-2' , viewValue : 'Tacos' } ,
2637
2632
] ;
2638
2633
selectedFood : { value : string , viewValue : string } = { value : 'pizza-1' , viewValue : 'Pizza' } ;
2639
- comparator : ( f1 : any , f2 : any ) => boolean ;
2634
+ comparator : ( ( f1 : any , f2 : any ) => boolean ) | null = this . compareByValue ;
2640
2635
2641
2636
@ViewChild ( MdSelect ) select : MdSelect ;
2642
2637
@ViewChildren ( MdOption ) options : QueryList < MdOption > ;
@@ -2645,6 +2640,8 @@ class NgModelCompareWithSelect {
2645
2640
2646
2641
useCompareByReference ( ) { this . comparator = this . compareByReference ; }
2647
2642
2643
+ useNullComparator ( ) { this . comparator = null ; }
2644
+
2648
2645
compareByValue ( f1 : any , f2 : any ) { return f1 && f2 && f1 . value === f2 . value ; }
2649
2646
2650
2647
compareByReference ( f1 : any , f2 : any ) { return f1 === f2 ; }
0 commit comments