@wordpress/hooks
Advanced tools
Comparing version 0.1.0-beta.5 to 1.0.0
@@ -1,34 +0,23 @@ | ||
import createAddHook from './createAddHook'; | ||
import createRemoveHook from './createRemoveHook'; | ||
import createHasHook from './createHasHook'; | ||
import createRunHook from './createRunHook'; | ||
import createCurrentHook from './createCurrentHook'; | ||
import createDoingHook from './createDoingHook'; | ||
import createDidHook from './createDidHook'; | ||
import createHooks from './createHooks'; | ||
function createHooks() { | ||
var actions = {}; | ||
var filters = {}; | ||
var _createHooks = createHooks(), | ||
addAction = _createHooks.addAction, | ||
addFilter = _createHooks.addFilter, | ||
removeAction = _createHooks.removeAction, | ||
removeFilter = _createHooks.removeFilter, | ||
hasAction = _createHooks.hasAction, | ||
hasFilter = _createHooks.hasFilter, | ||
removeAllActions = _createHooks.removeAllActions, | ||
removeAllFilters = _createHooks.removeAllFilters, | ||
doAction = _createHooks.doAction, | ||
applyFilters = _createHooks.applyFilters, | ||
currentAction = _createHooks.currentAction, | ||
currentFilter = _createHooks.currentFilter, | ||
doingAction = _createHooks.doingAction, | ||
doingFilter = _createHooks.doingFilter, | ||
didAction = _createHooks.didAction, | ||
didFilter = _createHooks.didFilter, | ||
actions = _createHooks.actions, | ||
filters = _createHooks.filters; | ||
return { | ||
addAction: createAddHook(actions), | ||
addFilter: createAddHook(filters), | ||
removeAction: createRemoveHook(actions), | ||
removeFilter: createRemoveHook(filters), | ||
hasAction: createHasHook(actions), | ||
hasFilter: createHasHook(filters), | ||
removeAllActions: createRemoveHook(actions, true), | ||
removeAllFilters: createRemoveHook(filters, true), | ||
doAction: createRunHook(actions), | ||
applyFilters: createRunHook(filters, true), | ||
currentAction: createCurrentHook(actions), | ||
currentFilter: createCurrentHook(filters), | ||
doingAction: createDoingHook(actions), | ||
doingFilter: createDoingHook(filters), | ||
didAction: createDidHook(actions), | ||
didFilter: createDidHook(filters), | ||
actions: actions, | ||
filters: filters | ||
}; | ||
}; | ||
export default createHooks; | ||
export { createHooks, addAction, addFilter, removeAction, removeFilter, hasAction, hasFilter, removeAllActions, removeAllFilters, doAction, applyFilters, currentAction, currentFilter, doingAction, doingFilter, didAction, didFilter, actions, filters }; |
@@ -16,4 +16,4 @@ /** | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.\-]*$/.test(namespace)) { | ||
console.error('The namespace can only contain numbers, letters, dashes, periods and underscores.'); | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) { | ||
console.error('The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'); | ||
return false; | ||
@@ -20,0 +20,0 @@ } |
@@ -6,58 +6,48 @@ 'use strict'; | ||
}); | ||
exports.filters = exports.actions = exports.didFilter = exports.didAction = exports.doingFilter = exports.doingAction = exports.currentFilter = exports.currentAction = exports.applyFilters = exports.doAction = exports.removeAllFilters = exports.removeAllActions = exports.hasFilter = exports.hasAction = exports.removeFilter = exports.removeAction = exports.addFilter = exports.addAction = exports.createHooks = undefined; | ||
var _createAddHook = require('./createAddHook'); | ||
var _createHooks2 = require('./createHooks'); | ||
var _createAddHook2 = _interopRequireDefault(_createAddHook); | ||
var _createHooks3 = _interopRequireDefault(_createHooks2); | ||
var _createRemoveHook = require('./createRemoveHook'); | ||
var _createRemoveHook2 = _interopRequireDefault(_createRemoveHook); | ||
var _createHasHook = require('./createHasHook'); | ||
var _createHasHook2 = _interopRequireDefault(_createHasHook); | ||
var _createRunHook = require('./createRunHook'); | ||
var _createRunHook2 = _interopRequireDefault(_createRunHook); | ||
var _createCurrentHook = require('./createCurrentHook'); | ||
var _createCurrentHook2 = _interopRequireDefault(_createCurrentHook); | ||
var _createDoingHook = require('./createDoingHook'); | ||
var _createDoingHook2 = _interopRequireDefault(_createDoingHook); | ||
var _createDidHook = require('./createDidHook'); | ||
var _createDidHook2 = _interopRequireDefault(_createDidHook); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function createHooks() { | ||
var actions = {}; | ||
var filters = {}; | ||
var _createHooks = (0, _createHooks3.default)(), | ||
addAction = _createHooks.addAction, | ||
addFilter = _createHooks.addFilter, | ||
removeAction = _createHooks.removeAction, | ||
removeFilter = _createHooks.removeFilter, | ||
hasAction = _createHooks.hasAction, | ||
hasFilter = _createHooks.hasFilter, | ||
removeAllActions = _createHooks.removeAllActions, | ||
removeAllFilters = _createHooks.removeAllFilters, | ||
doAction = _createHooks.doAction, | ||
applyFilters = _createHooks.applyFilters, | ||
currentAction = _createHooks.currentAction, | ||
currentFilter = _createHooks.currentFilter, | ||
doingAction = _createHooks.doingAction, | ||
doingFilter = _createHooks.doingFilter, | ||
didAction = _createHooks.didAction, | ||
didFilter = _createHooks.didFilter, | ||
actions = _createHooks.actions, | ||
filters = _createHooks.filters; | ||
return { | ||
addAction: (0, _createAddHook2.default)(actions), | ||
addFilter: (0, _createAddHook2.default)(filters), | ||
removeAction: (0, _createRemoveHook2.default)(actions), | ||
removeFilter: (0, _createRemoveHook2.default)(filters), | ||
hasAction: (0, _createHasHook2.default)(actions), | ||
hasFilter: (0, _createHasHook2.default)(filters), | ||
removeAllActions: (0, _createRemoveHook2.default)(actions, true), | ||
removeAllFilters: (0, _createRemoveHook2.default)(filters, true), | ||
doAction: (0, _createRunHook2.default)(actions), | ||
applyFilters: (0, _createRunHook2.default)(filters, true), | ||
currentAction: (0, _createCurrentHook2.default)(actions), | ||
currentFilter: (0, _createCurrentHook2.default)(filters), | ||
doingAction: (0, _createDoingHook2.default)(actions), | ||
doingFilter: (0, _createDoingHook2.default)(filters), | ||
didAction: (0, _createDidHook2.default)(actions), | ||
didFilter: (0, _createDidHook2.default)(filters), | ||
actions: actions, | ||
filters: filters | ||
}; | ||
}; | ||
exports.default = createHooks; | ||
exports.createHooks = _createHooks3.default; | ||
exports.addAction = addAction; | ||
exports.addFilter = addFilter; | ||
exports.removeAction = removeAction; | ||
exports.removeFilter = removeFilter; | ||
exports.hasAction = hasAction; | ||
exports.hasFilter = hasFilter; | ||
exports.removeAllActions = removeAllActions; | ||
exports.removeAllFilters = removeAllFilters; | ||
exports.doAction = doAction; | ||
exports.applyFilters = applyFilters; | ||
exports.currentAction = currentAction; | ||
exports.currentFilter = currentFilter; | ||
exports.doingAction = doingAction; | ||
exports.doingFilter = doingFilter; | ||
exports.didAction = didAction; | ||
exports.didFilter = didFilter; | ||
exports.actions = actions; | ||
exports.filters = filters; |
@@ -21,4 +21,4 @@ 'use strict'; | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.\-]*$/.test(namespace)) { | ||
console.error('The namespace can only contain numbers, letters, dashes, periods and underscores.'); | ||
if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) { | ||
console.error('The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'); | ||
return false; | ||
@@ -25,0 +25,0 @@ } |
{ | ||
"name": "@wordpress/hooks", | ||
"version": "0.1.0-beta.5", | ||
"version": "1.0.0", | ||
"repository": { | ||
@@ -11,3 +11,2 @@ "type": "git", | ||
"module": "build-module/index.js", | ||
"browser": "build-browser/index.js", | ||
"author": "WordPress", | ||
@@ -14,0 +13,0 @@ "license": "GPL-2.0+", |
@@ -21,2 +21,3 @@ # @wordpress/hooks | ||
* `createHooks()` | ||
* `addAction( 'hookName', 'functionName', callback, priority )` | ||
@@ -36,5 +37,7 @@ * `addFilter( 'hookName', 'functionName', callback, priority )` | ||
* `hasFilter( 'hookName' )` | ||
* `actions` | ||
* `filters` | ||
Hooks can be added to an object via composition: | ||
`import createHooks from '../';` | ||
`import { createHooks } from '@wordpress/hooks';` | ||
@@ -41,0 +44,0 @@ `myObject.hooks = createHooks();` |
@@ -1,34 +0,44 @@ | ||
import createAddHook from './createAddHook'; | ||
import createRemoveHook from './createRemoveHook'; | ||
import createHasHook from './createHasHook'; | ||
import createRunHook from './createRunHook'; | ||
import createCurrentHook from './createCurrentHook'; | ||
import createDoingHook from './createDoingHook'; | ||
import createDidHook from './createDidHook'; | ||
import createHooks from './createHooks'; | ||
function createHooks() { | ||
const actions = {}; | ||
const filters = {}; | ||
const { | ||
addAction, | ||
addFilter, | ||
removeAction, | ||
removeFilter, | ||
hasAction, | ||
hasFilter, | ||
removeAllActions, | ||
removeAllFilters, | ||
doAction, | ||
applyFilters, | ||
currentAction, | ||
currentFilter, | ||
doingAction, | ||
doingFilter, | ||
didAction, | ||
didFilter, | ||
actions, | ||
filters, | ||
} = createHooks(); | ||
return { | ||
addAction: createAddHook( actions ), | ||
addFilter: createAddHook( filters ), | ||
removeAction: createRemoveHook( actions ), | ||
removeFilter: createRemoveHook( filters ), | ||
hasAction: createHasHook( actions ), | ||
hasFilter: createHasHook( filters ), | ||
removeAllActions: createRemoveHook( actions, true ), | ||
removeAllFilters: createRemoveHook( filters, true ), | ||
doAction: createRunHook( actions ), | ||
applyFilters: createRunHook( filters, true ), | ||
currentAction: createCurrentHook( actions ), | ||
currentFilter: createCurrentHook( filters ), | ||
doingAction: createDoingHook( actions ), | ||
doingFilter: createDoingHook( filters ), | ||
didAction: createDidHook( actions ), | ||
didFilter: createDidHook( filters ), | ||
actions: actions, | ||
filters: filters, | ||
}; | ||
export { | ||
createHooks, | ||
addAction, | ||
addFilter, | ||
removeAction, | ||
removeFilter, | ||
hasAction, | ||
hasFilter, | ||
removeAllActions, | ||
removeAllFilters, | ||
doAction, | ||
applyFilters, | ||
currentAction, | ||
currentFilter, | ||
doingAction, | ||
doingFilter, | ||
didAction, | ||
didFilter, | ||
actions, | ||
filters, | ||
}; | ||
export default createHooks; |
@@ -6,7 +6,24 @@ /* eslint-disable no-console */ | ||
*/ | ||
import createHooks from '../'; | ||
import { | ||
createHooks, | ||
addAction, | ||
addFilter, | ||
removeAction, | ||
removeFilter, | ||
hasAction, | ||
hasFilter, | ||
removeAllActions, | ||
removeAllFilters, | ||
doAction, | ||
applyFilters, | ||
currentAction, | ||
currentFilter, | ||
doingAction, | ||
doingFilter, | ||
didAction, | ||
didFilter, | ||
actions, | ||
filters, | ||
} from '../'; | ||
const testObject = {}; | ||
testObject.hooks = createHooks(); | ||
function filter_a( str ) { | ||
@@ -25,3 +42,3 @@ return str + 'a'; | ||
function filter_b_removes_self( str ) { | ||
testObject.hooks.removeFilter( 'test.filter', 'my_callback_filter_b_removes_self' ); | ||
removeFilter( 'test.filter', 'my_callback_filter_b_removes_self' ); | ||
return str + 'b'; | ||
@@ -31,3 +48,3 @@ } | ||
function filter_removes_b( str ) { | ||
testObject.hooks.removeFilter( 'test.filter', 'my_callback_filter_b' ); | ||
removeFilter( 'test.filter', 'my_callback_filter_b' ); | ||
return str; | ||
@@ -37,3 +54,3 @@ } | ||
function filter_removes_c( str ) { | ||
testObject.hooks.removeFilter( 'test.filter', 'my_callback_filter_c' ); | ||
removeFilter( 'test.filter', 'my_callback_filter_c' ); | ||
return str; | ||
@@ -58,6 +75,6 @@ } | ||
window.actionValue = ''; | ||
// Reset state in between tests (clear all callbacks, `testObject.hooks.didAction` counts, | ||
// Reset state in between tests (clear all callbacks, `didAction` counts, | ||
// etc.) Just reseting actions and filters is not enough | ||
// because the internal functions have references to the original objects. | ||
[ testObject.hooks.actions, testObject.hooks.filters ].forEach( hooks => { | ||
[ actions, filters ].forEach( hooks => { | ||
for ( const k in hooks ) { | ||
@@ -75,34 +92,36 @@ delete hooks[ k ]; | ||
test( 'hooks can be instantiated', () => { | ||
expect( typeof testObject.hooks ).toEqual( 'object' ); | ||
const hooks = createHooks(); | ||
expect( typeof hooks ).toBe( 'object' ); | ||
} ); | ||
test( 'run a filter with no callbacks', () => { | ||
expect( testObject.hooks.applyFilters( 'test.filter', 42 ) ).toEqual( 42 ); | ||
expect( applyFilters( 'test.filter', 42 ) ).toBe( 42 ); | ||
} ); | ||
test( 'add and remove a filter', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', filter_a ); | ||
expect( testObject.hooks.removeAllFilters( 'test.filter' ) ).toEqual( 1 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'test' ); | ||
expect( testObject.hooks.removeAllFilters( 'test.filter' ) ).toEqual( 0 ); | ||
addFilter( 'test.filter', 'my_callback', filter_a ); | ||
expect( removeAllFilters( 'test.filter' ) ).toBe( 1 ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'test' ); | ||
expect( removeAllFilters( 'test.filter' ) ).toBe( 0 ); | ||
} ); | ||
test( 'add a filter and run it', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', filter_a ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testa' ); | ||
addFilter( 'test.filter', 'my_callback', filter_a ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testa' ); | ||
} ); | ||
test( 'add 2 filters in a row and run them', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', filter_a ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', filter_b ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' ); | ||
addFilter( 'test.filter', 'my_callback', filter_a ); | ||
addFilter( 'test.filter', 'my_callback', filter_b ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' ); | ||
} ); | ||
test( 'remove a non-existent filter', () => { | ||
expect( testObject.hooks.removeFilter( 'test.filter', 'my_callback', filter_a ) ).toEqual( 0 ); | ||
expect( testObject.hooks.removeAllFilters( 'test.filter' ) ).toEqual( 0 ); | ||
expect( removeFilter( 'test.filter', 'my_callback', filter_a ) ).toBe( 0 ); | ||
expect( removeAllFilters( 'test.filter' ) ).toBe( 0 ); | ||
} ); | ||
test( 'remove an invalid namespace from a filter', () => { | ||
expect( testObject.hooks.removeFilter( 'test.filter', 42 ) ).toEqual( undefined ); | ||
expect( removeFilter( 'test.filter', 42 ) ).toBe( undefined ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
@@ -114,3 +133,3 @@ 'The namespace must be a non-empty string.' | ||
test( 'cannot add filters with non-string hook names', () => { | ||
testObject.hooks.addFilter( 42, 'my_callback', () => null ); | ||
addFilter( 42, 'my_callback', () => null ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
@@ -122,3 +141,3 @@ 'The hook name must be a non-empty string.' | ||
test( 'cannot add filters with empty-string hook names', () => { | ||
testObject.hooks.addFilter( '', 'my_callback', () => null ); | ||
addFilter( '', 'my_callback', () => null ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
@@ -130,3 +149,3 @@ 'The hook name must be a non-empty string.' | ||
test( 'cannot add filters with empty-string namespaces', () => { | ||
testObject.hooks.addFilter( 'hook_name', '', () => null ); | ||
addFilter( 'hook_name', '', () => null ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
@@ -138,17 +157,10 @@ 'The namespace must be a non-empty string.' | ||
test( 'cannot add filters with invalid namespaces', () => { | ||
testObject.hooks.addFilter( 'hook_name', 'invalid_%&name', () => null ); | ||
addFilter( 'hook_name', 'invalid_%&name', () => null ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
'The namespace can only contain numbers, letters, dashes, periods and underscores.' | ||
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.' | ||
); | ||
} ); | ||
test( 'cannot add filters with namespaces missing a functionDescription', () => { | ||
testObject.hooks.addFilter( 'hook_name', 'invalid_name/', () => null ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
'The namespace can only contain numbers, letters, dashes, periods and underscores.' | ||
); | ||
} ); | ||
test( 'Can add filters with dashes in namespaces', () => { | ||
testObject.hooks.addFilter( 'hook_name', 'with-dashes', () => null ); | ||
addFilter( 'hook_name', 'with-dashes', () => null ); | ||
expect( console.error ).toHaveBeenCalledTimes( 0 ); | ||
@@ -158,8 +170,18 @@ } ); | ||
test( 'Can add filters with capitals in namespaces', () => { | ||
testObject.hooks.addFilter( 'hook_name', 'my_name-OhNoaction', () => null ); | ||
addFilter( 'hook_name', 'My_Name-OhNoaction', () => null ); | ||
expect( console.error ).toHaveBeenCalledTimes( 0 ); | ||
} ); | ||
test( 'Can add filters with slashes in namespaces', () => { | ||
addFilter( 'hook_name', 'my/name/action', () => null ); | ||
expect( console.error ).toHaveBeenCalledTimes( 0 ); | ||
} ); | ||
test( 'Can add filters with periods in namespaces', () => { | ||
addFilter( 'hook_name', 'my.name.action', () => null ); | ||
expect( console.error ).toHaveBeenCalledTimes( 0 ); | ||
} ); | ||
test( 'Can add filters with capitals in hookName', () => { | ||
testObject.hooks.addFilter( 'hookName', 'action', () => null ); | ||
addFilter( 'hookName', 'action', () => null ); | ||
expect( console.error ).toHaveBeenCalledTimes( 0 ); | ||
@@ -169,3 +191,3 @@ } ); | ||
test( 'Can add filters with periods in namespaces', () => { | ||
testObject.hooks.addFilter( 'hook_name', 'ok.action', () => null ); | ||
addFilter( 'hook_name', 'ok.action', () => null ); | ||
expect( console.error ).toHaveBeenCalledTimes( 0 ); | ||
@@ -175,3 +197,3 @@ } ); | ||
test( 'Can add filters with periods in hookName', () => { | ||
testObject.hooks.addFilter( 'hook.name', 'action', () => null ); | ||
addFilter( 'hook.name', 'action', () => null ); | ||
expect( console.error ).toHaveBeenCalledTimes( 0 ); | ||
@@ -181,10 +203,17 @@ } ); | ||
test( 'cannot add filters with invalid namespaces', () => { | ||
testObject.hooks.addFilter( 'hook_name', '/invalid_name', () => null ); | ||
addFilter( 'hook_name', '/invalid_name', () => null ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
'The namespace can only contain numbers, letters, dashes, periods and underscores.' | ||
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.' | ||
); | ||
} ); | ||
test( 'cannot add filters with namespace containing backslash', () => { | ||
addFilter( 'hook_name', 'i\n\v\a\l\i\d\n\a\m\e', () => null ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.' | ||
); | ||
} ); | ||
test( 'cannot add filters named with __ prefix', () => { | ||
testObject.hooks.addFilter( '__test', 'my_callback', () => null ); | ||
addFilter( '__test', 'my_callback', () => null ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
@@ -196,3 +225,3 @@ 'The hook name cannot begin with `__`.' | ||
test( 'cannot add filters with non-function callbacks', () => { | ||
testObject.hooks.addFilter( 'test', 'my_callback', '42' ); | ||
addFilter( 'test', 'my_callback', '42' ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
@@ -204,3 +233,3 @@ 'The hook callback must be a function.' | ||
test( 'cannot add filters with non-numeric priorities', () => { | ||
testObject.hooks.addFilter( 'test', 'my_callback', () => null, '42' ); | ||
addFilter( 'test', 'my_callback', () => null, '42' ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
@@ -212,3 +241,3 @@ 'If specified, the hook priority must be a number.' | ||
test( 'cannot run filters with non-string names', () => { | ||
expect( testObject.hooks.applyFilters( () => {}, 42 ) ).toBe( undefined ); | ||
expect( applyFilters( () => {}, 42 ) ).toBe( undefined ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
@@ -220,3 +249,3 @@ 'The hook name must be a non-empty string.' | ||
test( 'cannot run filters named with __ prefix', () => { | ||
expect( testObject.hooks.applyFilters( '__test', 42 ) ).toBe( undefined ); | ||
expect( applyFilters( '__test', 42 ) ).toBe( undefined ); | ||
expect( console.error ).toHaveBeenCalledWith( | ||
@@ -228,6 +257,6 @@ 'The hook name cannot begin with `__`.' | ||
test( 'add 3 filters with different priorities and run them', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testbca' ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testbca' ); | ||
} ); | ||
@@ -246,32 +275,32 @@ | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_3a', callbacks.fn_3a, 3 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_3b', callbacks.fn_3b, 3 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_3c', callbacks.fn_3c, 3 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_2a', callbacks.fn_2a, 2 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_2b', callbacks.fn_2b, 2 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_2c', callbacks.fn_2c, 2 ); | ||
addFilter( 'test_order', 'my_callback_fn_3a', callbacks.fn_3a, 3 ); | ||
addFilter( 'test_order', 'my_callback_fn_3b', callbacks.fn_3b, 3 ); | ||
addFilter( 'test_order', 'my_callback_fn_3c', callbacks.fn_3c, 3 ); | ||
addFilter( 'test_order', 'my_callback_fn_2a', callbacks.fn_2a, 2 ); | ||
addFilter( 'test_order', 'my_callback_fn_2b', callbacks.fn_2b, 2 ); | ||
addFilter( 'test_order', 'my_callback_fn_2c', callbacks.fn_2c, 2 ); | ||
expect( testObject.hooks.applyFilters( 'test_order', [] ) ).toEqual( | ||
expect( applyFilters( 'test_order', [] ) ).toEqual( | ||
[ '2a', '2b', '2c', '3a', '3b', '3c' ] | ||
); | ||
testObject.hooks.removeFilter( 'test_order', 'my_callback_fn_2b', callbacks.fn_2b ); | ||
testObject.hooks.removeFilter( 'test_order', 'my_callback_fn_3a', callbacks.fn_3a ); | ||
removeFilter( 'test_order', 'my_callback_fn_2b', callbacks.fn_2b ); | ||
removeFilter( 'test_order', 'my_callback_fn_3a', callbacks.fn_3a ); | ||
expect( testObject.hooks.applyFilters( 'test_order', [] ) ).toEqual( | ||
expect( applyFilters( 'test_order', [] ) ).toEqual( | ||
[ '2a', '2c', '3b', '3c' ] | ||
); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_4a', callbacks.fn_4a, 4 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_4b', callbacks.fn_4b, 4 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_1a', callbacks.fn_1a, 1 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_4c', callbacks.fn_4c, 4 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_1b', callbacks.fn_1b, 1 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_3d', callbacks.fn_3d, 3 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_4d', callbacks.fn_4d, 4 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_1c', callbacks.fn_1c, 1 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_2d', callbacks.fn_2d, 2 ); | ||
testObject.hooks.addFilter( 'test_order', 'my_callback_fn_1d', callbacks.fn_1d, 1 ); | ||
addFilter( 'test_order', 'my_callback_fn_4a', callbacks.fn_4a, 4 ); | ||
addFilter( 'test_order', 'my_callback_fn_4b', callbacks.fn_4b, 4 ); | ||
addFilter( 'test_order', 'my_callback_fn_1a', callbacks.fn_1a, 1 ); | ||
addFilter( 'test_order', 'my_callback_fn_4c', callbacks.fn_4c, 4 ); | ||
addFilter( 'test_order', 'my_callback_fn_1b', callbacks.fn_1b, 1 ); | ||
addFilter( 'test_order', 'my_callback_fn_3d', callbacks.fn_3d, 3 ); | ||
addFilter( 'test_order', 'my_callback_fn_4d', callbacks.fn_4d, 4 ); | ||
addFilter( 'test_order', 'my_callback_fn_1c', callbacks.fn_1c, 1 ); | ||
addFilter( 'test_order', 'my_callback_fn_2d', callbacks.fn_2d, 2 ); | ||
addFilter( 'test_order', 'my_callback_fn_1d', callbacks.fn_1d, 1 ); | ||
expect( testObject.hooks.applyFilters( 'test_order', [] ) ).toEqual( [ | ||
expect( applyFilters( 'test_order', [] ) ).toEqual( [ | ||
// all except 2b and 3a, which we removed earlier | ||
@@ -286,5 +315,5 @@ '1a', '1b', '1c', '1d', | ||
test( 'add and remove an action', () => { | ||
testObject.hooks.addAction( 'test.action', 'my_callback', action_a ); | ||
expect( testObject.hooks.removeAllActions( 'test.action' ) ).toEqual( 1 ); | ||
expect( testObject.hooks.doAction( 'test.action' ) ).toBe( undefined ); | ||
addAction( 'test.action', 'my_callback', action_a ); | ||
expect( removeAllActions( 'test.action' ) ).toBe( 1 ); | ||
expect( doAction( 'test.action' ) ).toBe( undefined ); | ||
expect( window.actionValue ).toBe( '' ); | ||
@@ -294,4 +323,4 @@ } ); | ||
test( 'add an action and run it', () => { | ||
testObject.hooks.addAction( 'test.action', 'my_callback', action_a ); | ||
testObject.hooks.doAction( 'test.action' ); | ||
addAction( 'test.action', 'my_callback', action_a ); | ||
doAction( 'test.action' ); | ||
expect( window.actionValue ).toBe( 'a' ); | ||
@@ -301,5 +330,5 @@ } ); | ||
test( 'add 2 actions in a row and then run them', () => { | ||
testObject.hooks.addAction( 'test.action', 'my_callback', action_a ); | ||
testObject.hooks.addAction( 'test.action', 'my_callback', action_b ); | ||
testObject.hooks.doAction( 'test.action' ); | ||
addAction( 'test.action', 'my_callback', action_a ); | ||
addAction( 'test.action', 'my_callback', action_b ); | ||
doAction( 'test.action' ); | ||
expect( window.actionValue ).toBe( 'ab' ); | ||
@@ -309,6 +338,6 @@ } ); | ||
test( 'add 3 actions with different priorities and run them', () => { | ||
testObject.hooks.addAction( 'test.action', 'my_callback', action_a ); | ||
testObject.hooks.addAction( 'test.action', 'my_callback', action_b, 2 ); | ||
testObject.hooks.addAction( 'test.action', 'my_callback', action_c, 8 ); | ||
testObject.hooks.doAction( 'test.action' ); | ||
addAction( 'test.action', 'my_callback', action_a ); | ||
addAction( 'test.action', 'my_callback', action_b, 2 ); | ||
addAction( 'test.action', 'my_callback', action_c, 8 ); | ||
doAction( 'test.action' ); | ||
expect( window.actionValue ).toBe( 'bca' ); | ||
@@ -321,7 +350,7 @@ } ); | ||
testObject.hooks.addAction( 'test.action', 'my_callback', ( a, b ) => { | ||
addAction( 'test.action', 'my_callback', ( a, b ) => { | ||
expect( a ).toBe( arg1 ); | ||
expect( b ).toBe( arg2 ); | ||
} ); | ||
testObject.hooks.doAction( 'test.action', arg1, arg2 ); | ||
doAction( 'test.action', arg1, arg2 ); | ||
} ); | ||
@@ -336,41 +365,41 @@ | ||
testObject.hooks.addAction( 'test.action', 'my_callback', func ); | ||
testObject.hooks.doAction( 'test.action' ); | ||
testObject.hooks.doAction( 'test.action' ); | ||
addAction( 'test.action', 'my_callback', func ); | ||
doAction( 'test.action' ); | ||
doAction( 'test.action' ); | ||
} ); | ||
test( 'add a filter before the one currently executing', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => val + 'a', 1 ); | ||
addFilter( 'test.filter', 'my_callback', val => { | ||
addFilter( 'test.filter', 'my_callback', val => val + 'a', 1 ); | ||
return val + 'b'; | ||
}, 2 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test_' ) ).toEqual( 'test_b' ); | ||
expect( applyFilters( 'test.filter', 'test_' ) ).toBe( 'test_b' ); | ||
} ); | ||
test( 'add a filter after the one currently executing', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => val + 'b', 2 ); | ||
addFilter( 'test.filter', 'my_callback', val => { | ||
addFilter( 'test.filter', 'my_callback', val => val + 'b', 2 ); | ||
return val + 'a'; | ||
}, 1 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test_' ) ).toEqual( 'test_ab' ); | ||
expect( applyFilters( 'test.filter', 'test_' ) ).toBe( 'test_ab' ); | ||
} ); | ||
test( 'add a filter immediately after the one currently executing', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', val => val + 'b', 1 ); | ||
addFilter( 'test.filter', 'my_callback', val => { | ||
addFilter( 'test.filter', 'my_callback', val => val + 'b', 1 ); | ||
return val + 'a'; | ||
}, 1 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test_' ) ).toEqual( 'test_ab' ); | ||
expect( applyFilters( 'test.filter', 'test_' ) ).toBe( 'test_ab' ); | ||
} ); | ||
test( 'remove specific action callback', () => { | ||
testObject.hooks.addAction( 'test.action', 'my_callback_action_a', action_a ); | ||
testObject.hooks.addAction( 'test.action', 'my_callback_action_b', action_b, 2 ); | ||
testObject.hooks.addAction( 'test.action', 'my_callback_action_c', action_c, 8 ); | ||
addAction( 'test.action', 'my_callback_action_a', action_a ); | ||
addAction( 'test.action', 'my_callback_action_b', action_b, 2 ); | ||
addAction( 'test.action', 'my_callback_action_c', action_c, 8 ); | ||
expect( testObject.hooks.removeAction( 'test.action', 'my_callback_action_b' ) ).toEqual( 1 ); | ||
testObject.hooks.doAction( 'test.action' ); | ||
expect( removeAction( 'test.action', 'my_callback_action_b' ) ).toBe( 1 ); | ||
doAction( 'test.action' ); | ||
expect( window.actionValue ).toBe( 'ca' ); | ||
@@ -380,8 +409,8 @@ } ); | ||
test( 'remove all action callbacks', () => { | ||
testObject.hooks.addAction( 'test.action', 'my_callback_action_a', action_a ); | ||
testObject.hooks.addAction( 'test.action', 'my_callback_action_b', action_b, 2 ); | ||
testObject.hooks.addAction( 'test.action', 'my_callback_action_c', action_c, 8 ); | ||
addAction( 'test.action', 'my_callback_action_a', action_a ); | ||
addAction( 'test.action', 'my_callback_action_b', action_b, 2 ); | ||
addAction( 'test.action', 'my_callback_action_c', action_c, 8 ); | ||
expect( testObject.hooks.removeAllActions( 'test.action' ) ).toEqual( 3 ); | ||
testObject.hooks.doAction( 'test.action' ); | ||
expect( removeAllActions( 'test.action' ) ).toBe( 3 ); | ||
doAction( 'test.action' ); | ||
expect( window.actionValue ).toBe( '' ); | ||
@@ -391,135 +420,135 @@ } ); | ||
test( 'remove specific filter callback', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 ); | ||
expect( testObject.hooks.removeFilter( 'test.filter', 'my_callback_filter_b' ) ).toEqual( 1 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testca' ); | ||
expect( removeFilter( 'test.filter', 'my_callback_filter_b' ) ).toBe( 1 ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testca' ); | ||
} ); | ||
test( 'filter removes a callback that has already executed', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 4 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' ); | ||
} ); | ||
test( 'filter removes a callback that has already executed (same priority)', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' ); | ||
} ); | ||
test( 'filter removes the current callback', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b_removes_self', filter_b_removes_self, 3 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b_removes_self', filter_b_removes_self, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' ); | ||
} ); | ||
test( 'filter removes a callback that has not yet executed (last)', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_removes_c', filter_removes_c, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 5 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_c', filter_removes_c, 4 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' ); | ||
} ); | ||
test( 'filter removes a callback that has not yet executed (middle)', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 3 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' ); | ||
} ); | ||
test( 'filter removes a callback that has not yet executed (same priority)', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a, 1 ); | ||
addFilter( 'test.filter', 'my_callback_filter_removes_b', filter_removes_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 4 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' ); | ||
} ); | ||
test( 'remove all filter callbacks', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_a', filter_a ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 ); | ||
addFilter( 'test.filter', 'my_callback_filter_a', filter_a ); | ||
addFilter( 'test.filter', 'my_callback_filter_b', filter_b, 2 ); | ||
addFilter( 'test.filter', 'my_callback_filter_c', filter_c, 8 ); | ||
expect( testObject.hooks.removeAllFilters( 'test.filter' ) ).toEqual( 3 ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'test' ); | ||
expect( removeAllFilters( 'test.filter' ) ).toBe( 3 ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'test' ); | ||
} ); | ||
// Test testObject.hooks.doingAction, testObject.hooks.didAction, testObject.hooks.hasAction. | ||
test( 'Test testObject.hooks.doingAction, testObject.hooks.didAction and testObject.hooks.hasAction.', () => { | ||
// Test doingAction, didAction, hasAction. | ||
test( 'Test doingAction, didAction and hasAction.', () => { | ||
let actionCalls = 0; | ||
testObject.hooks.addAction( 'another.action', 'my_callback', () => {} ); | ||
testObject.hooks.doAction( 'another.action' ); | ||
addAction( 'another.action', 'my_callback', () => {} ); | ||
doAction( 'another.action' ); | ||
// Verify no action is running yet. | ||
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false ); | ||
expect( doingAction( 'test.action' ) ).toBe( false ); | ||
expect( testObject.hooks.didAction( 'test.action' ) ).toBe( 0 ); | ||
expect( testObject.hooks.hasAction( 'test.action' ) ).toBe( 0 ); | ||
expect( didAction( 'test.action' ) ).toBe( 0 ); | ||
expect( hasAction( 'test.action' ) ).toBe( 0 ); | ||
testObject.hooks.addAction( 'test.action', 'my_callback', () => { | ||
addAction( 'test.action', 'my_callback', () => { | ||
actionCalls++; | ||
expect( testObject.hooks.currentAction() ).toBe( 'test.action' ); | ||
expect( testObject.hooks.doingAction() ).toBe( true ); | ||
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( true ); | ||
expect( currentAction() ).toBe( 'test.action' ); | ||
expect( doingAction() ).toBe( true ); | ||
expect( doingAction( 'test.action' ) ).toBe( true ); | ||
} ); | ||
// Verify action added, not running yet. | ||
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false ); | ||
expect( testObject.hooks.didAction( 'test.action' ) ).toBe( 0 ); | ||
expect( testObject.hooks.hasAction( 'test.action' ) ).toBe( 1 ); | ||
expect( doingAction( 'test.action' ) ).toBe( false ); | ||
expect( didAction( 'test.action' ) ).toBe( 0 ); | ||
expect( hasAction( 'test.action' ) ).toBe( 1 ); | ||
testObject.hooks.doAction( 'test.action' ); | ||
doAction( 'test.action' ); | ||
// Verify action added and running. | ||
expect( actionCalls ).toBe( 1 ); | ||
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false ); | ||
expect( testObject.hooks.didAction( 'test.action' ) ).toBe( 1 ); | ||
expect( testObject.hooks.hasAction( 'test.action' ) ).toBe( 1 ); | ||
expect( testObject.hooks.doingAction() ).toBe( false ); | ||
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false ); | ||
expect( testObject.hooks.doingAction( 'notatest.action' ) ).toBe( false ); | ||
expect( testObject.hooks.currentAction() ).toBe( null ); | ||
expect( doingAction( 'test.action' ) ).toBe( false ); | ||
expect( didAction( 'test.action' ) ).toBe( 1 ); | ||
expect( hasAction( 'test.action' ) ).toBe( 1 ); | ||
expect( doingAction() ).toBe( false ); | ||
expect( doingAction( 'test.action' ) ).toBe( false ); | ||
expect( doingAction( 'notatest.action' ) ).toBe( false ); | ||
expect( currentAction() ).toBe( null ); | ||
testObject.hooks.doAction( 'test.action' ); | ||
doAction( 'test.action' ); | ||
expect( actionCalls ).toBe( 2 ); | ||
expect( testObject.hooks.didAction( 'test.action' ) ).toBe( 2 ); | ||
expect( didAction( 'test.action' ) ).toBe( 2 ); | ||
expect( testObject.hooks.removeAllActions( 'test.action' ) ).toEqual( 1 ); | ||
expect( removeAllActions( 'test.action' ) ).toBe( 1 ); | ||
// Verify state is reset appropriately. | ||
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false ); | ||
expect( testObject.hooks.didAction( 'test.action' ) ).toBe( 2 ); | ||
expect( testObject.hooks.hasAction( 'test.action' ) ).toBe( 0 ); | ||
expect( doingAction( 'test.action' ) ).toBe( false ); | ||
expect( didAction( 'test.action' ) ).toBe( 2 ); | ||
expect( hasAction( 'test.action' ) ).toBe( 0 ); | ||
testObject.hooks.doAction( 'another.action' ); | ||
expect( testObject.hooks.doingAction( 'test.action' ) ).toBe( false ); | ||
doAction( 'another.action' ); | ||
expect( doingAction( 'test.action' ) ).toBe( false ); | ||
// Verify testObject.hooks.hasAction returns 0 when no matching action. | ||
expect( testObject.hooks.hasAction( 'notatest.action' ) ).toBe( 0 ); | ||
// Verify hasAction returns 0 when no matching action. | ||
expect( hasAction( 'notatest.action' ) ).toBe( 0 ); | ||
} ); | ||
test( 'Verify testObject.hooks.doingFilter, testObject.hooks.didFilter and testObject.hooks.hasFilter.', () => { | ||
test( 'Verify doingFilter, didFilter and hasFilter.', () => { | ||
let filterCalls = 0; | ||
testObject.hooks.addFilter( 'runtest.filter', 'my_callback', arg => { | ||
addFilter( 'runtest.filter', 'my_callback', arg => { | ||
filterCalls++; | ||
expect( testObject.hooks.currentFilter() ).toBe( 'runtest.filter' ); | ||
expect( testObject.hooks.doingFilter() ).toBe( true ); | ||
expect( testObject.hooks.doingFilter( 'runtest.filter' ) ).toBe( true ); | ||
expect( currentFilter() ).toBe( 'runtest.filter' ); | ||
expect( doingFilter() ).toBe( true ); | ||
expect( doingFilter( 'runtest.filter' ) ).toBe( true ); | ||
return arg; | ||
@@ -529,46 +558,46 @@ } ); | ||
// Verify filter added and running. | ||
const test = testObject.hooks.applyFilters( 'runtest.filter', 'someValue' ); | ||
const test = applyFilters( 'runtest.filter', 'someValue' ); | ||
expect( test ).toBe( 'someValue' ); | ||
expect( filterCalls ).toBe( 1 ); | ||
expect( testObject.hooks.didFilter( 'runtest.filter' ) ).toBe( 1 ); | ||
expect( testObject.hooks.hasFilter( 'runtest.filter' ) ).toBe( 1 ); | ||
expect( testObject.hooks.hasFilter( 'notatest.filter' ) ).toBe( 0 ); | ||
expect( testObject.hooks.doingFilter() ).toBe( false ); | ||
expect( testObject.hooks.doingFilter( 'runtest.filter' ) ).toBe( false ); | ||
expect( testObject.hooks.doingFilter( 'notatest.filter' ) ).toBe( false ); | ||
expect( testObject.hooks.currentFilter() ).toBe( null ); | ||
expect( didFilter( 'runtest.filter' ) ).toBe( 1 ); | ||
expect( hasFilter( 'runtest.filter' ) ).toBe( 1 ); | ||
expect( hasFilter( 'notatest.filter' ) ).toBe( 0 ); | ||
expect( doingFilter() ).toBe( false ); | ||
expect( doingFilter( 'runtest.filter' ) ).toBe( false ); | ||
expect( doingFilter( 'notatest.filter' ) ).toBe( false ); | ||
expect( currentFilter() ).toBe( null ); | ||
expect( testObject.hooks.removeAllFilters( 'runtest.filter' ) ).toEqual( 1 ); | ||
expect( removeAllFilters( 'runtest.filter' ) ).toBe( 1 ); | ||
expect( testObject.hooks.hasFilter( 'runtest.filter' ) ).toBe( 0 ); | ||
expect( testObject.hooks.didFilter( 'runtest.filter' ) ).toBe( 1 ); | ||
expect( hasFilter( 'runtest.filter' ) ).toBe( 0 ); | ||
expect( didFilter( 'runtest.filter' ) ).toBe( 1 ); | ||
} ); | ||
test( 'recursively calling a filter', () => { | ||
testObject.hooks.addFilter( 'test.filter', 'my_callback', value => { | ||
addFilter( 'test.filter', 'my_callback', value => { | ||
if ( value.length === 7 ) { | ||
return value; | ||
} | ||
return testObject.hooks.applyFilters( 'test.filter', value + 'X' ); | ||
return applyFilters( 'test.filter', value + 'X' ); | ||
} ); | ||
expect( testObject.hooks.applyFilters( 'test.filter', 'test' ) ).toBe( 'testXXX' ); | ||
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testXXX' ); | ||
} ); | ||
test( 'current filter when multiple filters are running', () => { | ||
testObject.hooks.addFilter( 'test.filter1', 'my_callback', value => { | ||
return testObject.hooks.applyFilters( 'test.filter2', value.concat( testObject.hooks.currentFilter() ) ); | ||
addFilter( 'test.filter1', 'my_callback', value => { | ||
return applyFilters( 'test.filter2', value.concat( currentFilter() ) ); | ||
} ); | ||
testObject.hooks.addFilter( 'test.filter2', 'my_callback', value => { | ||
return value.concat( testObject.hooks.currentFilter() ); | ||
addFilter( 'test.filter2', 'my_callback', value => { | ||
return value.concat( currentFilter() ); | ||
} ); | ||
expect( testObject.hooks.currentFilter() ).toBe( null ); | ||
expect( currentFilter() ).toBe( null ); | ||
expect( testObject.hooks.applyFilters( 'test.filter1', [ 'test' ] ) ).toEqual( | ||
expect( applyFilters( 'test.filter1', [ 'test' ] ) ).toEqual( | ||
[ 'test', 'test.filter1', 'test.filter2' ] | ||
); | ||
expect( testObject.hooks.currentFilter() ).toBe( null ); | ||
expect( currentFilter() ).toBe( null ); | ||
} ); | ||
@@ -578,34 +607,30 @@ | ||
function removeRecurseAndAdd2( val ) { | ||
expect( testObject.hooks.removeFilter( 'remove_and_add', 'my_callback_recurse' ) ).toEqual( 1 ); | ||
val += '-' + testObject.hooks.applyFilters( 'remove_and_add', '' ) + '-'; | ||
testObject.hooks.addFilter( 'remove_and_add', 'my_callback_recurse', 10 ); | ||
expect( removeFilter( 'remove_and_add', 'my_callback_recurse' ) ).toBe( 1 ); | ||
val += '-' + applyFilters( 'remove_and_add', '' ) + '-'; | ||
addFilter( 'remove_and_add', 'my_callback_recurse', 10 ); | ||
return val + '2'; | ||
} | ||
testObject.hooks.addFilter( 'remove_and_add', 'my_callback', val => val + '1', 11 ); | ||
testObject.hooks.addFilter( 'remove_and_add', 'my_callback_recurse', removeRecurseAndAdd2, 12 ); | ||
testObject.hooks.addFilter( 'remove_and_add', 'my_callback', val => val + '3', 13 ); | ||
testObject.hooks.addFilter( 'remove_and_add', 'my_callback', val => val + '4', 14 ); | ||
addFilter( 'remove_and_add', 'my_callback', val => val + '1', 11 ); | ||
addFilter( 'remove_and_add', 'my_callback_recurse', removeRecurseAndAdd2, 12 ); | ||
addFilter( 'remove_and_add', 'my_callback', val => val + '3', 13 ); | ||
addFilter( 'remove_and_add', 'my_callback', val => val + '4', 14 ); | ||
expect( testObject.hooks.applyFilters( 'remove_and_add', '' ) ).toEqual( '1-134-234' ); | ||
expect( applyFilters( 'remove_and_add', '' ) ).toBe( '1-134-234' ); | ||
} ); | ||
// Test adding via composition. | ||
test( 'adding hooks via composition', () => { | ||
const testObject = {}; | ||
testObject.hooks = createHooks(); | ||
var testObject2 = {}; | ||
testObject2.hooks = createHooks(); | ||
expect( typeof testObject2.hooks.applyFilters ).toEqual( 'function' ); | ||
expect( typeof testObject.hooks.applyFilters ).toBe( 'function' ); | ||
} ); | ||
// Test adding as a mixin. | ||
test( 'adding hooks as a mixin', () => { | ||
const testObject = {}; | ||
Object.assign( testObject, createHooks() ); | ||
var testObject3 = {}; | ||
Object.assign( testObject3, createHooks() ); | ||
expect( typeof testObject3.applyFilters ).toEqual( 'function' ); | ||
expect( typeof testObject.applyFilters ).toBe( 'function' ); | ||
} ); | ||
@@ -615,17 +640,14 @@ | ||
test( 'Test `this` context via composition', () => { | ||
const testObject = { test: 'test this' }; | ||
var testObject2 = { test: 'test this' }; | ||
testObject.hooks = createHooks(); | ||
testObject2.hooks = createHooks(); | ||
var theCallback = function() { | ||
expect( this.test ).toEqual( 'test this' ); | ||
const theCallback = function() { | ||
expect( this.test ).toBe( 'test this' ); | ||
}; | ||
testObject.hooks.addAction( 'test.action', 'my_callback', theCallback.apply( testObject2 ) ); | ||
testObject.hooks.doAction( 'test.action' ); | ||
addAction( 'test.action', 'my_callback', theCallback.apply( testObject ) ); | ||
doAction( 'test.action' ); | ||
var testObject3 = {}; | ||
Object.assign( testObject3, createHooks() ); | ||
const testObject2 = {}; | ||
Object.assign( testObject2, createHooks() ); | ||
} ); | ||
@@ -16,4 +16,4 @@ /** | ||
if ( ! /^[a-zA-Z][a-zA-Z0-9_.\-]*$/.test( namespace ) ) { | ||
console.error( 'The namespace can only contain numbers, letters, dashes, periods and underscores.' ); | ||
if ( ! /^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test( namespace ) ) { | ||
console.error( 'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.' ); | ||
return false; | ||
@@ -20,0 +20,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
83058
38
2211
1
45