-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
key.ts
44 lines (37 loc) · 1.24 KB
/
key.ts
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
import asArray from 'asArray';
import { isNullish } from 'isNullish';
import * as nestedArray from 'nestedArray';
import { deferThrow } from 'throwError';
import VestTest from 'VestTest';
import { useCurrentPath, useIsolate } from 'isolate';
import { useTestObjects } from 'stateHooks';
export function usePrevKeys(): Record<string, VestTest> {
const [{ prev }] = useTestObjects();
return asArray(nestedArray.getCurrent(prev, useCurrentPath())).reduce(
(prevKeys, testObject) => {
if (!(testObject instanceof VestTest)) {
return prevKeys;
}
if (isNullish(testObject.key)) {
return prevKeys;
}
prevKeys[testObject.key] = testObject;
return prevKeys;
},
{} as Record<string, VestTest>
);
}
export function usePrevTestByKey(key: string): VestTest | undefined {
const prev = useIsolate().keys.prev;
return prev[key];
}
export function useRetainTestKey(key: string, testObject: VestTest) {
const current = useIsolate().keys.current;
if (isNullish(current[key])) {
current[key] = testObject;
} else {
deferThrow(
`Encountered the same test key "${key}" twice. This may lead to tests overriding each other's results, or to tests being unexpectedly omitted.`
);
}
}