Skip to content

Commit 33cb54a

Browse files
committed
feat: Add mockInitialState to /mock
1 parent 1651871 commit 33cb54a

File tree

5 files changed

+77
-112
lines changed

5 files changed

+77
-112
lines changed

.changeset/breezy-humans-go.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@data-client/core': patch
3+
---
4+
5+
Add mockInitialState to /mock
6+
7+
```ts
8+
import { mockInitialState } from '@data-client/core/mock';
9+
import { ArticleResource } from './resources';
10+
11+
const state = mockInitialState([
12+
{
13+
endpoint: ArticleResource.get,
14+
args: [{ id: 5 }],
15+
response: { id: 5, title: 'Hello', content: 'World' },
16+
},
17+
]);
18+
```

packages/core/src/mock/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export type {
1313
} from './fixtureTypes.js';
1414
export { collapseFixture } from './collapseFixture.js';
1515
export { createFixtureMap } from './createFixtureMap.js';
16+
export { default as mockInitialState } from './mockState.js';
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
SetAction,
3+
State,
4+
ActionTypes,
5+
Controller,
6+
createReducer,
7+
__INTERNAL__,
8+
} from '../index.js';
9+
import type { Fixture } from './fixtureTypes.js';
10+
11+
const { initialState } = __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+
}

packages/test/src/mockState.ts

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1 @@
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/core/mock';

packages/vue/src/test/mockState.ts

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1 @@
1-
import {
2-
SetAction,
3-
State,
4-
ActionTypes,
5-
Controller,
6-
createReducer,
7-
__INTERNAL__,
8-
} from '@data-client/core';
9-
import type { Fixture } from '@data-client/core/mock';
10-
11-
const { initialState } = __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/core/mock';

0 commit comments

Comments
 (0)