piral-search 1.3.3-beta.6190 → 1.3.3-beta.6204
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/package.json +3 -3
- package/src/actions.test.ts +56 -27
- package/src/create.test.ts +12 -8
- package/src/useDebounce.test.ts +26 -22
- package/src/useSearch.test.ts +71 -45
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-search",
|
|
3
|
-
"version": "1.3.3-beta.
|
|
3
|
+
"version": "1.3.3-beta.6204",
|
|
4
4
|
"description": "Plugin for centralizing search in Piral.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"piral",
|
|
@@ -62,8 +62,8 @@
|
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@types/react": "^18.0.0",
|
|
65
|
-
"piral-core": "1.3.3-beta.
|
|
65
|
+
"piral-core": "1.3.3-beta.6204",
|
|
66
66
|
"react": "^18.0.0"
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "8a1fa7b847b0e146a5bdb8e0497c436fffe4fd4d"
|
|
69
69
|
}
|
package/src/actions.test.ts
CHANGED
|
@@ -1,8 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
1
4
|
import create from 'zustand';
|
|
2
|
-
import {
|
|
3
|
-
import { createActions } from 'piral-core';
|
|
5
|
+
import { describe, it, expect, vitest } from 'vitest';
|
|
4
6
|
import { createActions as createSearchActions } from './actions';
|
|
5
7
|
|
|
8
|
+
function createListener() {
|
|
9
|
+
return {
|
|
10
|
+
on: vitest.fn(),
|
|
11
|
+
off: vitest.fn(),
|
|
12
|
+
emit: vitest.fn(),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function createActions(state, listener) {
|
|
17
|
+
const obj = {
|
|
18
|
+
...listener,
|
|
19
|
+
state: state.getState(),
|
|
20
|
+
defineActions(actions) {
|
|
21
|
+
Object.entries(actions).forEach(([name, cb]) => {
|
|
22
|
+
obj[name] = (cb as any).bind(obj, obj);
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
readState(select) {
|
|
26
|
+
return select(state.getState());
|
|
27
|
+
},
|
|
28
|
+
dispatch(change) {
|
|
29
|
+
state.setState(change(state.getState()));
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
return obj;
|
|
33
|
+
}
|
|
34
|
+
|
|
6
35
|
const state = {
|
|
7
36
|
search: {
|
|
8
37
|
input: 'abc',
|
|
@@ -25,7 +54,7 @@ describe('Search Action Module', () => {
|
|
|
25
54
|
},
|
|
26
55
|
},
|
|
27
56
|
}));
|
|
28
|
-
const ctx = createActions(state, createListener(
|
|
57
|
+
const ctx = createActions(state, createListener());
|
|
29
58
|
ctx.defineActions(createSearchActions());
|
|
30
59
|
ctx.appendSearchResults(['a', 'b'], false);
|
|
31
60
|
expect(state.getState()).toEqual({
|
|
@@ -51,7 +80,7 @@ describe('Search Action Module', () => {
|
|
|
51
80
|
},
|
|
52
81
|
},
|
|
53
82
|
}));
|
|
54
|
-
const ctx = createActions(state, createListener(
|
|
83
|
+
const ctx = createActions(state, createListener());
|
|
55
84
|
ctx.defineActions(createSearchActions());
|
|
56
85
|
ctx.appendSearchResults(['a'], true);
|
|
57
86
|
expect(state.getState()).toEqual({
|
|
@@ -77,7 +106,7 @@ describe('Search Action Module', () => {
|
|
|
77
106
|
},
|
|
78
107
|
},
|
|
79
108
|
}));
|
|
80
|
-
const ctx = createActions(state, createListener(
|
|
109
|
+
const ctx = createActions(state, createListener());
|
|
81
110
|
ctx.defineActions(createSearchActions());
|
|
82
111
|
ctx.prependSearchResults(['a', 'b'], false);
|
|
83
112
|
expect(state.getState()).toEqual({
|
|
@@ -103,7 +132,7 @@ describe('Search Action Module', () => {
|
|
|
103
132
|
},
|
|
104
133
|
},
|
|
105
134
|
}));
|
|
106
|
-
const ctx = createActions(state, createListener(
|
|
135
|
+
const ctx = createActions(state, createListener());
|
|
107
136
|
ctx.defineActions(createSearchActions());
|
|
108
137
|
ctx.prependSearchResults(['a'], true);
|
|
109
138
|
expect(state.getState()).toEqual({
|
|
@@ -129,7 +158,7 @@ describe('Search Action Module', () => {
|
|
|
129
158
|
},
|
|
130
159
|
},
|
|
131
160
|
}));
|
|
132
|
-
const ctx = createActions(state, createListener(
|
|
161
|
+
const ctx = createActions(state, createListener());
|
|
133
162
|
ctx.defineActions(createSearchActions());
|
|
134
163
|
ctx.resetSearchResults('yo', true);
|
|
135
164
|
expect(state.getState()).toEqual({
|
|
@@ -155,7 +184,7 @@ describe('Search Action Module', () => {
|
|
|
155
184
|
},
|
|
156
185
|
},
|
|
157
186
|
}));
|
|
158
|
-
const ctx = createActions(state, createListener(
|
|
187
|
+
const ctx = createActions(state, createListener());
|
|
159
188
|
ctx.defineActions(createSearchActions());
|
|
160
189
|
ctx.resetSearchResults('yo y', false);
|
|
161
190
|
expect(state.getState()).toEqual({
|
|
@@ -181,7 +210,7 @@ describe('Search Action Module', () => {
|
|
|
181
210
|
},
|
|
182
211
|
},
|
|
183
212
|
}));
|
|
184
|
-
const ctx = createActions(state, createListener(
|
|
213
|
+
const ctx = createActions(state, createListener());
|
|
185
214
|
ctx.defineActions(createSearchActions());
|
|
186
215
|
ctx.setSearchInput('test input');
|
|
187
216
|
expect(state.getState()).toEqual({
|
|
@@ -199,7 +228,7 @@ describe('Search Action Module', () => {
|
|
|
199
228
|
it('immediately resets with loading false if some value is given but no provider found', () => {
|
|
200
229
|
state.search.input = 'foo';
|
|
201
230
|
const globalState: any = create(() => state);
|
|
202
|
-
const ctx = createActions(globalState, createListener(
|
|
231
|
+
const ctx = createActions(globalState, createListener());
|
|
203
232
|
ctx.defineActions(createSearchActions());
|
|
204
233
|
ctx.triggerSearch();
|
|
205
234
|
expect(globalState.getState().search.results.loading).toBe(false);
|
|
@@ -215,7 +244,7 @@ describe('Search Action Module', () => {
|
|
|
215
244
|
cancel() {},
|
|
216
245
|
};
|
|
217
246
|
const globalState: any = create(() => state);
|
|
218
|
-
const ctx = createActions(globalState, createListener(
|
|
247
|
+
const ctx = createActions(globalState, createListener());
|
|
219
248
|
ctx.defineActions(createSearchActions());
|
|
220
249
|
ctx.triggerSearch();
|
|
221
250
|
expect(globalState.getState().search.results.loading).toBe(true);
|
|
@@ -224,7 +253,7 @@ describe('Search Action Module', () => {
|
|
|
224
253
|
it('immediately resets with loading false if no value is given implicitly', () => {
|
|
225
254
|
state.search.input = '';
|
|
226
255
|
const globalState: any = create(() => state);
|
|
227
|
-
const ctx = createActions(globalState, createListener(
|
|
256
|
+
const ctx = createActions(globalState, createListener());
|
|
228
257
|
ctx.defineActions(createSearchActions());
|
|
229
258
|
ctx.triggerSearch();
|
|
230
259
|
expect(globalState.getState().search.results.loading).toBe(false);
|
|
@@ -232,7 +261,7 @@ describe('Search Action Module', () => {
|
|
|
232
261
|
|
|
233
262
|
it('immediately resets with loading false if no value is given explicitly', () => {
|
|
234
263
|
const gs: any = create(() => state);
|
|
235
|
-
const ctx = createActions(gs, createListener(
|
|
264
|
+
const ctx = createActions(gs, createListener());
|
|
236
265
|
ctx.defineActions(createSearchActions());
|
|
237
266
|
const dispose = ctx.triggerSearch('');
|
|
238
267
|
dispose();
|
|
@@ -241,7 +270,7 @@ describe('Search Action Module', () => {
|
|
|
241
270
|
|
|
242
271
|
it('immediately resets with loading false if no value is given explicitly though immediate', () => {
|
|
243
272
|
const gs: any = create(() => state);
|
|
244
|
-
const ctx = createActions(gs, createListener(
|
|
273
|
+
const ctx = createActions(gs, createListener());
|
|
245
274
|
ctx.defineActions(createSearchActions());
|
|
246
275
|
const dispose = ctx.triggerSearch('', true);
|
|
247
276
|
dispose();
|
|
@@ -258,23 +287,23 @@ describe('Search Action Module', () => {
|
|
|
258
287
|
cancel() {},
|
|
259
288
|
};
|
|
260
289
|
const gs: any = create(() => state);
|
|
261
|
-
const ctx = createActions(gs, createListener(
|
|
290
|
+
const ctx = createActions(gs, createListener());
|
|
262
291
|
ctx.defineActions(createSearchActions());
|
|
263
292
|
ctx.triggerSearch('foo');
|
|
264
293
|
expect(gs.getState().search.results.loading).toBe(true);
|
|
265
294
|
});
|
|
266
295
|
|
|
267
296
|
it('walks over all search providers', () => {
|
|
268
|
-
const search =
|
|
269
|
-
const clear =
|
|
270
|
-
const cancel =
|
|
297
|
+
const search = vitest.fn(() => Promise.resolve([]));
|
|
298
|
+
const clear = vitest.fn();
|
|
299
|
+
const cancel = vitest.fn();
|
|
271
300
|
state.search.input = 'test';
|
|
272
301
|
state.registry.searchProviders = {
|
|
273
302
|
foo: { search, clear, cancel },
|
|
274
303
|
bar: { search, clear, cancel },
|
|
275
304
|
};
|
|
276
305
|
const gs: any = create(() => state);
|
|
277
|
-
const ctx = createActions(gs, createListener(
|
|
306
|
+
const ctx = createActions(gs, createListener());
|
|
278
307
|
ctx.defineActions(createSearchActions());
|
|
279
308
|
ctx.triggerSearch();
|
|
280
309
|
expect(search).toHaveBeenCalledTimes(2);
|
|
@@ -292,14 +321,14 @@ describe('Search Action Module', () => {
|
|
|
292
321
|
},
|
|
293
322
|
};
|
|
294
323
|
const gs: any = create(() => state);
|
|
295
|
-
const ctx = createActions(gs, createListener(
|
|
324
|
+
const ctx = createActions(gs, createListener());
|
|
296
325
|
ctx.defineActions(createSearchActions());
|
|
297
326
|
ctx.triggerSearch();
|
|
298
327
|
await (state.registry.searchProviders as any).foo.search().catch((m) => m);
|
|
299
328
|
});
|
|
300
329
|
|
|
301
330
|
it('stops existing search', async () => {
|
|
302
|
-
const cancel =
|
|
331
|
+
const cancel = vitest.fn();
|
|
303
332
|
state.search.input = 'test';
|
|
304
333
|
state.registry.searchProviders = {
|
|
305
334
|
foo: {
|
|
@@ -311,7 +340,7 @@ describe('Search Action Module', () => {
|
|
|
311
340
|
},
|
|
312
341
|
};
|
|
313
342
|
const gs: any = create(() => state);
|
|
314
|
-
const ctx = createActions(gs, createListener(
|
|
343
|
+
const ctx = createActions(gs, createListener());
|
|
315
344
|
ctx.defineActions(createSearchActions());
|
|
316
345
|
ctx.triggerSearch();
|
|
317
346
|
await (state.registry.searchProviders as any).foo.search().catch((m) => m);
|
|
@@ -319,7 +348,7 @@ describe('Search Action Module', () => {
|
|
|
319
348
|
});
|
|
320
349
|
|
|
321
350
|
it('cancels existing search', async () => {
|
|
322
|
-
const cancel =
|
|
351
|
+
const cancel = vitest.fn();
|
|
323
352
|
state.search.input = 'test';
|
|
324
353
|
state.registry.searchProviders = {
|
|
325
354
|
foo: {
|
|
@@ -331,7 +360,7 @@ describe('Search Action Module', () => {
|
|
|
331
360
|
},
|
|
332
361
|
};
|
|
333
362
|
const gs: any = create(() => state);
|
|
334
|
-
const ctx = createActions(gs, createListener(
|
|
363
|
+
const ctx = createActions(gs, createListener());
|
|
335
364
|
ctx.defineActions(createSearchActions());
|
|
336
365
|
const dispose = ctx.triggerSearch();
|
|
337
366
|
dispose();
|
|
@@ -340,7 +369,7 @@ describe('Search Action Module', () => {
|
|
|
340
369
|
});
|
|
341
370
|
|
|
342
371
|
it('catches any emitted exceptions', async () => {
|
|
343
|
-
console.warn =
|
|
372
|
+
console.warn = vitest.fn();
|
|
344
373
|
state.search.input = 'test';
|
|
345
374
|
state.registry.searchProviders = {
|
|
346
375
|
foo: {
|
|
@@ -352,7 +381,7 @@ describe('Search Action Module', () => {
|
|
|
352
381
|
},
|
|
353
382
|
};
|
|
354
383
|
const gs: any = create(() => state);
|
|
355
|
-
const ctx = createActions(gs, createListener(
|
|
384
|
+
const ctx = createActions(gs, createListener());
|
|
356
385
|
ctx.defineActions(createSearchActions());
|
|
357
386
|
ctx.triggerSearch();
|
|
358
387
|
await (state.registry.searchProviders as any).foo.search().catch((m) => m);
|
|
@@ -367,7 +396,7 @@ describe('Search Action Module', () => {
|
|
|
367
396
|
searchProviders: {},
|
|
368
397
|
},
|
|
369
398
|
}));
|
|
370
|
-
const ctx = createActions(state, createListener(
|
|
399
|
+
const ctx = createActions(state, createListener());
|
|
371
400
|
ctx.defineActions(createSearchActions());
|
|
372
401
|
ctx.registerSearchProvider('foo', 10 as any);
|
|
373
402
|
expect(state.getState()).toEqual({
|
package/src/create.test.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
1
4
|
import create from 'zustand';
|
|
5
|
+
import { describe, it, expect, vitest } from 'vitest';
|
|
2
6
|
import { createSearchApi } from './create';
|
|
3
7
|
|
|
4
8
|
function createMockContainer() {
|
|
@@ -10,9 +14,9 @@ function createMockContainer() {
|
|
|
10
14
|
}));
|
|
11
15
|
return {
|
|
12
16
|
context: {
|
|
13
|
-
on:
|
|
14
|
-
off:
|
|
15
|
-
emit:
|
|
17
|
+
on: vitest.fn(),
|
|
18
|
+
off: vitest.fn(),
|
|
19
|
+
emit: vitest.fn(),
|
|
16
20
|
defineActions() {},
|
|
17
21
|
state,
|
|
18
22
|
readState(cb) {
|
|
@@ -41,8 +45,8 @@ const moduleMetadata = {
|
|
|
41
45
|
describe('Create Search API Extensions', () => {
|
|
42
46
|
it('createCoreApi can register and unregister a search provider', () => {
|
|
43
47
|
const container = createMockContainer();
|
|
44
|
-
container.context.registerSearchProvider =
|
|
45
|
-
container.context.unregisterSearchProvider =
|
|
48
|
+
container.context.registerSearchProvider = vitest.fn();
|
|
49
|
+
container.context.unregisterSearchProvider = vitest.fn();
|
|
46
50
|
const api = (createSearchApi()(container.context) as any)(container.api, moduleMetadata);
|
|
47
51
|
api.registerSearchProvider('my-sp', () => Promise.resolve([]));
|
|
48
52
|
expect(container.context.registerSearchProvider).toHaveBeenCalledTimes(1);
|
|
@@ -56,10 +60,10 @@ describe('Create Search API Extensions', () => {
|
|
|
56
60
|
|
|
57
61
|
it('createCoreApi registration of a search provider wraps it', () => {
|
|
58
62
|
const container = createMockContainer();
|
|
59
|
-
container.context.registerSearchProvider =
|
|
60
|
-
container.context.unregisterSearchProvider =
|
|
63
|
+
container.context.registerSearchProvider = vitest.fn();
|
|
64
|
+
container.context.unregisterSearchProvider = vitest.fn();
|
|
61
65
|
const api = (createSearchApi()(container.context) as any)(container.api, moduleMetadata);
|
|
62
|
-
const search =
|
|
66
|
+
const search = vitest.fn();
|
|
63
67
|
api.registerSearchProvider('my-sp', search);
|
|
64
68
|
container.context.registerSearchProvider.mock.calls[0][1].search('foo');
|
|
65
69
|
expect(search).toHaveBeenCalledWith('foo', container.api);
|
package/src/useDebounce.test.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
1
4
|
import * as React from 'react';
|
|
5
|
+
import { describe, it, expect, vitest } from 'vitest';
|
|
2
6
|
import { useDebounce } from './useDebounce';
|
|
3
7
|
|
|
4
|
-
|
|
8
|
+
vitest.mock('react');
|
|
5
9
|
|
|
6
10
|
describe('Debounce Hook Module', () => {
|
|
7
11
|
it('just returns initial value if nothing has been changed', () => {
|
|
8
|
-
const usedEffect =
|
|
9
|
-
const usedState =
|
|
12
|
+
const usedEffect = vitest.fn();
|
|
13
|
+
const usedState = vitest.fn((value) => [value]);
|
|
10
14
|
(React as any).useState = usedState;
|
|
11
15
|
(React as any).useEffect = usedEffect;
|
|
12
16
|
const result = useDebounce('foo');
|
|
@@ -16,9 +20,9 @@ describe('Debounce Hook Module', () => {
|
|
|
16
20
|
});
|
|
17
21
|
|
|
18
22
|
it('invokes useEffect immediately, but does not set value immediately', () => {
|
|
19
|
-
const usedEffect =
|
|
20
|
-
const setValue =
|
|
21
|
-
const usedState =
|
|
23
|
+
const usedEffect = vitest.fn((fn) => fn());
|
|
24
|
+
const setValue = vitest.fn();
|
|
25
|
+
const usedState = vitest.fn((value) => [value, setValue]);
|
|
22
26
|
(React as any).useState = usedState;
|
|
23
27
|
(React as any).useEffect = usedEffect;
|
|
24
28
|
useDebounce('foo');
|
|
@@ -26,42 +30,42 @@ describe('Debounce Hook Module', () => {
|
|
|
26
30
|
});
|
|
27
31
|
|
|
28
32
|
it('invokes useEffect immediately, but sets value immediately if 0', () => {
|
|
29
|
-
|
|
30
|
-
const usedEffect =
|
|
31
|
-
const setValue =
|
|
32
|
-
const usedState =
|
|
33
|
+
vitest.useFakeTimers();
|
|
34
|
+
const usedEffect = vitest.fn((fn) => fn());
|
|
35
|
+
const setValue = vitest.fn();
|
|
36
|
+
const usedState = vitest.fn((value) => [value, setValue]);
|
|
33
37
|
(React as any).useState = usedState;
|
|
34
38
|
(React as any).useEffect = usedEffect;
|
|
35
39
|
useDebounce('foo', 0);
|
|
36
|
-
|
|
40
|
+
vitest.advanceTimersByTime(0);
|
|
37
41
|
expect(setValue).toHaveBeenCalled();
|
|
38
42
|
});
|
|
39
43
|
|
|
40
44
|
it('invokes useEffect immediately, but sets value after wait time', () => {
|
|
41
|
-
|
|
42
|
-
const usedEffect =
|
|
43
|
-
const setValue =
|
|
44
|
-
const usedState =
|
|
45
|
+
vitest.useFakeTimers();
|
|
46
|
+
const usedEffect = vitest.fn((fn) => fn());
|
|
47
|
+
const setValue = vitest.fn();
|
|
48
|
+
const usedState = vitest.fn((value) => [value, setValue]);
|
|
45
49
|
(React as any).useState = usedState;
|
|
46
50
|
(React as any).useEffect = usedEffect;
|
|
47
51
|
expect(setValue).not.toHaveBeenCalled();
|
|
48
52
|
useDebounce('foo', 300);
|
|
49
|
-
|
|
53
|
+
vitest.advanceTimersByTime(300);
|
|
50
54
|
expect(setValue).toHaveBeenCalled();
|
|
51
55
|
});
|
|
52
56
|
|
|
53
57
|
it('invokes useEffect immediately and resets timer if needed', () => {
|
|
54
|
-
|
|
55
|
-
const usedEffect =
|
|
56
|
-
const setValue =
|
|
57
|
-
const usedState =
|
|
58
|
+
vitest.useFakeTimers();
|
|
59
|
+
const usedEffect = vitest.fn((fn) => fn());
|
|
60
|
+
const setValue = vitest.fn();
|
|
61
|
+
const usedState = vitest.fn((value) => [value, setValue]);
|
|
58
62
|
(React as any).useState = usedState;
|
|
59
63
|
(React as any).useEffect = usedEffect;
|
|
60
64
|
expect(setValue).not.toHaveBeenCalled();
|
|
61
65
|
useDebounce('foo', 300);
|
|
62
|
-
|
|
66
|
+
vitest.advanceTimersByTime(250);
|
|
63
67
|
usedEffect.mock.results[0].value();
|
|
64
|
-
|
|
68
|
+
vitest.advanceTimersByTime(50);
|
|
65
69
|
expect(setValue).not.toHaveBeenCalled();
|
|
66
70
|
});
|
|
67
71
|
});
|
package/src/useSearch.test.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
1
4
|
import * as React from 'react';
|
|
5
|
+
import { describe, it, expect, vitest } from 'vitest';
|
|
2
6
|
|
|
3
7
|
const state = {
|
|
4
8
|
search: {
|
|
@@ -10,63 +14,85 @@ const state = {
|
|
|
10
14
|
};
|
|
11
15
|
|
|
12
16
|
const availableActions = {
|
|
13
|
-
setSearchInput:
|
|
14
|
-
triggerSearch:
|
|
17
|
+
setSearchInput: vitest.fn(),
|
|
18
|
+
triggerSearch: vitest.fn(),
|
|
15
19
|
};
|
|
16
20
|
|
|
17
|
-
const useGlobalState =
|
|
18
|
-
const useActions =
|
|
19
|
-
const useDebounce =
|
|
21
|
+
const useGlobalState = vitest.fn((select: any) => select(state));
|
|
22
|
+
const useActions = vitest.fn(() => availableActions);
|
|
23
|
+
const useDebounce = vitest.fn((value) => value);
|
|
20
24
|
|
|
21
|
-
|
|
22
|
-
...
|
|
25
|
+
vitest.mock('piral-core', async () => ({
|
|
26
|
+
...((await vitest.importActual('piral-core')) as any),
|
|
23
27
|
useGlobalState,
|
|
24
28
|
useActions,
|
|
25
29
|
}));
|
|
26
30
|
|
|
27
|
-
|
|
31
|
+
vitest.mock('./useDebounce.ts', () => ({
|
|
28
32
|
useDebounce,
|
|
29
33
|
}));
|
|
30
34
|
|
|
35
|
+
vitest.mock('react');
|
|
36
|
+
|
|
37
|
+
const testOptions = {
|
|
38
|
+
timeout: 30000,
|
|
39
|
+
};
|
|
40
|
+
|
|
31
41
|
describe('Search Hook Module', () => {
|
|
32
|
-
it(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
it(
|
|
43
|
+
'just returns current input value',
|
|
44
|
+
async () => {
|
|
45
|
+
const { useSearch } = await import('./useSearch');
|
|
46
|
+
const usedEffect = vitest.fn();
|
|
47
|
+
(React as any).useEffect = usedEffect;
|
|
48
|
+
(React as any).useRef = (current) => ({ current });
|
|
49
|
+
const [value] = useSearch();
|
|
50
|
+
expect(value).toBe('abc');
|
|
51
|
+
},
|
|
52
|
+
testOptions,
|
|
53
|
+
);
|
|
40
54
|
|
|
41
|
-
it(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
55
|
+
it(
|
|
56
|
+
'sets the value using the action',
|
|
57
|
+
async () => {
|
|
58
|
+
const { useSearch } = await import('./useSearch');
|
|
59
|
+
const usedEffect = vitest.fn();
|
|
60
|
+
(React as any).useEffect = usedEffect;
|
|
61
|
+
(React as any).useRef = (current) => ({ current });
|
|
62
|
+
(React as any).useState = (initial) => [initial, vitest.fn()];
|
|
63
|
+
const [_, setValue] = useSearch();
|
|
64
|
+
setValue('foo');
|
|
65
|
+
expect(availableActions.setSearchInput).toHaveBeenCalledWith('foo');
|
|
66
|
+
},
|
|
67
|
+
testOptions,
|
|
68
|
+
);
|
|
51
69
|
|
|
52
|
-
it(
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
it(
|
|
71
|
+
'triggers the search without immediate mode',
|
|
72
|
+
async () => {
|
|
73
|
+
const { useSearch } = await import('./useSearch');
|
|
74
|
+
const usedEffect = vitest.fn((fn) => fn());
|
|
75
|
+
(React as any).useEffect = usedEffect;
|
|
76
|
+
(React as any).useRef = (current) => ({ current });
|
|
77
|
+
(React as any).useState = (initial) => [initial, vitest.fn()];
|
|
78
|
+
useSearch();
|
|
79
|
+
expect(availableActions.triggerSearch).toHaveBeenCalledWith('abc', false);
|
|
80
|
+
},
|
|
81
|
+
testOptions,
|
|
82
|
+
);
|
|
61
83
|
|
|
62
|
-
it(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
84
|
+
it(
|
|
85
|
+
'cancels the current search',
|
|
86
|
+
async () => {
|
|
87
|
+
const { useSearch } = await import('./useSearch');
|
|
88
|
+
const usedEffect = vitest.fn((fn) => fn());
|
|
89
|
+
const cancel = vitest.fn();
|
|
90
|
+
(React as any).useEffect = usedEffect;
|
|
91
|
+
(React as any).useRef = (_) => ({ current: cancel });
|
|
92
|
+
(React as any).useState = (initial) => [initial, vitest.fn()];
|
|
93
|
+
useSearch();
|
|
94
|
+
expect(cancel).toHaveBeenCalledTimes(1);
|
|
95
|
+
},
|
|
96
|
+
testOptions,
|
|
97
|
+
);
|
|
72
98
|
});
|