Skip to content
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 Nav block editing wrong entity on creation of new Menu #36880

Merged
merged 6 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,24 @@ function Navigation( {
setIsPlaceholderShown( ! isEntityAvailable );
}, [ isEntityAvailable ] );

// If the ref no longer exists the reset the inner blocks
// to provide a clean slate.
useEffect( () => {
if ( ref === undefined && innerBlocks.length > 0 ) {
replaceInnerBlocks( clientId, [] );
}
// innerBlocks are intentionally not listed as deps. This function is only concerned
// with the snapshot from the time when ref became undefined.
}, [ clientId, ref, innerBlocks ] );
getdave marked this conversation as resolved.
Show resolved Hide resolved

const startWithEmptyMenu = useCallback( () => {
replaceInnerBlocks( clientId, [] );
if ( navigationArea ) {
setAreaMenu( 0 );
}
setAttributes( {
ref: undefined,
} );

setIsPlaceholderShown( true );
}, [ clientId ] );

Expand Down Expand Up @@ -476,7 +486,6 @@ function Navigation( {
<NavigationMenuNameControl />
<NavigationMenuDeleteControl
onDelete={ () => {
replaceInnerBlocks( clientId, [] );
if ( navigationArea ) {
setAreaMenu( 0 );
}
Expand Down
88 changes: 88 additions & 0 deletions packages/e2e-tests/specs/experiments/blocks/navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,4 +784,92 @@ describe.skip( 'Navigation', () => {

expect( isScriptLoaded ).toBe( true );
} );

describe( 'Creating and restarting', () => {
getdave marked this conversation as resolved.
Show resolved Hide resolved
async function populateNavWithOneItem() {
// Add a Link block first.
await page.waitForSelector(
'.wp-block-navigation .block-list-appender'
);
await page.click( '.wp-block-navigation .block-list-appender' );
// Add a link to the Link block.
await updateActiveNavigationLink( {
url: 'https://wordpress.org',
label: 'WP',
type: 'url',
} );
}

async function resetNavBlockToInitialState() {
await page.waitForSelector( '[aria-label="Select Menu"]' );
await page.click( '[aria-label="Select Menu"]' );

await page.waitForXPath( '//span[text()="Create new menu"]' );
const newMenuButton = await page.$x(
'//span[text()="Create new menu"]'
);
newMenuButton[ 0 ].click();
}

it( 'only update a single entity currently linked with the block', async () => {
// Mock the response from the Pages endpoint. This is done so that the pages returned are always
// consistent and to test the feature more rigorously than the single default sample page.
await mockPagesResponse( [
{
title: 'Home',
slug: 'home',
},
{
title: 'About',
slug: 'about',
},
{
title: 'Contact Us',
slug: 'contact',
},
] );

// Add the navigation block.
await insertBlock( 'Navigation' );

// Create an empty nav block.
await createEmptyNavBlock();
await populateNavWithOneItem();

// Let's confirm that the menu entity was updated.
await page.waitForSelector(
'.editor-post-publish-panel__toggle:not([aria-disabled="true"])'
);
await page.click( '.editor-post-publish-panel__toggle' );

const NAV_ENTITY_SELECTOR =
'//div[@class="entities-saved-states__panel"]//label//strong[contains(text(), "Navigation")]';
await page.waitForXPath( NAV_ENTITY_SELECTOR );
expect( await page.$x( NAV_ENTITY_SELECTOR ) ).toHaveLength( 1 );

// Publish the post
await page.click( '.editor-entities-saved-states__save-button' );
await page.waitForSelector( '.editor-post-publish-button' );
await page.click( '.editor-post-publish-button' );

// A success notice should show up
await page.waitForSelector( '.components-snackbar' );

// Now try inserting another Link block via the quick inserter.
await page.focus( '.wp-block-navigation' );

await resetNavBlockToInitialState();
await createEmptyNavBlock();
await populateNavWithOneItem();

// Let's confirm that only the last menu entity was updated.
await page.waitForSelector(
'.editor-post-publish-button__button:not([aria-disabled="true"])'
);
await page.click( '.editor-post-publish-button__button' );

await page.waitForXPath( NAV_ENTITY_SELECTOR );
expect( await page.$x( NAV_ENTITY_SELECTOR ) ).toHaveLength( 1 );
} );
} );
} );