Skip to content

Commit

Permalink
Fix unit tests for useSetting, run hooks inside React render
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Oct 13, 2023
1 parent 7afc068 commit 21134f9
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions packages/block-editor/src/components/use-setting/test/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/**
* External dependencies
*/
import { render } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { addFilter, removeFilter } from '@wordpress/hooks';
import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -38,6 +44,18 @@ const mockCurrentBlockContext = (
);
};

function runHook( hookCb ) {
let storedResult;
function TestHook() {
const result = hookCb();
useEffect( () => {
storedResult = result;
}, [ result ] );
}
render( <TestHook /> );
return storedResult;
}

describe( 'useSetting', () => {
beforeEach( () => {
setupSelectMock();
Expand All @@ -59,7 +77,8 @@ describe( 'useSetting', () => {
name: 'core/test-block',
} );

expect( useSetting( 'layout.contentSize' ) ).toBe( '840px' );
const result = runHook( () => useSetting( 'layout.contentSize' ) );
expect( result ).toBe( '840px' );
} );

it( 'uses blockEditor.useSetting.before hook override', () => {
Expand Down Expand Up @@ -89,7 +108,8 @@ describe( 'useSetting', () => {
}
);

expect( useSetting( 'layout.contentSize' ) ).toBe( '960px' );
const result = runHook( () => useSetting( 'layout.contentSize' ) );
expect( result ).toBe( '960px' );

removeFilter(
'blockEditor.useSetting.before',
Expand Down

0 comments on commit 21134f9

Please sign in to comment.