From 79c3f3448118fd8ab5f953138df033b25edfd77d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=28Greg=29=20Zi=C3=B3=C5=82kowski?= Date: Thu, 14 Dec 2017 17:55:42 +0100 Subject: [PATCH] Extensibility: Apply filters without function wrappers (#3933) * Extensibility: Add blocks hooks in the files were they are defined * Tests: Rename generated class name test file --- blocks/hooks/anchor.js | 8 ++-- blocks/hooks/custom-class-name.js | 8 ++-- blocks/hooks/generated-class-name.js | 4 +- blocks/hooks/index.js | 13 ++----- blocks/hooks/matchers.js | 4 +- blocks/hooks/test/anchor.js | 39 +++++++------------ blocks/hooks/test/custom-class-name.js | 24 ++++-------- ...-class-name.js => generated-class-name.js} | 25 ++++-------- 8 files changed, 41 insertions(+), 84 deletions(-) rename blocks/hooks/test/{geenerated-class-name.js => generated-class-name.js} (69%) diff --git a/blocks/hooks/anchor.js b/blocks/hooks/anchor.js index 3b740b650c7875..161ab2fee14966 100644 --- a/blocks/hooks/anchor.js +++ b/blocks/hooks/anchor.js @@ -96,8 +96,6 @@ export function addSaveProps( extraProps, blockType, attributes ) { return extraProps; } -export default function anchor() { - addFilter( 'blocks.registerBlockType', 'core/anchor/attribute', addAttribute ); - addFilter( 'blocks.BlockEdit', 'core/anchor/inspector-control', withInspectorControl ); - addFilter( 'blocks.getSaveContent.extraProps', 'core/anchor/save-props', addSaveProps ); -} +addFilter( 'blocks.registerBlockType', 'core/anchor/attribute', addAttribute ); +addFilter( 'blocks.BlockEdit', 'core/anchor/inspector-control', withInspectorControl ); +addFilter( 'blocks.getSaveContent.extraProps', 'core/anchor/save-props', addSaveProps ); diff --git a/blocks/hooks/custom-class-name.js b/blocks/hooks/custom-class-name.js index d7c65b0c6a5cbc..3b9585a57388ff 100644 --- a/blocks/hooks/custom-class-name.js +++ b/blocks/hooks/custom-class-name.js @@ -86,8 +86,6 @@ export function addSaveProps( extraProps, blockType, attributes ) { return extraProps; } -export default function customClassName() { - addFilter( 'blocks.registerBlockType', 'core/custom-class-name/attribute', addAttribute ); - addFilter( 'blocks.BlockEdit', 'core/custom-class-name/inspector-control', withInspectorControl ); - addFilter( 'blocks.getSaveContent.extraProps', 'core/custom-class-name/save-props', addSaveProps ); -} +addFilter( 'blocks.registerBlockType', 'core/custom-class-name/attribute', addAttribute ); +addFilter( 'blocks.BlockEdit', 'core/custom-class-name/inspector-control', withInspectorControl ); +addFilter( 'blocks.getSaveContent.extraProps', 'core/custom-class-name/save-props', addSaveProps ); diff --git a/blocks/hooks/generated-class-name.js b/blocks/hooks/generated-class-name.js index 4d5b4fde73801c..6a9740b3eb9248 100644 --- a/blocks/hooks/generated-class-name.js +++ b/blocks/hooks/generated-class-name.js @@ -35,6 +35,4 @@ export function addGeneratedClassName( extraProps, blockType ) { return extraProps; } -export default function generatedClassName() { - addFilter( 'blocks.getSaveContent.extraProps', 'core/generated-class-name/save-props', addGeneratedClassName ); -} +addFilter( 'blocks.getSaveContent.extraProps', 'core/generated-class-name/save-props', addGeneratedClassName ); diff --git a/blocks/hooks/index.js b/blocks/hooks/index.js index 9a2df3438b30a3..64431ec798d060 100644 --- a/blocks/hooks/index.js +++ b/blocks/hooks/index.js @@ -1,12 +1,7 @@ /** * Internal dependencies */ -import anchor from './anchor'; -import customClassName from './custom-class-name'; -import generatedClassName from './generated-class-name'; -import matchers from './matchers'; - -anchor(); -customClassName(); -generatedClassName(); -matchers(); +import './anchor'; +import './custom-class-name'; +import './generated-class-name'; +import './matchers'; diff --git a/blocks/hooks/matchers.js b/blocks/hooks/matchers.js index 84efa94f07d87f..be3b99d9f89871 100644 --- a/blocks/hooks/matchers.js +++ b/blocks/hooks/matchers.js @@ -96,6 +96,4 @@ export function resolveAttributes( settings ) { return settings; } -export default function matchers() { - addFilter( 'blocks.registerBlockType', 'core/matchers', resolveAttributes ); -} +addFilter( 'blocks.registerBlockType', 'core/matchers', resolveAttributes ); diff --git a/blocks/hooks/test/anchor.js b/blocks/hooks/test/anchor.js index 58e75622d4054a..74bfa2e20dfc25 100644 --- a/blocks/hooks/test/anchor.js +++ b/blocks/hooks/test/anchor.js @@ -6,41 +6,31 @@ import { noop } from 'lodash'; /** * WordPress dependencies */ -import { applyFilters, removeAllFilters } from '@wordpress/hooks'; +import { applyFilters } from '@wordpress/hooks'; /** * Internal dependencies */ -import anchor from '../anchor'; +import '../anchor'; describe( 'anchor', () => { - let blockSettings; - beforeEach( () => { - anchor(); - - blockSettings = { - save: noop, - category: 'common', - title: 'block title', - }; - } ); - - afterEach( () => { - removeAllFilters( 'blocks.registerBlockType' ); - removeAllFilters( 'blocks.getSaveContent.extraProps' ); - } ); + const blockSettings = { + save: noop, + category: 'common', + title: 'block title', + }; describe( 'addAttribute()', () => { - const addAttribute = applyFilters.bind( null, 'blocks.registerBlockType' ); + const registerBlockType = applyFilters.bind( null, 'blocks.registerBlockType' ); it( 'should do nothing if the block settings do not define anchor support', () => { - const settings = addAttribute( blockSettings ); + const settings = registerBlockType( blockSettings ); expect( settings.attributes ).toBe( undefined ); } ); it( 'should assign a new anchor attribute', () => { - const settings = addAttribute( { + const settings = registerBlockType( { ...blockSettings, supports: { anchor: true, @@ -52,24 +42,23 @@ describe( 'anchor', () => { } ); describe( 'addSaveProps', () => { - const addSaveProps = applyFilters.bind( null, 'blocks.getSaveContent.extraProps' ); + const getSaveContentExtraProps = applyFilters.bind( null, 'blocks.getSaveContent.extraProps' ); it( 'should do nothing if the block settings do not define anchor support', () => { const attributes = { anchor: 'foo' }; - const extraProps = addSaveProps( {}, blockSettings, attributes ); + const extraProps = getSaveContentExtraProps( {}, blockSettings, attributes ); expect( extraProps ).not.toHaveProperty( 'id' ); } ); it( 'should inject anchor attribute ID', () => { const attributes = { anchor: 'foo' }; - blockSettings = { + const extraProps = getSaveContentExtraProps( {}, { ...blockSettings, supports: { anchor: true, }, - }; - const extraProps = addSaveProps( {}, blockSettings, attributes ); + }, attributes ); expect( extraProps.id ).toBe( 'foo' ); } ); diff --git a/blocks/hooks/test/custom-class-name.js b/blocks/hooks/test/custom-class-name.js index 92692e2bb81cd7..fc946d03c13d69 100644 --- a/blocks/hooks/test/custom-class-name.js +++ b/blocks/hooks/test/custom-class-name.js @@ -6,29 +6,19 @@ import { noop } from 'lodash'; /** * WordPress dependencies */ -import { applyFilters, removeAllFilters } from '@wordpress/hooks'; +import { applyFilters } from '@wordpress/hooks'; /** * Internal dependencies */ -import customClassName from '../custom-class-name'; +import '../custom-class-name'; describe( 'custom className', () => { - let blockSettings; - beforeEach( () => { - customClassName(); - - blockSettings = { - save: noop, - category: 'common', - title: 'block title', - }; - } ); - - afterEach( () => { - removeAllFilters( 'blocks.registerBlockType' ); - removeAllFilters( 'blocks.getSaveContent.extraProps' ); - } ); + const blockSettings = { + save: noop, + category: 'common', + title: 'block title', + }; describe( 'addAttribute()', () => { const addAttribute = applyFilters.bind( null, 'blocks.registerBlockType' ); diff --git a/blocks/hooks/test/geenerated-class-name.js b/blocks/hooks/test/generated-class-name.js similarity index 69% rename from blocks/hooks/test/geenerated-class-name.js rename to blocks/hooks/test/generated-class-name.js index 2590f402618a91..922c783a5e7cad 100644 --- a/blocks/hooks/test/geenerated-class-name.js +++ b/blocks/hooks/test/generated-class-name.js @@ -6,29 +6,20 @@ import { noop } from 'lodash'; /** * External dependencies */ -import { applyFilters, removeAllFilters } from '@wordpress/hooks'; +import { applyFilters } from '@wordpress/hooks'; /** * Internal dependencies */ -import generatedClassName from '../generated-class-name'; +import '../generated-class-name'; describe( 'generated className', () => { - let blockSettings; - beforeEach( () => { - generatedClassName(); - - blockSettings = { - name: 'chicken/ribs', - save: noop, - category: 'common', - title: 'block title', - }; - } ); - - afterEach( () => { - removeAllFilters( 'blocks.getSaveContent.extraProps' ); - } ); + const blockSettings = { + name: 'chicken/ribs', + save: noop, + category: 'common', + title: 'block title', + }; describe( 'addSaveProps', () => { const addSaveProps = applyFilters.bind( null, 'blocks.getSaveContent.extraProps' );