Skip to content

Commit

Permalink
Extensibility: Apply filters without function wrappers (#3933)
Browse files Browse the repository at this point in the history
* Extensibility: Add blocks hooks in the files were they are defined

* Tests: Rename generated class name test file
  • Loading branch information
gziolo committed Dec 14, 2017
1 parent 9aefea3 commit 79c3f34
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 84 deletions.
8 changes: 3 additions & 5 deletions blocks/hooks/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
8 changes: 3 additions & 5 deletions blocks/hooks/custom-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
4 changes: 1 addition & 3 deletions blocks/hooks/generated-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
13 changes: 4 additions & 9 deletions blocks/hooks/index.js
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 1 addition & 3 deletions blocks/hooks/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
39 changes: 14 additions & 25 deletions blocks/hooks/test/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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' );
} );
Expand Down
24 changes: 7 additions & 17 deletions blocks/hooks/test/custom-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down

0 comments on commit 79c3f34

Please sign in to comment.