Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Comment thread
mikachan marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import type { ReactNode } from 'react';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { act, renderHook } from '@testing-library/react';
import {
createBlock,
registerBlockType,
unregisterBlockType,
} from '@wordpress/blocks';
import type { Block } from '@wordpress/blocks';
import { createRegistry, RegistryProvider } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useInnerBlocks } from '../use-inner-blocks';

type Registry = ReturnType< typeof createRegistry >;

const BLOCK_NAMES = [
'core/navigation',
'core/navigation-link',
'core/page-list',
];

function setup( innerBlocks: Block[] = [] ) {
const registry = createRegistry();
registry.register( blockEditorStore );

const navigationBlock = createBlock( 'core/navigation', {}, innerBlocks );
registry.dispatch( blockEditorStore ).resetBlocks( [ navigationBlock ] );

const { result } = renderHook(
() => useInnerBlocks( navigationBlock.clientId ),
{
wrapper: ( { children }: { children: ReactNode } ) => (
<RegistryProvider value={ registry }>
{ children }
</RegistryProvider>
),
}
);

return { registry, clientId: navigationBlock.clientId, result };
}

function controlInnerBlocks(
registry: Registry,
clientId: string,
blocks: Block[]
) {
const { setHasControlledInnerBlocks, replaceInnerBlocks } =
registry.dispatch( blockEditorStore );
act( () => {
registry.batch( () => {
setHasControlledInnerBlocks( clientId, true );
replaceInnerBlocks( clientId, blocks );
} );
} );
}

describe( 'useInnerBlocks', () => {
beforeAll( () => {
BLOCK_NAMES.forEach( ( name ) =>
registerBlockType( name, {
apiVersion: 3,
title: name,
category: 'design',
attributes: {},
save: () => null,
} )
);
} );

afterAll( () => {
BLOCK_NAMES.forEach( ( name ) => unregisterBlockType( name ) );
} );

it( 'returns no inner blocks for an empty Navigation block', () => {
const { result } = setup();

expect( result.current ).toMatchObject( {
innerBlocks: [],
hasUncontrolledInnerBlocks: false,
uncontrolledInnerBlocks: [],
controlledInnerBlocks: [],
} );
} );

it( 'returns uncontrolled inner blocks', () => {
const { result } = setup( [ createBlock( 'core/page-list' ) ] );

expect( result.current ).toMatchObject( {
innerBlocks: [ { name: 'core/page-list' } ],
hasUncontrolledInnerBlocks: true,
uncontrolledInnerBlocks: [ { name: 'core/page-list' } ],
controlledInnerBlocks: [],
} );
} );

it( 'returns controlled inner blocks once uncontrolled ones are replaced', () => {
const { registry, clientId, result } = setup( [
createBlock( 'core/page-list' ),
] );

controlInnerBlocks( registry, clientId, [
createBlock( 'core/navigation-link' ),
] );

expect( result.current ).toMatchObject( {
innerBlocks: [ { name: 'core/navigation-link' } ],
hasUncontrolledInnerBlocks: false,
uncontrolledInnerBlocks: [],
controlledInnerBlocks: [ { name: 'core/navigation-link' } ],
} );
} );
} );
36 changes: 0 additions & 36 deletions packages/block-library/src/navigation/edit/use-inner-blocks.js

This file was deleted.

48 changes: 48 additions & 0 deletions packages/block-library/src/navigation/edit/use-inner-blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import type { Block } from '@wordpress/blocks';

const EMPTY_ARRAY: Block[] = [];

interface InnerBlocks {
innerBlocks: Block[];
hasUncontrolledInnerBlocks: boolean;
uncontrolledInnerBlocks: Block[];
controlledInnerBlocks: Block[];
isInnerBlockSelected: boolean;
}

export function useInnerBlocks( clientId: string ): InnerBlocks {
return useSelect(
( select ) => {
const {
areInnerBlocksControlled,
getBlocks,
hasSelectedInnerBlock,
} = select( blockEditorStore );

// Inner blocks become controlled once they are synced with a
// `wp_navigation` entity. Until then, any inner blocks belong to
// the Navigation block itself.
const _innerBlocks = getBlocks( clientId );
const _areInnerBlocksControlled =
areInnerBlocksControlled( clientId );

const _hasUncontrolledInnerBlocks =
! _areInnerBlocksControlled && !! _innerBlocks.length;

return {
innerBlocks: _innerBlocks,
hasUncontrolledInnerBlocks: _hasUncontrolledInnerBlocks,
uncontrolledInnerBlocks: _areInnerBlocksControlled
? EMPTY_ARRAY
: _innerBlocks,
controlledInnerBlocks: _areInnerBlocksControlled
? _innerBlocks
: EMPTY_ARRAY,
isInnerBlockSelected: hasSelectedInnerBlock( clientId, true ),
Comment thread
mikachan marked this conversation as resolved.
Resolved
};
},
[ clientId ]
);
}
Loading