-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix DisableNonPageContentBlocks
#59297
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
] ); | ||
} ); | ||
} ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a question for my own understanding: it looks like this is safely not creating a new reference too frequently (i.e. it's used in the
useEffect
's dependencies and doesn't cause any issues). I'm used to the idea of trying to avoid.filter()
calls within auseSelect
because we then wind up with a new reference. In this case, is the reason that it's safe thatcontentIds
is the value returned by the callback here, rather than destructuring a value within an object?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, this is a "hack";
mapSelect
returns an array ofclientId
strings, which can correctly pass shallow equality check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, gotcha. Thanks for confirming!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, I didn't know it was a "hack" 😀 I just prefer not returning multiple things from
useSelect
where possible as I think it's easier to read.There's a warning if you do it wrong so I just let the computer do the thinking about this stuff.
gutenberg/packages/data/src/components/use-select/index.js
Lines 163 to 165 in 8f61b9b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean “hack” because it’s not obvious why it’s okay to use
filter
in current situation if you’re not familiar withuseSelect
internals.