-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract hook for offcanvas inserted block (#46618)
* Extract hook * Add tests * Use the hook
- Loading branch information
Showing
3 changed files
with
163 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/block-editor/src/components/off-canvas-editor/test/use-inserted-block.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useInsertedBlock } from '../use-inserted-block'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { act, renderHook } from '@testing-library/react'; | ||
|
||
jest.mock( '@wordpress/data/src/components/use-select', () => { | ||
// This allows us to tweak the returned value on each test. | ||
const mock = jest.fn(); | ||
return mock; | ||
} ); | ||
|
||
jest.mock( '@wordpress/data/src/components/use-dispatch', () => ( { | ||
useDispatch: jest.fn(), | ||
} ) ); | ||
|
||
describe( 'useInsertedBlock', () => { | ||
const mockUpdateBlockAttributes = jest.fn(); | ||
|
||
it( 'returns undefined values when called without a block clientId', () => { | ||
useSelect.mockImplementation( () => ( { | ||
insertedBlockAttributes: { | ||
'some-attribute': 'some-value', | ||
}, | ||
insertedBlockName: 'core/navigation-link', | ||
} ) ); | ||
|
||
useDispatch.mockImplementation( () => ( { | ||
updateBlockAttributes: mockUpdateBlockAttributes, | ||
} ) ); | ||
|
||
const { result } = renderHook( () => useInsertedBlock() ); | ||
|
||
const { | ||
insertedBlockName, | ||
insertedBlockAttributes, | ||
setInsertedBlockAttributes, | ||
} = result.current; | ||
|
||
expect( insertedBlockName ).toBeUndefined(); | ||
expect( insertedBlockAttributes ).toBeUndefined(); | ||
expect( | ||
setInsertedBlockAttributes( { 'some-attribute': 'new-value' } ) | ||
).toBeUndefined(); | ||
} ); | ||
|
||
it( 'returns name and attributes when called with a block clientId', () => { | ||
useSelect.mockImplementation( () => ( { | ||
insertedBlockAttributes: { | ||
'some-attribute': 'some-value', | ||
}, | ||
insertedBlockName: 'core/navigation-link', | ||
} ) ); | ||
|
||
useDispatch.mockImplementation( () => ( { | ||
updateBlockAttributes: mockUpdateBlockAttributes, | ||
} ) ); | ||
|
||
const { result } = renderHook( () => | ||
useInsertedBlock( 'some-client-id-here' ) | ||
); | ||
|
||
const { insertedBlockName, insertedBlockAttributes } = result.current; | ||
|
||
expect( insertedBlockName ).toBe( 'core/navigation-link' ); | ||
expect( insertedBlockAttributes ).toEqual( { | ||
'some-attribute': 'some-value', | ||
} ); | ||
} ); | ||
|
||
it( 'dispatches updateBlockAttributes on provided client ID with new attributes when setInsertedBlockAttributes is called', () => { | ||
useSelect.mockImplementation( () => ( { | ||
insertedBlockAttributes: { | ||
'some-attribute': 'some-value', | ||
}, | ||
insertedBlockName: 'core/navigation-link', | ||
} ) ); | ||
|
||
useDispatch.mockImplementation( () => ( { | ||
updateBlockAttributes: mockUpdateBlockAttributes, | ||
} ) ); | ||
|
||
const clientId = '123456789'; | ||
|
||
const { result } = renderHook( () => useInsertedBlock( clientId ) ); | ||
|
||
const { setInsertedBlockAttributes } = result.current; | ||
|
||
act( () => { | ||
setInsertedBlockAttributes( { | ||
'some-attribute': 'new-value', | ||
} ); | ||
} ); | ||
|
||
expect( mockUpdateBlockAttributes ).toHaveBeenCalledWith( clientId, { | ||
'some-attribute': 'new-value', | ||
} ); | ||
} ); | ||
} ); |
47 changes: 47 additions & 0 deletions
47
packages/block-editor/src/components/off-canvas-editor/use-inserted-block.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
export const useInsertedBlock = ( insertedBlockClientId ) => { | ||
const { insertedBlockAttributes, insertedBlockName } = useSelect( | ||
( select ) => { | ||
const { getBlockName, getBlockAttributes } = | ||
select( blockEditorStore ); | ||
|
||
return { | ||
insertedBlockAttributes: getBlockAttributes( | ||
insertedBlockClientId | ||
), | ||
insertedBlockName: getBlockName( insertedBlockClientId ), | ||
}; | ||
}, | ||
[ insertedBlockClientId ] | ||
); | ||
|
||
const { updateBlockAttributes } = useDispatch( blockEditorStore ); | ||
|
||
const setInsertedBlockAttributes = ( _updatedAttributes ) => { | ||
if ( ! insertedBlockClientId ) return; | ||
updateBlockAttributes( insertedBlockClientId, _updatedAttributes ); | ||
}; | ||
|
||
if ( ! insertedBlockClientId ) { | ||
return { | ||
insertedBlockAttributes: undefined, | ||
insertedBlockName: undefined, | ||
setInsertedBlockAttributes, | ||
}; | ||
} | ||
|
||
return { | ||
insertedBlockAttributes, | ||
insertedBlockName, | ||
setInsertedBlockAttributes, | ||
}; | ||
}; |
69b2830
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3739491248
📝 Reported issues:
specs/local/demo.test.js
Browse all
#45332 inspecs/editor/various/inserting-blocks.test.js