From ca02280501e72ade5e22e69f822e1ef1ce45a040 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Mon, 22 Mar 2021 14:49:14 -0700 Subject: [PATCH] Navigation: move block filter so code will not be shaken out in final build, add some basic tests --- .../src/navigation-link/hooks.js | 9 +- .../src/navigation-link/index.js | 10 +- .../test/__snapshots__/hooks.js.snap | 184 ++++++++++++++++++ .../src/navigation-link/test/hooks.js | 84 ++++++++ 4 files changed, 278 insertions(+), 9 deletions(-) create mode 100644 packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap create mode 100644 packages/block-library/src/navigation-link/test/hooks.js diff --git a/packages/block-library/src/navigation-link/hooks.js b/packages/block-library/src/navigation-link/hooks.js index 410bf8541369cc..7fb89cafe414dd 100644 --- a/packages/block-library/src/navigation-link/hooks.js +++ b/packages/block-library/src/navigation-link/hooks.js @@ -1,7 +1,6 @@ /** * WordPress dependencies */ -import { addFilter } from '@wordpress/hooks'; import { category, page, @@ -30,7 +29,7 @@ function getIcon( variationName ) { } } -function enhanceNavigationLinkVariations( settings, name ) { +export function enhanceNavigationLinkVariations( settings, name ) { if ( name !== 'core/navigation-link' ) { return settings; } @@ -67,9 +66,3 @@ function enhanceNavigationLinkVariations( settings, name ) { } return settings; } - -addFilter( - 'blocks.registerBlockType', - 'core/navigation-link', - enhanceNavigationLinkVariations -); diff --git a/packages/block-library/src/navigation-link/index.js b/packages/block-library/src/navigation-link/index.js index 95de1e963db52f..b183e069360f85 100644 --- a/packages/block-library/src/navigation-link/index.js +++ b/packages/block-library/src/navigation-link/index.js @@ -4,6 +4,7 @@ import { __, _x } from '@wordpress/i18n'; import { customLink as linkIcon } from '@wordpress/icons'; import { InnerBlocks } from '@wordpress/block-editor'; +import { addFilter } from '@wordpress/hooks'; /** * Internal dependencies @@ -11,7 +12,7 @@ import { InnerBlocks } from '@wordpress/block-editor'; import metadata from './block.json'; import edit from './edit'; import save from './save'; -import './hooks'; +import { enhanceNavigationLinkVariations } from './hooks'; const { name } = metadata; @@ -88,3 +89,10 @@ export const settings = { }, ], }; + +// ensure that we import and use this from hooks.js, so code is not shaken out in final build. +addFilter( + 'blocks.registerBlockType', + 'core/navigation-link', + enhanceNavigationLinkVariations +); diff --git a/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap b/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap new file mode 100644 index 00000000000000..26422f280fc0fb --- /dev/null +++ b/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap @@ -0,0 +1,184 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`hooks enhanceNavigationLinkVariations adds fallback variations when variations are missing 1`] = ` +Object { + "name": "core/navigation-link", + "one": "one", + "three": "three", + "two": "two", + "variations": Array [ + Object { + "attributes": Object {}, + "description": "A link to a URL.", + "isActive": [Function], + "isDefault": true, + "name": "link", + "title": "Link", + }, + Object { + "attributes": Object { + "type": "post", + }, + "description": "A link to a post.", + "icon": + + , + "isActive": [Function], + "name": "post", + "title": "Post Link", + }, + Object { + "attributes": Object { + "type": "page", + }, + "description": "A link to a page.", + "icon": + + , + "isActive": [Function], + "name": "page", + "title": "Page Link", + }, + Object { + "attributes": Object { + "type": "category", + }, + "description": "A link to a category.", + "icon": + + , + "isActive": [Function], + "name": "category", + "title": "Category Link", + }, + Object { + "attributes": Object { + "type": "tag", + }, + "description": "A link to a tag.", + "icon": + + , + "isActive": [Function], + "name": "tag", + "title": "Tag Link", + }, + ], +} +`; + +exports[`hooks enhanceNavigationLinkVariations enhances variations with icon and isActive functions 1`] = ` +Object { + "extraProp": "extraProp", + "name": "core/navigation-link", + "variations": Array [ + Object { + "attributes": Object {}, + "description": "A link to a URL.", + "icon": + + , + "isActive": [Function], + "name": "link", + "title": "Link", + }, + Object { + "attributes": Object { + "type": "post", + }, + "description": "A link to a post.", + "icon": + + , + "isActive": [Function], + "name": "post", + "title": "Post Link", + }, + Object { + "attributes": Object { + "type": "page", + }, + "description": "A link to a page.", + "icon": + + , + "isActive": [Function], + "name": "page", + "title": "Page Link", + }, + Object { + "attributes": Object { + "type": "category", + }, + "description": "A link to a category.", + "icon": + + , + "isActive": [Function], + "name": "category", + "title": "Category Link", + }, + Object { + "attributes": Object { + "type": "tag", + }, + "description": "A link to a tag.", + "icon": + + , + "isActive": [Function], + "name": "tag", + "title": "Tag Link", + }, + ], +} +`; diff --git a/packages/block-library/src/navigation-link/test/hooks.js b/packages/block-library/src/navigation-link/test/hooks.js new file mode 100644 index 00000000000000..16c4aad1fba5b5 --- /dev/null +++ b/packages/block-library/src/navigation-link/test/hooks.js @@ -0,0 +1,84 @@ +/** + * Internal dependencies + */ +import { enhanceNavigationLinkVariations } from '../hooks'; +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +describe( 'hooks', () => { + describe( 'enhanceNavigationLinkVariations', () => { + it( 'does not modify settings when settings do not belong to a navigation link', () => { + const updatedSettings = enhanceNavigationLinkVariations( + { + name: 'core/test', + one: 'one', + two: 'two', + three: 'three', + }, + 'core/test' + ); + expect( updatedSettings ).toEqual( { + name: 'core/test', + one: 'one', + two: 'two', + three: 'three', + } ); + } ); + it( 'adds fallback variations when variations are missing', () => { + const updatedSettings = enhanceNavigationLinkVariations( + { + name: 'core/navigation-link', + one: 'one', + two: 'two', + three: 'three', + }, + 'core/navigation-link' + ); + expect( updatedSettings ).toMatchSnapshot(); + } ); + it( 'enhances variations with icon and isActive functions', () => { + const updatedSettings = enhanceNavigationLinkVariations( + { + name: 'core/navigation-link', + extraProp: 'extraProp', + variations: [ + { + name: 'link', + title: __( 'Link' ), + description: __( 'A link to a URL.' ), + attributes: {}, + }, + { + name: 'post', + title: __( 'Post Link' ), + description: __( 'A link to a post.' ), + attributes: { type: 'post' }, + }, + { + name: 'page', + title: __( 'Page Link' ), + description: __( 'A link to a page.' ), + attributes: { type: 'page' }, + }, + { + name: 'category', + title: __( 'Category Link' ), + description: __( 'A link to a category.' ), + attributes: { type: 'category' }, + }, + { + name: 'tag', + title: __( 'Tag Link' ), + description: __( 'A link to a tag.' ), + attributes: { type: 'tag' }, + }, + ], + }, + 'core/navigation-link' + ); + expect( updatedSettings ).toMatchSnapshot(); + } ); + } ); +} );