Skip to content

Commit

Permalink
Merge pull request #102 from WordPress/fix/has-return-boolean
Browse files Browse the repository at this point in the history
return a boolean from `hasHook` not the hook count
  • Loading branch information
Adam Silverstein authored Apr 10, 2018
2 parents ddc18d0 + d166c75 commit d674f3b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
7 changes: 2 additions & 5 deletions packages/hooks/src/createHasHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ function createHasHook( hooks ) {
*
* @param {string} hookName The name of the hook to check for.
*
* @return {number} The number of handlers that are attached to
* the given hook.
* @return {boolean} Whether there are handlers that are attached to the given hook.
*/
return function hasHook( hookName ) {
return hooks[ hookName ]
? hooks[ hookName ].handlers.length
: 0;
return hookName in hooks;
};
}

Expand Down
16 changes: 8 additions & 8 deletions packages/hooks/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ test( 'Test doingAction, didAction and hasAction.', () => {
expect( doingAction( 'test.action' ) ).toBe( false );

expect( didAction( 'test.action' ) ).toBe( 0 );
expect( hasAction( 'test.action' ) ).toBe( 0 );
expect( hasAction( 'test.action' ) ).toBe( false );

addAction( 'test.action', 'my_callback', () => {
actionCalls++;
Expand All @@ -466,15 +466,15 @@ test( 'Test doingAction, didAction and hasAction.', () => {
// Verify action added, not running yet.
expect( doingAction( 'test.action' ) ).toBe( false );
expect( didAction( 'test.action' ) ).toBe( 0 );
expect( hasAction( 'test.action' ) ).toBe( 1 );
expect( hasAction( 'test.action' ) ).toBe( true );

doAction( 'test.action' );

// Verify action added and running.
expect( actionCalls ).toBe( 1 );
expect( doingAction( 'test.action' ) ).toBe( false );
expect( didAction( 'test.action' ) ).toBe( 1 );
expect( hasAction( 'test.action' ) ).toBe( 1 );
expect( hasAction( 'test.action' ) ).toBe( true );
expect( doingAction() ).toBe( false );
expect( doingAction( 'test.action' ) ).toBe( false );
expect( doingAction( 'notatest.action' ) ).toBe( false );
Expand All @@ -489,13 +489,13 @@ test( 'Test doingAction, didAction and hasAction.', () => {
// Verify state is reset appropriately.
expect( doingAction( 'test.action' ) ).toBe( false );
expect( didAction( 'test.action' ) ).toBe( 2 );
expect( hasAction( 'test.action' ) ).toBe( 0 );
expect( hasAction( 'test.action' ) ).toBe( true );

doAction( 'another.action' );
expect( doingAction( 'test.action' ) ).toBe( false );

// Verify hasAction returns 0 when no matching action.
expect( hasAction( 'notatest.action' ) ).toBe( 0 );
expect( hasAction( 'notatest.action' ) ).toBe( false );
} );

test( 'Verify doingFilter, didFilter and hasFilter.', () => {
Expand All @@ -514,16 +514,16 @@ test( 'Verify doingFilter, didFilter and hasFilter.', () => {
expect( test ).toBe( 'someValue' );
expect( filterCalls ).toBe( 1 );
expect( didFilter( 'runtest.filter' ) ).toBe( 1 );
expect( hasFilter( 'runtest.filter' ) ).toBe( 1 );
expect( hasFilter( 'notatest.filter' ) ).toBe( 0 );
expect( hasFilter( 'runtest.filter' ) ).toBe( true );
expect( hasFilter( 'notatest.filter' ) ).toBe( false );
expect( doingFilter() ).toBe( false );
expect( doingFilter( 'runtest.filter' ) ).toBe( false );
expect( doingFilter( 'notatest.filter' ) ).toBe( false );
expect( currentFilter() ).toBe( null );

expect( removeAllFilters( 'runtest.filter' ) ).toBe( 1 );

expect( hasFilter( 'runtest.filter' ) ).toBe( 0 );
expect( hasFilter( 'runtest.filter' ) ).toBe( true );
expect( didFilter( 'runtest.filter' ) ).toBe( 1 );
} );

Expand Down

0 comments on commit d674f3b

Please sign in to comment.