@@ -99,6 +99,59 @@ describe('KeyValuePipe', () => {
9999 const pipe = new KeyValuePipe ( defaultKeyValueDiffers ) ;
100100 expect ( pipe . transform ( value ) ) . toEqual ( null ) ;
101101 } ) ;
102+
103+ it ( 'should accept an object with optional keys' , ( ) => {
104+ interface MyInterface {
105+ one : string ;
106+ two : number ;
107+ three : string ;
108+ four : string ;
109+ }
110+ const myData : Partial < MyInterface > = {
111+ one : 'One' ,
112+ two : 2 ,
113+ three : undefined ,
114+ } ;
115+
116+ const pipe = new KeyValuePipe ( defaultKeyValueDiffers ) ;
117+ expect ( pipe . transform ( myData ) ?. length ) . toEqual ( 3 ) ;
118+
119+ const differ = ( a : string | number | undefined , b : string | number | undefined ) : number => {
120+ return 1 ;
121+ } ;
122+ expect ( pipe . transform ( myData , differ ) ?. length ) . toEqual ( 3 ) ;
123+ } ) ;
124+
125+ it ( 'should accept an nullable object with optional keys' , ( ) => {
126+ interface MyInterface {
127+ one ?: string ;
128+ two ?: string ;
129+ three ?: string ;
130+ }
131+
132+ let value ! : MyInterface | null ;
133+ const pipe = new KeyValuePipe ( defaultKeyValueDiffers ) ;
134+ expect ( pipe . transform ( value ) ) . toEqual ( null ) ;
135+ } ) ;
136+
137+ it ( 'should accept an nullable object with optional keys' , ( ) => {
138+ interface MyInterface {
139+ one ?: string ;
140+ two ?: string ;
141+ three ?: string ;
142+ }
143+
144+ const value : MyInterface | null = { } ;
145+ const pipe = new KeyValuePipe ( defaultKeyValueDiffers ) ;
146+ expect ( pipe . transform ( value ) ?. length ) . toEqual ( 0 ) ;
147+
148+ // we use the random condition to make sure the typing includes null (else TS's inference is too smart and strips null)
149+ const value2 : MyInterface | null = Math . random ( ) <= 1 ? { one : '1' , three : '3' } : null ;
150+ const kv = pipe . transform ( value2 ) ;
151+ expect ( kv ?. length ) . toEqual ( 2 ) ;
152+ expect ( kv ) . toContain ( { key : 'one' , value : '1' } ) ;
153+ expect ( kv ) . toContain ( { key : 'three' , value : '3' } ) ;
154+ } ) ;
102155 } ) ;
103156
104157 describe ( 'Map' , ( ) => {
0 commit comments