-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathtest-helpers.ts
More file actions
33 lines (31 loc) · 1.02 KB
/
test-helpers.ts
File metadata and controls
33 lines (31 loc) · 1.02 KB
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
import type { Persistor } from '@segment/sovran-react-native';
export const createMockStore = <T>(initialState: T) => {
let state = initialState;
return {
getState: jest.fn((...args: unknown[]) => {
// Both overloads return a Promise in the mock for simplicity.
// Supports getState() and getState(true).
void args;
return Promise.resolve(state);
}),
dispatch: jest.fn((action: unknown) => {
if (typeof action === 'function') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
state = action(state);
} else {
state = (action as { payload: unknown }).payload as T;
}
return Promise.resolve(state);
}),
};
};
export const createTestPersistor = (
storage: Record<string, unknown> = {}
): Persistor => ({
get: async <T>(key: string): Promise<T | undefined> =>
Promise.resolve(storage[key] as T),
set: async <T>(key: string, state: T): Promise<void> => {
storage[key] = state;
return Promise.resolve();
},
});