@@ -2433,34 +2433,27 @@ describe('MdSelect', () => {
2433
2433
beforeEach ( async ( ( ) => {
2434
2434
fixture = TestBed . createComponent ( NgModelCompareWithSelect ) ;
2435
2435
instance = fixture . componentInstance ;
2436
- spyOn ( instance , 'compareByReference' ) . and . callThrough ( ) ;
2437
2436
fixture . detectChanges ( ) ;
2438
2437
} ) ) ;
2439
2438
2440
- const testCompareByReferenceBehavior = ( ) => {
2441
- it ( 'should initialize with no selection despite having a value' , ( ) => {
2442
- expect ( instance . selectedFood . value ) . toBe ( 'pizza-1' ) ;
2443
- expect ( instance . select . selected ) . toBeUndefined ( ) ;
2444
- } ) ;
2445
-
2446
- it ( 'should not update the selection when changing the value' , async ( ( ) => {
2447
- instance . options . first . _selectViaInteraction ( ) ;
2448
- fixture . detectChanges ( ) ;
2449
- fixture . whenStable ( ) . then ( ( ) => {
2450
- expect ( instance . selectedFood . value ) . toEqual ( 'steak-0' ) ;
2451
- expect ( instance . select . selected ) . toBeUndefined ( ) ;
2452
- } ) ;
2453
- } ) ) ;
2454
- } ;
2455
-
2456
- it ( 'should not use the comparator' , ( ) => {
2457
- expect ( instance . compareByReference ) . not . toHaveBeenCalled ( ) ;
2439
+ it ( 'should have a selection' , ( ) => {
2440
+ const selectedOption = instance . select . selected as MdOption ;
2441
+ expect ( selectedOption . value . value ) . toEqual ( 'pizza-1' ) ;
2458
2442
} ) ;
2459
2443
2460
- testCompareByReferenceBehavior ( ) ;
2444
+ it ( 'should update when making a new selection' , async ( ( ) => {
2445
+ instance . options . last . _selectViaInteraction ( ) ;
2446
+ fixture . detectChanges ( ) ;
2447
+ fixture . whenStable ( ) . then ( ( ) => {
2448
+ const selectedOption = instance . select . selected as MdOption ;
2449
+ expect ( instance . selectedFood . value ) . toEqual ( 'tacos-2' ) ;
2450
+ expect ( selectedOption . value . value ) . toEqual ( 'tacos-2' ) ;
2451
+ } ) ;
2452
+ } ) ) ;
2461
2453
2462
2454
describe ( 'when comparing by reference' , ( ) => {
2463
2455
beforeEach ( async ( ( ) => {
2456
+ spyOn ( instance , 'compareByReference' ) . and . callThrough ( ) ;
2464
2457
instance . useCompareByReference ( ) ;
2465
2458
fixture . detectChanges ( ) ;
2466
2459
} ) ) ;
@@ -2469,30 +2462,32 @@ describe('MdSelect', () => {
2469
2462
expect ( instance . compareByReference ) . toHaveBeenCalled ( ) ;
2470
2463
} ) ;
2471
2464
2472
- testCompareByReferenceBehavior ( ) ;
2473
- } ) ;
2474
-
2475
- describe ( 'when comparing by value' , ( ) => {
2476
- beforeEach ( async ( ( ) => {
2477
- instance . useCompareByValue ( ) ;
2478
- fixture . detectChanges ( ) ;
2479
- } ) ) ;
2480
-
2481
- it ( 'should have a selection' , ( ) => {
2482
- const selectedOption = instance . select . selected as MdOption ;
2483
- expect ( selectedOption . value . value ) . toEqual ( 'pizza-1' ) ;
2465
+ it ( 'should initialize with no selection despite having a value' , ( ) => {
2466
+ expect ( instance . selectedFood . value ) . toBe ( 'pizza-1' ) ;
2467
+ expect ( instance . select . selected ) . toBeUndefined ( ) ;
2484
2468
} ) ;
2485
2469
2486
- it ( 'should update when making a new selection ' , async ( ( ) => {
2487
- instance . options . last . _selectViaInteraction ( ) ;
2470
+ it ( 'should not update the selection when changing the value ' , async ( ( ) => {
2471
+ instance . options . first . _selectViaInteraction ( ) ;
2488
2472
fixture . detectChanges ( ) ;
2489
2473
fixture . whenStable ( ) . then ( ( ) => {
2490
- const selectedOption = instance . select . selected as MdOption ;
2491
- expect ( instance . selectedFood . value ) . toEqual ( 'tacos-2' ) ;
2492
- expect ( selectedOption . value . value ) . toEqual ( 'tacos-2' ) ;
2474
+ expect ( instance . selectedFood . value ) . toEqual ( 'steak-0' ) ;
2475
+ expect ( instance . select . selected ) . toBeUndefined ( ) ;
2493
2476
} ) ;
2494
2477
} ) ) ;
2495
2478
} ) ;
2479
+
2480
+ describe ( 'when using a non-function comparator' , ( ) => {
2481
+ beforeEach ( ( ) => {
2482
+ instance . useNullComparator ( ) ;
2483
+ } ) ;
2484
+
2485
+ it ( 'should throw an error' , ( ) => {
2486
+ expect ( ( ) => {
2487
+ fixture . detectChanges ( ) ;
2488
+ } ) . toThrowError ( 'compareWith must be a function, but received null' ) ;
2489
+ } ) ;
2490
+ } ) ;
2496
2491
} ) ;
2497
2492
} ) ;
2498
2493
@@ -2958,7 +2953,7 @@ class NgModelCompareWithSelect {
2958
2953
{ value : 'tacos-2' , viewValue : 'Tacos' } ,
2959
2954
] ;
2960
2955
selectedFood : { value : string , viewValue : string } = { value : 'pizza-1' , viewValue : 'Pizza' } ;
2961
- comparator : ( f1 : any , f2 : any ) => boolean ;
2956
+ comparator : ( ( f1 : any , f2 : any ) => boolean ) | null = this . compareByValue ;
2962
2957
2963
2958
@ViewChild ( MdSelect ) select : MdSelect ;
2964
2959
@ViewChildren ( MdOption ) options : QueryList < MdOption > ;
@@ -2967,6 +2962,8 @@ class NgModelCompareWithSelect {
2967
2962
2968
2963
useCompareByReference ( ) { this . comparator = this . compareByReference ; }
2969
2964
2965
+ useNullComparator ( ) { this . comparator = null ; }
2966
+
2970
2967
compareByValue ( f1 : any , f2 : any ) { return f1 && f2 && f1 . value === f2 . value ; }
2971
2968
2972
2969
compareByReference ( f1 : any , f2 : any ) { return f1 === f2 ; }
0 commit comments