@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.
Files changed (147) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/dist/es/index.js +183 -120
  3. package/dist/fetch/fetch-request-matches-mock.d.ts +5 -0
  4. package/dist/fetch/fetch-request-matches-mock.js.flow +17 -0
  5. package/dist/fetch/mock-fetch.d.ts +5 -0
  6. package/dist/fetch/mock-fetch.js.flow +13 -0
  7. package/dist/fetch/types.d.ts +9 -0
  8. package/dist/fetch/types.js.flow +19 -0
  9. package/dist/fixtures/fixtures.basic.stories.d.ts +13 -0
  10. package/dist/fixtures/fixtures.basic.stories.js.flow +22 -0
  11. package/dist/fixtures/fixtures.d.ts +19 -0
  12. package/dist/fixtures/fixtures.defaultwrapper.stories.d.ts +9 -0
  13. package/dist/fixtures/fixtures.defaultwrapper.stories.js.flow +17 -0
  14. package/dist/fixtures/fixtures.js.flow +33 -0
  15. package/dist/fixtures/types.d.ts +36 -0
  16. package/dist/fixtures/types.js.flow +37 -0
  17. package/dist/gql/gql-request-matches-mock.d.ts +3 -0
  18. package/dist/gql/gql-request-matches-mock.js.flow +15 -0
  19. package/dist/gql/mock-gql-fetch.d.ts +5 -0
  20. package/dist/gql/mock-gql-fetch.js.flow +13 -0
  21. package/dist/gql/types.d.ts +15 -0
  22. package/dist/gql/types.js.flow +39 -0
  23. package/dist/harness/adapters/adapters.d.ts +35 -0
  24. package/dist/harness/adapters/adapters.js.flow +69 -0
  25. package/dist/harness/adapters/css.d.ts +12 -0
  26. package/dist/harness/adapters/css.js.flow +23 -0
  27. package/dist/harness/adapters/data.d.ts +18 -0
  28. package/dist/harness/adapters/data.js.flow +32 -0
  29. package/dist/harness/adapters/portal.d.ts +12 -0
  30. package/dist/harness/adapters/portal.js.flow +18 -0
  31. package/dist/harness/adapters/router.d.ts +94 -0
  32. package/dist/harness/adapters/router.js.flow +122 -0
  33. package/dist/harness/hook-harness.d.ts +13 -0
  34. package/dist/harness/hook-harness.js.flow +23 -0
  35. package/dist/harness/make-hook-harness.d.ts +17 -0
  36. package/dist/harness/make-hook-harness.js.flow +42 -0
  37. package/dist/harness/make-test-harness.d.ts +18 -0
  38. package/dist/harness/make-test-harness.js.flow +48 -0
  39. package/dist/harness/render-adapters.d.ts +6 -0
  40. package/dist/harness/render-adapters.js.flow +24 -0
  41. package/dist/harness/test-harness.d.ts +32 -0
  42. package/dist/harness/test-harness.js.flow +83 -0
  43. package/dist/harness/types.d.ts +46 -0
  44. package/dist/harness/types.js.flow +66 -0
  45. package/dist/index.d.ts +15 -0
  46. package/dist/index.js +185 -127
  47. package/dist/index.js.flow +26 -2
  48. package/dist/mock-requester.d.ts +5 -0
  49. package/dist/mock-requester.js.flow +22 -0
  50. package/dist/respond-with.d.ts +57 -0
  51. package/dist/respond-with.js.flow +91 -0
  52. package/dist/response-impl.d.ts +1 -0
  53. package/dist/response-impl.js.flow +8 -0
  54. package/dist/settle-controller.d.ts +19 -0
  55. package/dist/settle-controller.js.flow +26 -0
  56. package/dist/settle-signal.d.ts +18 -0
  57. package/dist/settle-signal.js.flow +26 -0
  58. package/dist/types.d.ts +25 -0
  59. package/dist/types.js.flow +46 -0
  60. package/package.json +6 -6
  61. package/src/__tests__/{mock-requester.test.js → mock-requester.test.ts} +3 -4
  62. package/src/__tests__/{respond-with.test.js → respond-with.test.ts} +2 -3
  63. package/src/__tests__/response-impl.test.js +3 -3
  64. package/src/__tests__/{settle-controller.test.js → settle-controller.test.ts} +2 -3
  65. package/src/__tests__/{settle-signal.test.js → settle-signal.test.ts} +1 -2
  66. package/src/fetch/__tests__/__snapshots__/{mock-fetch.test.js.snap → mock-fetch.test.ts.snap} +3 -3
  67. package/src/fetch/__tests__/{fetch-request-matches-mock.test.js → fetch-request-matches-mock.test.ts} +6 -7
  68. package/src/fetch/__tests__/{mock-fetch.test.js → mock-fetch.test.ts} +3 -4
  69. package/src/fetch/{fetch-request-matches-mock.js → fetch-request-matches-mock.ts} +4 -3
  70. package/src/fetch/{mock-fetch.js → mock-fetch.ts} +5 -6
  71. package/src/fetch/types.ts +14 -0
  72. package/src/fixtures/__tests__/{fixtures.test.js → fixtures.test.tsx} +7 -5
  73. package/src/fixtures/{fixtures.basic.stories.js → fixtures.basic.stories.tsx} +17 -14
  74. package/src/fixtures/{fixtures.defaultwrapper.stories.js → fixtures.defaultwrapper.stories.tsx} +9 -9
  75. package/src/fixtures/{fixtures.js → fixtures.tsx} +17 -12
  76. package/src/fixtures/{types.js → types.ts} +9 -13
  77. package/src/gql/__tests__/{gql-request-matches-mock.test.js → gql-request-matches-mock.test.ts} +18 -19
  78. package/src/gql/__tests__/{mock-gql-fetch.test.js → mock-gql-fetch.test.tsx} +46 -47
  79. package/src/gql/__tests__/{wb-data-integration.test.js → wb-data-integration.test.tsx} +23 -24
  80. package/src/gql/{gql-request-matches-mock.js → gql-request-matches-mock.ts} +2 -5
  81. package/src/gql/mock-gql-fetch.ts +15 -0
  82. package/src/gql/types.ts +33 -0
  83. package/src/harness/__tests__/{hook-harness.test.js → hook-harness.test.ts} +13 -14
  84. package/src/harness/__tests__/{make-hook-harness.test.js → make-hook-harness.test.tsx} +12 -13
  85. package/src/harness/__tests__/{make-test-harness.test.js → make-test-harness.test.tsx} +7 -8
  86. package/src/harness/__tests__/{render-adapters.test.js → render-adapters.test.tsx} +10 -11
  87. package/src/harness/__tests__/{test-harness.test.js → test-harness.test.ts} +13 -14
  88. package/src/harness/__tests__/{types.flowtest.js → types.flowtest.tsx} +25 -28
  89. package/src/harness/adapters/__tests__/{css.test.js → css.test.tsx} +3 -4
  90. package/src/harness/adapters/__tests__/{data.test.js → data.test.tsx} +5 -4
  91. package/src/harness/adapters/__tests__/{portal.test.js → portal.test.tsx} +1 -2
  92. package/src/harness/adapters/__tests__/{router.test.js → router.test.tsx} +76 -57
  93. package/src/harness/adapters/{adapters.js → adapters.ts} +6 -7
  94. package/src/harness/adapters/{css.js → css.tsx} +22 -19
  95. package/src/harness/adapters/{data.js → data.tsx} +8 -6
  96. package/src/harness/adapters/{portal.js → portal.tsx} +4 -5
  97. package/src/harness/adapters/router.tsx +218 -0
  98. package/src/harness/{hook-harness.js → hook-harness.ts} +5 -6
  99. package/src/harness/{make-hook-harness.js → make-hook-harness.ts} +8 -8
  100. package/src/harness/{make-test-harness.js → make-test-harness.tsx} +19 -22
  101. package/src/harness/{render-adapters.js → render-adapters.ts} +5 -5
  102. package/src/harness/test-harness.ts +13 -0
  103. package/src/harness/{types.js → types.ts} +14 -12
  104. package/src/index.ts +20 -0
  105. package/src/{mock-requester.js → mock-requester.ts} +6 -5
  106. package/src/{respond-with.js → respond-with.ts} +45 -40
  107. package/src/{response-impl.js → response-impl.ts} +1 -2
  108. package/src/settle-controller.ts +37 -0
  109. package/src/{settle-signal.js → settle-signal.ts} +10 -7
  110. package/src/types.ts +40 -0
  111. package/tsconfig.json +11 -0
  112. package/tsconfig.tsbuildinfo +1 -0
  113. package/src/__docs__/_overview_.stories.mdx +0 -18
  114. package/src/__docs__/_overview_fixtures.stories.mdx +0 -18
  115. package/src/__docs__/_overview_mocking.stories.mdx +0 -14
  116. package/src/__docs__/_overview_test_harness.stories.mdx +0 -18
  117. package/src/__docs__/exports.fixtures.stories.mdx +0 -31
  118. package/src/__docs__/exports.harness-adapters.stories.mdx +0 -187
  119. package/src/__docs__/exports.hook-harness.stories.mdx +0 -22
  120. package/src/__docs__/exports.make-hook-harness.stories.mdx +0 -25
  121. package/src/__docs__/exports.make-test-harness.stories.mdx +0 -28
  122. package/src/__docs__/exports.mock-fetch.stories.mdx +0 -40
  123. package/src/__docs__/exports.mock-gql-fetch.stories.mdx +0 -64
  124. package/src/__docs__/exports.respond-with.stories.mdx +0 -84
  125. package/src/__docs__/exports.settle-controller.stories.mdx +0 -32
  126. package/src/__docs__/exports.test-harness.stories.mdx +0 -23
  127. package/src/__docs__/types.fetch-mock-fn.stories.mdx +0 -22
  128. package/src/__docs__/types.fetch-mock-operation.stories.mdx +0 -18
  129. package/src/__docs__/types.fixture-fn.stories.mdx +0 -46
  130. package/src/__docs__/types.fixture-props.stories.mdx +0 -20
  131. package/src/__docs__/types.get-props-options.stories.mdx +0 -52
  132. package/src/__docs__/types.gql-fetch-mock-fn.stories.mdx +0 -27
  133. package/src/__docs__/types.gql-mock-operation.stories.mdx +0 -26
  134. package/src/__docs__/types.mock-response.stories.mdx +0 -22
  135. package/src/__docs__/types.test-harness-adapter.stories.mdx +0 -21
  136. package/src/__docs__/types.test-harness-adapters.stories.mdx +0 -46
  137. package/src/__docs__/types.test-harness-config.stories.mdx +0 -18
  138. package/src/__docs__/types.test-harness-configs.stories.mdx +0 -59
  139. package/src/fetch/types.js +0 -15
  140. package/src/gql/mock-gql-fetch.js +0 -18
  141. package/src/gql/types.js +0 -34
  142. package/src/harness/adapters/router.js +0 -206
  143. package/src/harness/test-harness.js +0 -24
  144. package/src/index.js +0 -26
  145. package/src/settle-controller.js +0 -35
  146. package/src/types.js +0 -39
  147. /package/src/harness/adapters/__tests__/__snapshots__/{router.test.js.snap → router.test.tsx.snap} +0 -0
