@khanacademy/wonder-blocks-testing 7.1.10 → 7.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/dist/es/index.js +183 -120
- package/dist/fetch/fetch-request-matches-mock.d.ts +5 -0
- package/dist/fetch/fetch-request-matches-mock.js.flow +17 -0
- package/dist/fetch/mock-fetch.d.ts +5 -0
- package/dist/fetch/mock-fetch.js.flow +13 -0
- package/dist/fetch/types.d.ts +9 -0
- package/dist/fetch/types.js.flow +19 -0
- package/dist/fixtures/fixtures.basic.stories.d.ts +13 -0
- package/dist/fixtures/fixtures.basic.stories.js.flow +22 -0
- package/dist/fixtures/fixtures.d.ts +19 -0
- package/dist/fixtures/fixtures.defaultwrapper.stories.d.ts +9 -0
- package/dist/fixtures/fixtures.defaultwrapper.stories.js.flow +17 -0
- package/dist/fixtures/fixtures.js.flow +33 -0
- package/dist/fixtures/types.d.ts +36 -0
- package/dist/fixtures/types.js.flow +37 -0
- package/dist/gql/gql-request-matches-mock.d.ts +3 -0
- package/dist/gql/gql-request-matches-mock.js.flow +15 -0
- package/dist/gql/mock-gql-fetch.d.ts +5 -0
- package/dist/gql/mock-gql-fetch.js.flow +13 -0
- package/dist/gql/types.d.ts +15 -0
- package/dist/gql/types.js.flow +39 -0
- package/dist/harness/adapters/adapters.d.ts +35 -0
- package/dist/harness/adapters/adapters.js.flow +69 -0
- package/dist/harness/adapters/css.d.ts +12 -0
- package/dist/harness/adapters/css.js.flow +23 -0
- package/dist/harness/adapters/data.d.ts +18 -0
- package/dist/harness/adapters/data.js.flow +32 -0
- package/dist/harness/adapters/portal.d.ts +12 -0
- package/dist/harness/adapters/portal.js.flow +18 -0
- package/dist/harness/adapters/router.d.ts +94 -0
- package/dist/harness/adapters/router.js.flow +122 -0
- package/dist/harness/hook-harness.d.ts +13 -0
- package/dist/harness/hook-harness.js.flow +23 -0
- package/dist/harness/make-hook-harness.d.ts +17 -0
- package/dist/harness/make-hook-harness.js.flow +42 -0
- package/dist/harness/make-test-harness.d.ts +18 -0
- package/dist/harness/make-test-harness.js.flow +48 -0
- package/dist/harness/render-adapters.d.ts +6 -0
- package/dist/harness/render-adapters.js.flow +24 -0
- package/dist/harness/test-harness.d.ts +32 -0
- package/dist/harness/test-harness.js.flow +83 -0
- package/dist/harness/types.d.ts +46 -0
- package/dist/harness/types.js.flow +66 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +185 -127
- package/dist/index.js.flow +26 -2
- package/dist/mock-requester.d.ts +5 -0
- package/dist/mock-requester.js.flow +22 -0
- package/dist/respond-with.d.ts +57 -0
- package/dist/respond-with.js.flow +91 -0
- package/dist/response-impl.d.ts +1 -0
- package/dist/response-impl.js.flow +8 -0
- package/dist/settle-controller.d.ts +19 -0
- package/dist/settle-controller.js.flow +26 -0
- package/dist/settle-signal.d.ts +18 -0
- package/dist/settle-signal.js.flow +26 -0
- package/dist/types.d.ts +25 -0
- package/dist/types.js.flow +46 -0
- package/package.json +6 -6
- package/src/__tests__/{mock-requester.test.js → mock-requester.test.ts} +1 -2
- package/src/__tests__/{respond-with.test.js → respond-with.test.ts} +0 -1
- package/src/__tests__/response-impl.test.js +1 -1
- package/src/__tests__/{settle-controller.test.js → settle-controller.test.ts} +0 -1
- package/src/__tests__/{settle-signal.test.js → settle-signal.test.ts} +0 -1
- package/src/fetch/__tests__/__snapshots__/{mock-fetch.test.js.snap → mock-fetch.test.ts.snap} +3 -3
- package/src/fetch/__tests__/{fetch-request-matches-mock.test.js → fetch-request-matches-mock.test.ts} +5 -6
- package/src/fetch/__tests__/{mock-fetch.test.js → mock-fetch.test.ts} +1 -2
- package/src/fetch/{fetch-request-matches-mock.js → fetch-request-matches-mock.ts} +3 -2
- package/src/fetch/{mock-fetch.js → mock-fetch.ts} +2 -3
- package/src/fetch/{types.js → types.ts} +5 -6
- package/src/fixtures/__tests__/{fixtures.test.js → fixtures.test.tsx} +6 -4
- package/src/fixtures/{fixtures.basic.stories.js → fixtures.basic.stories.tsx} +16 -13
- package/src/fixtures/{fixtures.defaultwrapper.stories.js → fixtures.defaultwrapper.stories.tsx} +8 -8
- package/src/fixtures/{fixtures.js → fixtures.tsx} +16 -11
- package/src/fixtures/{types.js → types.ts} +9 -13
- package/src/gql/__tests__/{gql-request-matches-mock.test.js → gql-request-matches-mock.test.ts} +17 -18
- package/src/gql/__tests__/{mock-gql-fetch.test.js → mock-gql-fetch.test.tsx} +44 -45
- package/src/gql/__tests__/{wb-data-integration.test.js → wb-data-integration.test.tsx} +21 -22
- package/src/gql/{gql-request-matches-mock.js → gql-request-matches-mock.ts} +1 -4
- package/src/gql/{mock-gql-fetch.js → mock-gql-fetch.ts} +3 -6
- package/src/gql/types.ts +33 -0
- package/src/harness/__tests__/{hook-harness.test.js → hook-harness.test.ts} +5 -6
- package/src/harness/__tests__/{make-hook-harness.test.js → make-hook-harness.test.tsx} +10 -11
- package/src/harness/__tests__/{make-test-harness.test.js → make-test-harness.test.tsx} +4 -5
- package/src/harness/__tests__/{render-adapters.test.js → render-adapters.test.tsx} +8 -9
- package/src/harness/__tests__/{test-harness.test.js → test-harness.test.ts} +5 -6
- package/src/harness/__tests__/{types.flowtest.js → types.flowtest.tsx} +24 -27
- package/src/harness/adapters/__tests__/{css.test.js → css.test.tsx} +2 -3
- package/src/harness/adapters/__tests__/{data.test.js → data.test.tsx} +4 -3
- package/src/harness/adapters/__tests__/{portal.test.js → portal.test.tsx} +0 -1
- package/src/harness/adapters/__tests__/{router.test.js → router.test.tsx} +75 -56
- package/src/harness/adapters/{adapters.js → adapters.ts} +1 -2
- package/src/harness/adapters/{css.js → css.tsx} +21 -18
- package/src/harness/adapters/{data.js → data.tsx} +7 -5
- package/src/harness/adapters/{portal.js → portal.tsx} +3 -4
- package/src/harness/adapters/router.tsx +218 -0
- package/src/harness/{hook-harness.js → hook-harness.ts} +2 -3
- package/src/harness/{make-hook-harness.js → make-hook-harness.ts} +6 -6
- package/src/harness/{make-test-harness.js → make-test-harness.tsx} +17 -20
- package/src/harness/{render-adapters.js → render-adapters.ts} +4 -4
- package/src/harness/{test-harness.js → test-harness.ts} +1 -12
- package/src/harness/{types.js → types.ts} +14 -12
- package/src/{index.js → index.ts} +0 -2
- package/src/{mock-requester.js → mock-requester.ts} +4 -3
- package/src/{respond-with.js → respond-with.ts} +42 -37
- package/src/{response-impl.js → response-impl.ts} +1 -2
- package/src/{settle-controller.js → settle-controller.ts} +9 -7
- package/src/{settle-signal.js → settle-signal.ts} +10 -7
- package/src/types.ts +40 -0
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/src/__docs__/_overview_.stories.mdx +0 -18
- package/src/__docs__/_overview_fixtures.stories.mdx +0 -18
- package/src/__docs__/_overview_mocking.stories.mdx +0 -14
- package/src/__docs__/_overview_test_harness.stories.mdx +0 -18
- package/src/__docs__/exports.fixtures.stories.mdx +0 -31
- package/src/__docs__/exports.harness-adapters.stories.mdx +0 -187
- package/src/__docs__/exports.hook-harness.stories.mdx +0 -22
- package/src/__docs__/exports.make-hook-harness.stories.mdx +0 -25
- package/src/__docs__/exports.make-test-harness.stories.mdx +0 -28
- package/src/__docs__/exports.mock-fetch.stories.mdx +0 -40
- package/src/__docs__/exports.mock-gql-fetch.stories.mdx +0 -64
- package/src/__docs__/exports.respond-with.stories.mdx +0 -84
- package/src/__docs__/exports.settle-controller.stories.mdx +0 -32
- package/src/__docs__/exports.test-harness.stories.mdx +0 -23
- package/src/__docs__/types.fetch-mock-fn.stories.mdx +0 -22
- package/src/__docs__/types.fetch-mock-operation.stories.mdx +0 -18
- package/src/__docs__/types.fixture-fn.stories.mdx +0 -46
- package/src/__docs__/types.fixture-props.stories.mdx +0 -20
- package/src/__docs__/types.get-props-options.stories.mdx +0 -52
- package/src/__docs__/types.gql-fetch-mock-fn.stories.mdx +0 -27
- package/src/__docs__/types.gql-mock-operation.stories.mdx +0 -26
- package/src/__docs__/types.mock-response.stories.mdx +0 -22
- package/src/__docs__/types.test-harness-adapter.stories.mdx +0 -21
- package/src/__docs__/types.test-harness-adapters.stories.mdx +0 -46
- package/src/__docs__/types.test-harness-config.stories.mdx +0 -18
- package/src/__docs__/types.test-harness-configs.stories.mdx +0 -59
- package/src/gql/types.js +0 -34
- package/src/harness/adapters/router.js +0 -206
- package/src/types.js +0 -39
- /package/src/harness/adapters/__tests__/__snapshots__/{router.test.js.snap → router.test.tsx.snap} +0 -0
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
import {withRouter, Prompt} from "react-router-dom";
|
|
4
3
|
import {render} from "@testing-library/react";
|
|
@@ -25,17 +24,22 @@ describe("Router.adapter", () => {
|
|
|
25
24
|
${"string"} | ${"/math"}
|
|
26
25
|
${"location"} | ${{location: "/math"}}
|
|
27
26
|
${"full"} | ${{initialEntries: ["/math"]}}
|
|
28
|
-
`("with $type config", ({config}) => {
|
|
27
|
+
`("with $type config", ({config}: any) => {
|
|
29
28
|
it("should allow navigation", () => {
|
|
30
29
|
// Arrange
|
|
31
30
|
const historyListen = jest.fn();
|
|
32
|
-
const HistoryListener = withRouter(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
31
|
+
const HistoryListener = withRouter(
|
|
32
|
+
({history}: any): React.ReactElement | null => {
|
|
33
|
+
React.useEffect(
|
|
34
|
+
() => history.listen(historyListen),
|
|
35
|
+
[history],
|
|
36
|
+
);
|
|
37
|
+
if (history.location.pathname === "/math") {
|
|
38
|
+
history.push("/math/calculator");
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
},
|
|
42
|
+
);
|
|
39
43
|
|
|
40
44
|
// Act
|
|
41
45
|
render(Router.adapter(<HistoryListener />, config));
|
|
@@ -47,15 +51,17 @@ describe("Router.adapter", () => {
|
|
|
47
51
|
it("should have default route match of root /", () => {
|
|
48
52
|
// Arrange
|
|
49
53
|
const matchCatcherFn = jest.fn();
|
|
50
|
-
const MatchCatcher = withRouter(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
history.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
const MatchCatcher = withRouter(
|
|
55
|
+
({match, history}: any): React.ReactElement | null => {
|
|
56
|
+
React.useEffect(() => {
|
|
57
|
+
if (history.location.pathname === "/math") {
|
|
58
|
+
history.push("/math/calculator");
|
|
59
|
+
}
|
|
60
|
+
matchCatcherFn(match);
|
|
61
|
+
}, [match, history]);
|
|
62
|
+
return null;
|
|
63
|
+
},
|
|
64
|
+
);
|
|
59
65
|
|
|
60
66
|
// Act
|
|
61
67
|
render(Router.adapter(<MatchCatcher />, config));
|
|
@@ -74,16 +80,18 @@ describe("Router.adapter", () => {
|
|
|
74
80
|
type | config
|
|
75
81
|
${"location"} | ${{location: "/math/calculator", path: "/math/*"}}
|
|
76
82
|
${"full"} | ${{initialEntries: ["/math/calculator"], path: "/math/*"}}
|
|
77
|
-
`("with $type config including path", ({config}) => {
|
|
83
|
+
`("with $type config including path", ({config}: any) => {
|
|
78
84
|
it("should include routing for the given path", () => {
|
|
79
85
|
// Arrange
|
|
80
86
|
const matchCatcherFn = jest.fn();
|
|
81
|
-
const MatchCatcher = withRouter(
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
+
const MatchCatcher = withRouter(
|
|
88
|
+
({match}: any): React.ReactElement | null => {
|
|
89
|
+
React.useEffect(() => {
|
|
90
|
+
matchCatcherFn(match);
|
|
91
|
+
}, [match]);
|
|
92
|
+
return null;
|
|
93
|
+
},
|
|
94
|
+
);
|
|
87
95
|
|
|
88
96
|
// Act
|
|
89
97
|
render(Router.adapter(<MatchCatcher />, config));
|
|
@@ -106,7 +114,7 @@ describe("Router.adapter", () => {
|
|
|
106
114
|
const badConfig = {
|
|
107
115
|
...config,
|
|
108
116
|
path: "/something/else/entirely",
|
|
109
|
-
};
|
|
117
|
+
} as const;
|
|
110
118
|
|
|
111
119
|
// Act
|
|
112
120
|
const underTest = () =>
|
|
@@ -121,13 +129,18 @@ describe("Router.adapter", () => {
|
|
|
121
129
|
it("should not navigate", () => {
|
|
122
130
|
// Arrange
|
|
123
131
|
const historyListen = jest.fn();
|
|
124
|
-
const HistoryListener = withRouter(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
132
|
+
const HistoryListener = withRouter(
|
|
133
|
+
({history}: any): React.ReactElement | null => {
|
|
134
|
+
React.useEffect(
|
|
135
|
+
() => history.listen(historyListen),
|
|
136
|
+
[history],
|
|
137
|
+
);
|
|
138
|
+
if (history.location.pathname === "/math") {
|
|
139
|
+
history.push("/math/calculator");
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
},
|
|
143
|
+
);
|
|
131
144
|
|
|
132
145
|
// Act
|
|
133
146
|
render(
|
|
@@ -146,12 +159,14 @@ describe("Router.adapter", () => {
|
|
|
146
159
|
it("should use the defaultConfig location if initialEntries is empty", () => {
|
|
147
160
|
// Arrange
|
|
148
161
|
const matchCatcherFn = jest.fn();
|
|
149
|
-
const MatchCatcher = withRouter(
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
162
|
+
const MatchCatcher = withRouter(
|
|
163
|
+
({match, history}: any): React.ReactElement | null => {
|
|
164
|
+
React.useEffect(() => {
|
|
165
|
+
matchCatcherFn(match);
|
|
166
|
+
}, [match, history]);
|
|
167
|
+
return null;
|
|
168
|
+
},
|
|
169
|
+
);
|
|
155
170
|
|
|
156
171
|
// Act
|
|
157
172
|
render(
|
|
@@ -171,12 +186,14 @@ describe("Router.adapter", () => {
|
|
|
171
186
|
it("should set initialIndex prop on MemoryRouter if given in configuration", () => {
|
|
172
187
|
// Arrange
|
|
173
188
|
const matchCatcherFn = jest.fn();
|
|
174
|
-
const MatchCatcher = withRouter(
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
189
|
+
const MatchCatcher = withRouter(
|
|
190
|
+
({match}: any): React.ReactElement | null => {
|
|
191
|
+
React.useEffect(() => {
|
|
192
|
+
matchCatcherFn(match);
|
|
193
|
+
}, [match]);
|
|
194
|
+
return null;
|
|
195
|
+
},
|
|
196
|
+
);
|
|
180
197
|
|
|
181
198
|
// Act
|
|
182
199
|
render(
|
|
@@ -199,20 +216,22 @@ describe("Router.adapter", () => {
|
|
|
199
216
|
// Arrange
|
|
200
217
|
const getUserConfirmationSpy = jest
|
|
201
218
|
.fn()
|
|
202
|
-
.mockImplementation((message, cb) => {
|
|
219
|
+
.mockImplementation((message: any, cb: any) => {
|
|
203
220
|
cb(true);
|
|
204
221
|
});
|
|
205
222
|
const matchCatcherFn = jest.fn();
|
|
206
|
-
const MatchCatcher = withRouter(
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
223
|
+
const MatchCatcher = withRouter(
|
|
224
|
+
({match, history}: any): React.ReactElement => {
|
|
225
|
+
React.useEffect(() => {
|
|
226
|
+
if (history.location.pathname === "/location/old") {
|
|
227
|
+
// Fire off a location change.
|
|
228
|
+
history.goForward();
|
|
229
|
+
}
|
|
230
|
+
matchCatcherFn(match);
|
|
231
|
+
}, [match, history]);
|
|
232
|
+
return <Prompt message="Are you sure?" />;
|
|
233
|
+
},
|
|
234
|
+
);
|
|
216
235
|
|
|
217
236
|
// Act
|
|
218
237
|
render(
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as css from "./css";
|
|
3
2
|
import * as data from "./data";
|
|
4
3
|
import * as portal from "./portal";
|
|
@@ -20,7 +19,7 @@ export const DefaultAdapters = {
|
|
|
20
19
|
data: data.adapter,
|
|
21
20
|
portal: portal.adapter,
|
|
22
21
|
router: router.adapter,
|
|
23
|
-
};
|
|
22
|
+
} as const;
|
|
24
23
|
|
|
25
24
|
/**
|
|
26
25
|
* The default configurations to use with the `DefaultAdapters`.
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
|
|
4
3
|
import type {CSSProperties} from "aphrodite";
|
|
@@ -9,36 +8,40 @@ type Config =
|
|
|
9
8
|
| string
|
|
10
9
|
| Array<string>
|
|
11
10
|
| CSSProperties
|
|
12
|
-
| {
|
|
13
|
-
classes: Array<string
|
|
14
|
-
style: CSSProperties
|
|
15
|
-
|
|
11
|
+
| {
|
|
12
|
+
classes: Array<string>;
|
|
13
|
+
style: CSSProperties;
|
|
14
|
+
};
|
|
16
15
|
|
|
17
16
|
// The default configuration is to omit this adapter.
|
|
18
|
-
export const defaultConfig:
|
|
17
|
+
export const defaultConfig: Config | null | undefined = null;
|
|
19
18
|
|
|
20
19
|
const normalizeConfig = (
|
|
21
20
|
config: Config,
|
|
22
|
-
): {
|
|
21
|
+
): {
|
|
22
|
+
classes: Array<string>;
|
|
23
|
+
style: CSSProperties;
|
|
24
|
+
} => {
|
|
23
25
|
if (typeof config === "string") {
|
|
24
|
-
return {classes: [config], style:
|
|
26
|
+
return {classes: [config], style: {} as Partial<CSSProperties>};
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
if (Array.isArray(config)) {
|
|
28
|
-
return {classes: config, style:
|
|
30
|
+
return {classes: config, style: {} as Partial<CSSProperties>};
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
if (typeof config === "object") {
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
if (
|
|
35
|
+
"classes" in config &&
|
|
36
|
+
config.classes != null &&
|
|
37
|
+
"style" in config &&
|
|
38
|
+
config.style != null
|
|
39
|
+
) {
|
|
36
40
|
return config;
|
|
37
41
|
}
|
|
38
42
|
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
// $FlowIgnore[prop-missing]
|
|
43
|
+
// @ts-expect-error: at this point, `CSSProperties` is the only thing
|
|
44
|
+
// that `config` can be.
|
|
42
45
|
return {classes: [], style: config};
|
|
43
46
|
}
|
|
44
47
|
|
|
@@ -49,9 +52,9 @@ const normalizeConfig = (
|
|
|
49
52
|
* Test harness adapter for adding CSS to the harnessed component wrapper.
|
|
50
53
|
*/
|
|
51
54
|
export const adapter: TestHarnessAdapter<Config> = (
|
|
52
|
-
children: React.
|
|
55
|
+
children: React.ReactNode,
|
|
53
56
|
config: Config,
|
|
54
|
-
): React.
|
|
57
|
+
): React.ReactElement<any> => {
|
|
55
58
|
const {classes, style} = normalizeConfig(config);
|
|
56
59
|
return (
|
|
57
60
|
<div
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
import {InterceptRequests} from "@khanacademy/wonder-blocks-data";
|
|
4
3
|
import type {TestHarnessAdapter} from "../types";
|
|
5
4
|
|
|
6
|
-
type Interceptor =
|
|
5
|
+
type Interceptor = JSX.LibraryManagedAttributes<
|
|
6
|
+
typeof InterceptRequests,
|
|
7
|
+
React.ComponentProps<typeof InterceptRequests>
|
|
8
|
+
>["interceptor"];
|
|
7
9
|
|
|
8
10
|
type Config = Interceptor | Array<Interceptor>;
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Default configuration for the Wonder Blocks Data adapter.
|
|
12
14
|
*/
|
|
13
|
-
export const defaultConfig =
|
|
15
|
+
export const defaultConfig = [] as Array<Interceptor>;
|
|
14
16
|
|
|
15
17
|
/**
|
|
16
18
|
* Test harness adapter to mock Wonder Blocks Data usage.
|
|
@@ -20,9 +22,9 @@ export const defaultConfig = ([]: Array<Interceptor>);
|
|
|
20
22
|
* are not overriding one another.
|
|
21
23
|
*/
|
|
22
24
|
export const adapter: TestHarnessAdapter<Config> = (
|
|
23
|
-
children: React.
|
|
25
|
+
children: React.ReactNode,
|
|
24
26
|
config: Config,
|
|
25
|
-
): React.
|
|
27
|
+
): React.ReactElement<any> => {
|
|
26
28
|
// First we render the cache intercepts.
|
|
27
29
|
let currentChildren = children;
|
|
28
30
|
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
|
|
4
3
|
import type {TestHarnessAdapter} from "../types";
|
|
@@ -6,7 +5,7 @@ import type {TestHarnessAdapter} from "../types";
|
|
|
6
5
|
type Config = string;
|
|
7
6
|
|
|
8
7
|
// The default configuration is to omit this adapter.
|
|
9
|
-
export const defaultConfig:
|
|
8
|
+
export const defaultConfig: Config | null | undefined = null;
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* Test harness adapter for supporting portals.
|
|
@@ -16,9 +15,9 @@ export const defaultConfig: ?Config = null;
|
|
|
16
15
|
* identifier.
|
|
17
16
|
*/
|
|
18
17
|
export const adapter: TestHarnessAdapter<Config> = (
|
|
19
|
-
children: React.
|
|
18
|
+
children: React.ReactNode,
|
|
20
19
|
config: Config,
|
|
21
|
-
): React.
|
|
20
|
+
): React.ReactElement<any> => (
|
|
22
21
|
<>
|
|
23
22
|
<div id={config} data-test-id={config} />
|
|
24
23
|
{children}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import {StaticRouter, MemoryRouter, Route, Switch} from "react-router-dom";
|
|
4
|
+
|
|
5
|
+
// @ts-expect-error [FEI-5019] - TS2305 - Module '"react-router-dom"' has no exported member 'LocationShape'. | TS2305 - Module '"react-router-dom"' has no exported member 'Location'.
|
|
6
|
+
import type {LocationShape, Location} from "react-router-dom";
|
|
7
|
+
import type {TestHarnessAdapter} from "../types";
|
|
8
|
+
|
|
9
|
+
type MemoryRouterProps = JSX.LibraryManagedAttributes<
|
|
10
|
+
typeof MemoryRouter,
|
|
11
|
+
React.ComponentProps<typeof MemoryRouter>
|
|
12
|
+
>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Configuration for the withLocation test harness adapter.
|
|
16
|
+
*/
|
|
17
|
+
type Config = // The initial location to use.
|
|
18
|
+
|
|
19
|
+
| Readonly<
|
|
20
|
+
| {
|
|
21
|
+
/**
|
|
22
|
+
* See MemoryRouter prop for initialEntries.
|
|
23
|
+
*/
|
|
24
|
+
initialEntries: MemoryRouterProps["initialEntries"];
|
|
25
|
+
/**
|
|
26
|
+
* See MemoryRouter prop for initialIndex.
|
|
27
|
+
*/
|
|
28
|
+
initialIndex?: MemoryRouterProps["initialIndex"];
|
|
29
|
+
/**
|
|
30
|
+
* See MemoryRouter prop for getUserConfirmation.
|
|
31
|
+
*/
|
|
32
|
+
getUserConfirmation?: MemoryRouterProps["getUserConfirmation"];
|
|
33
|
+
/**
|
|
34
|
+
* A path match to use.
|
|
35
|
+
*
|
|
36
|
+
* When this is specified, the harnessed component will be
|
|
37
|
+
* rendered inside a `Route` handler with this path.
|
|
38
|
+
*
|
|
39
|
+
* If the path matches the location, then the route will
|
|
40
|
+
* render the component.
|
|
41
|
+
*
|
|
42
|
+
* If the path does not match the location, then the route
|
|
43
|
+
* will not render the component.
|
|
44
|
+
*/
|
|
45
|
+
path?: string;
|
|
46
|
+
}
|
|
47
|
+
| {
|
|
48
|
+
/**
|
|
49
|
+
* The location to use.
|
|
50
|
+
*/
|
|
51
|
+
location: string | Location;
|
|
52
|
+
/**
|
|
53
|
+
* Force the use of a StaticRouter, instead of MemoryRouter.
|
|
54
|
+
*/
|
|
55
|
+
forceStatic: true;
|
|
56
|
+
/**
|
|
57
|
+
* A path match to use.
|
|
58
|
+
*
|
|
59
|
+
* When this is specified, the harnessed component will be
|
|
60
|
+
* rendered inside a `Route` handler with this path.
|
|
61
|
+
*
|
|
62
|
+
* If the path matches the location, then the route will
|
|
63
|
+
* render the component.
|
|
64
|
+
*
|
|
65
|
+
* If the path does not match the location, then the route
|
|
66
|
+
* will not render the component.
|
|
67
|
+
*/
|
|
68
|
+
path?: string;
|
|
69
|
+
}
|
|
70
|
+
| {
|
|
71
|
+
/**
|
|
72
|
+
* The initial location to use.
|
|
73
|
+
*/
|
|
74
|
+
location: string | LocationShape;
|
|
75
|
+
/**
|
|
76
|
+
* A path match to use.
|
|
77
|
+
*
|
|
78
|
+
* When this is specified, the harnessed component will be
|
|
79
|
+
* rendered inside a `Route` handler with this path.
|
|
80
|
+
*
|
|
81
|
+
* If the path matches the location, then the route will
|
|
82
|
+
* render the component.
|
|
83
|
+
*
|
|
84
|
+
* If the path does not match the location, then the route
|
|
85
|
+
* will not render the component.
|
|
86
|
+
*/
|
|
87
|
+
path?: string;
|
|
88
|
+
}
|
|
89
|
+
>
|
|
90
|
+
| string;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The default configuration for this adapter.
|
|
94
|
+
*/
|
|
95
|
+
export const defaultConfig = {location: "/"} as const;
|
|
96
|
+
|
|
97
|
+
const maybeWithRoute = (
|
|
98
|
+
children: React.ReactNode,
|
|
99
|
+
path?: string | null,
|
|
100
|
+
): React.ReactElement => {
|
|
101
|
+
if (path == null) {
|
|
102
|
+
// @ts-expect-error [FEI-5019] - TS2322 - Type 'ReactNode' is not assignable to type 'ReactElement<any, string | JSXElementConstructor<any>>'.
|
|
103
|
+
return children;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<Switch>
|
|
108
|
+
<Route exact={true} path={path}>
|
|
109
|
+
{children}
|
|
110
|
+
</Route>
|
|
111
|
+
<Route
|
|
112
|
+
path="*"
|
|
113
|
+
render={() => {
|
|
114
|
+
throw new Error(
|
|
115
|
+
"The configured path must match the configured location or your harnessed component will not render.",
|
|
116
|
+
);
|
|
117
|
+
}}
|
|
118
|
+
/>
|
|
119
|
+
</Switch>
|
|
120
|
+
);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Adapter that sets up a router and AppShell location-specific contexts.
|
|
125
|
+
*
|
|
126
|
+
* This allows you to ensure that components are being tested in the
|
|
127
|
+
* AppShell world.
|
|
128
|
+
*
|
|
129
|
+
* NOTE(somewhatabstract): The AppShell component itself already does
|
|
130
|
+
* the work of setting up routing and the AppShellContext and so using this
|
|
131
|
+
* adapter with the App component will have zero-effect since AppShell will
|
|
132
|
+
* override it.
|
|
133
|
+
*/
|
|
134
|
+
export const adapter: TestHarnessAdapter<Config> = (
|
|
135
|
+
children: React.ReactNode,
|
|
136
|
+
config: Config,
|
|
137
|
+
): React.ReactElement<any> => {
|
|
138
|
+
if (typeof config === "string") {
|
|
139
|
+
config = {
|
|
140
|
+
location: config,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Wrap children with the various contexts and routes, as per the config.
|
|
145
|
+
const wrappedWithRoute = maybeWithRoute(children, config.path);
|
|
146
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'forceStatic' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
147
|
+
if (config.forceStatic) {
|
|
148
|
+
/**
|
|
149
|
+
* There may be times (SSR testing comes to mind) where we will be
|
|
150
|
+
* really strict about not permitting client-side navigation events.
|
|
151
|
+
*/
|
|
152
|
+
return (
|
|
153
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'location' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
154
|
+
<StaticRouter location={config.location} context={{}}>
|
|
155
|
+
{wrappedWithRoute}
|
|
156
|
+
</StaticRouter>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* OK, we must be OK with a memory router.
|
|
162
|
+
*
|
|
163
|
+
* There are two flavors of config for this. The easy one with just a
|
|
164
|
+
* location, and the complex one for those gnarlier setups.
|
|
165
|
+
*
|
|
166
|
+
* First, the easy one.
|
|
167
|
+
*/
|
|
168
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'location' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
169
|
+
if (typeof config.location !== "undefined") {
|
|
170
|
+
return (
|
|
171
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'location' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
172
|
+
<MemoryRouter initialEntries={[config.location]}>
|
|
173
|
+
{wrappedWithRoute}
|
|
174
|
+
</MemoryRouter>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* If it's not the easy one, it should be the complex one.
|
|
180
|
+
* Let's make sure we have good data (also keeps TypeScript happy).
|
|
181
|
+
*/
|
|
182
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'initialEntries' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
183
|
+
if (typeof config.initialEntries === "undefined") {
|
|
184
|
+
throw new Error(
|
|
185
|
+
"A location or initial history entries must be provided.",
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* What should happen if no entries were in the array?
|
|
191
|
+
* It likely uses the root one anyway, but a consistent API is what
|
|
192
|
+
* we want, so let's ensure we always have our default location at least.
|
|
193
|
+
*/
|
|
194
|
+
const entries =
|
|
195
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'initialEntries' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
196
|
+
config.initialEntries.length === 0
|
|
197
|
+
? [defaultConfig.location]
|
|
198
|
+
: // @ts-expect-error [FEI-5019] - TS2339 - Property 'initialEntries' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
199
|
+
config.initialEntries;
|
|
200
|
+
|
|
201
|
+
// Memory router doesn't allow us to pass maybe types in its TypeScript types.
|
|
202
|
+
// So let's build props then spread them.
|
|
203
|
+
const routerProps: MemoryRouterProps = {
|
|
204
|
+
initialEntries: entries,
|
|
205
|
+
};
|
|
206
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'initialIndex' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
207
|
+
if (config.initialIndex != null) {
|
|
208
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'initialIndex' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
209
|
+
routerProps.initialIndex = config.initialIndex;
|
|
210
|
+
}
|
|
211
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'getUserConfirmation' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
212
|
+
if (config.getUserConfirmation != null) {
|
|
213
|
+
// @ts-expect-error [FEI-5019] - TS2339 - Property 'getUserConfirmation' does not exist on type 'Readonly<{ initialEntries: LocationDescriptor<unknown>[] | undefined; initialIndex?: number | undefined; getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined; path?: string | undefined; } | { ...; } | { ...; }>'.
|
|
214
|
+
routerProps.getUserConfirmation = config.getUserConfirmation;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return <MemoryRouter {...routerProps}>{wrappedWithRoute}</MemoryRouter>;
|
|
218
|
+
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
|
|
4
3
|
import {makeHookHarness} from "./make-hook-harness";
|
|
@@ -16,8 +15,8 @@ import type {TestHarnessConfigs} from "./types";
|
|
|
16
15
|
* function.
|
|
17
16
|
*/
|
|
18
17
|
export const hookHarness: (
|
|
19
|
-
configs?:
|
|
20
|
-
) => React.
|
|
18
|
+
configs?: Partial<TestHarnessConfigs<typeof DefaultAdapters>>,
|
|
19
|
+
) => React.ForwardRefExoticComponent<any> = makeHookHarness(
|
|
21
20
|
DefaultAdapters,
|
|
22
21
|
DefaultConfigs,
|
|
23
22
|
);
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
|
|
4
3
|
import {makeTestHarness} from "./make-test-harness";
|
|
5
4
|
|
|
6
5
|
import type {TestHarnessAdapters, TestHarnessConfigs} from "./types";
|
|
7
6
|
|
|
7
|
+
// @ts-expect-error [FEI-5019] - TS7031 - Binding element 'children' implicitly has an 'any' type.
|
|
8
8
|
const HookHarness = ({children}) => children;
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -21,12 +21,12 @@ const HookHarness = ({children}) => children;
|
|
|
21
21
|
* configs?: $Shape<TestHarnessConfigs<TAdapters>>,
|
|
22
22
|
* ) => React.AbstractComponent<any, any>} A test harness.
|
|
23
23
|
*/
|
|
24
|
-
export const makeHookHarness = <TAdapters
|
|
24
|
+
export const makeHookHarness = <TAdapters extends TestHarnessAdapters>(
|
|
25
25
|
adapters: TAdapters,
|
|
26
26
|
defaultConfigs: TestHarnessConfigs<TAdapters>,
|
|
27
27
|
): ((
|
|
28
|
-
configs?:
|
|
29
|
-
) => React.
|
|
28
|
+
configs?: Partial<TestHarnessConfigs<TAdapters>>,
|
|
29
|
+
) => React.ForwardRefExoticComponent<any>) => {
|
|
30
30
|
const testHarness = makeTestHarness<TAdapters>(adapters, defaultConfigs);
|
|
31
31
|
/**
|
|
32
32
|
* Create a harness to use as a wrapper when rendering hooks.
|
|
@@ -34,6 +34,6 @@ export const makeHookHarness = <TAdapters: TestHarnessAdapters>(
|
|
|
34
34
|
* @param {$Shape<Configs<typeof DefaultAdapters>>} [configs] Any adapter
|
|
35
35
|
* configuration that you want to override from the DefaultConfigs values.
|
|
36
36
|
*/
|
|
37
|
-
return (configs?:
|
|
38
|
-
testHarness<any
|
|
37
|
+
return (configs?: Partial<TestHarnessConfigs<TAdapters>>) =>
|
|
38
|
+
testHarness<any>(HookHarness, configs);
|
|
39
39
|
};
|