Skip to content

Commit

Permalink
Merge pull request #5518 from WordPress/fix/make-reducer-and-tests-de…
Browse files Browse the repository at this point in the history
…terministic

Make preferences reducer deterministic (again)
  • Loading branch information
noisysocks authored Mar 9, 2018
2 parents 2ee3351 + c49e4bf commit 26938b1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
2 changes: 2 additions & 0 deletions editor/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export function replaceBlocks( uids, blocks ) {
type: 'REPLACE_BLOCKS',
uids: castArray( uids ),
blocks: castArray( blocks ),
time: Date.now(),
};
}

Expand Down Expand Up @@ -221,6 +222,7 @@ export function insertBlocks( blocks, index, rootUID ) {
blocks: castArray( blocks ),
index,
rootUID,
time: Date.now(),
};
}

Expand Down
2 changes: 1 addition & 1 deletion editor/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
10 changes: 8 additions & 2 deletions editor/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ describe( 'actions', () => {
type: 'REPLACE_BLOCKS',
uids: [ 'chicken' ],
blocks: [ block ],
time: expect.any( Number ),
} );
} );
} );
Expand All @@ -177,6 +178,7 @@ describe( 'actions', () => {
type: 'REPLACE_BLOCKS',
uids: [ 'chicken' ],
blocks,
time: expect.any( Number ),
} );
} );
} );
Expand All @@ -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 ),
} );
} );
} );
Expand All @@ -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 ),
} );
} );
} );
Expand Down
44 changes: 26 additions & 18 deletions editor/store/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -246,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 ),
} );
} );
} );

Expand Down Expand Up @@ -844,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 ),
} );
} );
} );

Expand Down Expand Up @@ -881,12 +888,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 ),
} );
} );
} );
} );
Expand Down
10 changes: 6 additions & 4 deletions editor/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
},
Expand All @@ -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' },
},
Expand All @@ -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 },
},
Expand Down

0 comments on commit 26938b1

Please sign in to comment.