@khanacademy/wonder-blocks-testing 7.1.9 → 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 +36 -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} +3 -4
- package/src/__tests__/{respond-with.test.js → respond-with.test.ts} +2 -3
- package/src/__tests__/response-impl.test.js +3 -3
- package/src/__tests__/{settle-controller.test.js → settle-controller.test.ts} +2 -3
- package/src/__tests__/{settle-signal.test.js → settle-signal.test.ts} +1 -2
- 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} +6 -7
- package/src/fetch/__tests__/{mock-fetch.test.js → mock-fetch.test.ts} +3 -4
- package/src/fetch/{fetch-request-matches-mock.js → fetch-request-matches-mock.ts} +4 -3
- package/src/fetch/{mock-fetch.js → mock-fetch.ts} +5 -6
- package/src/fetch/types.ts +14 -0
- package/src/fixtures/__tests__/{fixtures.test.js → fixtures.test.tsx} +7 -5
- package/src/fixtures/{fixtures.basic.stories.js → fixtures.basic.stories.tsx} +17 -14
- package/src/fixtures/{fixtures.defaultwrapper.stories.js → fixtures.defaultwrapper.stories.tsx} +9 -9
- package/src/fixtures/{fixtures.js → fixtures.tsx} +17 -12
- 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} +18 -19
- package/src/gql/__tests__/{mock-gql-fetch.test.js → mock-gql-fetch.test.tsx} +46 -47
- package/src/gql/__tests__/{wb-data-integration.test.js → wb-data-integration.test.tsx} +23 -24
- package/src/gql/{gql-request-matches-mock.js → gql-request-matches-mock.ts} +2 -5
- package/src/gql/mock-gql-fetch.ts +15 -0
- package/src/gql/types.ts +33 -0
- package/src/harness/__tests__/{hook-harness.test.js → hook-harness.test.ts} +13 -14
- package/src/harness/__tests__/{make-hook-harness.test.js → make-hook-harness.test.tsx} +12 -13
- package/src/harness/__tests__/{make-test-harness.test.js → make-test-harness.test.tsx} +7 -8
- package/src/harness/__tests__/{render-adapters.test.js → render-adapters.test.tsx} +10 -11
- package/src/harness/__tests__/{test-harness.test.js → test-harness.test.ts} +13 -14
- package/src/harness/__tests__/{types.flowtest.js → types.flowtest.tsx} +25 -28
- package/src/harness/adapters/__tests__/{css.test.js → css.test.tsx} +3 -4
- package/src/harness/adapters/__tests__/{data.test.js → data.test.tsx} +5 -4
- package/src/harness/adapters/__tests__/{portal.test.js → portal.test.tsx} +1 -2
- package/src/harness/adapters/__tests__/{router.test.js → router.test.tsx} +76 -57
- package/src/harness/adapters/{adapters.js → adapters.ts} +6 -7
- package/src/harness/adapters/{css.js → css.tsx} +22 -19
- package/src/harness/adapters/{data.js → data.tsx} +8 -6
- package/src/harness/adapters/{portal.js → portal.tsx} +4 -5
- package/src/harness/adapters/router.tsx +218 -0
- package/src/harness/{hook-harness.js → hook-harness.ts} +5 -6
- package/src/harness/{make-hook-harness.js → make-hook-harness.ts} +8 -8
- package/src/harness/{make-test-harness.js → make-test-harness.tsx} +19 -22
- package/src/harness/{render-adapters.js → render-adapters.ts} +5 -5
- package/src/harness/test-harness.ts +13 -0
- package/src/harness/{types.js → types.ts} +14 -12
- package/src/index.ts +20 -0
- package/src/{mock-requester.js → mock-requester.ts} +6 -5
- package/src/{respond-with.js → respond-with.ts} +45 -40
- package/src/{response-impl.js → response-impl.ts} +1 -2
- package/src/settle-controller.ts +37 -0
- 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/fetch/types.js +0 -15
- package/src/gql/mock-gql-fetch.js +0 -18
- package/src/gql/types.js +0 -34
- package/src/harness/adapters/router.js +0 -206
- package/src/harness/test-harness.js +0 -24
- package/src/index.js +0 -26
- package/src/settle-controller.js +0 -35
- package/src/types.js +0 -39
- /package/src/harness/adapters/__tests__/__snapshots__/{router.test.js.snap → router.test.tsx.snap} +0 -0
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Test Harness / Exports / harnessAdapters"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# harnessAdapters
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
/**
|
|
16
|
-
* The default adapters provided by Wonder Blocks.
|
|
17
|
-
*/
|
|
18
|
-
const DefaultAdapters = {
|
|
19
|
-
css: css.adapter,
|
|
20
|
-
data: data.adapter,
|
|
21
|
-
portal: portal.adapter,
|
|
22
|
-
router: router.adapter,
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The default configurations to use with the `DefaultAdapters`.
|
|
27
|
-
*/
|
|
28
|
-
const DefaultConfigs: Configs<typeof DefaultAdapters> = {
|
|
29
|
-
css: css.defaultConfig,
|
|
30
|
-
data: data.defaultConfig,
|
|
31
|
-
portal: portal.defaultConfig,
|
|
32
|
-
router: router.defaultConfig,
|
|
33
|
-
};
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
These are the adapters available for use with the test harness framework and their default configurations. The [`testHarness()`](/docs/testing-test-harness-exports-testharness--page) and [`hookHarness()`](/docs/testing-test-harness-exports-hookharness--page) methods are constructed using these.
|
|
37
|
-
|
|
38
|
-
If you want to make your own test harness or hook harness using some of these defaults and some of your own, you can combine them into your call to [`makeTestHarness()`](/docs/testing-test-harness-exports-maketestharness--page) or [`makeHookHarness()`](/docs/testing-test-harness-exports-makehookharness--page).
|
|
39
|
-
|
|
40
|
-
There are four build-in adapters:
|
|
41
|
-
- [`css`](#css)
|
|
42
|
-
- [`data`](#data)
|
|
43
|
-
- [`portal`](#portal)
|
|
44
|
-
- [`router`](#router)
|
|
45
|
-
|
|
46
|
-
## css
|
|
47
|
-
|
|
48
|
-
The `css` adapter can be used to apply CSS classes and adhoc styles around the harnessed component.
|
|
49
|
-
|
|
50
|
-
```ts
|
|
51
|
-
type Config =
|
|
52
|
-
| string
|
|
53
|
-
| Array<string>
|
|
54
|
-
| CSSProperties
|
|
55
|
-
| {|
|
|
56
|
-
classes: Array<string>,
|
|
57
|
-
style: CSSProperties,
|
|
58
|
-
|};
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
The configuration for this adapter can be a class name, an array of class names, an Aphrodite-compatible CSS style object, or an object definining both an array of `classes` and a `style` object.
|
|
62
|
-
|
|
63
|
-
The default configuration for this adapter is for it to not be used.
|
|
64
|
-
|
|
65
|
-
## data
|
|
66
|
-
|
|
67
|
-
The `data` adapter provides a convenient way to intercept Wonder Blocks Data requests. It renders one [`InterceptRequests`](/docs/data-exports-interceptrequests--page) component per configured interceptor.
|
|
68
|
-
|
|
69
|
-
```ts
|
|
70
|
-
type Interceptor = React.ElementConfig<typeof InterceptRequests>["interceptor"];
|
|
71
|
-
|
|
72
|
-
type Config = Interceptor | Array<Interceptor>;
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
The configuration is one or more interceptors, as defined by the `interceptor` prop of the [`InterceptRequests`](/docs/data-exports-interceptrequests--page) component, which is effectively.
|
|
76
|
-
|
|
77
|
-
The default configuration for this adapter is for there to be no interceptors.
|
|
78
|
-
|
|
79
|
-
## portal
|
|
80
|
-
|
|
81
|
-
The `portal` adapter ensures there is a mounting point in the DOM for components that rely on rendering with a React portal.
|
|
82
|
-
|
|
83
|
-
```ts
|
|
84
|
-
type Config = string;
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
The configuration is just a string providing the identifier to give the added element. The adapter will then render alongside the harnessed component, an empty `div` element with both the `id` and `data-test-id` attributes set to the configuration value.
|
|
88
|
-
|
|
89
|
-
The default configuration for this adapter is for it to not be used.
|
|
90
|
-
|
|
91
|
-
## router
|
|
92
|
-
|
|
93
|
-
The `router` adapter is useful for testing components that use React Router. It will render either a `MemoryRouter` or `StaticRouter`, depending on configuration. It may also optionally render a `Route` with suitable `path` match, depending on the configuration.
|
|
94
|
-
|
|
95
|
-
```ts
|
|
96
|
-
type Config =
|
|
97
|
-
| $ReadOnly<
|
|
98
|
-
| {|
|
|
99
|
-
/**
|
|
100
|
-
* See MemoryRouter prop for initialEntries.
|
|
101
|
-
*/
|
|
102
|
-
initialEntries: MemoryRouterProps["initialEntries"],
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* See MemoryRouter prop for initialIndex.
|
|
106
|
-
*/
|
|
107
|
-
initialIndex?: MemoryRouterProps["initialIndex"],
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* See MemoryRouter prop for getUserConfirmation.
|
|
111
|
-
*/
|
|
112
|
-
getUserConfirmation?: MemoryRouterProps["getUserConfirmation"],
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* A path match to use.
|
|
116
|
-
*
|
|
117
|
-
* When this is specified, the harnessed component will be
|
|
118
|
-
* rendered inside a `Route` handler with this path.
|
|
119
|
-
*
|
|
120
|
-
* If the path matches the location, then the route will
|
|
121
|
-
* render the component.
|
|
122
|
-
*
|
|
123
|
-
* If the path does not match the location, then the route
|
|
124
|
-
* will not render the component.
|
|
125
|
-
*/
|
|
126
|
-
path?: string,
|
|
127
|
-
|}
|
|
128
|
-
| {|
|
|
129
|
-
/**
|
|
130
|
-
* The location to use.
|
|
131
|
-
*/
|
|
132
|
-
location: string | Location,
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Force the use of a StaticRouter, instead of MemoryRouter.
|
|
136
|
-
*/
|
|
137
|
-
forceStatic: true,
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* A path match to use.
|
|
141
|
-
*
|
|
142
|
-
* When this is specified, the harnessed component will be
|
|
143
|
-
* rendered inside a `Route` handler with this path.
|
|
144
|
-
*
|
|
145
|
-
* If the path matches the location, then the route will
|
|
146
|
-
* render the component.
|
|
147
|
-
*
|
|
148
|
-
* If the path does not match the location, then the route
|
|
149
|
-
* will not render the component.
|
|
150
|
-
*/
|
|
151
|
-
path?: string,
|
|
152
|
-
|}
|
|
153
|
-
| {|
|
|
154
|
-
/**
|
|
155
|
-
* The initial location to use.
|
|
156
|
-
*/
|
|
157
|
-
location: string | LocationShape,
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* A path match to use.
|
|
161
|
-
*
|
|
162
|
-
* When this is specified, the harnessed component will be
|
|
163
|
-
* rendered inside a `Route` handler with this path.
|
|
164
|
-
*
|
|
165
|
-
* If the path matches the location, then the route will
|
|
166
|
-
* render the component.
|
|
167
|
-
*
|
|
168
|
-
* If the path does not match the location, then the route
|
|
169
|
-
* will not render the component.
|
|
170
|
-
*/
|
|
171
|
-
path?: string,
|
|
172
|
-
|},
|
|
173
|
-
>
|
|
174
|
-
// The initial location to use.
|
|
175
|
-
| string;
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
There are four distinct shapes to the configuration:
|
|
179
|
-
|
|
180
|
-
1. An object with `initialEntries` array (see `MemoryRouter` props), optional `initialIndex` and `getUserConfirmation` (see `MemoryRouter` props), and an optional `path` parameter if route matching is required.
|
|
181
|
-
1. An object with `forceStatic` set to true, the location as a `string` or `Location` object, with an optional `path` parameter if route matching is required.
|
|
182
|
-
1. An object specifying the location as a `string` or a `LocationShape` object, with an optional `path` parameter if route matching is required.
|
|
183
|
-
1. A `string` specifying the location to use
|
|
184
|
-
|
|
185
|
-
The first of these provides the most flexibility, basically supporting full configuration of a `MemoryRouter`, which supports history and navigation. The second configuration type is for scenarios where we absolutely do not want navigation, such as server-side rendering scenarios; it forces the use of a `StaticRouter` instance. The third type is for when you just need to render a single location with a matched route path. The fourth and final type allows you to just provide the location as a string.
|
|
186
|
-
|
|
187
|
-
By default, the router adapter is configured with `{location: "/"}`.
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Test Harness / Exports / hookHarness()"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# hookHarness()
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
hookHarness(
|
|
16
|
-
configs?: $Shape<TestHarnessConfigs<typeof DefaultAdapters>>,
|
|
17
|
-
): React.AbstractComponent<any, any>;
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
This method can be used to create a test harness for use with the `wrapper` option of calls like `renderHook()` from `@testing-library/react-hooks` when writing tests for React hooks.
|
|
21
|
-
|
|
22
|
-
This method is created by using [`makeHookHarness`](/docs/testing-test-harness-exports-makehookharness--page) with [`harnessAdapters.DefaultAdapters` and `harnessAdapters.DefaultConfigs`](/docs/testing-test-harness-exports-harnessadapters--page).
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Test Harness / Exports / makeHookHarness()"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# makeHookHarness()
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
makeHookHarness<TAdapters: TestHarnessAdapters>(
|
|
16
|
-
adapters: TAdapters,
|
|
17
|
-
defaultConfigs: TestHarnessConfigs<TAdapters>,
|
|
18
|
-
): ((
|
|
19
|
-
configs?: $Shape<TestHarnessConfigs<TAdapters>>,
|
|
20
|
-
) => React.AbstractComponent<any, any>);
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
This method takes a set of adapters (such as [`harnessAdapters.DefaultAdapters`](/docs/testing-test-harness-exports-harnessadapters--page)) and a set of default configurations for those adapters (such as [`harnessAdapters.DefaultConfigs`](/docs/testing-test-harness-exports-harnessadapters--page)), and returns a function that can be called to create a component that applys those adapters with those default configs, or overrides to those configs.
|
|
24
|
-
|
|
25
|
-
This returned method can then be used for the `wrapper` option of calls like `renderHook()` from `@testing-library/react-hooks` when writing tests for React hooks.
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Test Harness / Exports / makeTestHarness()"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# makeTestHarness()
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
makeTestHarness<TAdapters: TestHarnessAdapters>(
|
|
16
|
-
adapters: TAdapters,
|
|
17
|
-
defaultConfigs: TestHarnessConfigs<TAdapters>,
|
|
18
|
-
): (<-TProps, +Instance = mixed>(
|
|
19
|
-
Component: React.AbstractComponent<TProps, Instance>,
|
|
20
|
-
configs?: $Shape<TestHarnessConfigs<TAdapters>>,
|
|
21
|
-
) => React.AbstractComponent<TProps, Instance>);
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
This method takes a set of adapters (such as [`harnessAdapters.DefaultAdapters`](/docs/testing-test-harness-exports-harnessadapters--page)) and a set of default configurations for those adapters (such as [`harnessAdapters.DefaultConfigs`](/docs/testing-test-harness-exports-harnessadapters--page)), and returns a function that can be called to create a component that applys those adapters with those default configs, or overrides to those configs, around another component.
|
|
25
|
-
|
|
26
|
-
The returned method will ensure refs are forwarded to the harnessed component, and the component the method returns will have the same props as the component it is harnessing.
|
|
27
|
-
|
|
28
|
-
The harnessed component can be used in unit tests, such as in Jest, to reduce boilerplate needed to setup the test case. It can also be used as a a wrapper component when defining a fixture for a component in the Wonder Blocks Testing [Fixtures framework](/docs/testing-fixtures-overview--page).
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Mocking / Exports / mockFetch()"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# mockFetch()
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
mockFetch(): FetchMockFn;
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
The `mockFetch` function provides an API to easily mock `fetch()` responses. It follows the similar patterns of `jest.fn()` and jest mocks whereby the returned value is both a proxy for the fetch function as well as an API for modifying the behavior of that function.
|
|
19
|
-
|
|
20
|
-
# API
|
|
21
|
-
|
|
22
|
-
Besides being a function that fits the `fetch()` signature, the return value of `mockFetch()` has an API to customize the behavior of that function. Used in conjunction with the [`RespondWith`](/docs/testing-mocking-exports-respondwith--page) API, this can create a variety of responses for tests and stories.
|
|
23
|
-
|
|
24
|
-
| Function | Purpose |
|
|
25
|
-
| -------- | ------- |
|
|
26
|
-
| `mockOperation` | When called, any request that matches the defined mock will respond with the given response. |
|
|
27
|
-
| `mockOperationOnce` | When called, the first request that matches the defined mock will respond with the given response. The mock is only used once. |
|
|
28
|
-
|
|
29
|
-
Both of these functions have the same signature:
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
type FetchMockOperationFn = (
|
|
33
|
-
operation: FetchMockOperation,
|
|
34
|
-
response: MockResponse<any>,
|
|
35
|
-
) => FetchMockFn;
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
# Operation Matching
|
|
39
|
-
|
|
40
|
-
The `FetchMockOperation` type is either of type `string` or `RegExp`. When specified as a string, the URL of the request must match the string exactly. When specified as a regular expression, the URL of the request must match the regular expression.
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Mocking / Exports / mockGqlFetch()"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# mockGqlFetch()
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
mockGqlFetch(): GqlFetchMockFn;
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
The `mockGqlFetch` function provides an API to easily mock GraphQL responses for use with the [Wonder Blocks Data GraphQL API](/docs/data-graphql--page). It follows the similar patterns of `jest.fn()` and jest mocks whereby the returned value is both a proxy for the fetch function that is used by [`GqlRouter`](/docs/data-exports-gqlrouter--page) as well as an API for modifying the behavior of that function.
|
|
19
|
-
|
|
20
|
-
# API
|
|
21
|
-
|
|
22
|
-
Besides being a function that fits the [`GqlFetchFn`](/docs/data-types-gqlfetchfn--page) signature, the return value of `mockGqlFetch()` has an API to customize the behavior of that function. Used in conjunction with the [`RespondWith`](/docs/testing-mocking-exports-respondwith--page) API, this can create a variety of GraphQL responses for testing and stories.
|
|
23
|
-
|
|
24
|
-
| Function | Purpose |
|
|
25
|
-
| -------- | ------- |
|
|
26
|
-
| `mockOperation` | When called, any GraphQL operation that matches the defined mock operation will respond with the given response. |
|
|
27
|
-
| `mockOperationOnce` | When called, the first GraphQL operation that matches the defined mock operation will respond with the given response. The mock is only used once. |
|
|
28
|
-
|
|
29
|
-
Both of these functions have the same signature:
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
type GqlMockOperationFn = <
|
|
33
|
-
TData: {...},
|
|
34
|
-
TVariables: {...},
|
|
35
|
-
TContext: GqlContext,
|
|
36
|
-
TResponseData: GraphQLJson<TData>,
|
|
37
|
-
>(
|
|
38
|
-
operation: GqlMockOperation<TData, TVariables, TContext>,
|
|
39
|
-
response: MockResponse<TResponseData>,
|
|
40
|
-
) => GqlFetchMockFn;
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
# Operation Matching
|
|
44
|
-
|
|
45
|
-
The `matchOperation` parameter given to a `mockOperation` or `mockOperationOnce` function is a `GqlMockOperation` defining the actual GraphQL operation to be matched by the mock. The variables and context of the mocked operation change how the mock is matched against requests.
|
|
46
|
-
|
|
47
|
-
```ts
|
|
48
|
-
type GqlMockOperation<
|
|
49
|
-
TData: {...},
|
|
50
|
-
TVariables: {...},
|
|
51
|
-
TContext: GqlContext,
|
|
52
|
-
> = {|
|
|
53
|
-
operation: GqlOperation<TData, TVariables>,
|
|
54
|
-
variables?: TVariables,
|
|
55
|
-
context?: TContext,
|
|
56
|
-
|};
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
1. When `matchOperation.operation` is present but `matchOperation.variables` and `matchOperation.context` are not, the mock will match any request for the
|
|
60
|
-
same operation, regardless of variables or context on the request.
|
|
61
|
-
2. When `matchOperation.variables` is present but `matchOperation.context` is not, the mock will match any request for the same operation with matching variables, regardless of context on the request.
|
|
62
|
-
3. When `matchOperation.context` is present but `matchOperation.variables` is not, the mock will match any request for the same operation with matching context, regardless of variables on the request.
|
|
63
|
-
4. When `matchOperation.variables` and `matchOperation.context` are present, the mock will match any request for the same operation with matching variables and context.
|
|
64
|
-
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Mocking / Exports / RespondWith"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# RespondWith
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
interface RespondWith {
|
|
16
|
-
/**
|
|
17
|
-
* Rejects with an AbortError to simulate an aborted request.
|
|
18
|
-
*/
|
|
19
|
-
abortedRequest: (signal: ?SettleSignal = null) => MockResponse<any>;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* A non-200 status code with empty text body.
|
|
23
|
-
* Equivalent to calling `ResponseWith.text("", statusCode)`.
|
|
24
|
-
*/
|
|
25
|
-
errorStatusCode: (
|
|
26
|
-
statusCode: number,
|
|
27
|
-
signal: ?SettleSignal = null,
|
|
28
|
-
) => MockResponse<any>;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Response with GraphQL data JSON body and status code 200.
|
|
32
|
-
*/
|
|
33
|
-
graphQLData: <TData: {...}>(
|
|
34
|
-
data: TData,
|
|
35
|
-
signal: ?SettleSignal = null,
|
|
36
|
-
) => MockResponse<GraphQLJson<TData>>;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Response that is a GraphQL errors response with status code 200.
|
|
40
|
-
*/
|
|
41
|
-
graphQLErrors: (
|
|
42
|
-
errorMessages: $ReadOnlyArray<string>,
|
|
43
|
-
signal: ?SettleSignal = null,
|
|
44
|
-
) => MockResponse<any>;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Response with JSON body and status code 200.
|
|
48
|
-
*/
|
|
49
|
-
json: <TJson: {...}>(
|
|
50
|
-
json: TJson,
|
|
51
|
-
signal: ?SettleSignal = null,
|
|
52
|
-
): MockResponse<TJson>;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Response body that is valid JSON but not a valid GraphQL response.
|
|
56
|
-
*/
|
|
57
|
-
nonGraphQLBody: (signal: ?SettleSignal = null) => MockResponse<any>;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Rejects with the given error.
|
|
61
|
-
*/
|
|
62
|
-
reject: (error: Error, signal: ?SettleSignal = null) => MockResponse<any>;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Response with text body and status code.
|
|
66
|
-
* Status code defaults to 200.
|
|
67
|
-
*/
|
|
68
|
-
text: <TData = string>(
|
|
69
|
-
text: string,
|
|
70
|
-
statusCode: number = 200,
|
|
71
|
-
signal: ?SettleSignal = null,
|
|
72
|
-
) => MockResponse<TData>;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Response with body that will not parse as JSON and status code 200.
|
|
76
|
-
*/
|
|
77
|
-
unparseableBody: (signal: ?SettleSignal = null) => MockResponse<any>;
|
|
78
|
-
});
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
The `RespondWith` object is a helper for defining mock responses to use with
|
|
82
|
-
mock request methods such as [`mockGqlFetch`](/docs/testing-mocking-exports-mockgqlfetch--page).
|
|
83
|
-
|
|
84
|
-
Each call takes an optional `signal` that can be used to control when the promise generated from the call resolves. See [`SettleController`](/docs/testing-mocking-exports-settlecontroller--page) for related information.
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Mocking / Exports / SettleController"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# SettleController
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
class SettleController {
|
|
16
|
-
/**
|
|
17
|
-
* The signal to pass to the `RespondWith` API.
|
|
18
|
-
*/
|
|
19
|
-
get signal(): SettleSignal;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Settle the signal and therefore any associated responses.
|
|
23
|
-
*
|
|
24
|
-
* @throws {Error} if the signal has already been settled.
|
|
25
|
-
*/
|
|
26
|
-
settle(): void;
|
|
27
|
-
}
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
The `SettleController` is used to control the settling of a signal. This is specifically created to work with the [`RespondWith`](/docs/testing-mocking-exports-respondwith--page) API. The `signal` property it exposes can be passed to `RespondWith` methods and then the `settle` method can be invoked to settle the signal, causing the related responses to either reject or resolve as appropriate.
|
|
31
|
-
|
|
32
|
-
This can be useful for tests where the order of operations needs to be controlled in order to verify the expected behaviour of the system under test.
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Test Harness / Exports / testHarness()"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# testHarness()
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
testHarness<-TProps, +Instance = mixed>(
|
|
16
|
-
Component: React.AbstractComponent<TProps, Instance>,
|
|
17
|
-
configs?: $Shape<TestHarnessConfigs<typeof DefaultAdapters>>,
|
|
18
|
-
): React.AbstractComponent<TProps, Instance>
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
This method can be used to create a test harness for a given component. The resultant harnessed component will take the same props as the original component, but will render that component nested inside whatever boilerplate the various harness adapters provide for the given or default configurations.
|
|
22
|
-
|
|
23
|
-
This method is created by using [`makeTestHarness`](/docs/testing-test-harness-exports-maketestharness--page) with [`harnessAdapters.DefaultAdapters` and `harnessAdapters.DefaultConfigs`](/docs/testing-test-harness-exports-harnessadapters--page).
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Mocking / Types / FetchMockFn"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# FetchMockFn
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
type FetchMockFn = {|
|
|
16
|
-
(input: RequestInfo, init?: RequestOptions): Promise<Response>,
|
|
17
|
-
mockOperation: FetchMockOperationFn,
|
|
18
|
-
mockOperationOnce: FetchMockOperationFn,
|
|
19
|
-
|};
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
This type represents the mocking API for `fetch()`. It is the type of the function returned by the [`mockFetch()`](/docs/testing-mocking-exports-mockfetch--page), which is both a drop in replacement for a `fetch()`-like method, but also exposes the mocking API for configuration additional mocks.
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Mocking / Types / FetchMockOperation"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# FetchMockOperation
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
type FetchMockOperation = RegExp | string;
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
This defines the operation that a fetch mock will respond to. See [`mockFetch()`](/docs/testing-mocking-exports-mockfetch--page) for more details.
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Fixtures / Types / FixtureFn"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# FixtureFn
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
/**
|
|
16
|
-
* A function for defining a fixture.
|
|
17
|
-
*/
|
|
18
|
-
type FixtureFn<TProps: {...}> = (
|
|
19
|
-
/**
|
|
20
|
-
* The name of the fixture.
|
|
21
|
-
*/
|
|
22
|
-
description: string,
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The props for the fixture or a function that returns the props.
|
|
26
|
-
* The function is injected with an API to facilitate logging.
|
|
27
|
-
*/
|
|
28
|
-
props: FixtureProps<TProps>,
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* An alternative component to render for the fixture.
|
|
32
|
-
* Useful if the fixture requires some additional setup for testing the
|
|
33
|
-
* component.
|
|
34
|
-
*/
|
|
35
|
-
wrapper?: React.ComponentType<TProps>,
|
|
36
|
-
) => mixed;
|
|
37
|
-
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
The [`fixtures`](/docs/testing-fixtures-exports-fixtures--page) method returns a method of this type, specific to the component that was passed into `fixtures`.
|
|
41
|
-
|
|
42
|
-
This function takes two or three arguments:
|
|
43
|
-
|
|
44
|
-
- `description: string`: A string describing the fixture. This should be used to explain what the fixture is expected to show.
|
|
45
|
-
- `props: FixtureProps<TProps>`: The props that the fixture should be rendered with. See [`FixtureProps`](/docs/testing-fixtures-types-fixtureprops--page).
|
|
46
|
-
- `wrapper?: React.ComponentType<TProps>`: An optional component that will be rendered for the fixture. This can be used to wrap the fixture in a component that adds additional functionality, such as a test harness (see [`makeTestHarness`](/docs/testing-test-harness-exports-maketestharness--page)).
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Fixtures / Types / FixtureProps"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# FixtureProps
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
type FixtureProps<TProps: {...}> =
|
|
16
|
-
| $ReadOnly<TProps>
|
|
17
|
-
| ((options: $ReadOnly<GetPropsOptions>) => $ReadOnly<TProps>);
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
This type describes how props can be provided to a fixture as either a plain object, or a function that returns a plain object. The function will be called with an API to assist in generating the props (see [`GetPropsOptions`](/docs/testing-fixtures-types-getpropsoptions--page)).
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import {Meta} from "@storybook/addon-docs";
|
|
2
|
-
|
|
3
|
-
<Meta
|
|
4
|
-
title="Testing / Fixtures / Types / GetPropsOptions"
|
|
5
|
-
parameters={{
|
|
6
|
-
chromatic: {
|
|
7
|
-
disableSnapshot: true,
|
|
8
|
-
},
|
|
9
|
-
}}
|
|
10
|
-
/>
|
|
11
|
-
|
|
12
|
-
# GetPropsOptions
|
|
13
|
-
|
|
14
|
-
```ts
|
|
15
|
-
type GetPropsOptions = {|
|
|
16
|
-
/**
|
|
17
|
-
* A function to call that will log output.
|
|
18
|
-
*/
|
|
19
|
-
log: (message: string, ...args: Array<any>) => void,
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* A function to make a handler that will log all arguments with the given
|
|
23
|
-
* name or message. Useful for logging events as it avoids the boilerplate
|
|
24
|
-
* of the `log` function.
|
|
25
|
-
*/
|
|
26
|
-
logHandler: (name: string) => (...args: Array<any>) => void,
|
|
27
|
-
|};
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
A fixture can provide a callback that the framework invokes to obtain the props for the fixture component. This callback takes a single argument of type `GetPropsOptions`.
|
|
31
|
-
|
|
32
|
-
This has two calls available.
|
|
33
|
-
|
|
34
|
-
The `log` callback is for logging output in the context of the fixture. This can be useful for logging information during your fixture. However, in many situations, it is easier to use the `logHandler` callback. The `logHandler` callback takes a single argument of type `string` and returns a function that logs all arguments with the given name or message, allowing easy creation of event handlers that will log that event and its arguments.
|
|
35
|
-
|
|
36
|
-
For example:
|
|
37
|
-
|
|
38
|
-
```ts
|
|
39
|
-
fixture("My fixture that does logging", ({logHandler}) => ({
|
|
40
|
-
onClick: logHandler("onClick"),
|
|
41
|
-
}));
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
is equivalent to:
|
|
45
|
-
|
|
46
|
-
```ts
|
|
47
|
-
fixture("My fixture that does logging", ({log}) => ({
|
|
48
|
-
onClick: (...args) => log("onClick", ...args),
|
|
49
|
-
}));
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
|