From e4f1c1ba43c7ef42f690fce137ae1c7cf0a1a83d Mon Sep 17 00:00:00 2001 From: Robert Anderson <robert@noisysocks.com> Date: Tue, 6 Mar 2018 14:25:50 +1100 Subject: [PATCH] Make preferences reducer deterministic Remove Date.now() from the preferences reducer, making it a pure function. --- editor/store/actions.js | 2 ++ editor/store/reducer.js | 2 +- editor/store/test/actions.js | 10 +++++-- editor/store/test/effects.js | 55 +++++++++++++++++++++--------------- editor/store/test/reducer.js | 10 ++++--- 5 files changed, 49 insertions(+), 30 deletions(-) diff --git a/editor/store/actions.js b/editor/store/actions.js index aaf1b3646c5c1..ed60d1bfb515a 100644 --- a/editor/store/actions.js +++ b/editor/store/actions.js @@ -167,6 +167,7 @@ export function replaceBlocks( uids, blocks ) { type: 'REPLACE_BLOCKS', uids: castArray( uids ), blocks: castArray( blocks ), + time: Date.now(), }; } @@ -213,6 +214,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 6d9a0be3eba6d..900275cafc5a7 100644 --- a/editor/store/reducer.js +++ b/editor/store/reducer.js @@ -661,7 +661,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 afa245777567d..6162698b61528 100644 --- a/editor/store/test/effects.js +++ b/editor/store/test/effects.js @@ -21,7 +21,6 @@ import { setupEditorState, resetBlocks, mergeBlocks, - replaceBlocks, savePost, updateReusableBlock, saveReusableBlock, @@ -106,11 +105,16 @@ 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( { + type: 'REPLACE_BLOCKS', + uids: [ 'chicken', 'ribs' ], + blocks: [ { + 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', () => { @@ -201,11 +205,16 @@ 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( { + type: 'REPLACE_BLOCKS', + uids: [ 'chicken', 'ribs' ], + blocks: [ { + uid: 'chicken', + name: 'core/test-block', + attributes: { content: 'chicken ribs' }, + } ], + time: expect.any( Number ), + } ); } ); } ); @@ -743,12 +752,12 @@ describe( 'effects', () => { handler( convertBlockToStatic( staticBlock.uid ), store ); - expect( dispatch ).toHaveBeenCalledWith( - replaceBlocks( - [ staticBlock.uid ], - createBlock( reusableBlock.type, reusableBlock.attributes ) - ) - ); + expect( dispatch ).toHaveBeenCalledWith( { + type: 'REPLACE_BLOCKS', + uids: [ staticBlock.uid ], + blocks: [ createBlock( reusableBlock.type, reusableBlock.attributes ) ], + time: expect.any( Number ), + } ); } ); } ); @@ -780,12 +789,12 @@ describe( 'effects', () => { expect( dispatch ).toHaveBeenCalledWith( saveReusableBlock( expect.any( Number ) ) ); - expect( dispatch ).toHaveBeenCalledWith( - replaceBlocks( - [ staticBlock.uid ], - [ createBlock( 'core/block', { ref: expect.any( Number ) } ) ] - ) - ); + expect( dispatch ).toHaveBeenCalledWith( { + type: 'REPLACE_BLOCKS', + uids: [ staticBlock.uid ], + blocks: [ 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 c9e1523ca56dc..61a359163c454 100644 --- a/editor/store/test/reducer.js +++ b/editor/store/test/reducer.js @@ -1207,12 +1207,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' }, }, @@ -1222,7 +1223,7 @@ describe( 'state', () => { const twoRecentBlocks = preferences( deepFreeze( { insertUsage: { 'core-embed/twitter': { - time: expect.any( Number ), + time: 123456, count: 1, insert: { name: 'core-embed/twitter' }, }, @@ -1237,17 +1238,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 }, },