-
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.
Block Bindings: Accept client ID as parameter for `useBlockBindingsUt…
…ils` (#65818) * Pass `clientId` to `useBlockBindingsUtils` * Add unit tests for `useBlockBindingsUtils` * Use `resetBlocks` instead of `insertBlocks` in unit test Co-authored-by: SantosGuillamot <[email protected]> Co-authored-by: gziolo <[email protected]>
- Loading branch information
1 parent
c9bda99
commit cc70109
Showing
3 changed files
with
187 additions
and
6 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
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
174 changes: 174 additions & 0 deletions
174
packages/block-editor/src/utils/test/use-block-bindings-utils.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,174 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { renderHook } from '@testing-library/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
import { dispatch, select } from '@wordpress/data'; | ||
import { | ||
createBlock, | ||
getBlockTypes, | ||
unregisterBlockType, | ||
} from '@wordpress/blocks'; | ||
import { registerCoreBlocks } from '@wordpress/block-library'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useBlockBindingsUtils } from '../'; | ||
|
||
describe( 'useBlockBindingsUtils', () => { | ||
beforeAll( () => { | ||
// Register all core blocks | ||
registerCoreBlocks(); | ||
} ); | ||
|
||
let clientId; | ||
beforeEach( async () => { | ||
const block = createBlock( 'core/paragraph', { | ||
metadata: { | ||
name: 'Block name', | ||
bindings: { | ||
prop1: { | ||
source: 'core/post-meta', | ||
args: { | ||
key: 'initial_key', | ||
}, | ||
}, | ||
prop2: { | ||
source: 'core/post-meta', | ||
args: { | ||
key: 'initial_key', | ||
}, | ||
}, | ||
}, | ||
}, | ||
} ); | ||
await dispatch( blockEditorStore ).resetBlocks( [ block ] ); | ||
clientId = block.clientId; | ||
} ); | ||
|
||
afterAll( () => { | ||
// Remove blocks after all tests. | ||
dispatch( blockEditorStore ).resetBlocks( [] ); | ||
|
||
// Clean up registered blocks | ||
getBlockTypes().forEach( ( block ) => { | ||
unregisterBlockType( block.name ); | ||
} ); | ||
} ); | ||
|
||
it( 'should be possible to update just one connection', async () => { | ||
renderHook( () => { | ||
const { updateBlockBindings } = useBlockBindingsUtils( clientId ); | ||
updateBlockBindings( { | ||
prop1: { | ||
source: 'core/post-meta', | ||
args: { | ||
key: 'new_key', | ||
}, | ||
}, | ||
} ); | ||
} ); | ||
const { metadata } = | ||
await select( blockEditorStore ).getBlockAttributes( clientId ); | ||
expect( metadata ).toMatchObject( { | ||
// Other metadata properties shouldn't change. | ||
name: 'Block name', | ||
bindings: { | ||
prop1: { | ||
source: 'core/post-meta', | ||
args: { | ||
key: 'new_key', | ||
}, | ||
}, | ||
prop2: { | ||
source: 'core/post-meta', | ||
args: { | ||
key: 'initial_key', | ||
}, | ||
}, | ||
}, | ||
} ); | ||
} ); | ||
|
||
it( 'should be possible to update multiple connections at once', async () => { | ||
renderHook( () => { | ||
const { updateBlockBindings } = useBlockBindingsUtils( clientId ); | ||
updateBlockBindings( { | ||
prop1: { | ||
source: 'core/post-meta', | ||
args: { | ||
key: 'new_key', | ||
}, | ||
}, | ||
prop2: { | ||
source: 'core/post-meta', | ||
args: { | ||
key: 'new_key', | ||
}, | ||
}, | ||
} ); | ||
} ); | ||
const { metadata } = | ||
await select( blockEditorStore ).getBlockAttributes( clientId ); | ||
expect( metadata ).toMatchObject( { | ||
// Other metadata properties shouldn't change. | ||
name: 'Block name', | ||
bindings: { | ||
prop1: { | ||
source: 'core/post-meta', | ||
args: { | ||
key: 'new_key', | ||
}, | ||
}, | ||
prop2: { | ||
source: 'core/post-meta', | ||
args: { | ||
key: 'new_key', | ||
}, | ||
}, | ||
}, | ||
} ); | ||
} ); | ||
|
||
it( 'should be possible to remove connections', async () => { | ||
renderHook( () => { | ||
const { updateBlockBindings } = useBlockBindingsUtils( clientId ); | ||
updateBlockBindings( { | ||
prop2: undefined, | ||
} ); | ||
} ); | ||
const { metadata } = | ||
await select( blockEditorStore ).getBlockAttributes( clientId ); | ||
expect( metadata ).toMatchObject( { | ||
// Other metadata properties shouldn't change. | ||
name: 'Block name', | ||
bindings: { | ||
prop1: { | ||
source: 'core/post-meta', | ||
args: { | ||
key: 'initial_key', | ||
}, | ||
}, | ||
}, | ||
} ); | ||
} ); | ||
|
||
it( 'should be possible to remove all connections', async () => { | ||
renderHook( () => { | ||
const { removeAllBlockBindings } = | ||
useBlockBindingsUtils( clientId ); | ||
removeAllBlockBindings(); | ||
} ); | ||
const { metadata } = | ||
await select( blockEditorStore ).getBlockAttributes( clientId ); | ||
expect( metadata ).toMatchObject( { | ||
// Other metadata properties shouldn't change. | ||
name: 'Block name', | ||
} ); | ||
} ); | ||
} ); |