From 32a2c7f43bb08691f485b003ac1b313ccbf2bbf3 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 8 Mar 2018 16:19:11 -0800 Subject: [PATCH 1/2] Revert "Revert "Make preferences reducer deterministic"" This reverts commit 025d875f6ef87c38e4c603a0a3d71d5763b24788. --- editor/store/actions.js | 2 ++ editor/store/reducer.js | 2 +- editor/store/test/actions.js | 10 ++++++++-- editor/store/test/effects.js | 22 +++++++++++++--------- editor/store/test/reducer.js | 10 ++++++---- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/editor/store/actions.js b/editor/store/actions.js index fb9aa2a6357b8..686385291b1e2 100644 --- a/editor/store/actions.js +++ b/editor/store/actions.js @@ -175,6 +175,7 @@ export function replaceBlocks( uids, blocks ) { type: 'REPLACE_BLOCKS', uids: castArray( uids ), blocks: castArray( blocks ), + time: Date.now(), }; } @@ -221,6 +222,7 @@ export function insertBlocks( blocks, index, rootUID ) { blocks: castArray( blocks ), index, rootUID, + time: Date.now(), }; } diff --git a/editor/store/reducer.js b/editor/store/reducer.js index 54d3df1830724..bf74241ce2936 100644 --- a/editor/store/reducer.js +++ b/editor/store/reducer.js @@ -705,7 +705,7 @@ export function preferences( state = PREFERENCES_DEFAULTS, action ) { insertUsage: { ...prevState.insertUsage, [ id ]: { - time: Date.now(), + time: action.time, count: prevState.insertUsage[ id ] ? prevState.insertUsage[ id ].count + 1 : 1, insert, }, diff --git a/editor/store/test/actions.js b/editor/store/test/actions.js index f73555a4866bb..1bb16da38d36c 100644 --- a/editor/store/test/actions.js +++ b/editor/store/test/actions.js @@ -163,6 +163,7 @@ describe( 'actions', () => { type: 'REPLACE_BLOCKS', uids: [ 'chicken' ], blocks: [ block ], + time: expect.any( Number ), } ); } ); } ); @@ -177,6 +178,7 @@ describe( 'actions', () => { type: 'REPLACE_BLOCKS', uids: [ 'chicken' ], blocks, + time: expect.any( Number ), } ); } ); } ); @@ -187,10 +189,12 @@ describe( 'actions', () => { uid: 'ribs', }; const index = 5; - expect( insertBlock( block, index ) ).toEqual( { + expect( insertBlock( block, index, 'test_uid' ) ).toEqual( { type: 'INSERT_BLOCKS', blocks: [ block ], index, + rootUID: 'test_uid', + time: expect.any( Number ), } ); } ); } ); @@ -201,10 +205,12 @@ describe( 'actions', () => { uid: 'ribs', } ]; const index = 3; - expect( insertBlocks( blocks, index ) ).toEqual( { + expect( insertBlocks( blocks, index, 'test_uid' ) ).toEqual( { type: 'INSERT_BLOCKS', blocks, index, + rootUID: 'test_uid', + time: expect.any( Number ), } ); } ); } ); diff --git a/editor/store/test/effects.js b/editor/store/test/effects.js index 871dfac6ff333..2d3f420b606b3 100644 --- a/editor/store/test/effects.js +++ b/editor/store/test/effects.js @@ -151,11 +151,14 @@ describe( 'effects', () => { expect( dispatch ).toHaveBeenCalledTimes( 2 ); expect( dispatch ).toHaveBeenCalledWith( selectBlock( 'chicken', -1 ) ); - expect( dispatch ).toHaveBeenCalledWith( replaceBlocks( [ 'chicken', 'ribs' ], [ { - uid: 'chicken', - name: 'core/test-block', - attributes: { content: 'chicken ribs' }, - } ] ) ); + expect( dispatch ).toHaveBeenCalledWith( { + ...replaceBlocks( [ 'chicken', 'ribs' ], [ { + uid: 'chicken', + name: 'core/test-block', + attributes: { content: 'chicken ribs' }, + } ] ), + time: expect.any( Number ), + } ); } ); it( 'should not merge the blocks have different types without transformation', () => { @@ -881,12 +884,13 @@ describe( 'effects', () => { expect( dispatch ).toHaveBeenCalledWith( saveReusableBlock( expect.any( Number ) ) ); - expect( dispatch ).toHaveBeenCalledWith( - replaceBlocks( + expect( dispatch ).toHaveBeenCalledWith( { + ...replaceBlocks( [ staticBlock.uid ], [ createBlock( 'core/block', { ref: expect.any( Number ) } ) ] - ) - ); + ), + time: expect.any( Number ), + } ); } ); } ); } ); diff --git a/editor/store/test/reducer.js b/editor/store/test/reducer.js index 6bf9ce03c3281..5ddcc0896cb3e 100644 --- a/editor/store/test/reducer.js +++ b/editor/store/test/reducer.js @@ -1272,12 +1272,13 @@ describe( 'state', () => { uid: 'bacon', name: 'core-embed/twitter', } ], + time: 123456, } ); expect( state ).toEqual( { insertUsage: { 'core-embed/twitter': { - time: expect.any( Number ), + time: 123456, count: 1, insert: { name: 'core-embed/twitter' }, }, @@ -1287,7 +1288,7 @@ describe( 'state', () => { const twoRecentBlocks = preferences( deepFreeze( { insertUsage: { 'core-embed/twitter': { - time: expect.any( Number ), + time: 123456, count: 1, insert: { name: 'core-embed/twitter' }, }, @@ -1302,17 +1303,18 @@ describe( 'state', () => { name: 'core/block', attributes: { ref: 123 }, } ], + time: 123457, } ); expect( twoRecentBlocks ).toEqual( { insertUsage: { 'core-embed/twitter': { - time: expect.any( Number ), + time: 123457, count: 2, insert: { name: 'core-embed/twitter' }, }, 'core/block/123': { - time: expect.any( Number ), + time: 123457, count: 1, insert: { name: 'core/block', ref: 123 }, }, From c49e4bf73a6ea6abc84af6b586b1480c5d7e506b Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 8 Mar 2018 16:25:48 -0800 Subject: [PATCH 2/2] Handle all cases where an action's `time` might cause a test to fail da00a9a did not update two tests which sometimes fail due to an action's `time` being the current unix timetamp. --- editor/store/test/effects.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/editor/store/test/effects.js b/editor/store/test/effects.js index 2d3f420b606b3..a742701c6096f 100644 --- a/editor/store/test/effects.js +++ b/editor/store/test/effects.js @@ -249,11 +249,14 @@ describe( 'effects', () => { expect( dispatch ).toHaveBeenCalledTimes( 2 ); // expect( dispatch ).toHaveBeenCalledWith( focusBlock( 'chicken', { offset: -1 } ) ); - expect( dispatch ).toHaveBeenCalledWith( replaceBlocks( [ 'chicken', 'ribs' ], [ { - uid: 'chicken', - name: 'core/test-block', - attributes: { content: 'chicken ribs' }, - } ] ) ); + expect( dispatch ).toHaveBeenCalledWith( { + ...replaceBlocks( [ 'chicken', 'ribs' ], [ { + uid: 'chicken', + name: 'core/test-block', + attributes: { content: 'chicken ribs' }, + } ] ), + time: expect.any( Number ), + } ); } ); } ); @@ -847,12 +850,13 @@ describe( 'effects', () => { handler( convertBlockToStatic( staticBlock.uid ), store ); - expect( dispatch ).toHaveBeenCalledWith( - replaceBlocks( + expect( dispatch ).toHaveBeenCalledWith( { + ...replaceBlocks( [ staticBlock.uid ], createBlock( reusableBlock.type, reusableBlock.attributes ) - ) - ); + ), + time: expect.any( Number ), + } ); } ); } );