license import { setWith } from "../index" ; interface A { a : string ; } interface B { b : string ; } describe ( "setWith" , ( ) => { describe ( "when passed an object that overrides a key with a different value" , ( ) => { let state , result ; beforeAll ( ( ) => { state = { a : "hello" , b : "world" } ; result = setWith < typeof state , keyof A > ( state , { a : "goodbye" } ) ; } ) ; it ( "should not mutate the source object" , ( ) => { expect ( state ) . toEqual ( { a : "hello" , b : "world" } ) ; } ) ; it ( "should return a new object reference" , ( ) => { expect ( result ) . not . toBe ( state ) ; } ) ; it ( "should apply the update" , ( ) => { expect ( result ) . toEqual ( { a : "goodbye" , b : "world" } ) ; } ) ; } ) ; describe ( "when passed an object that overrides a key with the same value" , ( ) => { let state , result ; beforeAll ( ( ) => { state = { a : "hello" , b : "world" } ; result = setWith < typeof state , keyof A > ( state , { a : "hello" } ) ; } ) ; it ( "should not mutate the source object" , ( ) => { expect ( state ) . toEqual ( { a : "hello" , b : "world" } ) ; } ) ; it ( "should not return a new object reference" , ( ) => { expect ( result ) . toBe ( state ) ; } ) ; } ) ; describe ( "when passed an object with non-overlapping key" , ( ) => { let state , result ; beforeAll ( ( ) => { state = { a : "hello" } ; result = setWith < typeof state , keyof B > ( state , { b : "world" } ) ; } ) ; it ( "should not mutate the source object" , ( ) => { expect ( state ) . toEqual ( { a : "hello" } ) ; } ) ; it ( "should apply the update" , ( ) => { expect ( result ) . toEqual ( { a : "hello" , b : "world" } ) ; } ) ; } ) ; describe ( "`undefined` value handling" , ( ) => { it ( "should treat new `undefined` values as updates" , ( ) => { const state = { a : "hello" } ; const result = setWith ( state as any , { b : undefined } ) ; expect ( result ) . not . toBe ( state ) ; expect ( Object . prototype . hasOwnProperty . call ( result , "b" ) ) . toBe ( true ) ; expect ( result . b ) . toBeUndefined ( ) ; } ) ; it ( "should treat overriding `undefined` as no-op" , ( ) => { const state = { a : undefined } ; const result = setWith ( state , { a : undefined } ) ; expect ( result ) . toBe ( state ) ; } ) ; } ) ; } ) ;