|
1 | | -import { |
2 | | - SetAction, |
3 | | - State, |
4 | | - ActionTypes, |
5 | | - Controller, |
6 | | - __INTERNAL__, |
7 | | -} from '@data-client/react'; |
8 | | - |
9 | | -import type { Fixture } from './fixtureTypes.js'; |
10 | | - |
11 | | -const { initialState, createReducer } = __INTERNAL__; |
12 | | - |
13 | | -export default function mockInitialState( |
14 | | - fixtures: Fixture[] = [], |
15 | | -): State<unknown> { |
16 | | - const actions: SetAction[] = []; |
17 | | - const dispatch = (action: any) => { |
18 | | - actions.push(action); |
19 | | - return Promise.resolve(); |
20 | | - }; |
21 | | - const controller = new Controller({ dispatch }); |
22 | | - const reducer: ( |
23 | | - state: State<unknown> | undefined, |
24 | | - action: ActionTypes, |
25 | | - ) => State<unknown> = createReducer(controller); |
26 | | - |
27 | | - fixtures.forEach(fixture => { |
28 | | - dispatchFixture(fixture, fixture.args, controller); |
29 | | - }); |
30 | | - return actions.reduce(reducer, initialState); |
31 | | -} |
32 | | - |
33 | | -function dispatchFixture( |
34 | | - fixture: Fixture, |
35 | | - args: any[], |
36 | | - controller: Controller, |
37 | | - fetchedAt?: number, |
38 | | -) { |
39 | | - // eslint-disable-next-line prefer-const |
40 | | - let { endpoint } = fixture; |
41 | | - const { response, error } = fixture; |
42 | | - if (controller.resolve) { |
43 | | - controller.resolve(endpoint, { |
44 | | - args, |
45 | | - response, |
46 | | - error, |
47 | | - fetchedAt: fetchedAt ?? Date.now(), |
48 | | - }); |
49 | | - } else { |
50 | | - if (error === true) { |
51 | | - controller.setError(endpoint, ...args, response); |
52 | | - } else { |
53 | | - controller.setResponse(endpoint, ...args, response); |
54 | | - } |
55 | | - } |
56 | | -} |
| 1 | +export { mockInitialState as default } from '@data-client/react/mock'; |
0 commit comments