generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.test.js
67 lines (58 loc) · 2.91 KB
/
search.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { describe, test } from 'node:test';
import { ok, strictEqual, deepStrictEqual, throws, doesNotReject, rejects } from 'node:assert';
import { manageSearch } from './search.js';
describe('Test `manageSearch()` functionality', () => {
// Need to polyfill parts of `location` and `history` APIs for node.
globalThis.location = new URL(import.meta.url);
globalThis.history = {
state: null,
replaceState(state, unused, url) {
this.state = state;
globalThis.location = new URL(url, location);
}
};
test('Test basic functionality', async () => {
const [param, setParam] = manageSearch('test');
const signal = AbortSignal.timeout(1);
ok(param.addEventListener instanceof Function, 'Should support event listeners.');
const promise = new Promise((resolve, reject) => {
signal.addEventListener('abort', ({ target }) => reject(target.reason), { once: true });
param.addEventListener('change', resolve, { signal, once: true });
});
doesNotReject(() => promise, 'Events should dispatch.');
setParam('works');
setParam(undefined);
});
test('Assure rejections work', async () => {
const [param] = manageSearch('test');
const signal = AbortSignal.timeout(1);
const promise = new Promise((resolve, reject) => {
signal.addEventListener('abort', ({ target }) => reject(target.reason), { once: true });
param.addEventListener('change', resolve, { signal });
});
rejects(() => promise, 'Events should dispatch.');
});
test('Test single string params', () => {
const [name, setName] = manageSearch('name', '');
strictEqual(name.length, 0, 'Should proxy to underlying string length.');
setName('Fred');
ok(name.substring instanceof Function, 'Methods of params should be proxied to values.');
strictEqual(name.substring(0), 'Fred', 'Updating param should update the value.');
strictEqual(name.length, 4, 'Updating param should update the value length.');
strictEqual(location.search, '?name=Fred', 'Should update location correctly.');
setName(undefined);
strictEqual(location.search.length, 0, 'Setting no/empty values should remove the search param.');
});
test('Test arrays and multiple values.', () => {
const [list, setList] = manageSearch('list', [], { multiple: true });
strictEqual(list.length, 0, 'Should start off with a length of 0');
setList(['one', 'two', 'three']);
deepStrictEqual(list.map(item => item.toUpperCase()), ['ONE', 'TWO', 'THREE'], 'Should expose underlying methods of array.');
setList([...list, 'four']);
throws(() => list.push('five'), { name: 'TypeError' }, 'Params should be immutable.');
strictEqual(list.length, 4, 'Should implement the iterator protocol and spread syntax, updating values.');
strictEqual(location.search, '?list=one&list=two&list=three&list=four', 'Should update the `list` search param with multiple values.');
setList([]);
strictEqual(location.search.length, 0, 'Setting no/empty values should remove the search param.');
});
});