Skip to content

Commit

Permalink
Fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Dec 22, 2023
1 parent 9a914ce commit d1fbba3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,13 @@ describe( 'useBlockSync hook', () => {

expect( onInput ).toHaveBeenCalledWith(
[ { clientId: 'a', innerBlocks: [], attributes: { foo: 2 } } ],
{
expect.objectContaining( {
selection: {
selectionEnd: {},
selectionStart: {},
initialPosition: null,
},
}
} )
);
expect( onChange ).not.toHaveBeenCalled();
} );
Expand Down Expand Up @@ -303,13 +303,13 @@ describe( 'useBlockSync hook', () => {

expect( onChange ).toHaveBeenCalledWith(
[ { clientId: 'a', innerBlocks: [], attributes: { foo: 2 } } ],
{
expect.objectContaining( {
selection: {
selectionEnd: {},
selectionStart: {},
initialPosition: null,
},
}
} )
);
expect( onInput ).not.toHaveBeenCalled();
} );
Expand Down Expand Up @@ -406,13 +406,13 @@ describe( 'useBlockSync hook', () => {
attributes: { foo: 2 },
},
],
{
expect.objectContaining( {
selection: {
selectionEnd: {},
selectionStart: {},
initialPosition: null,
},
}
} )
);
expect( onInput ).not.toHaveBeenCalled();
} );
Expand Down Expand Up @@ -447,13 +447,16 @@ describe( 'useBlockSync hook', () => {
{ clientId: 'a', innerBlocks: [], attributes: { foo: 2 } },
];

expect( onChange1 ).toHaveBeenCalledWith( updatedBlocks1, {
selection: {
initialPosition: null,
selectionEnd: {},
selectionStart: {},
},
} );
expect( onChange1 ).toHaveBeenCalledWith(
updatedBlocks1,
expect.objectContaining( {
selection: {
initialPosition: null,
selectionEnd: {},
selectionStart: {},
},
} )
);

const newBlocks = [
{ clientId: 'b', innerBlocks: [], attributes: { foo: 1 } },
Expand Down Expand Up @@ -485,13 +488,13 @@ describe( 'useBlockSync hook', () => {
// The second callback should be called with the new change.
expect( onChange2 ).toHaveBeenCalledWith(
[ { clientId: 'b', innerBlocks: [], attributes: { foo: 3 } } ],
{
expect.objectContaining( {
selection: {
selectionEnd: {},
selectionStart: {},
initialPosition: null,
},
}
} )
);
} );

Expand Down Expand Up @@ -544,13 +547,13 @@ describe( 'useBlockSync hook', () => {
// Only the new callback should be called.
expect( onChange2 ).toHaveBeenCalledWith(
[ { clientId: 'b', innerBlocks: [], attributes: { foo: 3 } } ],
{
expect.objectContaining( {
selection: {
selectionEnd: {},
selectionStart: {},
initialPosition: null,
},
}
} )
);
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,18 @@ export default function useBlockSync( {
const updateParent = isPersistent
? onChangeRef.current
: onInputRef.current;
const undoIgnore = undoIgnoreBlocks.has( blocks );
if ( undoIgnore ) {
undoIgnoreBlocks.delete( blocks );
}
updateParent( blocks, {
selection: {
selectionStart: getSelectionStart(),
selectionEnd: getSelectionEnd(),
initialPosition:
getSelectedBlocksInitialCaretPosition(),
},
undoIgnore: undoIgnoreBlocks.has( blocks ),
undoIgnore,
} );
}
previousAreBlocksDifferent = areBlocksDifferent;
Expand Down

0 comments on commit d1fbba3

Please sign in to comment.