@@ -2003,34 +2003,27 @@ describe('MdSelect', () => {
2003
2003
beforeEach ( async ( ( ) => {
2004
2004
fixture = TestBed . createComponent ( NgModelCompareWithSelect ) ;
2005
2005
instance = fixture . componentInstance ;
2006
- spyOn ( instance , 'compareByReference' ) . and . callThrough ( ) ;
2007
2006
fixture . detectChanges ( ) ;
2008
2007
} ) ) ;
2009
2008
2010
- const testCompareByReferenceBehavior = ( ) => {
2011
- it ( 'should initialize with no selection despite having a value' , ( ) => {
2012
- expect ( instance . selectedFood . value ) . toBe ( 'pizza-1' ) ;
2013
- expect ( instance . select . selected ) . toBeUndefined ( ) ;
2014
- } ) ;
2015
-
2016
- it ( 'should not update the selection when changing the value' , async ( ( ) => {
2017
- instance . options . first . _selectViaInteraction ( ) ;
2018
- fixture . detectChanges ( ) ;
2019
- fixture . whenStable ( ) . then ( ( ) => {
2020
- expect ( instance . selectedFood . value ) . toEqual ( 'steak-0' ) ;
2021
- expect ( instance . select . selected ) . toBeUndefined ( ) ;
2022
- } ) ;
2023
- } ) ) ;
2024
- } ;
2025
-
2026
- it ( 'should not use the comparator' , ( ) => {
2027
- expect ( instance . compareByReference ) . not . toHaveBeenCalled ( ) ;
2009
+ it ( 'should have a selection' , ( ) => {
2010
+ const selectedOption = instance . select . selected as MdOption ;
2011
+ expect ( selectedOption . value . value ) . toEqual ( 'pizza-1' ) ;
2028
2012
} ) ;
2029
2013
2030
- testCompareByReferenceBehavior ( ) ;
2014
+ it ( 'should update when making a new selection' , async ( ( ) => {
2015
+ instance . options . last . _selectViaInteraction ( ) ;
2016
+ fixture . detectChanges ( ) ;
2017
+ fixture . whenStable ( ) . then ( ( ) => {
2018
+ const selectedOption = instance . select . selected as MdOption ;
2019
+ expect ( instance . selectedFood . value ) . toEqual ( 'tacos-2' ) ;
2020
+ expect ( selectedOption . value . value ) . toEqual ( 'tacos-2' ) ;
2021
+ } ) ;
2022
+ } ) ) ;
2031
2023
2032
2024
describe ( 'when comparing by reference' , ( ) => {
2033
2025
beforeEach ( async ( ( ) => {
2026
+ spyOn ( instance , 'compareByReference' ) . and . callThrough ( ) ;
2034
2027
instance . useCompareByReference ( ) ;
2035
2028
fixture . detectChanges ( ) ;
2036
2029
} ) ) ;
@@ -2039,30 +2032,32 @@ describe('MdSelect', () => {
2039
2032
expect ( instance . compareByReference ) . toHaveBeenCalled ( ) ;
2040
2033
} ) ;
2041
2034
2042
- testCompareByReferenceBehavior ( ) ;
2043
- } ) ;
2044
-
2045
- describe ( 'when comparing by value' , ( ) => {
2046
- beforeEach ( async ( ( ) => {
2047
- instance . useCompareByValue ( ) ;
2048
- fixture . detectChanges ( ) ;
2049
- } ) ) ;
2050
-
2051
- it ( 'should have a selection' , ( ) => {
2052
- const selectedOption = instance . select . selected as MdOption ;
2053
- expect ( selectedOption . value . value ) . toEqual ( 'pizza-1' ) ;
2035
+ it ( 'should initialize with no selection despite having a value' , ( ) => {
2036
+ expect ( instance . selectedFood . value ) . toBe ( 'pizza-1' ) ;
2037
+ expect ( instance . select . selected ) . toBeUndefined ( ) ;
2054
2038
} ) ;
2055
2039
2056
- it ( 'should update when making a new selection ' , async ( ( ) => {
2057
- instance . options . last . _selectViaInteraction ( ) ;
2040
+ it ( 'should not update the selection when changing the value ' , async ( ( ) => {
2041
+ instance . options . first . _selectViaInteraction ( ) ;
2058
2042
fixture . detectChanges ( ) ;
2059
2043
fixture . whenStable ( ) . then ( ( ) => {
2060
- const selectedOption = instance . select . selected as MdOption ;
2061
- expect ( instance . selectedFood . value ) . toEqual ( 'tacos-2' ) ;
2062
- expect ( selectedOption . value . value ) . toEqual ( 'tacos-2' ) ;
2044
+ expect ( instance . selectedFood . value ) . toEqual ( 'steak-0' ) ;
2045
+ expect ( instance . select . selected ) . toBeUndefined ( ) ;
2063
2046
} ) ;
2064
2047
} ) ) ;
2065
2048
} ) ;
2049
+
2050
+ describe ( 'when using a non-function comparator' , ( ) => {
2051
+ beforeEach ( ( ) => {
2052
+ instance . useNullComparator ( ) ;
2053
+ } ) ;
2054
+
2055
+ it ( 'should throw an error' , ( ) => {
2056
+ expect ( ( ) => {
2057
+ fixture . detectChanges ( ) ;
2058
+ } ) . toThrowError ( 'compareWith must be a function, but received null' ) ;
2059
+ } ) ;
2060
+ } ) ;
2066
2061
} ) ;
2067
2062
} ) ;
2068
2063
@@ -2425,7 +2420,7 @@ class NgModelCompareWithSelect {
2425
2420
{ value : 'tacos-2' , viewValue : 'Tacos' } ,
2426
2421
] ;
2427
2422
selectedFood : { value : string , viewValue : string } = { value : 'pizza-1' , viewValue : 'Pizza' } ;
2428
- comparator : ( f1 : any , f2 : any ) => boolean ;
2423
+ comparator : ( ( f1 : any , f2 : any ) => boolean ) | null = this . compareByValue ;
2429
2424
2430
2425
@ViewChild ( MdSelect ) select : MdSelect ;
2431
2426
@ViewChildren ( MdOption ) options : QueryList < MdOption > ;
@@ -2434,6 +2429,8 @@ class NgModelCompareWithSelect {
2434
2429
2435
2430
useCompareByReference ( ) { this . comparator = this . compareByReference ; }
2436
2431
2432
+ useNullComparator ( ) { this . comparator = null ; }
2433
+
2437
2434
compareByValue ( f1 : any , f2 : any ) { return f1 && f2 && f1 . value === f2 . value ; }
2438
2435
2439
2436
compareByReference ( f1 : any , f2 : any ) { return f1 === f2 ; }
0 commit comments