Skip to content

Commit

Permalink
Add some unit tests for the ‘all’ hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Silverstein committed May 1, 2019
1 parent d153423 commit adda6d7
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/hooks/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ beforeEach( () => {

delete hooks[ k ];
}
delete hooks.all;
} );
} );

Expand Down Expand Up @@ -697,3 +698,47 @@ test( 'removing a filter triggers a hookRemoved action passing all callback deta
'my_callback3'
);
} );

test( 'add an all filter and run it any hook to trigger it', () => {
addFilter( 'all', 'my_callback', filterA );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testa' );
expect( applyFilters( 'test.filter-anything', 'test' ) ).toBe( 'testa' );
} );

test( 'add an all action and run it any hook to trigger it', () => {
addAction( 'all', 'my_callback', actionA );
addAction( 'test.action', 'my_callback', actionA );// Doesn't get triggered.
doAction( 'test.action-anything' );
expect( window.actionValue ).toBe( 'a' );
} );

test( 'add multiple all filters and run it any hook to trigger them', () => {
addFilter( 'all', 'my_callback', filterA );
addFilter( 'all', 'my_callback', filterB );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' );
expect( applyFilters( 'test.filter-anything', 'test' ) ).toBe( 'testab' );
} );

test( 'add multiple all actions and run it any hook to trigger them', () => {
addAction( 'all', 'my_callback', actionA );
addAction( 'all', 'my_callback', actionB );
addAction( 'test.action', 'my_callback', actionA ); // Doesn't get triggered.
doAction( 'test.action-anything' );
expect( window.actionValue ).toBe( 'ab' );
} );

test( 'add multiple all filters and run it any hook to trigger them by priority', () => {
addFilter( 'all', 'my_callback', filterA, 11 );
addFilter( 'all', 'my_callback', filterB, 10 );
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testba' );
expect( applyFilters( 'test.filter-anything', 'test' ) ).toBe( 'testba' );
} );

test( 'add multiple all actions and run it any hook to trigger them by priority', () => {
addAction( 'all', 'my_callback', actionA, 11 );
addAction( 'all', 'my_callback', actionB, 10 );
addAction( 'test.action', 'my_callback', actionA ); // Doesn't get triggered.
doAction( 'test.action-anything' );
expect( window.actionValue ).toBe( 'ba' );
} );

0 comments on commit adda6d7

Please sign in to comment.