-
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.
Fix
DisableNonPageContentBlocks
(#59297)
* Fix DisableNonPageContentBlocks - Updates DisableNonPageContentBlocks to only affect top level content. For example, a Featured Image within a Content block should remain as is. - Refactors DisableNonPageContentBlocks to use a selector so that we can write unit tests for the logic. * Use useSelect() approach instead of private selector Co-authored-by: noisysocks <noisysocks@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: fabiankaegy <fabiankaegy@git.wordpress.org> Co-authored-by: andrewserong <andrewserong@git.wordpress.org> Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org> Co-authored-by: ramonjd <ramonopoly@git.wordpress.org> Co-authored-by: kmanijak <karolmanijak@git.wordpress.org>
- Loading branch information
1 parent
33cd576
commit 1e22838
Showing
3 changed files
with
124 additions
and
41 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
90 changes: 90 additions & 0 deletions
90
packages/editor/src/components/provider/test/disable-non-page-content-blocks.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,90 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render } from '@testing-library/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createRegistry, RegistryProvider } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import DisableNonPageContentBlocks from '../disable-non-page-content-blocks'; | ||
|
||
describe( 'DisableNonPageContentBlocks', () => { | ||
it( 'disables page content blocks', () => { | ||
const testBlocks = { | ||
0: 'core/template-part', | ||
/**/ '00': 'core/site-title', | ||
/**/ '01': 'core/navigation', | ||
1: 'core/group', | ||
/**/ 10: 'core/post-title', | ||
/**/ 11: 'core/post-featured-image', | ||
/**/ 12: 'core/post-content', | ||
/**/ /**/ 120: 'core/paragraph', | ||
/**/ /**/ 121: 'core/post-featured-image', | ||
2: 'core/query', | ||
/**/ 20: 'core/post-title', | ||
/**/ 21: 'core/post-featured-image', | ||
/**/ 22: 'core/post-content', | ||
3: 'core/template-part', | ||
/**/ 30: 'core/paragraph', | ||
}; | ||
|
||
const setBlockEditingMode = jest.fn( () => ( { | ||
type: 'SET_BLOCK_EDITING_MODE', | ||
} ) ); | ||
const unsetBlockEditingMode = jest.fn( () => ( { | ||
type: 'UNSET_BLOCK_EDITING_MODE', | ||
} ) ); | ||
|
||
const registry = createRegistry( { | ||
'core/block-editor': { | ||
reducer: () => {}, | ||
selectors: { | ||
getBlocksByName( state, blockNames ) { | ||
return Object.keys( testBlocks ).filter( ( clientId ) => | ||
blockNames.includes( testBlocks[ clientId ] ) | ||
); | ||
}, | ||
getBlockParents( state, clientId ) { | ||
return clientId.slice( 0, -1 ).split( '' ); | ||
}, | ||
getBlockName( state, clientId ) { | ||
return testBlocks[ clientId ]; | ||
}, | ||
}, | ||
actions: { | ||
setBlockEditingMode, | ||
unsetBlockEditingMode, | ||
}, | ||
}, | ||
} ); | ||
|
||
const { unmount } = render( | ||
<RegistryProvider value={ registry }> | ||
<DisableNonPageContentBlocks /> | ||
</RegistryProvider> | ||
); | ||
|
||
expect( setBlockEditingMode.mock.calls ).toEqual( [ | ||
[ '', 'disabled' ], // root | ||
[ '10', 'contentOnly' ], // post-title | ||
[ '11', 'contentOnly' ], // post-featured-image | ||
[ '12', 'contentOnly' ], // post-content | ||
// NOT the post-featured-image nested within post-content | ||
// NOT any of the content blocks within query | ||
] ); | ||
|
||
unmount(); | ||
|
||
expect( unsetBlockEditingMode.mock.calls ).toEqual( [ | ||
[ '' ], // root | ||
[ '10' ], // post-title | ||
[ '11' ], // post-featured-image | ||
[ '12' ], // post-content | ||
] ); | ||
} ); | ||
} ); |