@khanacademy/wonder-blocks-testing 4.0.4 → 5.0.0
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 +7 -0
- package/dist/es/index.js +212 -26
- package/dist/index.js +589 -62
- package/package.json +4 -2
- package/src/__docs__/_overview_.stories.mdx +3 -4
- package/src/__docs__/_overview_fixtures.stories.mdx +22 -0
- package/src/__docs__/_overview_mocking.stories.mdx +14 -0
- package/src/__docs__/_overview_test_harness.stories.mdx +18 -0
- package/src/__docs__/exports.fixture-adapters.stories.mdx +49 -0
- package/src/__docs__/exports.fixtures.stories.mdx +53 -0
- package/src/__docs__/exports.harness-adapters.stories.mdx +187 -0
- package/src/__docs__/exports.hook-harness.stories.mdx +22 -0
- package/src/__docs__/exports.make-hook-harness.stories.mdx +25 -0
- package/src/__docs__/exports.make-test-harness.stories.mdx +28 -0
- package/src/__docs__/exports.mock-fetch.stories.mdx +40 -0
- package/src/__docs__/exports.mock-gql-fetch.stories.mdx +13 -8
- package/src/__docs__/exports.respond-with.stories.mdx +54 -8
- package/src/__docs__/exports.setup-fixtures.stories.mdx +22 -0
- package/src/__docs__/exports.test-harness.stories.mdx +23 -0
- package/src/__docs__/types.custom-mount-props.stories.mdx +35 -0
- package/src/__docs__/types.fetch-mock-fn.stories.mdx +22 -0
- package/src/__docs__/types.fetch-mock-operation.stories.mdx +18 -0
- package/src/__docs__/types.fixtures-adapter-factory.stories.mdx +23 -0
- package/src/__docs__/types.fixtures-adapter-fixture-options.stories.mdx +35 -0
- package/src/__docs__/types.fixtures-adapter-group-options.stories.mdx +37 -0
- package/src/__docs__/types.fixtures-adapter-group.stories.mdx +43 -0
- package/src/__docs__/types.fixtures-adapter-options.stories.mdx +21 -0
- package/src/__docs__/types.fixtures-adapter.stories.mdx +35 -0
- package/src/__docs__/types.fixtures-configuration.stories.mdx +35 -0
- package/src/__docs__/types.fixtures-options.stories.mdx +51 -0
- package/src/__docs__/types.get-props-options.stories.mdx +25 -0
- package/src/__docs__/types.gql-fetch-mock-fn.stories.mdx +27 -0
- package/src/__docs__/types.gql-mock-operation.stories.mdx +26 -0
- package/src/__docs__/types.mock-response.stories.mdx +18 -0
- package/src/__docs__/types.test-harness-adapter.stories.mdx +21 -0
- package/src/__docs__/types.test-harness-adapters.stories.mdx +46 -0
- package/src/__docs__/types.test-harness-config.stories.mdx +18 -0
- package/src/__docs__/types.test-harness-configs.stories.mdx +59 -0
- package/src/fetch/types.js +0 -3
- package/src/fixtures/adapters/adapter-group.js +11 -11
- package/src/fixtures/adapters/adapter.js +8 -8
- package/src/fixtures/adapters/storybook.js +11 -8
- package/src/fixtures/fixtures.basic.stories.js +6 -2
- package/src/fixtures/fixtures.defaultwrapper.stories.js +6 -2
- package/src/fixtures/setup.js +8 -4
- package/src/fixtures/types.js +27 -16
- package/src/gql/types.js +1 -3
- package/src/harness/__tests__/hook-harness.test.js +72 -0
- package/src/harness/__tests__/make-hook-harness.test.js +94 -0
- package/src/harness/__tests__/make-test-harness.test.js +190 -0
- package/src/harness/__tests__/render-adapters.test.js +88 -0
- package/src/harness/__tests__/test-harness.test.js +74 -0
- package/src/harness/adapters/__tests__/__snapshots__/router.test.js.snap +5 -0
- package/src/harness/adapters/__tests__/css.test.js +96 -0
- package/src/harness/adapters/__tests__/data.test.js +66 -0
- package/src/harness/adapters/__tests__/portal.test.js +31 -0
- package/src/harness/adapters/__tests__/router.test.js +233 -0
- package/src/harness/adapters/adapters.js +33 -0
- package/src/harness/adapters/css.js +65 -0
- package/src/harness/adapters/data.js +46 -0
- package/src/harness/adapters/portal.js +26 -0
- package/src/harness/adapters/router.js +206 -0
- package/src/harness/hook-harness.js +23 -0
- package/src/harness/make-hook-harness.js +39 -0
- package/src/harness/make-test-harness.js +68 -0
- package/src/harness/render-adapters.js +27 -0
- package/src/harness/test-harness.js +24 -0
- package/src/harness/types.js +57 -0
- package/src/index.js +22 -18
|
@@ -0,0 +1,23 @@
|
|
|
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).
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Fixtures / Types / CustomMountProps<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# CustomMountProps<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type CustomMountProps<TProps: {...}> = {|
|
|
16
|
+
/**
|
|
17
|
+
* The fixture props for the component to be rendered.
|
|
18
|
+
*/
|
|
19
|
+
props: TProps,
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The component to render.
|
|
23
|
+
*/
|
|
24
|
+
component: React.ComponentType<TProps>,
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The log callback for logging information.
|
|
28
|
+
*/
|
|
29
|
+
log: (message: string, ...args: Array<any>) => mixed,
|
|
30
|
+
|};
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
When creating a new adapter instance, the [`FixtureAdapterFactory`](/docs/testing-fixtures-types-fixtureadapterfactory--page) can take an optional component to be used as the root component for each fixture, also know as the mounting component.
|
|
34
|
+
|
|
35
|
+
The `CustomMountProps<>` type defines the props that will be applied to that mounting component. It includes the `component` to be rendered as the fixture, the `props` to pass to that component, and the `log` callback, which the mounting component can use for any logging it may require.
|
|
@@ -0,0 +1,22 @@
|
|
|
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.
|
|
@@ -0,0 +1,18 @@
|
|
|
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.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Fixtures / Types / FixturesAdapterFactory<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# FixturesAdapterFactory<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type FixturesAdapterFactory<
|
|
16
|
+
TAdapterOptions: {...},
|
|
17
|
+
TAdapterExports: {...},
|
|
18
|
+
> = (
|
|
19
|
+
MountingComponent: ?React.ComponentType<CustomWrapperProps<any>>,
|
|
20
|
+
) => FixturesAdapter<TAdapterOptions, TAdapterExports>;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
When implementing a custom [`FixturesAdapter<>`](/docs/testing-fixtures-types-fixturesadapter--page), you should provide a factory function for producing instances of the adapter. This type describes the signature of the factory function.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Fixtures / Types / FixturesAdapterFixtureOptions<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# FixturesAdapterFixtureOptions<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type FixturesAdapterFixtureOptions<TProps: {...}> = {|
|
|
16
|
+
/**
|
|
17
|
+
* Description of the fixture.
|
|
18
|
+
*/
|
|
19
|
+
+description: string,
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Method to obtain props for the fixture.
|
|
23
|
+
*/
|
|
24
|
+
+getProps: (options: $ReadOnly<GetPropsOptions>) => $ReadOnly<TProps>,
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The component to render for this fixture.
|
|
28
|
+
*/
|
|
29
|
+
+component: React.ComponentType<TProps>,
|
|
30
|
+
|};
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
These options are passed to the `declareFixture()` function of a [`FixturesAdapterGroup<>`](/docs/testing-fixtures-types-fixturesadaptergroup--page).
|
|
34
|
+
|
|
35
|
+
The `description` property is used to describe the fixture, the `component` is the component that renders the fixture, and the `getProps` method is used to obtain the props for that component.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Fixtures / Types / FixturesAdapterGroupOptions"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# FixturesAdapterGroupOptions
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type FixturesAdapterGroupOptions = {|
|
|
16
|
+
/**
|
|
17
|
+
* The title of the group.
|
|
18
|
+
*
|
|
19
|
+
* If omitted, the adapter is free to generate a default or ask for one
|
|
20
|
+
* using the passed getDefaultTitle() function.
|
|
21
|
+
*/
|
|
22
|
+
+title: ?string,
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Description of the group.
|
|
26
|
+
*/
|
|
27
|
+
+description: ?string,
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Function that will generate a default title if an adapter cannot
|
|
31
|
+
* generate its own.
|
|
32
|
+
*/
|
|
33
|
+
+getDefaultTitle: () => string,
|
|
34
|
+
|};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
These are options that the fixtures framework provides when `declareGroup()` is called on the configured adapter to create a new [`FixturesAdapterGroup<>`](/docs/testing-fixtures-types-fixturesadaptergroup--page).
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Fixtures / Types / FixturesAdapterGroup<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# FixturesAdapterGroup<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
interface FixturesAdapterGroup<
|
|
16
|
+
TProps: {...},
|
|
17
|
+
TAdapterOptions: {...},
|
|
18
|
+
TAdapterExports: {...},
|
|
19
|
+
> {
|
|
20
|
+
/**
|
|
21
|
+
* Declare a fixture.
|
|
22
|
+
*/
|
|
23
|
+
declareFixture(
|
|
24
|
+
options: $ReadOnly<FixturesAdapterFixtureOptions<TProps>>,
|
|
25
|
+
): void;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Close the group and obtain the exports, if the adapter requires any.
|
|
29
|
+
*
|
|
30
|
+
* @param {Options} adapterOptions Some options to pass to the adapter.
|
|
31
|
+
* Allows callers to tailor things to a specific adapter. How these options
|
|
32
|
+
* are used is adapter-specific.
|
|
33
|
+
*
|
|
34
|
+
* @returns {?Exports} The exports that the adapter requires fixture files
|
|
35
|
+
* to export.
|
|
36
|
+
*/
|
|
37
|
+
closeGroup(
|
|
38
|
+
adapterOptions: $ReadOnly<Partial<TAdapterOptions>>,
|
|
39
|
+
): ?$ReadOnly<TAdapterExports>;
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Each call to the [`fixtures()`](/docs/testing-fixtures-exports-fixtures--page) function invokes the `declareGroup()` method on the [`FixturesAdapter<>`](/docs/testing-fixtures-types-fixturesadapter--page) instance that has been configured using [`setupFixtures()`](/docs/testing-fixtures-exports-setupfixtures--page). The group that is returned by `declareGroup()` must match this interface.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Fixtures / Types / FixturesAdapterOptions"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# FixturesAdapterOptions
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type FixturesAdapterOptions = {|
|
|
16
|
+
storybook?: StorybookOptions,
|
|
17
|
+
[adapterName: string]: {...},
|
|
18
|
+
|};
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Defines adapter-specific options. These are used with fixtures when needing to configure them for specific adapters.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Fixtures / Types / FixturesAdapter<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# FixturesAdapter<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
interface FixturesAdapter<TAdapterOptions: {...}, TAdapterExports: {...}> {
|
|
16
|
+
/**
|
|
17
|
+
* The name of the adapter.
|
|
18
|
+
*/
|
|
19
|
+
get name(): string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Declare a fixture group.
|
|
23
|
+
*
|
|
24
|
+
* @returns {FixturesAdapterGroup<TProps, TAdapterOptions, TAdapterExports>} The
|
|
25
|
+
* declared group.
|
|
26
|
+
*/
|
|
27
|
+
declareGroup<TProps: {...}>(
|
|
28
|
+
options: $ReadOnly<AdapterGroupOptions>,
|
|
29
|
+
): FixturesAdapterGroup<TProps, TAdapterOptions, TAdapterExports>;
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
This type describes an adapter for use with our fixtures framework.
|
|
34
|
+
|
|
35
|
+
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Fixtures / Types / FixturesConfiguration<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# FixturesConfiguration<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type FixturesConfiguration<
|
|
16
|
+
TAdapterOptions: {...},
|
|
17
|
+
TAdapterExports: {...},
|
|
18
|
+
> = {|
|
|
19
|
+
/**
|
|
20
|
+
* The adapter to use for declaring fixtures.
|
|
21
|
+
*/
|
|
22
|
+
+adapter: FixturesAdapter<TAdapterOptions, TAdapterExports>,
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Default options to apply to every fixture group.
|
|
26
|
+
*
|
|
27
|
+
* Each top-level option in this object will be merged with the equivalent
|
|
28
|
+
* top-level option that a specific fixture requests. Where collisions
|
|
29
|
+
* occur, the fixture options win.
|
|
30
|
+
*/
|
|
31
|
+
+defaultAdapterOptions?: $ReadOnly<Partial<TAdapterOptions>>,
|
|
32
|
+
|};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The configuration type passed to [`setupFixtures()`](/docs/testing-fixtures-exports-setupfixtures--page).
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Fixtures / Types / FixturesOptions<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# FixturesOptions
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type FixturesOptions<TProps: {...}> = {|
|
|
16
|
+
/**
|
|
17
|
+
* The component being tested by the fixtures.
|
|
18
|
+
*/
|
|
19
|
+
component: React.ComponentType<TProps>,
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Optional title of the fixture collection.
|
|
23
|
+
*
|
|
24
|
+
* Adapters may enforce a title, otherwise the component name is used.
|
|
25
|
+
*/
|
|
26
|
+
title?: string,
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Optional description of the fixture collection.
|
|
30
|
+
*/
|
|
31
|
+
description?: string,
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Optional default wrapper to apply around the component under test.
|
|
35
|
+
*/
|
|
36
|
+
defaultWrapper?: React.ComponentType<TProps>,
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Additional options to apply to specific adapters.
|
|
40
|
+
*/
|
|
41
|
+
additionalAdapterOptions?: FixturesAdapterOptions,
|
|
42
|
+
|};
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
This type is used to pass options to the [`fixtures()`](/docs/testing-fixtures-types-fixturesoptions--page) method when describing fixtures for a specific component.
|
|
46
|
+
|
|
47
|
+
It specifies the `component` being rendered and tested by each fixtures. Optionally, it can also specify a `title` and `description` for the fixtures, as well as a `defaultWrapper` component to use by default to wrap the component under test for each fixture, and an `additionalAdapterOptions` object that allows each adapter type to be individually configured for the fixtures.
|
|
48
|
+
|
|
49
|
+
If the `title` is omitted, the adapter can either request a default title, which will be based off the name of the `component` React component, or it can provide its own title based as necessary. For example, Storybook has a default title based off the file location of the component when the `title` is omitted.
|
|
50
|
+
|
|
51
|
+
Some adapters may ignore the `description` field.
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
|
|
23
|
+
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`.
|
|
24
|
+
|
|
25
|
+
This has a single `log` property; a callback for logging output in the context of the fixture. This can be useful for logging event handler invocations, for example.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Mocking / Types / GqlFetchMockFn"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# GqlFetchMockFn
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type GqlFetchMockFn = {|
|
|
16
|
+
(
|
|
17
|
+
operation: GqlOperation<any, any>,
|
|
18
|
+
variables: ?{...},
|
|
19
|
+
context: GqlContext,
|
|
20
|
+
): Promise<Response>,
|
|
21
|
+
mockOperation: GqlMockOperationFn,
|
|
22
|
+
mockOperationOnce: GqlMockOperationFn,
|
|
23
|
+
|};
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This type represents the mocking API for use with the [Wonder Blocks Data GraphQL API](/docs/data-graphql--page). It is the type of the function returned by the [`mockGqlFetch()`](/docs/testing-mocking-exports-mockgqlfetch--page).
|
|
27
|
+
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Mocking / Types / GqlMockOperation<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# GqlMockOperation<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type GqlMockOperation<
|
|
16
|
+
TData: {...},
|
|
17
|
+
TVariables: {...},
|
|
18
|
+
TContext: GqlContext,
|
|
19
|
+
> = {|
|
|
20
|
+
operation: GqlOperation<TData, TVariables>,
|
|
21
|
+
variables?: TVariables,
|
|
22
|
+
context?: TContext,
|
|
23
|
+
|};
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This defines the operation that a GraphQL mock will respond to. See [`mockGqlFetch`](/docs/testing-mocking-exports-mockgqlfetch--page) for more details.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Mocking / Types / MockResponse<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# MockResponse<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
opaque type MockResponse<TJson>;
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
This opaque type specifies a mock response. Values of this type are generated by the [`RespondWith`](/docs/testing-mocking-exports-respondwith--page) API.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Test Harness / Types / TestHarnessAdapter<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# TestHarnessAdapter<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type TestHarnessAdapter<TConfig> = (
|
|
16
|
+
children: React.Node,
|
|
17
|
+
config: TConfig,
|
|
18
|
+
) => React.Element<any>;
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
This type represents the signature of a an adapter function for use with the test harness framework methods, [`makeTestHarness()`](/docs/testing-test-harness-exports-maketestharness--page) and [`makeHookHarness()`](/docs/testing-test-harness-exports-makehookharness--page).
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Test Harness / Types / TestHarnessAdapters"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# TestHarnessAdapters
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type TestHarnessAdapters = {|
|
|
16
|
+
[adapterID: string]: TestHarnessAdapter<any>,
|
|
17
|
+
|};
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Defines a generic collection of test harness adapters.
|
|
21
|
+
|
|
22
|
+
Only use `TestHarnessAdapters` in input locations to verify a set of adapters conforms to that type, but avoid using it in output locations as it can erase useful type information.
|
|
23
|
+
|
|
24
|
+
For example, the [`harnessAdapters.DefaultAdapters`](/docs/testing-test-harness-exports-harnessadapters--page) type is specific to the adapters it contains.
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
const DefaultAdapters = {
|
|
28
|
+
css: css.adapter,
|
|
29
|
+
data: data.adapter,
|
|
30
|
+
portal: portal.adapter,
|
|
31
|
+
router: router.adapter,
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`DefaultAdapters` is not strongly typed to `TestHarnessAdapters`. Instead, its type is:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
type DefaultAdaptersType = {|
|
|
39
|
+
css: typeof css.adapter,
|
|
40
|
+
data: typeof data.adapter,
|
|
41
|
+
portal: typeof portal.adapter,
|
|
42
|
+
router: typeof router.adapter,
|
|
43
|
+
|};
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
It conforms to the `TestHarnessAdapters` type because each key is a string and the value of each property is a variation of `TestHarnessAdapter<TConfig>` with a different type for `TConfig` in each case, but it is not equivalent to the `TestHarnessAdapters` type where each key is a string and each value is exactly `TestHarnessAdapter<any>`.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Test Harness / Types / TestHarnessConfig<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# TestHarnessConfig<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type TestHarnessConfig<TAdapter>;
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
When given an adapter type conforming to [`TestHarnessAdapter`](/docs/testing-test-harness-types-testharnessadapter--page), this type will represent that adapter type's configuration type.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {Meta} from "@storybook/addon-docs";
|
|
2
|
+
|
|
3
|
+
<Meta
|
|
4
|
+
title="Testing / Test Harness / Types / TestHarnessConfigs<>"
|
|
5
|
+
parameters={{
|
|
6
|
+
chromatic: {
|
|
7
|
+
disableSnapshot: true,
|
|
8
|
+
},
|
|
9
|
+
}}
|
|
10
|
+
/>
|
|
11
|
+
|
|
12
|
+
# TestHarnessConfigs<>
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
type TestHarnessConfigs<TAdapters: TestHarnessAdapters>;
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
When given the type of a set of adapters conforming to [`TestHarnessAdapters`](/docs/testing-test-harness-types-testharnessadapters--page), this type will represent a set of configurations for those adapters.
|
|
19
|
+
|
|
20
|
+
It is important to note here that if the `TAdapters` type passed in is the actual `TestHarnessAdapters` type, then the resulting configuration type will have each adapter's config being set to `any`. Instead of using the `TestHarnessAdapters` type directly, the passed object should not be typed as that, but should merely conform to that type.
|
|
21
|
+
|
|
22
|
+
For example, the [`harnessAdapters.DefaultAdapters`](/docs/testing-test-harness-exports-harnessadapters--page) type is specific to the adapters it contains.
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
const DefaultAdapters = {
|
|
26
|
+
css: css.adapter,
|
|
27
|
+
data: data.adapter,
|
|
28
|
+
portal: portal.adapter,
|
|
29
|
+
router: router.adapter,
|
|
30
|
+
};
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`DefaultAdapters` is not strongly typed to `TestHarnessAdapters`. Instead, its type is:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
type DefaultAdaptersType = {|
|
|
37
|
+
css: typeof css.adapter,
|
|
38
|
+
data: typeof data.adapter,
|
|
39
|
+
portal: typeof portal.adapter,
|
|
40
|
+
router: typeof router.adapter,
|
|
41
|
+
|};
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
It conforms to the `TestHarnessAdapters` type, but it is not equivalent to the `TestHarnessAdapters` type. This is important when we consider the companion export, [`harnessAdapters.DefaultConfigs`](/docs/testing-test-harness-exports-harnessadapters--page).
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const DefaultConfigs: TestHarnessConfigs<typeof DefaultAdapters> = {
|
|
48
|
+
css: css.defaultConfig,
|
|
49
|
+
data: data.defaultConfig,
|
|
50
|
+
portal: portal.defaultConfig,
|
|
51
|
+
router: router.defaultConfig,
|
|
52
|
+
};
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`DefaultConfigs` is typed using `TestHarnessConfigs<typeof DefaultAdapters>`. Because `DefaultAdapters` is strongly typed specifically to each adapter it contains, the type that `TestHarnessConfigs<>` creates ensures that there is one configuration per adapter key, and that the configuration type for each adapter key is correct for the corresponding adapter.
|
|
56
|
+
|
|
57
|
+
If we had typed `DefaultAdapters` as `TestHarnessAdapters`, then although we would still enforce one configuration per adapter key, we would allow `any` type to provide that configuration, which does not give us any real type safety.
|
|
58
|
+
|
|
59
|
+
So, to summarize, use `TestHarnessAdapters` in input locations to verify a set of adapters conforms to that type, but avoid using it in output locations as it can erase useful type information.
|
package/src/fetch/types.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
//@flow
|
|
2
|
-
import type {OperationMock} from "../types.js";
|
|
3
2
|
import type {MockResponse} from "../make-mock-response.js";
|
|
4
3
|
|
|
5
4
|
export type FetchMockOperation = RegExp | string;
|
|
@@ -14,5 +13,3 @@ export type FetchMockFn = {|
|
|
|
14
13
|
mockOperation: FetchMockOperationFn,
|
|
15
14
|
mockOperationOnce: FetchMockOperationFn,
|
|
16
15
|
|};
|
|
17
|
-
|
|
18
|
-
export type FetchMock = OperationMock<FetchMockOperation>;
|