import * as O from '../src/index' ; describe ( 'lens/optional' , ( ) => { type Source = { foo : { bar : { baz : string ; } | undefined ; } ; xyzzy : number ; } ; const source1 = { foo : { bar : { baz : 'quux' } } , xyzzy : 42 } ; const source2 = { foo : { bar : undefined } , xyzzy : 42 } ; const prism = O . optic_ < Source > ( ) . prop ( 'foo' ) . prop ( 'bar' ) . optional ( ) . prop ( 'baz' ) ; type Focus = string | undefined ; it ( 'preview defined' , ( ) => { const result = O . preview ( prism ) ( source1 ) ; expect ( result ) . toEqual ( 'quux' ) ; } ) ; it ( 'preview undefined' , ( ) => { const result = O . preview ( prism ) ( source2 ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; it ( 'set defined - monomorphic' , ( ) => { const result = O . set ( prism ) ( 'UPDATED' ) ( source1 ) ; expect ( result ) . toEqual ( { foo : { bar : { baz : 'UPDATED' } } , xyzzy : 42 , } ) ; } ) ; it ( 'set undefined - monomorphic' , ( ) => { const result = O . set ( prism ) ( 'UPDATED' ) ( source2 ) ; expect ( result ) . toEqual ( source2 ) ; } ) ; type Target = { foo : { bar : { baz : number ; } | undefined ; } ; xyzzy : number ; } ; it ( 'set defined - polymorphic' , ( ) => { const result = O . set ( prism ) ( 999 ) ( source1 ) ; expect ( result ) . toEqual ( { foo : { bar : { baz : 999 } } , xyzzy : 42 , } ) ; } ) ; it ( 'set undefined - polymorphic' , ( ) => { const result = O . set ( prism ) ( 999 ) ( source2 ) ; expect ( result ) . toEqual ( source2 ) ; } ) ; } ) ; describe ( 'prism/optional' , ( ) => { type Source = { foo : { bar : string ; } | undefined ; } | undefined ; const source1 = { foo : { bar : 'baz' } } ; const source2 = { foo : undefined } ; const prism = O . optic_ < Source > ( ) . optional ( ) . prop ( 'foo' ) . optional ( ) . prop ( 'bar' ) ; type Focus = string | undefined ; it ( 'preview defined' , ( ) => { const result = O . preview ( prism ) ( source1 ) ; expect ( result ) . toEqual ( 'baz' ) ; } ) ; it ( 'preview undefined' , ( ) => { const result = O . preview ( prism ) ( source2 ) ; expect ( result ) . toBeUndefined ( ) ; } ) ; it ( 'set defined - monomorphic' , ( ) => { const result = O . set ( prism ) ( 'UPDATED' ) ( source1 ) ; expect ( result ) . toEqual ( { foo : { bar : 'UPDATED' } , } ) ; } ) ; it ( 'set undefined - monomorphic' , ( ) => { const result = O . set ( prism ) ( 'UPDATED' ) ( source2 ) ; expect ( result ) . toEqual ( source2 ) ; } ) ; type Target = { foo : { bar : number ; } | undefined ; } | undefined ; it ( 'set defined - polymorphic' , ( ) => { const result = O . set ( prism ) ( 42 ) ( source1 ) ; expect ( result ) . toEqual ( { foo : { bar : 42 } , } ) ; } ) ; it ( 'set undefined - polymorphic' , ( ) => { const result = O . set ( prism ) ( 42 ) ( source2 ) ; expect ( result ) . toEqual ( source2 ) ; } ) ; } ) ; describe ( 'traversal/optional' , ( ) => { type Source = { bar : string ; } | undefined ; const source = [ { bar : 'baz' } , undefined , { bar : 'xyzzy' } ] ; const traversal = O . optic_ < Source [ ] > ( ) . elems ( ) . optional ( ) . prop ( 'bar' ) ; type Focus = Array < string > ; it ( 'collect' , ( ) => { const result = O . collect ( traversal ) ( source ) ; expect ( result ) . toEqual ( [ 'baz' , 'xyzzy' ] ) ; } ) ; it ( 'modify - monomorphic' , ( ) => { const result = O . modify ( traversal ) ( ( x ) => `${ x } UPDATED` ) ( source ) ; expect ( result ) . toEqual ( [ { bar : 'baz UPDATED' } , undefined , { bar : 'xyzzy UPDATED' } , ] ) ; } ) ; type Target = { bar : number ; } | undefined ; it ( 'modify - polymorphic' , ( ) => { const result = O . modify ( traversal ) ( ( x ) => x . length ) ( source ) ; expect ( result ) . toEqual ( [ { bar : 3 } , undefined , { bar : 5 } ] ) ; } ) ; } ) ;