@@ -250,35 +250,37 @@ describe('option update', () => {
250250 expect ( Widget . option . mock . calls . length ) . toBe ( 1 ) ;
251251 expect ( Widget . option . mock . calls [ 0 ] ) . toEqual ( [ 'items[0].subItems[0].a' , 234 ] ) ;
252252 } ) ;
253- } ) ;
254253
255- it ( 'updates sub-nested collection item within a custom component' , ( ) => {
256- const MySetting = ( props : any ) => {
257- const { value } = props ;
254+ it ( 'updates sub-nested collection item within a custom component' , ( ) => {
255+ const MySetting = ( props : any ) => {
256+ const { value } = props ;
258257
259- return (
260- < CollectionNestedComponent >
261- < CollectionSubNestedComponent a = { value } />
262- </ CollectionNestedComponent >
263- ) ;
264- } ;
258+ return (
259+ < CollectionNestedComponent >
260+ < CollectionSubNestedComponent a = { value } />
261+ </ CollectionNestedComponent >
262+ ) ;
263+ } ;
265264
266- const TestContainer = ( props : any ) => {
267- const { value } = props ;
268- return (
269- < TestComponentWithExpectation >
270- < MySetting value = { value } />
271- </ TestComponentWithExpectation >
272- ) ;
273- } ;
274- const { rerender } = render ( < TestContainer value = { 123 } /> ) ;
275- rerender ( < TestContainer value = { 234 } /> ) ;
265+ const TestContainer = ( props : any ) => {
266+ const { value } = props ;
267+ return (
268+ < TestComponentWithExpectation >
269+ < MySetting value = { value } />
270+ </ TestComponentWithExpectation >
271+ ) ;
272+ } ;
273+ const { rerender } = render ( < TestContainer value = { 123 } /> ) ;
274+ rerender ( < TestContainer value = { 234 } /> ) ;
276275
277- jest . runAllTimers ( ) ;
278- expect ( Widget . option . mock . calls . length ) . toBe ( 1 ) ;
279- expect ( Widget . option . mock . calls [ 0 ] ) . toEqual ( [ 'items[0].subItems[0].a' , 234 ] ) ;
276+ jest . runAllTimers ( ) ;
277+ expect ( Widget . option . mock . calls . length ) . toBe ( 1 ) ;
278+ expect ( Widget . option . mock . calls [ 0 ] ) . toEqual ( [ 'items[0].subItems[0].a' , 234 ] ) ;
279+ } ) ;
280280} ) ;
281281
282+
283+
282284describe ( 'option control' , ( ) => {
283285 afterEach ( ( ) => {
284286 jest . clearAllMocks ( ) ;
@@ -359,26 +361,36 @@ describe('option control', () => {
359361 } ) ;
360362
361363 it ( 'should not rollback option if optionChanged is fired in endUpdate on props updating' , ( ) => {
362- const { rerender } = render (
364+ const obj = { a : 'changed' } ;
365+
366+ let { rerender } = render (
363367 < ControlledComponent
364368 controlledOption = "controlled"
369+ complexOption = { { a : "controlled" } }
365370 /> ,
366371 ) ;
367372
368- Widget . endUpdate . mockImplementation (
369- ( ) => {
370- fireOptionChange ( 'controlledOption' , 'changed' ) ;
371- } ,
372- ) ;
373+ try {
374+ Widget . endUpdate . mockImplementation (
375+ ( ) => {
376+ fireOptionChange ( 'controlledOption' , 'changed' ) ;
377+ fireOptionChange ( 'complexOption' , obj ) ;
378+ } ,
379+ ) ;
373380
374- rerender ( < ControlledComponent
375- controlledOption = "changed"
376- /> ) ;
381+ rerender ( < ControlledComponent
382+ controlledOption = "changed"
383+ complexOption = { obj }
384+ /> ) ;
377385
378- jest . runAllTimers ( ) ; // it is necessary to test that setGuard is not called
386+ jest . runAllTimers ( ) ; // it is necessary to test that setGuard is not called
379387
380- expect ( Widget . option ) . toHaveBeenCalledTimes ( 1 ) ;
381- expect ( Widget . option ) . toHaveBeenCalledWith ( 'controlledOption' , 'changed' ) ;
388+ expect ( Widget . option ) . toHaveBeenCalledTimes ( 2 ) ;
389+ expect ( Widget . option ) . toHaveBeenCalledWith ( 'controlledOption' , 'changed' ) ;
390+ expect ( Widget . option ) . toHaveBeenCalledWith ( 'complexOption' , obj ) ;
391+ } finally {
392+ Widget . endUpdate . mockRestore ( ) ;
393+ }
382394 } ) ;
383395
384396 it ( 'is not updated on other prop updating' , ( ) => {
@@ -542,8 +554,42 @@ describe('option control', () => {
542554 ) ;
543555
544556 jest . runAllTimers ( ) ;
557+ expect ( Widget . option . mock . calls . length ) . toBe ( 0 ) ;
558+ } ) ;
559+
560+ it ( 'applies option changed twice' , ( ) => {
561+ const { rerender } = render (
562+ < ControlledComponent controlledOption = 'a' /> ,
563+ ) ;
564+
565+ fireOptionChange ( 'controlledOption' , 'b' ) ;
566+ fireOptionChange ( 'controlledOption' , 'c' ) ;
567+
568+ rerender (
569+ < ControlledComponent controlledOption = 'c' /> ,
570+ ) ;
571+
572+ jest . runAllTimers ( ) ;
573+
574+ expect ( Widget . option . mock . calls . length ) . toBe ( 0 ) ;
575+ } ) ;
576+
577+ it ( 'rolls back an option changed one extra time' , ( ) => {
578+ const { rerender } = render (
579+ < ControlledComponent controlledOption = 'a' /> ,
580+ ) ;
581+
582+ fireOptionChange ( 'controlledOption' , 'b' ) ;
583+ fireOptionChange ( 'controlledOption' , 'c' ) ;
584+
585+ rerender (
586+ < ControlledComponent controlledOption = 'b' /> ,
587+ ) ;
588+
589+ jest . runAllTimers ( ) ;
590+
545591 expect ( Widget . option . mock . calls . length ) . toBe ( 1 ) ;
546- expect ( Widget . option . mock . calls [ 0 ] ) . toEqual ( [ 'everyOption ' , 234 ] ) ;
592+ expect ( Widget . option . mock . calls [ 0 ] ) . toEqual ( [ 'controlledOption ' , 'b' ] ) ;
547593 } ) ;
548594
549595 it ( 'applies option change with async React 18+ update' , ( ) => {
@@ -561,8 +607,7 @@ describe('option control', () => {
561607 ) ;
562608
563609 jest . runAllTimers ( ) ;
564- expect ( Widget . option . mock . calls . length ) . toBe ( 1 ) ;
565- expect ( Widget . option . mock . calls [ 0 ] ) . toEqual ( [ 'everyOption' , 234 ] ) ;
610+ expect ( Widget . option . mock . calls . length ) . toBe ( 0 ) ;
566611 } ) ;
567612
568613 it ( 'applies complex option change' , ( ) => {
@@ -939,8 +984,7 @@ describe('cfg-component option control', () => {
939984 rerender ( < TestContainer value = { 234 } /> ) ;
940985
941986 jest . runAllTimers ( ) ;
942- expect ( Widget . option . mock . calls . length ) . toBe ( 1 ) ;
943- expect ( Widget . option . mock . calls [ 0 ] ) . toEqual ( [ 'nestedOption.a' , 234 ] ) ;
987+ expect ( Widget . option . mock . calls . length ) . toBe ( 0 ) ;
944988 } ) ;
945989
946990 it ( 'apply cfg-component option change if value really change (option in custom configuration component)' , ( ) => {
@@ -961,8 +1005,7 @@ describe('cfg-component option control', () => {
9611005 rerender ( < TestContainer value = { 234 } /> ) ;
9621006
9631007 jest . runAllTimers ( ) ;
964- expect ( Widget . option . mock . calls . length ) . toBe ( 1 ) ;
965- expect ( Widget . option . mock . calls [ 0 ] ) . toEqual ( [ 'nestedOption.a' , 234 ] ) ;
1008+ expect ( Widget . option . mock . calls . length ) . toBe ( 0 ) ;
9661009 } ) ;
9671010
9681011 it ( 'does not control not specified cfg-component option' , ( ) => {
0 commit comments