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 navigation editor error #28190

Merged
merged 5 commits into from
Jan 18, 2021
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Navigation editor allows creation of a menu 1`] = `
"<!-- wp:navigation -->
<!-- wp:navigation-link {\\"label\\":\\"Home\\",\\"type\\":\\"page\\",\\"id\\":1,\\"url\\":\\"https://this/is/a/test/page/home\\"} /-->
<!-- wp:navigation-link {\\"label\\":\\"About\\",\\"type\\":\\"page\\",\\"id\\":2,\\"url\\":\\"https://this/is/a/test/page/about\\"} /-->
<!-- wp:navigation-link {\\"label\\":\\"Contact\\",\\"type\\":\\"page\\",\\"id\\":3,\\"url\\":\\"https://this/is/a/test/page/contact\\"} /-->
<!-- /wp:navigation -->"
`;

exports[`Navigation editor displays the first menu from the REST response when at least one menu exists 1`] = `
"<!-- wp:navigation -->
<!-- wp:navigation-link {\\"label\\":\\"Home\\",\\"description\\":\\"\\",\\"rel\\":\\"\\",\\"url\\":\\"http://localhost:8889/\\",\\"title\\":\\"\\",\\"className\\":\\"\\"} /-->
158 changes: 125 additions & 33 deletions packages/e2e-tests/specs/experiments/navigation-editor.test.js
Original file line number Diff line number Diff line change
@@ -29,6 +29,21 @@ const menusFixture = [
},
];

const pagesFixture = [
{
title: 'Home',
slug: 'home',
},
{
title: 'About',
slug: 'about',
},
{
title: 'Contact',
slug: 'contact',
},
];

// Matching against variations of the same URL encoded and non-encoded
// produces the most reliable mocking.
const REST_MENUS_ROUTES = [
@@ -39,6 +54,10 @@ const REST_MENU_ITEMS_ROUTES = [
'/__experimental/menu-items',
`rest_route=${ encodeURIComponent( '/__experimental/menu-items' ) }`,
];
const REST_PAGES_ROUTES = [
'/wp/v2/pages',
`rest_route=${ encodeURIComponent( '/wp/v2/pages' ) }`,
];

/**
* Determines if a given URL matches any of a given collection of
@@ -51,38 +70,63 @@ function matchUrlToRoute( reqUrl, routes ) {
return routes.some( ( route ) => reqUrl.includes( route ) );
}

/**
* Creates mocked REST API responses for calls to menus and menu-items
* endpoints.
* Note: this needs to be within a single call to
* `setUpResponseMocking` as you can only setup response mocking once per test run.
*
* @param {Array} menus menus to provide as mocked responses to menus entity API requests.
* @param {Array} menuItems menu items to provide as mocked responses to menu-items entity API requests.
*/
async function mockAllMenusResponses(
menus = menusFixture,
menuItems = menuItemsFixture
function getEndpointMocks(
matchingRoutes,
responsesByMethod,
processor = ( data ) => data
) {
const mappedMenus = menus.length
? menus.map( ( menu, index ) => ( {
...menu,
id: index + 1,
} ) )
: [];

await setUpResponseMocking( [
{
match: ( request ) =>
matchUrlToRoute( request.url(), REST_MENUS_ROUTES ),
onRequestMatch: createJSONResponse( mappedMenus ),
},
{
match: ( request ) =>
matchUrlToRoute( request.url(), REST_MENU_ITEMS_ROUTES ),
onRequestMatch: createJSONResponse( menuItems ),
},
] );
return [ 'GET', 'POST', 'DELETE', 'PUT' ].reduce( ( mocks, restMethod ) => {
if ( responsesByMethod[ restMethod ] ) {
return [
...mocks,
{
match: ( request ) =>
matchUrlToRoute( request.url(), matchingRoutes ) &&
request.method() === restMethod,
onRequestMatch: createJSONResponse(
processor( responsesByMethod[ restMethod ] )
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the test is mostly working, this processor shouldn't run for every method, a POST to create a menu only returns a single item so doesn't need to be mapped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably best to get rid of this concept and process the data before it's passed into the mocking function.

),
},
];
}

return mocks;
}, [] );
}

function getMenuMocks( responsesByMethod ) {
const assignMenuIds = ( menus ) =>
menus.length
? menus.map( ( menu, index ) => ( {
...menu,
id: index + 1,
} ) )
: [];

return getEndpointMocks(
REST_MENUS_ROUTES,
responsesByMethod,
assignMenuIds
);
}

function getMenuItemMocks( responsesByMethod ) {
return getEndpointMocks( REST_MENU_ITEMS_ROUTES, responsesByMethod );
}

function getPagesMocks( responsesByMethod ) {
const buildPages = ( pages ) =>
pages.map( ( { title, slug }, index ) => ( {
id: index + 1,
type: 'page',
link: `https://this/is/a/test/page/${ slug }`,
title: {
rendered: title,
raw: title,
},
} ) );

return getEndpointMocks( REST_PAGES_ROUTES, responsesByMethod, buildPages );
}

async function visitNavigationEditor() {
@@ -100,13 +144,61 @@ async function getSerializedBlocks() {

describe( 'Navigation editor', () => {
useExperimentalFeatures( [ '#gutenberg-navigation' ] );

afterEach( async () => {
await setUpResponseMocking( [] );
} );

it( 'allows creation of a menu', async () => {
await setUpResponseMocking( [
...getMenuMocks( {
GET: [],
POST: {
id: 4,
description: '',
name: 'Main Menu',
slug: 'main-menu',
meta: [],
auto_add: false,
},
} ),
...getMenuItemMocks( { GET: [] } ),
...getPagesMocks( { GET: pagesFixture } ),
] );
await visitNavigationEditor();

// Wait for the header to show that no menus are available.
await page.waitForXPath( '//h2[contains(., "No menus available")]' );

// Add a new menu.
const [ addNewButton ] = await page.$x(
'//button[contains(., "Add new")]'
);
await addNewButton.click();
await page.keyboard.type( 'Main Menu' );
const [ createMenuButton ] = await page.$x(
'//button[contains(., "Create menu")]'
);
await createMenuButton.click();

// Close the dropdown.
await page.keyboard.press( 'Escape' );

// Select the navigation block and create a block from existing pages.
await page.click( 'div[aria-label="Block: Navigation"]' );

const [ addAllPagesButton ] = await page.$x(
'//button[contains(., "Add all pages")]'
);
await addAllPagesButton.click();

expect( await getSerializedBlocks() ).toMatchSnapshot();
} );

it( 'displays the first menu from the REST response when at least one menu exists', async () => {
await mockAllMenusResponses();
await setUpResponseMocking( [
...getMenuMocks( { GET: menusFixture } ),
...getMenuItemMocks( { GET: menuItemsFixture } ),
] );
await visitNavigationEditor();

// Wait for the header to show the menu name.
Original file line number Diff line number Diff line change
@@ -21,11 +21,11 @@ export default function useNavigationBlockEditor( post ) {
);

const onChange = useCallback(
async ( updatedBlocks ) => {
await _onChange( updatedBlocks );
async ( ...args ) => {
await _onChange( ...args );
createMissingMenuItems( post );
},
[ blocks, _onChange ]
[ _onChange, post ]
);

return [ blocks, onInput, onChange ];