@@ -1,27 +0,0 @@
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
-
@@ -1,26 +0,0 @@
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&lt;&gt;
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.
@@ -1,22 +0,0 @@
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&lt;&gt;
13
-
14
- ```ts
15
- type MockResponse<TJson> = {|
16
- toPromise: () => Promise<Response>,
17
- |};
18
- ```
19
-
20
- This type specifies a mock response. Values of this type are generated by the [`RespondWith`](/docs/testing-mocking-exports-respondwith--page) API. The type parameter is included to allow uses to enforce if they only support `MockResponses` that resolve to a specific JSON pattern.
21
-
22
- The `toPromise` method can be used to generate a new promise from the mocked response definition. Note that `toPromise` will always generate a new promise and any promise created will settle according to any signal passed to the corresponding `RespondWith` API call.
@@ -1,21 +0,0 @@
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&lt;&gt;
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).
@@ -1,46 +0,0 @@
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>`.
@@ -1,18 +0,0 @@
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&lt;&gt;
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.
@@ -1,59 +0,0 @@
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&lt;&gt;
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.
@@ -1,15 +0,0 @@
1
- //@flow
2
- import type {MockResponse} from "../respond-with.js";
3
-
4
- export type FetchMockOperation = RegExp | string;
5
-
6
- type FetchMockOperationFn = (
7
- operation: FetchMockOperation,
8
- response: MockResponse<any>,
9
- ) => FetchMockFn;
10
-
11
- export type FetchMockFn = {|
12
- (input: RequestInfo, init?: RequestOptions): Promise<Response>,
13
- mockOperation: FetchMockOperationFn,
14
- mockOperationOnce: FetchMockOperationFn,
15
- |};
@@ -1,18 +0,0 @@
1
- // @flow
2
- import {gqlRequestMatchesMock} from "./gql-request-matches-mock.js";
3
- import {mockRequester} from "../mock-requester.js";
4
- import type {GqlFetchMockFn, GqlMockOperation} from "./types.js";
5
-
6
- /**
7
- * A mock for the fetch function passed to GqlRouter.
8
- */
9
- export const mockGqlFetch = (): GqlFetchMockFn =>
10
- mockRequester<GqlMockOperation<any, any, any>, _>(
11
- gqlRequestMatchesMock,
12
- (operation, variables, context) =>
13
- `Operation: ${operation.type} ${operation.id}
14
- Variables: ${
15
- variables == null ? "None" : JSON.stringify(variables, null, 2)
16
- }
17
- Context: ${JSON.stringify(context, null, 2)}`,
18
- );
package/src/gql/types.js DELETED
@@ -1,34 +0,0 @@
1
- //@flow
2
- import type {GqlOperation, GqlContext} from "@khanacademy/wonder-blocks-data";
3
- import type {GraphQLJson} from "../types.js";
4
- import type {MockResponse} from "../respond-with.js";
5
-
6
- export type GqlMockOperation<
7
- TData: {...},
8
- TVariables: {...},
9
- TContext: GqlContext,
10
- > = {|
11
- operation: GqlOperation<TData, TVariables>,
12
- variables?: TVariables,
13
- context?: TContext,
14
- |};
15
-
16
- type GqlMockOperationFn = <
17
- TData: {...},
18
- TVariables: {...},
19
- TContext: GqlContext,
20
- TResponseData: GraphQLJson<TData>,
21
- >(
22
- operation: GqlMockOperation<TData, TVariables, TContext>,
23
- response: MockResponse<TResponseData>,
24
- ) => GqlFetchMockFn;
25
-
26
- export type GqlFetchMockFn = {|
27
- (
28
- operation: GqlOperation<any, any>,
29
- variables: ?{...},
30
- context: GqlContext,
31
- ): Promise<Response>,
32
- mockOperation: GqlMockOperationFn,
33
- mockOperationOnce: GqlMockOperationFn,
34
- |};
@@ -1,206 +0,0 @@
1
- // @flow
2
- import * as React from "react";
3
-
4
- import {StaticRouter, MemoryRouter, Route, Switch} from "react-router-dom";
5
-
6
- import type {LocationShape, Location} from "react-router-dom";
7
- import type {TestHarnessAdapter} from "../types.js";
8
-
9
- type MemoryRouterProps = React.ElementConfig<typeof MemoryRouter>;
10
-
11
- /**
12
- * Configuration for the withLocation test harness adapter.
13
- */
14
- type Config =
15
- | $ReadOnly<
16
- | {|
17
- /**
18
- * See MemoryRouter prop for initialEntries.
19
- */
20
- initialEntries: MemoryRouterProps["initialEntries"],
21
-
22
- /**
23
- * See MemoryRouter prop for initialIndex.
24
- */
25
- initialIndex?: MemoryRouterProps["initialIndex"],
26
-
27
- /**
28
- * See MemoryRouter prop for getUserConfirmation.
29
- */
30
- getUserConfirmation?: MemoryRouterProps["getUserConfirmation"],
31
-
32
- /**
33
- * A path match to use.
34
- *
35
- * When this is specified, the harnessed component will be
36
- * rendered inside a `Route` handler with this path.
37
- *
38
- * If the path matches the location, then the route will
39
- * render the component.
40
- *
41
- * If the path does not match the location, then the route
42
- * will not render the component.
43
- */
44
- path?: string,
45
- |}
46
- | {|
47
- /**
48
- * The location to use.
49
- */
50
- location: string | Location,
51
-
52
- /**
53
- * Force the use of a StaticRouter, instead of MemoryRouter.
54
- */
55
- forceStatic: true,
56
-
57
- /**
58
- * A path match to use.
59
- *
60
- * When this is specified, the harnessed component will be
61
- * rendered inside a `Route` handler with this path.
62
- *
63
- * If the path matches the location, then the route will
64
- * render the component.
65
- *
66
- * If the path does not match the location, then the route
67
- * will not render the component.
68
- */
69
- path?: string,
70
- |}
71
- | {|
72
- /**
73
- * The initial location to use.
74
- */
75
- location: string | LocationShape,
76
-
77
- /**
78
- * A path match to use.
79
- *
80
- * When this is specified, the harnessed component will be
81
- * rendered inside a `Route` handler with this path.
82
- *
83
- * If the path matches the location, then the route will
84
- * render the component.
85
- *
86
- * If the path does not match the location, then the route
87
- * will not render the component.
88
- */
89
- path?: string,
90
- |},
91
- >
92
- // The initial location to use.
93
- | string;
94
-
95
- /**
96
- * The default configuration for this adapter.
97
- */
98
- export const defaultConfig = {location: "/"};
99
-
100
- const maybeWithRoute = (children: React.Node, path: ?string): React.Node => {
101
- if (path == null) {
102
- return children;
103
- }
104
-
105
- return (
106
- <Switch>
107
- <Route exact={true} path={path}>
108
- {children}
109
- </Route>
110
- <Route
111
- path="*"
112
- render={() => {
113
- throw new Error(
114
- "The configured path must match the configured location or your harnessed component will not render.",
115
- );
116
- }}
117
- />
118
- </Switch>
119
- );
120
- };
121
-
122
- /**
123
- * Adapter that sets up a router and AppShell location-specific contexts.
124
- *
125
- * This allows you to ensure that components are being tested in the
126
- * AppShell world.
127
- *
128
- * NOTE(somewhatabstract): The AppShell component itself already does
129
- * the work of setting up routing and the AppShellContext and so using this
130
- * adapter with the App component will have zero-effect since AppShell will
131
- * override it.
132
- */
133
- export const adapter: TestHarnessAdapter<Config> = (
134
- children: React.Node,
135
- config: Config,
136
- ): React.Element<any> => {
137
- if (typeof config === "string") {
138
- config = {
139
- location: config,
140
- };
141
- }
142
-
143
- // Wrap children with the various contexts and routes, as per the config.
144
- const wrappedWithRoute = maybeWithRoute(children, config.path);
145
- if (config.forceStatic) {
146
- /**
147
- * There may be times (SSR testing comes to mind) where we will be
148
- * really strict about not permitting client-side navigation events.
149
- */
150
- return (
151
- <StaticRouter location={config.location} context={{}}>
152
- {wrappedWithRoute}
153
- </StaticRouter>
154
- );
155
- }
156
-
157
- /**
158
- * OK, we must be OK with a memory router.
159
- *
160
- * There are two flavors of config for this. The easy one with just a
161
- * location, and the complex one for those gnarlier setups.
162
- *
163
- * First, the easy one.
164
- */
165
- if (typeof config.location !== "undefined") {
166
- return (
167
- <MemoryRouter initialEntries={[config.location]}>
168
- {wrappedWithRoute}
169
- </MemoryRouter>
170
- );
171
- }
172
-
173
- /**
174
- * If it's not the easy one, it should be the complex one.
175
- * Let's make sure we have good data (also keeps flow happy).
176
- */
177
- if (typeof config.initialEntries === "undefined") {
178
- throw new Error(
179
- "A location or initial history entries must be provided.",
180
- );
181
- }
182
-
183
- /**
184
- * What should happen if no entries were in the array?
185
- * It likely uses the root one anyway, but a consistent API is what
186
- * we want, so let's ensure we always have our default location at least.
187
- */
188
- const entries =
189
- config.initialEntries.length === 0
190
- ? [defaultConfig.location]
191
- : config.initialEntries;
192
-
193
- // Memory router doesn't allow us to pass maybe types in its flow types.
194
- // So let's build props then spread them.
195
- const routerProps: MemoryRouterProps = {
196
- initialEntries: entries,
197
- };
198
- if (config.initialIndex != null) {
199
- routerProps.initialIndex = config.initialIndex;
200
- }
201
- if (config.getUserConfirmation != null) {
202
- routerProps.getUserConfirmation = config.getUserConfirmation;
203
- }
204
-
205
- return <MemoryRouter {...routerProps}>{wrappedWithRoute}</MemoryRouter>;
206
- };
@@ -1,24 +0,0 @@
1
- // @flow
2
- import * as React from "react";
3
-
4
- import {makeTestHarness} from "./make-test-harness.js";
5
- import {DefaultAdapters, DefaultConfigs} from "./adapters/adapters.js";
6
-
7
- import type {TestHarnessConfigs} from "./types.js";
8
-
9
- /**
10
- * Wrap a component with a test harness using Wonder Blocks default adapters.
11
- *
12
- * This is primarily useful for tests within Wonder Blocks.
13
- *
14
- * If you want to expand the range of adapters or change the default
15
- * configurations, use `makeTestHarness` to create a new `testHarness`
16
- * function.
17
- */
18
- export const testHarness: <-TProps, +Instance = mixed>(
19
- Component: React.AbstractComponent<TProps, Instance>,
20
- configs?: $Shape<TestHarnessConfigs<typeof DefaultAdapters>>,
21
- ) => React.AbstractComponent<TProps, Instance> = makeTestHarness(
22
- DefaultAdapters,
23
- DefaultConfigs,
24
- );
package/src/index.js DELETED
@@ -1,26 +0,0 @@
1
- // @flow
2
-
3
- // Fixtures framework
4
- export {fixtures} from "./fixtures/fixtures.js";
5
- export type {
6
- FixtureFn,
7
- FixtureProps,
8
- GetPropsOptions,
9
- } from "./fixtures/types.js";
10
-
11
- // Fetch mocking framework
12
- export {mockFetch} from "./fetch/mock-fetch.js";
13
- export {mockGqlFetch} from "./gql/mock-gql-fetch.js";
14
- export {RespondWith} from "./respond-with.js";
15
- export {SettleController} from "./settle-controller.js";
16
- export type {MockResponse} from "./respond-with.js";
17
- export type {FetchMockFn, FetchMockOperation} from "./fetch/types.js";
18
- export type {GqlFetchMockFn, GqlMockOperation} from "./gql/types.js";
19
-
20
- // Test harness framework
21
- export * from "./harness/types.js";
22
- export * as harnessAdapters from "./harness/adapters/adapters.js";
23
- export {makeHookHarness} from "./harness/make-hook-harness.js";
24
- export {makeTestHarness} from "./harness/make-test-harness.js";
25
- export {hookHarness} from "./harness/hook-harness.js";
26
- export {testHarness} from "./harness/test-harness.js";
@@ -1,35 +0,0 @@
1
- // @flow
2
- import {SettleSignal} from "./settle-signal.js";
3
-
4
- /**
5
- * A controller for the `RespondWith` API to control response settlement.
6
- */
7
- export class SettleController {
8
- #settleFn: () => void;
9
- #signal: SettleSignal;
10
-
11
- constructor() {
12
- // Create our signal.
13
- // We pass in a method to capture it's settle function so that
14
- // only we can call it.
15
- this.#signal = new SettleSignal(
16
- (settleFn) => (this.#settleFn = settleFn),
17
- );
18
- }
19
-
20
- /**
21
- * The signal to pass to the `RespondWith` API.
22
- */
23
- get signal(): SettleSignal {
24
- return this.#signal;
25
- }
26
-
27
- /**
28
- * Settle the signal and therefore any associated responses.
29
- *
30
- * @throws {Error} if the signal has already been settled.
31
- */
32
- settle(): void {
33
- this.#settleFn();
34
- }
35
- }
package/src/types.js DELETED
@@ -1,39 +0,0 @@
1
- // @flow
2
- import type {MockResponse} from "./respond-with.js";
3
-
4
- /**
5
- * A valid GraphQL response as supported by our mocking framework.
6
- * Note that we don't currently support both data and errors being set.
7
- */
8
- export type GraphQLJson<TData: {...}> =
9
- | {|
10
- data: TData,
11
- |}
12
- | {|
13
- errors: Array<{|
14
- message: string,
15
- |}>,
16
- |};
17
-
18
- export type MockFn<TOperationType> = {|
19
- (...args: Array<any>): Promise<Response>,
20
- mockOperation: MockOperationFn<TOperationType>,
21
- mockOperationOnce: MockOperationFn<TOperationType>,
22
- |};
23
-
24
- export type OperationMock<TOperation> = {|
25
- operation: TOperation,
26
- onceOnly: boolean,
27
- used: boolean,
28
- response: () => Promise<Response>,
29
- |};
30
-
31
- export type OperationMatcher<TOperation> = (
32
- operation: TOperation,
33
- ...args: Array<any>
34
- ) => boolean;
35
-
36
- export type MockOperationFn<TOperationType> = <TOperation: TOperationType>(
37
- operation: TOperation,
38
- response: MockResponse<any>,
39
- ) => MockFn<TOperationType>;