@wordpress/hooks 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +72 -0
- package/LICENSE.md +788 -0
- package/README.md +66 -0
- package/benchmark/index.js +56 -0
- package/build/createAddHook.js +114 -0
- package/build/createAddHook.js.map +1 -0
- package/build/createCurrentHook.js +29 -0
- package/build/createCurrentHook.js.map +1 -0
- package/build/createDidHook.js +49 -0
- package/build/createDidHook.js.map +1 -0
- package/build/createDoingHook.js +43 -0
- package/build/createDoingHook.js.map +1 -0
- package/build/createHasHook.js +44 -0
- package/build/createHasHook.js.map +1 -0
- package/build/createHooks.js +80 -0
- package/build/createHooks.js.map +1 -0
- package/build/createRemoveHook.js +99 -0
- package/build/createRemoveHook.js.map +1 -0
- package/build/createRunHook.js +73 -0
- package/build/createRunHook.js.map +1 -0
- package/build/index.js +94 -0
- package/build/index.js.map +1 -0
- package/build/validateHookName.js +41 -0
- package/build/validateHookName.js.map +1 -0
- package/build/validateNamespace.js +34 -0
- package/build/validateNamespace.js.map +1 -0
- package/build-module/createAddHook.js +102 -0
- package/build-module/createAddHook.js.map +1 -0
- package/build-module/createCurrentHook.js +21 -0
- package/build-module/createCurrentHook.js.map +1 -0
- package/build-module/createDidHook.js +38 -0
- package/build-module/createDidHook.js.map +1 -0
- package/build-module/createDoingHook.js +35 -0
- package/build-module/createDoingHook.js.map +1 -0
- package/build-module/createHasHook.js +36 -0
- package/build-module/createHasHook.js.map +1 -0
- package/build-module/createHooks.js +60 -0
- package/build-module/createHooks.js.map +1 -0
- package/build-module/createRemoveHook.js +87 -0
- package/build-module/createRemoveHook.js.map +1 -0
- package/build-module/createRunHook.js +65 -0
- package/build-module/createRunHook.js.map +1 -0
- package/build-module/index.js +60 -0
- package/build-module/index.js.map +1 -0
- package/build-module/validateHookName.js +33 -0
- package/build-module/validateHookName.js.map +1 -0
- package/build-module/validateNamespace.js +26 -0
- package/build-module/validateNamespace.js.map +1 -0
- package/build-types/createAddHook.d.ts +25 -0
- package/build-types/createAddHook.d.ts.map +1 -0
- package/build-types/createCurrentHook.d.ts +13 -0
- package/build-types/createCurrentHook.d.ts.map +1 -0
- package/build-types/createDidHook.d.ts +25 -0
- package/build-types/createDidHook.d.ts.map +1 -0
- package/build-types/createDoingHook.d.ts +26 -0
- package/build-types/createDoingHook.d.ts.map +1 -0
- package/build-types/createHasHook.d.ts +28 -0
- package/build-types/createHasHook.d.ts.map +1 -0
- package/build-types/createHooks.d.ts +39 -0
- package/build-types/createHooks.d.ts.map +1 -0
- package/build-types/createRemoveHook.d.ts +31 -0
- package/build-types/createRemoveHook.d.ts.map +1 -0
- package/build-types/createRunHook.d.ts +15 -0
- package/build-types/createRunHook.d.ts.map +1 -0
- package/build-types/index.d.ts +88 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/validateHookName.d.ts +12 -0
- package/build-types/validateHookName.d.ts.map +1 -0
- package/build-types/validateNamespace.d.ts +11 -0
- package/build-types/validateNamespace.d.ts.map +1 -0
- package/package.json +35 -0
- package/src/createAddHook.js +107 -0
- package/src/createCurrentHook.js +22 -0
- package/src/createDidHook.js +39 -0
- package/src/createDoingHook.js +37 -0
- package/src/createHasHook.js +40 -0
- package/src/createHooks.js +59 -0
- package/src/createRemoveHook.js +88 -0
- package/src/createRunHook.js +66 -0
- package/src/index.js +82 -0
- package/src/test/index.test.js +945 -0
- package/src/validateHookName.js +34 -0
- package/src/validateNamespace.js +27 -0
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,945 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
createHooks,
|
|
6
|
+
addAction,
|
|
7
|
+
addFilter,
|
|
8
|
+
removeAction,
|
|
9
|
+
removeFilter,
|
|
10
|
+
hasAction,
|
|
11
|
+
hasFilter,
|
|
12
|
+
removeAllActions,
|
|
13
|
+
removeAllFilters,
|
|
14
|
+
doAction,
|
|
15
|
+
applyFilters,
|
|
16
|
+
currentAction,
|
|
17
|
+
currentFilter,
|
|
18
|
+
doingAction,
|
|
19
|
+
doingFilter,
|
|
20
|
+
didAction,
|
|
21
|
+
didFilter,
|
|
22
|
+
actions,
|
|
23
|
+
filters,
|
|
24
|
+
} from '..';
|
|
25
|
+
|
|
26
|
+
function filterA( str ) {
|
|
27
|
+
return str + 'a';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function filterB( str ) {
|
|
31
|
+
return str + 'b';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function filterC( str ) {
|
|
35
|
+
return str + 'c';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function filterCRemovesSelf( str ) {
|
|
39
|
+
removeFilter( 'test.filter', 'my_callback_filter_c_removes_self' );
|
|
40
|
+
return str + 'b';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function filterRemovesB( str ) {
|
|
44
|
+
removeFilter( 'test.filter', 'my_callback_filter_b' );
|
|
45
|
+
return str;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function filterRemovesC( str ) {
|
|
49
|
+
removeFilter( 'test.filter', 'my_callback_filter_c' );
|
|
50
|
+
return str;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function actionA() {
|
|
54
|
+
window.actionValue += 'a';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function actionB() {
|
|
58
|
+
window.actionValue += 'b';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function actionC() {
|
|
62
|
+
window.actionValue += 'c';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
beforeEach( () => {
|
|
66
|
+
window.actionValue = '';
|
|
67
|
+
// Reset state in between tests (clear all callbacks, `didAction` counts,
|
|
68
|
+
// etc.) Just reseting actions and filters is not enough
|
|
69
|
+
// because the internal functions have references to the original objects.
|
|
70
|
+
[ actions, filters ].forEach( ( hooks ) => {
|
|
71
|
+
for ( const k in hooks ) {
|
|
72
|
+
if ( '__current' === k ) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
delete hooks[ k ];
|
|
77
|
+
}
|
|
78
|
+
delete hooks.all;
|
|
79
|
+
} );
|
|
80
|
+
} );
|
|
81
|
+
|
|
82
|
+
test( 'hooks can be instantiated', () => {
|
|
83
|
+
const hooks = createHooks();
|
|
84
|
+
|
|
85
|
+
expect( typeof hooks ).toBe( 'object' );
|
|
86
|
+
} );
|
|
87
|
+
|
|
88
|
+
test( 'run a filter with no callbacks', () => {
|
|
89
|
+
expect( applyFilters( 'test.filter', 42 ) ).toBe( 42 );
|
|
90
|
+
} );
|
|
91
|
+
|
|
92
|
+
test( 'add and remove a filter', () => {
|
|
93
|
+
addFilter( 'test.filter', 'my_callback', filterA );
|
|
94
|
+
expect( removeAllFilters( 'test.filter' ) ).toBe( 1 );
|
|
95
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'test' );
|
|
96
|
+
expect( removeAllFilters( 'test.filter' ) ).toBe( 0 );
|
|
97
|
+
} );
|
|
98
|
+
|
|
99
|
+
test( 'add a filter and run it', () => {
|
|
100
|
+
addFilter( 'test.filter', 'my_callback', filterA );
|
|
101
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testa' );
|
|
102
|
+
} );
|
|
103
|
+
|
|
104
|
+
test( 'add 2 filters in a row and run them', () => {
|
|
105
|
+
addFilter( 'test.filter', 'my_callback', filterA );
|
|
106
|
+
addFilter( 'test.filter', 'my_callback', filterB );
|
|
107
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' );
|
|
108
|
+
} );
|
|
109
|
+
|
|
110
|
+
test( 'remove a non-existent filter', () => {
|
|
111
|
+
expect( removeFilter( 'test.filter', 'my_callback', filterA ) ).toBe( 0 );
|
|
112
|
+
expect( removeAllFilters( 'test.filter' ) ).toBe( 0 );
|
|
113
|
+
} );
|
|
114
|
+
|
|
115
|
+
test( 'remove an invalid namespace from a filter', () => {
|
|
116
|
+
expect( removeFilter( 'test.filter', 42 ) ).toBe( undefined );
|
|
117
|
+
expect( console ).toHaveErroredWith(
|
|
118
|
+
'The namespace must be a non-empty string.'
|
|
119
|
+
);
|
|
120
|
+
} );
|
|
121
|
+
|
|
122
|
+
test( 'cannot add filters with non-string hook names', () => {
|
|
123
|
+
addFilter( 42, 'my_callback', () => null );
|
|
124
|
+
expect( console ).toHaveErroredWith(
|
|
125
|
+
'The hook name must be a non-empty string.'
|
|
126
|
+
);
|
|
127
|
+
} );
|
|
128
|
+
|
|
129
|
+
test( 'cannot add filters with empty-string hook names', () => {
|
|
130
|
+
addFilter( '', 'my_callback', () => null );
|
|
131
|
+
expect( console ).toHaveErroredWith(
|
|
132
|
+
'The hook name must be a non-empty string.'
|
|
133
|
+
);
|
|
134
|
+
} );
|
|
135
|
+
|
|
136
|
+
test( 'cannot add filters with empty-string namespaces', () => {
|
|
137
|
+
addFilter( 'hook_name', '', () => null );
|
|
138
|
+
expect( console ).toHaveErroredWith(
|
|
139
|
+
'The namespace must be a non-empty string.'
|
|
140
|
+
);
|
|
141
|
+
} );
|
|
142
|
+
|
|
143
|
+
test( 'cannot add filters with invalid namespaces', () => {
|
|
144
|
+
addFilter( 'hook_name', 'invalid_%&name', () => null );
|
|
145
|
+
expect( console ).toHaveErroredWith(
|
|
146
|
+
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'
|
|
147
|
+
);
|
|
148
|
+
} );
|
|
149
|
+
|
|
150
|
+
test( 'cannot add filters with namespaces starting with a slash', () => {
|
|
151
|
+
addFilter( 'hook_name', '/invalid_name', () => null );
|
|
152
|
+
expect( console ).toHaveErroredWith(
|
|
153
|
+
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'
|
|
154
|
+
);
|
|
155
|
+
} );
|
|
156
|
+
|
|
157
|
+
test( 'Can add filters with dashes in namespaces', () => {
|
|
158
|
+
addFilter( 'hook_name', 'with-dashes', () => null );
|
|
159
|
+
expect( console ).not.toHaveErrored();
|
|
160
|
+
} );
|
|
161
|
+
|
|
162
|
+
test( 'Can add filters with capitals in namespaces', () => {
|
|
163
|
+
addFilter( 'hook_name', 'My_Name-OhNoaction', () => null );
|
|
164
|
+
expect( console ).not.toHaveErrored();
|
|
165
|
+
} );
|
|
166
|
+
|
|
167
|
+
test( 'Can add filters with slashes in namespaces', () => {
|
|
168
|
+
addFilter( 'hook_name', 'my/name/action', () => null );
|
|
169
|
+
expect( console ).not.toHaveErrored();
|
|
170
|
+
} );
|
|
171
|
+
|
|
172
|
+
test( 'Can add filters with periods in namespaces', () => {
|
|
173
|
+
addFilter( 'hook_name', 'my.name.action', () => null );
|
|
174
|
+
expect( console ).not.toHaveErrored();
|
|
175
|
+
} );
|
|
176
|
+
|
|
177
|
+
test( 'Can add filters with capitals in hookName', () => {
|
|
178
|
+
addFilter( 'hookName', 'action', () => null );
|
|
179
|
+
expect( console ).not.toHaveErrored();
|
|
180
|
+
} );
|
|
181
|
+
|
|
182
|
+
test( 'Can add filters with periods in hookName', () => {
|
|
183
|
+
addFilter( 'hook.name', 'action', () => null );
|
|
184
|
+
expect( console ).not.toHaveErrored();
|
|
185
|
+
} );
|
|
186
|
+
|
|
187
|
+
test( 'cannot add filters with namespace containing backslash', () => {
|
|
188
|
+
addFilter( 'hook_name', 'i\n\valid\name', () => null );
|
|
189
|
+
expect( console ).toHaveErroredWith(
|
|
190
|
+
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'
|
|
191
|
+
);
|
|
192
|
+
} );
|
|
193
|
+
|
|
194
|
+
test( 'cannot add filters named with __ prefix', () => {
|
|
195
|
+
addFilter( '__test', 'my_callback', () => null );
|
|
196
|
+
expect( console ).toHaveErroredWith(
|
|
197
|
+
'The hook name cannot begin with `__`.'
|
|
198
|
+
);
|
|
199
|
+
} );
|
|
200
|
+
|
|
201
|
+
test( 'cannot add filters with non-function callbacks', () => {
|
|
202
|
+
addFilter( 'test', 'my_callback', '42' );
|
|
203
|
+
expect( console ).toHaveErroredWith(
|
|
204
|
+
'The hook callback must be a function.'
|
|
205
|
+
);
|
|
206
|
+
} );
|
|
207
|
+
|
|
208
|
+
test( 'cannot add filters with non-numeric priorities', () => {
|
|
209
|
+
addFilter( 'test', 'my_callback', () => null, '42' );
|
|
210
|
+
expect( console ).toHaveErroredWith(
|
|
211
|
+
'If specified, the hook priority must be a number.'
|
|
212
|
+
);
|
|
213
|
+
} );
|
|
214
|
+
|
|
215
|
+
test( 'add 3 filters with different priorities and run them', () => {
|
|
216
|
+
addFilter( 'test.filter', 'my_callback_filter_a', filterA );
|
|
217
|
+
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 2 );
|
|
218
|
+
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 8 );
|
|
219
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testbca' );
|
|
220
|
+
} );
|
|
221
|
+
|
|
222
|
+
test( 'filters with the same and different priorities', () => {
|
|
223
|
+
const callbacks = {};
|
|
224
|
+
|
|
225
|
+
[ 1, 2, 3, 4 ].forEach( ( priority ) => {
|
|
226
|
+
[ 'a', 'b', 'c', 'd' ].forEach( ( string ) => {
|
|
227
|
+
callbacks[ 'fn_' + priority + string ] = ( value ) => {
|
|
228
|
+
return value.concat( priority + string );
|
|
229
|
+
};
|
|
230
|
+
} );
|
|
231
|
+
} );
|
|
232
|
+
|
|
233
|
+
addFilter( 'test_order', 'my_callback_fn_3a', callbacks.fn_3a, 3 );
|
|
234
|
+
addFilter( 'test_order', 'my_callback_fn_3b', callbacks.fn_3b, 3 );
|
|
235
|
+
addFilter( 'test_order', 'my_callback_fn_3c', callbacks.fn_3c, 3 );
|
|
236
|
+
addFilter( 'test_order', 'my_callback_fn_2a', callbacks.fn_2a, 2 );
|
|
237
|
+
addFilter( 'test_order', 'my_callback_fn_2b', callbacks.fn_2b, 2 );
|
|
238
|
+
addFilter( 'test_order', 'my_callback_fn_2c', callbacks.fn_2c, 2 );
|
|
239
|
+
|
|
240
|
+
expect( applyFilters( 'test_order', [] ) ).toEqual( [
|
|
241
|
+
'2a',
|
|
242
|
+
'2b',
|
|
243
|
+
'2c',
|
|
244
|
+
'3a',
|
|
245
|
+
'3b',
|
|
246
|
+
'3c',
|
|
247
|
+
] );
|
|
248
|
+
|
|
249
|
+
removeFilter( 'test_order', 'my_callback_fn_2b', callbacks.fn_2b );
|
|
250
|
+
removeFilter( 'test_order', 'my_callback_fn_3a', callbacks.fn_3a );
|
|
251
|
+
|
|
252
|
+
expect( applyFilters( 'test_order', [] ) ).toEqual( [
|
|
253
|
+
'2a',
|
|
254
|
+
'2c',
|
|
255
|
+
'3b',
|
|
256
|
+
'3c',
|
|
257
|
+
] );
|
|
258
|
+
|
|
259
|
+
addFilter( 'test_order', 'my_callback_fn_4a', callbacks.fn_4a, 4 );
|
|
260
|
+
addFilter( 'test_order', 'my_callback_fn_4b', callbacks.fn_4b, 4 );
|
|
261
|
+
addFilter( 'test_order', 'my_callback_fn_1a', callbacks.fn_1a, 1 );
|
|
262
|
+
addFilter( 'test_order', 'my_callback_fn_4c', callbacks.fn_4c, 4 );
|
|
263
|
+
addFilter( 'test_order', 'my_callback_fn_1b', callbacks.fn_1b, 1 );
|
|
264
|
+
addFilter( 'test_order', 'my_callback_fn_3d', callbacks.fn_3d, 3 );
|
|
265
|
+
addFilter( 'test_order', 'my_callback_fn_4d', callbacks.fn_4d, 4 );
|
|
266
|
+
addFilter( 'test_order', 'my_callback_fn_1c', callbacks.fn_1c, 1 );
|
|
267
|
+
addFilter( 'test_order', 'my_callback_fn_2d', callbacks.fn_2d, 2 );
|
|
268
|
+
addFilter( 'test_order', 'my_callback_fn_1d', callbacks.fn_1d, 1 );
|
|
269
|
+
|
|
270
|
+
expect( applyFilters( 'test_order', [] ) ).toEqual( [
|
|
271
|
+
// all except 2b and 3a, which we removed earlier
|
|
272
|
+
'1a',
|
|
273
|
+
'1b',
|
|
274
|
+
'1c',
|
|
275
|
+
'1d',
|
|
276
|
+
'2a',
|
|
277
|
+
'2c',
|
|
278
|
+
'2d',
|
|
279
|
+
'3b',
|
|
280
|
+
'3c',
|
|
281
|
+
'3d',
|
|
282
|
+
'4a',
|
|
283
|
+
'4b',
|
|
284
|
+
'4c',
|
|
285
|
+
'4d',
|
|
286
|
+
] );
|
|
287
|
+
} );
|
|
288
|
+
|
|
289
|
+
test( 'add and remove an action', () => {
|
|
290
|
+
addAction( 'test.action', 'my_callback', actionA );
|
|
291
|
+
expect( removeAllActions( 'test.action' ) ).toBe( 1 );
|
|
292
|
+
expect( doAction( 'test.action' ) ).toBe( undefined );
|
|
293
|
+
expect( window.actionValue ).toBe( '' );
|
|
294
|
+
} );
|
|
295
|
+
|
|
296
|
+
test( 'add an action and run it', () => {
|
|
297
|
+
addAction( 'test.action', 'my_callback', actionA );
|
|
298
|
+
doAction( 'test.action' );
|
|
299
|
+
expect( window.actionValue ).toBe( 'a' );
|
|
300
|
+
} );
|
|
301
|
+
|
|
302
|
+
test( 'add 2 actions in a row and then run them', () => {
|
|
303
|
+
addAction( 'test.action', 'my_callback', actionA );
|
|
304
|
+
addAction( 'test.action', 'my_callback', actionB );
|
|
305
|
+
doAction( 'test.action' );
|
|
306
|
+
expect( window.actionValue ).toBe( 'ab' );
|
|
307
|
+
} );
|
|
308
|
+
|
|
309
|
+
test( 'add 3 actions with different priorities and run them', () => {
|
|
310
|
+
addAction( 'test.action', 'my_callback', actionA );
|
|
311
|
+
addAction( 'test.action', 'my_callback', actionB, 2 );
|
|
312
|
+
addAction( 'test.action', 'my_callback', actionC, 8 );
|
|
313
|
+
doAction( 'test.action' );
|
|
314
|
+
expect( window.actionValue ).toBe( 'bca' );
|
|
315
|
+
} );
|
|
316
|
+
|
|
317
|
+
test( 'pass in two arguments to an action', () => {
|
|
318
|
+
const arg1 = { a: 10 };
|
|
319
|
+
const arg2 = { b: 20 };
|
|
320
|
+
|
|
321
|
+
addAction( 'test.action', 'my_callback', ( a, b ) => {
|
|
322
|
+
expect( a ).toBe( arg1 );
|
|
323
|
+
expect( b ).toBe( arg2 );
|
|
324
|
+
} );
|
|
325
|
+
doAction( 'test.action', arg1, arg2 );
|
|
326
|
+
} );
|
|
327
|
+
|
|
328
|
+
test( 'fire action multiple times', () => {
|
|
329
|
+
expect.assertions( 2 );
|
|
330
|
+
|
|
331
|
+
function func() {
|
|
332
|
+
expect( true ).toBe( true );
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
addAction( 'test.action', 'my_callback', func );
|
|
336
|
+
doAction( 'test.action' );
|
|
337
|
+
doAction( 'test.action' );
|
|
338
|
+
} );
|
|
339
|
+
|
|
340
|
+
test( 'add a filter before the one currently executing', () => {
|
|
341
|
+
addFilter(
|
|
342
|
+
'test.filter',
|
|
343
|
+
'my_callback',
|
|
344
|
+
( outerValue ) => {
|
|
345
|
+
addFilter(
|
|
346
|
+
'test.filter',
|
|
347
|
+
'my_callback',
|
|
348
|
+
( innerValue ) => innerValue + 'a',
|
|
349
|
+
1
|
|
350
|
+
);
|
|
351
|
+
return outerValue + 'b';
|
|
352
|
+
},
|
|
353
|
+
2
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
expect( applyFilters( 'test.filter', 'test_' ) ).toBe( 'test_b' );
|
|
357
|
+
} );
|
|
358
|
+
|
|
359
|
+
test( 'add a filter after the one currently executing', () => {
|
|
360
|
+
addFilter(
|
|
361
|
+
'test.filter',
|
|
362
|
+
'my_callback',
|
|
363
|
+
( outerValue ) => {
|
|
364
|
+
addFilter(
|
|
365
|
+
'test.filter',
|
|
366
|
+
'my_callback',
|
|
367
|
+
( innerValue ) => innerValue + 'b',
|
|
368
|
+
2
|
|
369
|
+
);
|
|
370
|
+
return outerValue + 'a';
|
|
371
|
+
},
|
|
372
|
+
1
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
expect( applyFilters( 'test.filter', 'test_' ) ).toBe( 'test_ab' );
|
|
376
|
+
} );
|
|
377
|
+
|
|
378
|
+
test( 'add a filter immediately after the one currently executing', () => {
|
|
379
|
+
addFilter(
|
|
380
|
+
'test.filter',
|
|
381
|
+
'my_callback',
|
|
382
|
+
( outerValue ) => {
|
|
383
|
+
addFilter(
|
|
384
|
+
'test.filter',
|
|
385
|
+
'my_callback',
|
|
386
|
+
( innerValue ) => innerValue + 'b',
|
|
387
|
+
1
|
|
388
|
+
);
|
|
389
|
+
return outerValue + 'a';
|
|
390
|
+
},
|
|
391
|
+
1
|
|
392
|
+
);
|
|
393
|
+
|
|
394
|
+
expect( applyFilters( 'test.filter', 'test_' ) ).toBe( 'test_ab' );
|
|
395
|
+
} );
|
|
396
|
+
|
|
397
|
+
test( 'remove specific action callback', () => {
|
|
398
|
+
addAction( 'test.action', 'my_callback_action_a', actionA );
|
|
399
|
+
addAction( 'test.action', 'my_callback_action_b', actionB, 2 );
|
|
400
|
+
addAction( 'test.action', 'my_callback_action_c', actionC, 8 );
|
|
401
|
+
|
|
402
|
+
expect( removeAction( 'test.action', 'my_callback_action_b' ) ).toBe( 1 );
|
|
403
|
+
doAction( 'test.action' );
|
|
404
|
+
expect( window.actionValue ).toBe( 'ca' );
|
|
405
|
+
} );
|
|
406
|
+
|
|
407
|
+
test( 'remove all action callbacks', () => {
|
|
408
|
+
addAction( 'test.action', 'my_callback_action_a', actionA );
|
|
409
|
+
addAction( 'test.action', 'my_callback_action_b', actionB, 2 );
|
|
410
|
+
addAction( 'test.action', 'my_callback_action_c', actionC, 8 );
|
|
411
|
+
|
|
412
|
+
expect( removeAllActions( 'test.action' ) ).toBe( 3 );
|
|
413
|
+
doAction( 'test.action' );
|
|
414
|
+
expect( window.actionValue ).toBe( '' );
|
|
415
|
+
} );
|
|
416
|
+
|
|
417
|
+
test( 'remove specific filter callback', () => {
|
|
418
|
+
addFilter( 'test.filter', 'my_callback_filter_a', filterA );
|
|
419
|
+
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 2 );
|
|
420
|
+
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 8 );
|
|
421
|
+
|
|
422
|
+
expect( removeFilter( 'test.filter', 'my_callback_filter_b' ) ).toBe( 1 );
|
|
423
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testca' );
|
|
424
|
+
} );
|
|
425
|
+
|
|
426
|
+
test( 'filter removes a callback that has already executed', () => {
|
|
427
|
+
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 );
|
|
428
|
+
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 3 );
|
|
429
|
+
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 5 );
|
|
430
|
+
addFilter(
|
|
431
|
+
'test.filter',
|
|
432
|
+
'my_callback_filter_removes_b',
|
|
433
|
+
filterRemovesB,
|
|
434
|
+
4
|
|
435
|
+
);
|
|
436
|
+
|
|
437
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' );
|
|
438
|
+
} );
|
|
439
|
+
|
|
440
|
+
test( 'filter removes a callback that has already executed (same priority)', () => {
|
|
441
|
+
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 );
|
|
442
|
+
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 2 );
|
|
443
|
+
addFilter(
|
|
444
|
+
'test.filter',
|
|
445
|
+
'my_callback_filter_removes_b',
|
|
446
|
+
filterRemovesB,
|
|
447
|
+
2
|
|
448
|
+
);
|
|
449
|
+
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 4 );
|
|
450
|
+
|
|
451
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' );
|
|
452
|
+
} );
|
|
453
|
+
|
|
454
|
+
test( 'filter removes the current callback', () => {
|
|
455
|
+
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 );
|
|
456
|
+
addFilter(
|
|
457
|
+
'test.filter',
|
|
458
|
+
'my_callback_filter_c_removes_self',
|
|
459
|
+
filterCRemovesSelf,
|
|
460
|
+
3
|
|
461
|
+
);
|
|
462
|
+
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 5 );
|
|
463
|
+
|
|
464
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testabc' );
|
|
465
|
+
} );
|
|
466
|
+
|
|
467
|
+
test( 'filter removes a callback that has not yet executed (last)', () => {
|
|
468
|
+
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 );
|
|
469
|
+
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 3 );
|
|
470
|
+
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 5 );
|
|
471
|
+
addFilter(
|
|
472
|
+
'test.filter',
|
|
473
|
+
'my_callback_filter_removes_c',
|
|
474
|
+
filterRemovesC,
|
|
475
|
+
4
|
|
476
|
+
);
|
|
477
|
+
|
|
478
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' );
|
|
479
|
+
} );
|
|
480
|
+
|
|
481
|
+
test( 'filter removes a callback that has not yet executed (middle)', () => {
|
|
482
|
+
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 );
|
|
483
|
+
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 3 );
|
|
484
|
+
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 4 );
|
|
485
|
+
addFilter(
|
|
486
|
+
'test.filter',
|
|
487
|
+
'my_callback_filter_removes_b',
|
|
488
|
+
filterRemovesB,
|
|
489
|
+
2
|
|
490
|
+
);
|
|
491
|
+
|
|
492
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' );
|
|
493
|
+
} );
|
|
494
|
+
|
|
495
|
+
test( 'filter removes a callback that has not yet executed (same priority)', () => {
|
|
496
|
+
addFilter( 'test.filter', 'my_callback_filter_a', filterA, 1 );
|
|
497
|
+
addFilter(
|
|
498
|
+
'test.filter',
|
|
499
|
+
'my_callback_filter_removes_b',
|
|
500
|
+
filterRemovesB,
|
|
501
|
+
2
|
|
502
|
+
);
|
|
503
|
+
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 2 );
|
|
504
|
+
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 4 );
|
|
505
|
+
|
|
506
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testac' );
|
|
507
|
+
} );
|
|
508
|
+
|
|
509
|
+
test( 'remove all filter callbacks', () => {
|
|
510
|
+
addFilter( 'test.filter', 'my_callback_filter_a', filterA );
|
|
511
|
+
addFilter( 'test.filter', 'my_callback_filter_b', filterB, 2 );
|
|
512
|
+
addFilter( 'test.filter', 'my_callback_filter_c', filterC, 8 );
|
|
513
|
+
|
|
514
|
+
expect( removeAllFilters( 'test.filter' ) ).toBe( 3 );
|
|
515
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'test' );
|
|
516
|
+
} );
|
|
517
|
+
|
|
518
|
+
// Test doingAction, didAction, hasAction.
|
|
519
|
+
test( 'doingAction, didAction and hasAction.', () => {
|
|
520
|
+
let actionCalls = 0;
|
|
521
|
+
|
|
522
|
+
addAction( 'another.action', 'my_callback', () => {} );
|
|
523
|
+
doAction( 'another.action' );
|
|
524
|
+
|
|
525
|
+
// Verify no action is running yet.
|
|
526
|
+
expect( doingAction( 'test.action' ) ).toBe( false );
|
|
527
|
+
|
|
528
|
+
expect( didAction( 'test.action' ) ).toBe( 0 );
|
|
529
|
+
expect( hasAction( 'test.action' ) ).toBe( false );
|
|
530
|
+
|
|
531
|
+
addAction( 'test.action', 'my_callback', () => {
|
|
532
|
+
actionCalls++;
|
|
533
|
+
expect( currentAction() ).toBe( 'test.action' );
|
|
534
|
+
expect( doingAction() ).toBe( true );
|
|
535
|
+
expect( doingAction( 'test.action' ) ).toBe( true );
|
|
536
|
+
} );
|
|
537
|
+
|
|
538
|
+
// Verify action added, not running yet.
|
|
539
|
+
expect( doingAction( 'test.action' ) ).toBe( false );
|
|
540
|
+
expect( didAction( 'test.action' ) ).toBe( 0 );
|
|
541
|
+
expect( hasAction( 'test.action' ) ).toBe( true );
|
|
542
|
+
|
|
543
|
+
doAction( 'test.action' );
|
|
544
|
+
|
|
545
|
+
// Verify action added and running.
|
|
546
|
+
expect( actionCalls ).toBe( 1 );
|
|
547
|
+
expect( doingAction( 'test.action' ) ).toBe( false );
|
|
548
|
+
expect( didAction( 'test.action' ) ).toBe( 1 );
|
|
549
|
+
expect( hasAction( 'test.action' ) ).toBe( true );
|
|
550
|
+
expect( doingAction() ).toBe( false );
|
|
551
|
+
expect( doingAction( 'test.action' ) ).toBe( false );
|
|
552
|
+
expect( doingAction( 'notatest.action' ) ).toBe( false );
|
|
553
|
+
expect( currentAction() ).toBe( null );
|
|
554
|
+
|
|
555
|
+
doAction( 'test.action' );
|
|
556
|
+
expect( actionCalls ).toBe( 2 );
|
|
557
|
+
expect( didAction( 'test.action' ) ).toBe( 2 );
|
|
558
|
+
|
|
559
|
+
expect( removeAllActions( 'test.action' ) ).toBe( 1 );
|
|
560
|
+
|
|
561
|
+
// Verify state is reset appropriately.
|
|
562
|
+
expect( doingAction( 'test.action' ) ).toBe( false );
|
|
563
|
+
expect( didAction( 'test.action' ) ).toBe( 2 );
|
|
564
|
+
expect( hasAction( 'test.action' ) ).toBe( true );
|
|
565
|
+
|
|
566
|
+
doAction( 'another.action' );
|
|
567
|
+
expect( doingAction( 'test.action' ) ).toBe( false );
|
|
568
|
+
|
|
569
|
+
// Verify an action with no handlers is still counted
|
|
570
|
+
expect( didAction( 'unattached.action' ) ).toBe( 0 );
|
|
571
|
+
doAction( 'unattached.action' );
|
|
572
|
+
expect( doingAction( 'unattached.action' ) ).toBe( false );
|
|
573
|
+
expect( didAction( 'unattached.action' ) ).toBe( 1 );
|
|
574
|
+
|
|
575
|
+
doAction( 'unattached.action' );
|
|
576
|
+
expect( doingAction( 'unattached.action' ) ).toBe( false );
|
|
577
|
+
expect( didAction( 'unattached.action' ) ).toBe( 2 );
|
|
578
|
+
|
|
579
|
+
// Verify hasAction returns 0 when no matching action.
|
|
580
|
+
expect( hasAction( 'notatest.action' ) ).toBe( false );
|
|
581
|
+
} );
|
|
582
|
+
|
|
583
|
+
test( 'Verify doingFilter, didFilter and hasFilter.', () => {
|
|
584
|
+
let filterCalls = 0;
|
|
585
|
+
|
|
586
|
+
addFilter( 'runtest.filter', 'my_callback', ( arg ) => {
|
|
587
|
+
filterCalls++;
|
|
588
|
+
expect( currentFilter() ).toBe( 'runtest.filter' );
|
|
589
|
+
expect( doingFilter() ).toBe( true );
|
|
590
|
+
expect( doingFilter( 'runtest.filter' ) ).toBe( true );
|
|
591
|
+
return arg;
|
|
592
|
+
} );
|
|
593
|
+
|
|
594
|
+
// Verify filter added and running.
|
|
595
|
+
const test = applyFilters( 'runtest.filter', 'someValue' );
|
|
596
|
+
expect( test ).toBe( 'someValue' );
|
|
597
|
+
expect( filterCalls ).toBe( 1 );
|
|
598
|
+
expect( didFilter( 'runtest.filter' ) ).toBe( 1 );
|
|
599
|
+
expect( hasFilter( 'runtest.filter' ) ).toBe( true );
|
|
600
|
+
expect( hasFilter( 'notatest.filter' ) ).toBe( false );
|
|
601
|
+
expect( doingFilter() ).toBe( false );
|
|
602
|
+
expect( doingFilter( 'runtest.filter' ) ).toBe( false );
|
|
603
|
+
expect( doingFilter( 'notatest.filter' ) ).toBe( false );
|
|
604
|
+
expect( currentFilter() ).toBe( null );
|
|
605
|
+
|
|
606
|
+
expect( removeAllFilters( 'runtest.filter' ) ).toBe( 1 );
|
|
607
|
+
|
|
608
|
+
expect( hasFilter( 'runtest.filter' ) ).toBe( true );
|
|
609
|
+
expect( didFilter( 'runtest.filter' ) ).toBe( 1 );
|
|
610
|
+
} );
|
|
611
|
+
|
|
612
|
+
test( 'recursively calling a filter', () => {
|
|
613
|
+
addFilter( 'test.filter', 'my_callback', ( value ) => {
|
|
614
|
+
if ( value.length === 7 ) {
|
|
615
|
+
return value;
|
|
616
|
+
}
|
|
617
|
+
return applyFilters( 'test.filter', value + 'X' );
|
|
618
|
+
} );
|
|
619
|
+
|
|
620
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testXXX' );
|
|
621
|
+
} );
|
|
622
|
+
|
|
623
|
+
test( 'current filter when multiple filters are running', () => {
|
|
624
|
+
addFilter( 'test.filter1', 'my_callback', ( value ) => {
|
|
625
|
+
return applyFilters( 'test.filter2', value.concat( currentFilter() ) );
|
|
626
|
+
} );
|
|
627
|
+
|
|
628
|
+
addFilter( 'test.filter2', 'my_callback', ( value ) => {
|
|
629
|
+
return value.concat( currentFilter() );
|
|
630
|
+
} );
|
|
631
|
+
|
|
632
|
+
expect( currentFilter() ).toBe( null );
|
|
633
|
+
|
|
634
|
+
expect( applyFilters( 'test.filter1', [ 'test' ] ) ).toEqual( [
|
|
635
|
+
'test',
|
|
636
|
+
'test.filter1',
|
|
637
|
+
'test.filter2',
|
|
638
|
+
] );
|
|
639
|
+
|
|
640
|
+
expect( currentFilter() ).toBe( null );
|
|
641
|
+
} );
|
|
642
|
+
|
|
643
|
+
test( 'adding and removing filters with recursion', () => {
|
|
644
|
+
function removeRecurseAndAdd2( val ) {
|
|
645
|
+
expect( removeFilter( 'remove_and_add', 'my_callback_recurse' ) ).toBe(
|
|
646
|
+
1
|
|
647
|
+
);
|
|
648
|
+
val += '-' + applyFilters( 'remove_and_add', '' ) + '-';
|
|
649
|
+
addFilter(
|
|
650
|
+
'remove_and_add',
|
|
651
|
+
'my_callback_recurse',
|
|
652
|
+
removeRecurseAndAdd2,
|
|
653
|
+
10
|
|
654
|
+
);
|
|
655
|
+
return val + '2';
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
addFilter( 'remove_and_add', 'my_callback', ( val ) => val + '1', 11 );
|
|
659
|
+
addFilter(
|
|
660
|
+
'remove_and_add',
|
|
661
|
+
'my_callback_recurse',
|
|
662
|
+
removeRecurseAndAdd2,
|
|
663
|
+
12
|
|
664
|
+
);
|
|
665
|
+
addFilter( 'remove_and_add', 'my_callback', ( val ) => val + '3', 13 );
|
|
666
|
+
addFilter( 'remove_and_add', 'my_callback', ( val ) => val + '4', 14 );
|
|
667
|
+
|
|
668
|
+
expect( applyFilters( 'remove_and_add', '' ) ).toBe( '1-134-234' );
|
|
669
|
+
} );
|
|
670
|
+
|
|
671
|
+
test( 'actions preserve arguments across handlers without return value', () => {
|
|
672
|
+
const arg1 = { a: 10 };
|
|
673
|
+
const arg2 = { b: 20 };
|
|
674
|
+
|
|
675
|
+
addAction( 'test.action', 'my_callback1', ( a, b ) => {
|
|
676
|
+
expect( a ).toBe( arg1 );
|
|
677
|
+
expect( b ).toBe( arg2 );
|
|
678
|
+
} );
|
|
679
|
+
|
|
680
|
+
addAction( 'test.action', 'my_callback2', ( a, b ) => {
|
|
681
|
+
expect( a ).toBe( arg1 );
|
|
682
|
+
expect( b ).toBe( arg2 );
|
|
683
|
+
} );
|
|
684
|
+
|
|
685
|
+
doAction( 'test.action', arg1, arg2 );
|
|
686
|
+
} );
|
|
687
|
+
|
|
688
|
+
test( 'filters pass first argument across handlers', () => {
|
|
689
|
+
addFilter( 'test.filter', 'my_callback1', ( count ) => count + 1 );
|
|
690
|
+
addFilter( 'test.filter', 'my_callback2', ( count ) => count + 1 );
|
|
691
|
+
|
|
692
|
+
const result = applyFilters( 'test.filter', 0 );
|
|
693
|
+
|
|
694
|
+
expect( result ).toBe( 2 );
|
|
695
|
+
} );
|
|
696
|
+
|
|
697
|
+
// Test adding via composition.
|
|
698
|
+
test( 'adding hooks via composition', () => {
|
|
699
|
+
const testObject = {};
|
|
700
|
+
testObject.hooks = createHooks();
|
|
701
|
+
|
|
702
|
+
expect( typeof testObject.hooks.applyFilters ).toBe( 'function' );
|
|
703
|
+
} );
|
|
704
|
+
|
|
705
|
+
// Test adding as a mixin.
|
|
706
|
+
test( 'adding hooks as a mixin', () => {
|
|
707
|
+
const testObject = {};
|
|
708
|
+
Object.assign( testObject, createHooks() );
|
|
709
|
+
|
|
710
|
+
expect( typeof testObject.applyFilters ).toBe( 'function' );
|
|
711
|
+
} );
|
|
712
|
+
|
|
713
|
+
// Test context.
|
|
714
|
+
test( '`this` context via composition', () => {
|
|
715
|
+
const testObject = { test: 'test this' };
|
|
716
|
+
|
|
717
|
+
testObject.hooks = createHooks();
|
|
718
|
+
|
|
719
|
+
const theCallback = function () {
|
|
720
|
+
expect( this.test ).toBe( 'test this' );
|
|
721
|
+
};
|
|
722
|
+
addAction( 'test.action', 'my_callback', theCallback.bind( testObject ) );
|
|
723
|
+
doAction( 'test.action' );
|
|
724
|
+
|
|
725
|
+
const testObject2 = {};
|
|
726
|
+
Object.assign( testObject2, createHooks() );
|
|
727
|
+
} );
|
|
728
|
+
|
|
729
|
+
const setupActionListener = ( hookName, callback ) =>
|
|
730
|
+
addAction( hookName, 'my_callback', callback );
|
|
731
|
+
|
|
732
|
+
test( 'adding an action triggers a hookAdded action passing all callback details', () => {
|
|
733
|
+
const hookAddedSpy = jest.fn();
|
|
734
|
+
|
|
735
|
+
setupActionListener( 'hookAdded', hookAddedSpy );
|
|
736
|
+
|
|
737
|
+
addAction( 'testAction', 'my_callback2', actionA, 9 );
|
|
738
|
+
expect( hookAddedSpy ).toHaveBeenCalledTimes( 1 );
|
|
739
|
+
expect( hookAddedSpy ).toHaveBeenCalledWith(
|
|
740
|
+
'testAction',
|
|
741
|
+
'my_callback2',
|
|
742
|
+
actionA,
|
|
743
|
+
9
|
|
744
|
+
);
|
|
745
|
+
|
|
746
|
+
// Private instance.
|
|
747
|
+
const hooksPrivateInstance = createHooks();
|
|
748
|
+
|
|
749
|
+
removeAction( 'hookAdded', 'my_callback' );
|
|
750
|
+
hookAddedSpy.mockClear();
|
|
751
|
+
|
|
752
|
+
hooksPrivateInstance.addAction( 'hookAdded', 'my_callback', hookAddedSpy );
|
|
753
|
+
hooksPrivateInstance.addAction( 'testAction', 'my_callback2', actionA, 9 );
|
|
754
|
+
|
|
755
|
+
expect( hookAddedSpy ).toHaveBeenCalledTimes( 1 );
|
|
756
|
+
expect( hookAddedSpy ).toHaveBeenCalledWith(
|
|
757
|
+
'testAction',
|
|
758
|
+
'my_callback2',
|
|
759
|
+
actionA,
|
|
760
|
+
9
|
|
761
|
+
);
|
|
762
|
+
} );
|
|
763
|
+
|
|
764
|
+
test( 'adding a filter triggers a hookAdded action passing all callback details', () => {
|
|
765
|
+
const hookAddedSpy = jest.fn();
|
|
766
|
+
|
|
767
|
+
setupActionListener( 'hookAdded', hookAddedSpy );
|
|
768
|
+
|
|
769
|
+
addFilter( 'testFilter', 'my_callback3', filterA, 8 );
|
|
770
|
+
expect( hookAddedSpy ).toHaveBeenCalledTimes( 1 );
|
|
771
|
+
expect( hookAddedSpy ).toHaveBeenCalledWith(
|
|
772
|
+
'testFilter',
|
|
773
|
+
'my_callback3',
|
|
774
|
+
filterA,
|
|
775
|
+
8
|
|
776
|
+
);
|
|
777
|
+
|
|
778
|
+
// Private instance.
|
|
779
|
+
const hooksPrivateInstance = createHooks();
|
|
780
|
+
|
|
781
|
+
removeAction( 'hookAdded', 'my_callback' );
|
|
782
|
+
hookAddedSpy.mockClear();
|
|
783
|
+
|
|
784
|
+
hooksPrivateInstance.addAction( 'hookAdded', 'my_callback', hookAddedSpy );
|
|
785
|
+
hooksPrivateInstance.addFilter( 'testFilter', 'my_callback3', filterA, 8 );
|
|
786
|
+
|
|
787
|
+
expect( hookAddedSpy ).toHaveBeenCalledTimes( 1 );
|
|
788
|
+
expect( hookAddedSpy ).toHaveBeenCalledWith(
|
|
789
|
+
'testFilter',
|
|
790
|
+
'my_callback3',
|
|
791
|
+
filterA,
|
|
792
|
+
8
|
|
793
|
+
);
|
|
794
|
+
} );
|
|
795
|
+
|
|
796
|
+
test( 'removing an action triggers a hookRemoved action passing all callback details', () => {
|
|
797
|
+
const hookRemovedSpy = jest.fn();
|
|
798
|
+
|
|
799
|
+
setupActionListener( 'hookRemoved', hookRemovedSpy );
|
|
800
|
+
|
|
801
|
+
addAction( 'testAction', 'my_callback2', actionA, 9 );
|
|
802
|
+
removeAction( 'testAction', 'my_callback2' );
|
|
803
|
+
|
|
804
|
+
expect( hookRemovedSpy ).toHaveBeenCalledTimes( 1 );
|
|
805
|
+
expect( hookRemovedSpy ).toHaveBeenCalledWith(
|
|
806
|
+
'testAction',
|
|
807
|
+
'my_callback2'
|
|
808
|
+
);
|
|
809
|
+
|
|
810
|
+
// Private instance.
|
|
811
|
+
const hooksPrivateInstance = createHooks();
|
|
812
|
+
|
|
813
|
+
removeAction( 'hookRemoved', 'my_callback' );
|
|
814
|
+
hookRemovedSpy.mockClear();
|
|
815
|
+
|
|
816
|
+
hooksPrivateInstance.addAction(
|
|
817
|
+
'hookRemoved',
|
|
818
|
+
'my_callback',
|
|
819
|
+
hookRemovedSpy
|
|
820
|
+
);
|
|
821
|
+
|
|
822
|
+
hooksPrivateInstance.addAction( 'testAction', 'my_callback2', actionA, 9 );
|
|
823
|
+
hooksPrivateInstance.removeAction( 'testAction', 'my_callback2' );
|
|
824
|
+
|
|
825
|
+
expect( hookRemovedSpy ).toHaveBeenCalledTimes( 1 );
|
|
826
|
+
expect( hookRemovedSpy ).toHaveBeenCalledWith(
|
|
827
|
+
'testAction',
|
|
828
|
+
'my_callback2'
|
|
829
|
+
);
|
|
830
|
+
} );
|
|
831
|
+
|
|
832
|
+
test( 'removing a filter triggers a hookRemoved action passing all callback details', () => {
|
|
833
|
+
const hookRemovedSpy = jest.fn();
|
|
834
|
+
|
|
835
|
+
setupActionListener( 'hookRemoved', hookRemovedSpy );
|
|
836
|
+
|
|
837
|
+
addFilter( 'testFilter', 'my_callback3', filterA, 8 );
|
|
838
|
+
removeFilter( 'testFilter', 'my_callback3' );
|
|
839
|
+
|
|
840
|
+
expect( hookRemovedSpy ).toHaveBeenCalledTimes( 1 );
|
|
841
|
+
expect( hookRemovedSpy ).toHaveBeenCalledWith(
|
|
842
|
+
'testFilter',
|
|
843
|
+
'my_callback3'
|
|
844
|
+
);
|
|
845
|
+
|
|
846
|
+
// Private instance.
|
|
847
|
+
const hooksPrivateInstance = createHooks();
|
|
848
|
+
|
|
849
|
+
removeAction( 'hookRemoved', 'my_callback' );
|
|
850
|
+
hookRemovedSpy.mockClear();
|
|
851
|
+
|
|
852
|
+
hooksPrivateInstance.addAction(
|
|
853
|
+
'hookRemoved',
|
|
854
|
+
'my_callback',
|
|
855
|
+
hookRemovedSpy
|
|
856
|
+
);
|
|
857
|
+
|
|
858
|
+
hooksPrivateInstance.addFilter( 'testFilter', 'my_callback3', filterA, 8 );
|
|
859
|
+
hooksPrivateInstance.removeFilter( 'testFilter', 'my_callback3' );
|
|
860
|
+
|
|
861
|
+
expect( hookRemovedSpy ).toHaveBeenCalledTimes( 1 );
|
|
862
|
+
expect( hookRemovedSpy ).toHaveBeenCalledWith(
|
|
863
|
+
'testFilter',
|
|
864
|
+
'my_callback3'
|
|
865
|
+
);
|
|
866
|
+
} );
|
|
867
|
+
|
|
868
|
+
test( 'add an all filter and run it any hook to trigger it', () => {
|
|
869
|
+
addFilter( 'all', 'my_callback', filterA );
|
|
870
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testa' );
|
|
871
|
+
expect( applyFilters( 'test.filter-anything', 'test' ) ).toBe( 'testa' );
|
|
872
|
+
} );
|
|
873
|
+
|
|
874
|
+
test( 'add an all action and run it any hook to trigger it', () => {
|
|
875
|
+
addAction( 'all', 'my_callback', actionA );
|
|
876
|
+
addAction( 'test.action', 'my_callback', actionA ); // Doesn't get triggered.
|
|
877
|
+
doAction( 'test.action-anything' );
|
|
878
|
+
expect( window.actionValue ).toBe( 'a' );
|
|
879
|
+
} );
|
|
880
|
+
|
|
881
|
+
test( 'add multiple all filters and run it any hook to trigger them', () => {
|
|
882
|
+
addFilter( 'all', 'my_callback', filterA );
|
|
883
|
+
addFilter( 'all', 'my_callback', filterB );
|
|
884
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testab' );
|
|
885
|
+
expect( applyFilters( 'test.filter-anything', 'test' ) ).toBe( 'testab' );
|
|
886
|
+
} );
|
|
887
|
+
|
|
888
|
+
test( 'add multiple all actions and run it any hook to trigger them', () => {
|
|
889
|
+
addAction( 'all', 'my_callback', actionA );
|
|
890
|
+
addAction( 'all', 'my_callback', actionB );
|
|
891
|
+
addAction( 'test.action', 'my_callback', actionA ); // Doesn't get triggered.
|
|
892
|
+
doAction( 'test.action-anything' );
|
|
893
|
+
expect( window.actionValue ).toBe( 'ab' );
|
|
894
|
+
} );
|
|
895
|
+
|
|
896
|
+
test( 'add multiple all filters and run it any hook to trigger them by priority', () => {
|
|
897
|
+
addFilter( 'all', 'my_callback', filterA, 11 );
|
|
898
|
+
addFilter( 'all', 'my_callback', filterB, 10 );
|
|
899
|
+
expect( applyFilters( 'test.filter', 'test' ) ).toBe( 'testba' );
|
|
900
|
+
expect( applyFilters( 'test.filter-anything', 'test' ) ).toBe( 'testba' );
|
|
901
|
+
} );
|
|
902
|
+
|
|
903
|
+
test( 'add multiple all actions and run it any hook to trigger them by priority', () => {
|
|
904
|
+
addAction( 'all', 'my_callback', actionA, 11 );
|
|
905
|
+
addAction( 'all', 'my_callback', actionB, 10 );
|
|
906
|
+
addAction( 'test.action', 'my_callback', actionA ); // Doesn't get triggered.
|
|
907
|
+
doAction( 'test.action-anything' );
|
|
908
|
+
expect( window.actionValue ).toBe( 'ba' );
|
|
909
|
+
} );
|
|
910
|
+
|
|
911
|
+
test( 'checking hasAction with named callbacks and removing', () => {
|
|
912
|
+
addAction( 'test.action', 'my_callback', () => {} );
|
|
913
|
+
expect( hasAction( 'test.action', 'not_my_callback' ) ).toBe( false );
|
|
914
|
+
expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true );
|
|
915
|
+
removeAction( 'test.action', 'my_callback' );
|
|
916
|
+
expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false );
|
|
917
|
+
} );
|
|
918
|
+
|
|
919
|
+
test( 'checking hasAction with named callbacks and removeAllActions', () => {
|
|
920
|
+
addAction( 'test.action', 'my_callback', () => {} );
|
|
921
|
+
addAction( 'test.action', 'my_second_callback', () => {} );
|
|
922
|
+
expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true );
|
|
923
|
+
expect( hasAction( 'test.action', 'my_callback' ) ).toBe( true );
|
|
924
|
+
removeAllActions( 'test.action' );
|
|
925
|
+
expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false );
|
|
926
|
+
expect( hasAction( 'test.action', 'my_callback' ) ).toBe( false );
|
|
927
|
+
} );
|
|
928
|
+
|
|
929
|
+
test( 'checking hasFilter with named callbacks and removing', () => {
|
|
930
|
+
addFilter( 'test.filter', 'my_callback', () => {} );
|
|
931
|
+
expect( hasFilter( 'test.filter', 'not_my_callback' ) ).toBe( false );
|
|
932
|
+
expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( true );
|
|
933
|
+
removeFilter( 'test.filter', 'my_callback' );
|
|
934
|
+
expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( false );
|
|
935
|
+
} );
|
|
936
|
+
|
|
937
|
+
test( 'checking hasFilter with named callbacks and removeAllActions', () => {
|
|
938
|
+
addFilter( 'test.filter', 'my_callback', () => {} );
|
|
939
|
+
addFilter( 'test.filter', 'my_second_callback', () => {} );
|
|
940
|
+
expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( true );
|
|
941
|
+
expect( hasFilter( 'test.filter', 'my_second_callback' ) ).toBe( true );
|
|
942
|
+
removeAllFilters( 'test.filter' );
|
|
943
|
+
expect( hasFilter( 'test.filter', 'my_callback' ) ).toBe( false );
|
|
944
|
+
expect( hasFilter( 'test.filter', 'my_second_callback' ) ).toBe( false );
|
|
945
|
+
} );
|