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

Extensibility: Apply filters without function wrappers #3933

Merged
merged 2 commits into from
Dec 14, 2017
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
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';
Copy link
Member

Choose a reason for hiding this comment

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

So the downside to this approach is that we can't clean up lingering handlers after tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

Test files are isolated, so they should be cleaned up anyways. I hope 😄

Copy link
Member

Choose a reason for hiding this comment

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

Test files are isolated, so they should be cleaned up anyways. I hope 😄

Hmm, the require cache is cleared between each? Would be (good) news to me if true.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it isn’t the case only for mocks imported in Jest setup file.

You can enable reseting modules per test using resetModules config option or call it programmatically with the method called the same.

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