Skip to content

Commit a4092a1

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

File tree

9 files changed

+90
-116
lines changed

9 files changed

+90
-116
lines changed

.changeset/breezy-humans-go.md

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"ci:build": "yarn ci:build:types && yarn workspaces foreach -Wptiv --no-private run build:lib",
2525
"ci:build:types": "yarn build:copy:ambient && tsc --build",
2626
"ci:build:legacy-types": "yarn workspaces foreach -WptivR -j 10 --from @data-client/react --from @data-client/rest --from @data-client/graphql run build:legacy-types",
27-
"ci:build-test-lib": "yarn workspace @data-client/test run build:lib && yarn workspace @data-client/test run build:bundle",
27+
"ci:build-test-lib": "yarn workspace @data-client/core run build:lib && yarn workspace @data-client/test run build:lib && yarn workspace @data-client/test run build:bundle",
2828
"ci:build:esmodule": "yarn workspaces foreach -WptivR --from @data-client/react --from @data-client/rest --from @data-client/graphql run build:lib && yarn workspace @data-client/normalizr run build:js:node && yarn workspace @data-client/endpoint run build:js:node",
2929
"ci:build:bundlesize": "yarn workspaces foreach -Wptiv --no-private run build:lib && yarn workspace test-bundlesize run build:sizecompare",
3030
"build:copy:ambient": "mkdirp ./packages/endpoint/lib && copyfiles --flat ./packages/endpoint/src/schema.d.ts ./packages/endpoint/lib/ && copyfiles --flat ./packages/endpoint/src/endpoint.d.ts ./packages/endpoint/lib/ && mkdirp ./packages/rest/lib && copyfiles --flat ./packages/rest/src/RestEndpoint.d.ts ./packages/rest/lib && copyfiles --flat ./packages/rest/src/next/RestEndpoint.d.ts ./packages/rest/lib/next && mkdirp ./packages/react/lib && copyfiles --flat ./packages/react/src/server/redux/redux.d.ts ./packages/react/lib/server/redux",

packages/core/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@
5656
},
5757
"./mock": {
5858
"types": "./lib/mock/index.d.ts",
59-
"require": "./lib/mock/index.js",
60-
"browser": "./lib/mock/index.js",
61-
"react-native": "./lib/mock/index.js",
6259
"default": "./lib/mock/index.js"
6360
},
6461
"./package.json": "./package.json"

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/react/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
"redux": [
7979
"lib/server/redux/index.d.ts"
8080
],
81+
"mock": [
82+
"lib/mock/index.d.ts"
83+
],
8184
"*": [
8285
"lib/index.d.ts"
8386
]
@@ -95,6 +98,9 @@
9598
"redux": [
9699
"ts3.4/server/redux/index.d.ts"
97100
],
101+
"mock": [
102+
"ts3.4/mock/index.d.ts"
103+
],
98104
"*": [
99105
"ts3.4/index.d.ts"
100106
]
@@ -132,6 +138,10 @@
132138
"react-native": "./native/server/redux/index.js",
133139
"default": "./lib/server/redux/index.js"
134140
},
141+
"./mock": {
142+
"types": "./lib/mock/index.d.ts",
143+
"default": "./lib/mock/index.js"
144+
},
135145
"./package.json": "./package.json"
136146
},
137147
"type": "module",

packages/react/src/mock/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '@data-client/core/mock';

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/react/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)