From bbdefa14822297fe1468c255ccd6963ed6c7b478 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Tue, 30 Apr 2019 18:46:44 -0600 Subject: [PATCH] from https://github.com/WordPress/packages/pull/106/files --- packages/hooks/src/createHasHook.js | 15 ++++++++--- packages/hooks/src/test/index.test.js | 38 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/hooks/src/createHasHook.js b/packages/hooks/src/createHasHook.js index de4ee3c17a793..f72edd6566ca3 100644 --- a/packages/hooks/src/createHasHook.js +++ b/packages/hooks/src/createHasHook.js @@ -5,17 +5,24 @@ * @param {Object} hooks Stored hooks, keyed by hook name. * * @return {Function} Function that returns whether any handlers are - * attached to a particular hook. + * attached to a particular hook and optional namespace. */ function createHasHook( hooks ) { /** - * Returns how many handlers are attached for the given hook. + * Returns whether any handlers are attached for the given hookName and optional namespace. * - * @param {string} hookName The name of the hook to check for. + * @param {string} hookName The name of the hook to check for. + * @param {string} namespace Optional. The unique namespace identifying the callback in the form `vendor/plugin/function`. * * @return {boolean} Whether there are handlers that are attached to the given hook. */ - return function hasHook( hookName ) { + return function hasHook( hookName, namespace ) { + // Use the namespace if provided. + if ( 'undefined' !== typeof namespace ) { + return hookName in hooks && + hooks[ hookName ].handlers.some( ( hook ) => hook.namespace === namespace ); + } + return hookName in hooks; }; } diff --git a/packages/hooks/src/test/index.test.js b/packages/hooks/src/test/index.test.js index a31a0d2d113ce..9f482dee4969a 100644 --- a/packages/hooks/src/test/index.test.js +++ b/packages/hooks/src/test/index.test.js @@ -697,3 +697,41 @@ test( 'removing a filter triggers a hookRemoved action passing all callback deta 'my_callback3' ); } ); + +test( 'checking hasAction and hasFilter with named callbacks', () => { + // hasAction tests + addAction( 'test.action', 'my_callback', () => {} ); + expect( hasAction( 'test.action', 'not_my_callback' ) ).toBe( false ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true ); + + // test removing + removeAction( 'test.action', 'my_callback' ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false ); + + // test removeAll + addAction( 'test.action', 'my_callback', () => {} ); + addAction( 'test.action', 'my_second_callback', () => {} ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true ); + removeAllActions( 'test.action' ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false ); + expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false ); + + // hasFilter tests + addFilter( 'test.filter', 'my_callback', () => {} ); + expect( hasFilter( 'test.filter', 'not_my_callback' ) ).toBe( false ); + expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( true ); + + // test removing + removeFilter( 'test.filter', 'my_callback' ); + expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( false ); + + // test removeAll + addFilter( 'test.filter', 'my_callback', () => {} ); + addFilter( 'test.filter', 'my_second_callback', () => {} ); + expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( true ); + expect( hasFilter( 'test.filter', 'my_second_callback' ) ).toBe( true ); + removeAllFilters( 'test.filter' ); + expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( false ); + expect( hasFilter( 'test.filter', 'my_second_callback' ) ).toBe( false ); +} );