Skip to content

Commit

Permalink
Add tests for hook
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Dec 15, 2022
1 parent 07c9b01 commit c132846
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
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',
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';

/**
* Internal dependencies
*/
Expand All @@ -26,9 +27,18 @@ export const useInsertedBlock = ( 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,
Expand Down

0 comments on commit c132846

Please sign in to comment.