-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose before filter hook in useSettings and the block metadata for C…
…SS classes generation (#45089)
- Loading branch information
1 parent
68ee410
commit 1c0d02f
Showing
2 changed files
with
112 additions
and
1 deletion.
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
99 changes: 99 additions & 0 deletions
99
packages/block-editor/src/components/use-setting/test/index.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,99 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter, removeFilter } from '@wordpress/hooks'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useSetting from '..'; | ||
import * as BlockEditContext from '../../block-edit/context'; | ||
|
||
// Mock useSelect() functions used by useSetting() | ||
jest.mock( '@wordpress/data/src/components/use-select' ); | ||
|
||
let selectMock = {}; | ||
const setupSelectMock = () => { | ||
selectMock = { | ||
getSettings: () => ( {} ), | ||
getBlockParents: () => [], | ||
getBlockName: () => '', | ||
}; | ||
}; | ||
|
||
useSelect.mockImplementation( ( callback ) => callback( () => selectMock ) ); | ||
|
||
const mockSettings = ( settings ) => { | ||
selectMock.getSettings = () => ( { | ||
__experimentalFeatures: settings, | ||
} ); | ||
}; | ||
|
||
const mockCurrentBlockContext = ( | ||
blockContext = { name: '', isSelected: false } | ||
) => { | ||
jest.spyOn( BlockEditContext, 'useBlockEditContext' ).mockReturnValue( | ||
blockContext | ||
); | ||
}; | ||
|
||
describe( 'useSetting', () => { | ||
beforeEach( () => { | ||
setupSelectMock(); | ||
mockCurrentBlockContext(); | ||
} ); | ||
|
||
it( 'uses block setting', () => { | ||
mockSettings( { | ||
blocks: { | ||
'core/test-block': { | ||
layout: { | ||
contentSize: '840px', | ||
}, | ||
}, | ||
}, | ||
} ); | ||
|
||
mockCurrentBlockContext( { | ||
name: 'core/test-block', | ||
} ); | ||
|
||
expect( useSetting( 'layout.contentSize' ) ).toBe( '840px' ); | ||
} ); | ||
|
||
it( 'uses blockEditor.useSetting.before hook override', () => { | ||
mockSettings( { | ||
blocks: { | ||
'core/test-block': { | ||
layout: { | ||
contentSize: '840px', | ||
}, | ||
}, | ||
}, | ||
} ); | ||
|
||
mockCurrentBlockContext( { | ||
name: 'core/test-block', | ||
} ); | ||
|
||
addFilter( | ||
'blockEditor.useSetting.before', | ||
'test/useSetting.before', | ||
( result, path, clientId, blockName ) => { | ||
if ( blockName === 'core/test-block' ) { | ||
return '960px'; | ||
} | ||
|
||
return result; | ||
} | ||
); | ||
|
||
expect( useSetting( 'layout.contentSize' ) ).toBe( '960px' ); | ||
|
||
removeFilter( | ||
'blockEditor.useSetting.before', | ||
'test/useSetting.before' | ||
); | ||
} ); | ||
} ); |