From d8f5fbcde12489ea1db3e83f3f7c742308ed32a8 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 4 Aug 2023 21:51:11 +1000 Subject: [PATCH 1/2] Footnotes: Add some test coverage for footnotes logic in useEntityBlockEditor --- packages/core-data/src/entity-provider.js | 4 +- .../core-data/src/test/entity-provider.js | 272 ++++++++++++++++++ 2 files changed, 274 insertions(+), 2 deletions(-) create mode 100644 packages/core-data/src/test/entity-provider.js diff --git a/packages/core-data/src/entity-provider.js b/packages/core-data/src/entity-provider.js index cf15c423456a29..d32b3853627b32 100644 --- a/packages/core-data/src/entity-provider.js +++ b/packages/core-data/src/entity-provider.js @@ -142,7 +142,7 @@ export function useEntityProp( kind, name, prop, _id ) { * The return value has the shape `[ blocks, onInput, onChange ]`. * `onInput` is for block changes that don't create undo levels * or dirty the post, non-persistent changes, and `onChange` is for - * peristent changes. They map directly to the props of a + * persistent changes. They map directly to the props of a * `BlockEditorProvider` and are intended to be used with it, * or similar components or hooks. * @@ -290,7 +290,7 @@ export function useEntityBlockEditor( kind, name, { id: _id } = {} ) { } ); } - // We need to go through all block attributs deeply and update the + // We need to go through all block attributes deeply and update the // footnote anchor numbering (textContent) to match the new order. const newBlocks = updateBlocksAttributes( _blocks ); diff --git a/packages/core-data/src/test/entity-provider.js b/packages/core-data/src/test/entity-provider.js new file mode 100644 index 00000000000000..e59aec5798541a --- /dev/null +++ b/packages/core-data/src/test/entity-provider.js @@ -0,0 +1,272 @@ +/** + * External dependencies + */ +import { act, render } from '@testing-library/react'; + +/** + * WordPress dependencies + */ +import { + createBlock, + registerBlockType, + unregisterBlockType, + getBlockTypes, +} from '@wordpress/blocks'; +import { RichText, useBlockProps } from '@wordpress/block-editor'; +import { createRegistry, RegistryProvider } from '@wordpress/data'; + +/** + * Internal dependencies + */ +import { store as coreDataStore } from '../index'; +import { useEntityBlockEditor } from '../entity-provider'; + +const postTypeConfig = { + kind: 'postType', + name: 'post', + baseURL: '/wp/v2/posts', + transientEdits: { blocks: true, selection: true }, + mergedEdits: { meta: true }, + rawAttributes: [ 'title', 'excerpt', 'content' ], +}; + +const postTypeEntity = { + slug: 'post', + rest_base: 'posts', + labels: { + item_updated: 'Updated Post', + item_published: 'Post published', + item_reverted_to_draft: 'Post reverted to draft.', + }, +}; + +const aSinglePost = { + id: 1, + type: 'post', + content: { + raw: '
apples
oranges
A paragraph
', + rendered: 'A paragraph
', + }, + meta: { + footnotes: '[]', + }, +}; + +function createRegistryWithStores() { + // Create a registry. + const registry = createRegistry(); + + // Register store. + registry.register( coreDataStore ); + + // Register post type entity. + registry.dispatch( coreDataStore ).addEntities( [ postTypeConfig ] ); + + // Store post type entity. + registry + .dispatch( coreDataStore ) + .receiveEntityRecords( 'root', 'postType', [ postTypeEntity ] ); + + // Store a single post for use by the tests. + registry + .dispatch( coreDataStore ) + .receiveEntityRecords( 'postType', 'post', [ aSinglePost ] ); + + return registry; +} + +describe( 'useEntityBlockEditor', () => { + let registry; + + beforeEach( () => { + registry = createRegistryWithStores(); + + const edit = ( { children } ) => <>{ children }>; + + registerBlockType( 'core/test-block', { + supports: { + className: false, + }, + save: ( { attributes } ) => { + const { content } = attributes; + return ( +
+
{ item }
+ ) ) } +A paragraph1
', + rendered: 'A paragraph
', + }, + meta: { + footnotes: '[]', + }, + }, + ] ); + + let blocks, onChange; + + const TestComponent = () => { + [ blocks, , onChange ] = useEntityBlockEditor( 'postType', 'post', { + id: 1, + } ); + + return ; + }; + + render( +