@khanacademy/wonder-blocks-testing-core 1.0.1 → 1.1.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/es/index.js +22 -4
  3. package/dist/fetch/types.d.ts +50 -1
  4. package/dist/index.d.ts +1 -1
  5. package/dist/index.js +22 -4
  6. package/dist/mock-requester.d.ts +1 -1
  7. package/dist/types.d.ts +67 -5
  8. package/package.json +32 -32
  9. package/src/__tests__/mock-requester.test.ts +0 -212
  10. package/src/__tests__/render-hook-static.test.ts +0 -48
  11. package/src/__tests__/respond-with.test.ts +0 -524
  12. package/src/__tests__/response-impl.test.js +0 -47
  13. package/src/__tests__/settle-controller.test.ts +0 -28
  14. package/src/__tests__/settle-signal.test.ts +0 -104
  15. package/src/fetch/__tests__/__snapshots__/mock-fetch.test.ts.snap +0 -29
  16. package/src/fetch/__tests__/fetch-request-matches-mock.test.ts +0 -98
  17. package/src/fetch/__tests__/mock-fetch.test.ts +0 -83
  18. package/src/fetch/fetch-request-matches-mock.ts +0 -42
  19. package/src/fetch/mock-fetch.ts +0 -20
  20. package/src/fetch/types.ts +0 -14
  21. package/src/fixtures/__tests__/fixtures.test.tsx +0 -147
  22. package/src/fixtures/fixtures.basic.stories.tsx +0 -62
  23. package/src/fixtures/fixtures.defaultwrapper.stories.tsx +0 -49
  24. package/src/fixtures/fixtures.tsx +0 -72
  25. package/src/fixtures/types.ts +0 -42
  26. package/src/harness/__tests__/adapt.test.tsx +0 -248
  27. package/src/harness/__tests__/hook-harness.test.ts +0 -73
  28. package/src/harness/__tests__/make-hook-harness.test.tsx +0 -93
  29. package/src/harness/__tests__/make-test-harness.test.tsx +0 -195
  30. package/src/harness/__tests__/test-harness.test.ts +0 -75
  31. package/src/harness/__tests__/types.typestest.tsx +0 -103
  32. package/src/harness/adapt.tsx +0 -41
  33. package/src/harness/adapters/__tests__/__snapshots__/router.test.tsx.snap +0 -5
  34. package/src/harness/adapters/__tests__/css.test.tsx +0 -95
  35. package/src/harness/adapters/__tests__/error-boundary.test.tsx +0 -121
  36. package/src/harness/adapters/__tests__/portal.test.tsx +0 -30
  37. package/src/harness/adapters/__tests__/router.test.tsx +0 -252
  38. package/src/harness/adapters/adapters.ts +0 -35
  39. package/src/harness/adapters/css.tsx +0 -66
  40. package/src/harness/adapters/error-boundary.tsx +0 -56
  41. package/src/harness/adapters/portal.tsx +0 -25
  42. package/src/harness/adapters/router.tsx +0 -205
  43. package/src/harness/get-named-adapter-component.tsx +0 -36
  44. package/src/harness/hook-harness.ts +0 -22
  45. package/src/harness/make-hook-harness.tsx +0 -40
  46. package/src/harness/make-test-harness.tsx +0 -60
  47. package/src/harness/test-harness.ts +0 -13
  48. package/src/harness/types.ts +0 -47
  49. package/src/index.ts +0 -29
  50. package/src/mock-requester.ts +0 -68
  51. package/src/render-hook-static.tsx +0 -60
  52. package/src/respond-with.ts +0 -263
  53. package/src/response-impl.ts +0 -8
  54. package/src/settle-controller.ts +0 -34
  55. package/src/settle-signal.ts +0 -42
  56. package/src/types.ts +0 -40
  57. package/tsconfig-build.json +0 -10
  58. package/tsconfig-build.tsbuildinfo +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @khanacademy/wonder-blocks-testing-core
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 16565a85: Add support for hard fails to the request mocking features
8
+
9
+ ## 1.0.2
10
+
11
+ ### Patch Changes
12
+
13
+ - 02a1b298: Make sure we don't package tsconfig and tsbuildinfo files
14
+
3
15
  ## 1.0.1
4
16
 
5
17
  ### Patch Changes
package/dist/es/index.js CHANGED
@@ -52,19 +52,33 @@ const fetchRequestMatchesMock = (mock, input, init) => {
52
52
 
53
53
  const mockRequester = (operationMatcher, operationToString) => {
54
54
  const mocks = [];
55
- const mockFn = (...args) => {
55
+ const configuration = {
56
+ hardFailOnUnmockedRequests: false
57
+ };
58
+ const getMatchingMock = (...args) => {
56
59
  for (const mock of mocks) {
57
60
  if (mock.onceOnly && mock.used) {
58
61
  continue;
59
62
  }
60
63
  if (operationMatcher(mock.operation, ...args)) {
61
64
  mock.used = true;
62
- return mock.response();
65
+ return mock;
63
66
  }
64
67
  }
68
+ return null;
69
+ };
70
+ const mockFn = (...args) => {
71
+ const matchingMock = getMatchingMock(...args);
72
+ if (matchingMock) {
73
+ return matchingMock.response();
74
+ }
65
75
  const operation = operationToString(...args);
66
- return Promise.reject(new Error(`No matching mock response found for request:
67
- ${operation}`));
76
+ const noMatchError = new Error(`No matching mock response found for request:
77
+ ${operation}`);
78
+ if (configuration.hardFailOnUnmockedRequests) {
79
+ throw noMatchError;
80
+ }
81
+ return Promise.reject(noMatchError);
68
82
  };
69
83
  const addMockedOperation = (operation, response, onceOnly) => {
70
84
  const mockResponse = () => response.toPromise();
@@ -78,6 +92,10 @@ const mockRequester = (operationMatcher, operationToString) => {
78
92
  };
79
93
  mockFn.mockOperation = (operation, response) => addMockedOperation(operation, response, false);
80
94
  mockFn.mockOperationOnce = (operation, response) => addMockedOperation(operation, response, true);
95
+ mockFn.configure = config => {
96
+ Object.assign(configuration, config);
97
+ return mockFn;
98
+ };
81
99
  return mockFn;
82
100
  };
83
101
 
@@ -1,9 +1,58 @@
1
1
  import type { MockResponse } from "../respond-with";
2
+ import { ConfigureFn } from "../types";
2
3
  export type FetchMockOperation = RegExp | string;
3
- type FetchMockOperationFn = (operation: FetchMockOperation, response: MockResponse<any>) => FetchMockFn;
4
+ interface FetchMockOperationFn {
5
+ (
6
+ /**
7
+ * The operation to match.
8
+ *
9
+ * This is a string for an exact match, or a regex. This is compared to
10
+ * to the URL of the fetch request to determine if it is a matching
11
+ * request.
12
+ */
13
+ operation: FetchMockOperation,
14
+ /**
15
+ * The response to return when the operation is matched.
16
+ */
17
+ response: MockResponse<any>): FetchMockFn;
18
+ }
4
19
  export type FetchMockFn = {
20
+ /**
21
+ * The mock fetch function.
22
+ *
23
+ * This function is a drop-in replacement for the fetch function. You should
24
+ * not need to call this function directly. Just replace the normal fetch
25
+ * function implementation with this.
26
+ */
5
27
  (input: RequestInfo, init?: RequestInit): Promise<Response>;
28
+ /**
29
+ * Mock a fetch operation.
30
+ *
31
+ * This adds a response for a given mocked operation. Regardless of how
32
+ * many times this mock is matched, it will be used.
33
+ *
34
+ * @returns The mock fetch function for chaining.
35
+ */
6
36
  mockOperation: FetchMockOperationFn;
37
+ /**
38
+ * Mock a fetch operation once.
39
+ *
40
+ * This adds a response for a given mocked operation that will only be used
41
+ * once and discarded.
42
+ *
43
+ * @returns The mock fetch function for chaining.
44
+ */
7
45
  mockOperationOnce: FetchMockOperationFn;
46
+ /**
47
+ * Configure the mock fetch function with the given configuration.
48
+ *
49
+ * This function is provided as a convenience to allow for configuring the
50
+ * mock fetch function in a fluent manner. The configuration is applied
51
+ * to all mocks for a given fetch function; the last configuration applied
52
+ * will be the one that is used for all mocked operations.
53
+ *
54
+ * @returns The mock fetch function for chaining.
55
+ */
56
+ configure: ConfigureFn<FetchMockOperation, any>;
8
57
  };
9
58
  export {};
package/dist/index.d.ts CHANGED
@@ -6,7 +6,7 @@ export { RespondWith } from "./respond-with";
6
6
  export { SettleController } from "./settle-controller";
7
7
  export type { MockResponse } from "./respond-with";
8
8
  export type { FetchMockFn, FetchMockOperation } from "./fetch/types";
9
- export type { GraphQLJson, MockFn, OperationMock, OperationMatcher, MockOperationFn, } from "./types";
9
+ export type { GraphQLJson, MockFn, OperationMock, OperationMatcher, MockOperationFn, MockConfiguration, ConfigureFn, } from "./types";
10
10
  export * from "./harness/types";
11
11
  export * as harnessAdapters from "./harness/adapters/adapters";
12
12
  export { makeHookHarness } from "./harness/make-hook-harness";
package/dist/index.js CHANGED
@@ -80,19 +80,33 @@ const fetchRequestMatchesMock = (mock, input, init) => {
80
80
 
81
81
  const mockRequester = (operationMatcher, operationToString) => {
82
82
  const mocks = [];
83
- const mockFn = (...args) => {
83
+ const configuration = {
84
+ hardFailOnUnmockedRequests: false
85
+ };
86
+ const getMatchingMock = (...args) => {
84
87
  for (const mock of mocks) {
85
88
  if (mock.onceOnly && mock.used) {
86
89
  continue;
87
90
  }
88
91
  if (operationMatcher(mock.operation, ...args)) {
89
92
  mock.used = true;
90
- return mock.response();
93
+ return mock;
91
94
  }
92
95
  }
96
+ return null;
97
+ };
98
+ const mockFn = (...args) => {
99
+ const matchingMock = getMatchingMock(...args);
100
+ if (matchingMock) {
101
+ return matchingMock.response();
102
+ }
93
103
  const operation = operationToString(...args);
94
- return Promise.reject(new Error(`No matching mock response found for request:
95
- ${operation}`));
104
+ const noMatchError = new Error(`No matching mock response found for request:
105
+ ${operation}`);
106
+ if (configuration.hardFailOnUnmockedRequests) {
107
+ throw noMatchError;
108
+ }
109
+ return Promise.reject(noMatchError);
96
110
  };
97
111
  const addMockedOperation = (operation, response, onceOnly) => {
98
112
  const mockResponse = () => response.toPromise();
@@ -106,6 +120,10 @@ const mockRequester = (operationMatcher, operationToString) => {
106
120
  };
107
121
  mockFn.mockOperation = (operation, response) => addMockedOperation(operation, response, false);
108
122
  mockFn.mockOperationOnce = (operation, response) => addMockedOperation(operation, response, true);
123
+ mockFn.configure = config => {
124
+ Object.assign(configuration, config);
125
+ return mockFn;
126
+ };
109
127
  return mockFn;
110
128
  };
111
129
 
@@ -2,4 +2,4 @@ import type { OperationMatcher, MockFn } from "./types";
2
2
  /**
3
3
  * A generic mock request function for using when mocking fetch or gqlFetch.
4
4
  */
5
- export declare const mockRequester: <TOperationType>(operationMatcher: OperationMatcher<any>, operationToString: (...args: Array<any>) => string) => MockFn<TOperationType>;
5
+ export declare const mockRequester: <TOperationType, TResponseData>(operationMatcher: OperationMatcher<any>, operationToString: (...args: Array<any>) => string) => MockFn<TOperationType, TResponseData>;
package/dist/types.d.ts CHANGED
@@ -10,11 +10,43 @@ export type GraphQLJson<TData extends Record<any, any>> = {
10
10
  message: string;
11
11
  }>;
12
12
  };
13
- export type MockFn<TOperationType> = {
13
+ export interface MockFn<TOperationType, TResponseData> {
14
+ /**
15
+ * The mock fetch function.
16
+ *
17
+ * This function is a drop-in replacement for the fetch function being
18
+ * mocked. It is recommended that a more strongly-typed definition is
19
+ * provided in the consuming codebase, as this definition is intentionally
20
+ * loose to allow for mocking any fetch operation.
21
+ */
14
22
  (...args: Array<any>): Promise<Response>;
15
- mockOperation: MockOperationFn<TOperationType>;
16
- mockOperationOnce: MockOperationFn<TOperationType>;
17
- };
23
+ /**
24
+ * Mock a fetch operation.
25
+ *
26
+ * This adds a response for a given mocked operation of the given type.
27
+ * Matches are determined by the operation matcher provided to the
28
+ * mockRequester function that creates the mock fetch function.
29
+ */
30
+ mockOperation: MockOperationFn<TOperationType, TResponseData>;
31
+ /**
32
+ * Mock a fetch operation once.
33
+ *
34
+ * This adds a response for a given mocked operation of the given type that
35
+ * will only be used once and discarded. Matches are determined by the
36
+ * operation matcher provided to the mockRequester function that creates the
37
+ * mock fetch function.
38
+ */
39
+ mockOperationOnce: MockOperationFn<TOperationType, TResponseData>;
40
+ /**
41
+ * Configure the mock fetch function with the given configuration.
42
+ *
43
+ * This function is provided as a convenience to allow for configuring the
44
+ * mock fetch function in a fluent manner. The configuration is applied
45
+ * to all mocks for a given fetch function; the last configuration applied
46
+ * will be the one that is used for all mocked operations.
47
+ */
48
+ configure: ConfigureFn<TOperationType, TResponseData>;
49
+ }
18
50
  export type OperationMock<TOperation> = {
19
51
  operation: TOperation;
20
52
  onceOnly: boolean;
@@ -22,4 +54,34 @@ export type OperationMock<TOperation> = {
22
54
  response: () => Promise<Response>;
23
55
  };
24
56
  export type OperationMatcher<TOperation> = (operation: TOperation, ...args: Array<any>) => boolean;
25
- export type MockOperationFn<TOperationType> = <TOperation extends TOperationType>(operation: TOperation, response: MockResponse<any>) => MockFn<TOperationType>;
57
+ export type MockOperationFn<TOperationType, TResponseData> = <TOperation extends TOperationType>(operation: TOperation, response: MockResponse<TResponseData>) => MockFn<TOperationType, TResponseData>;
58
+ /**
59
+ * Configuration options for mocked fetches.
60
+ */
61
+ export type MockConfiguration = {
62
+ /**
63
+ * If true, any requests that don't match a mock will throw an error
64
+ * immediately on the request being made; otherwise, if false, unmatched
65
+ * requests will return a rejected promise.
66
+ *
67
+ * Defaults to false. When true, this is akin to the Apollo MockLink
68
+ * behavior that throws upon the request being. This is useful as it will
69
+ * clearly fail a test early, indicating that a request was not mocked.
70
+ * However, that mode requires all requests to be mocked, which can be
71
+ * cumbersome and unncessary. Having unmocked requests return a rejected
72
+ * promise is more flexible and allows for more granular control over
73
+ * mocking, allowing developers to mock only the requests they care about
74
+ * and let the error handling of their code deal with the rejected promises.
75
+ */
76
+ hardFailOnUnmockedRequests: boolean;
77
+ };
78
+ export interface ConfigureFn<TOperationType, TResponseData> {
79
+ /**
80
+ * Configure the mock fetch function with the given configuration.
81
+ *
82
+ * @param config The configuration changes to apply to the mock fetch
83
+ * function.
84
+ * @returns The mock fetch function .
85
+ */
86
+ (config: Partial<MockConfiguration>): MockFn<TOperationType, TResponseData>;
87
+ }
package/package.json CHANGED
@@ -1,33 +1,33 @@
1
1
  {
2
- "name": "@khanacademy/wonder-blocks-testing-core",
3
- "version": "1.0.1",
4
- "design": "v1",
5
- "publishConfig": {
6
- "access": "public"
7
- },
8
- "description": "",
9
- "main": "dist/index.js",
10
- "module": "dist/es/index.js",
11
- "types": "dist/index.d.ts",
12
- "scripts": {
13
- "test": "echo \"Error: no test specified\" && exit 1"
14
- },
15
- "dependencies": {
16
- "@babel/runtime": "^7.18.6"
17
- },
18
- "peerDependencies": {
19
- "@khanacademy/wonder-stuff-core": "^1.2.2",
20
- "@storybook/addon-actions": "^7.0.0",
21
- "aphrodite": "^1.2.5",
22
- "node-fetch": "^2.6.7",
23
- "react": "16.14.0",
24
- "react-dom": "16.14.0",
25
- "react-router-dom": "5.3.0"
26
- },
27
- "devDependencies": {
28
- "@khanacademy/wb-dev-build-settings": "^1.0.1",
29
- "@khanacademy/wonder-stuff-testing": "^3.0.1"
30
- },
31
- "author": "",
32
- "license": "MIT"
33
- }
2
+ "name": "@khanacademy/wonder-blocks-testing-core",
3
+ "version": "1.1.0",
4
+ "design": "v1",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "description": "",
9
+ "main": "dist/index.js",
10
+ "module": "dist/es/index.js",
11
+ "types": "dist/index.d.ts",
12
+ "scripts": {
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "dependencies": {
16
+ "@babel/runtime": "^7.18.6"
17
+ },
18
+ "peerDependencies": {
19
+ "@khanacademy/wonder-stuff-core": "^1.2.2",
20
+ "@storybook/addon-actions": "^7.0.0",
21
+ "aphrodite": "^1.2.5",
22
+ "node-fetch": "^2.6.7",
23
+ "react": "16.14.0",
24
+ "react-dom": "16.14.0",
25
+ "react-router-dom": "5.3.0"
26
+ },
27
+ "devDependencies": {
28
+ "@khanacademy/wb-dev-build-settings": "^1.0.1",
29
+ "@khanacademy/wonder-stuff-testing": "^3.0.1"
30
+ },
31
+ "author": "",
32
+ "license": "MIT"
33
+ }
@@ -1,212 +0,0 @@
1
- import {RespondWith} from "../respond-with";
2
- import {mockRequester} from "../mock-requester";
3
-
4
- describe("#mockRequester", () => {
5
- it("should return a function", () => {
6
- // Arrange
7
-
8
- // Act
9
- const result = mockRequester(jest.fn(), jest.fn());
10
-
11
- // Assert
12
- expect(result).toBeInstanceOf(Function);
13
- });
14
-
15
- it("should provide mockOperation API", () => {
16
- // Arrange
17
-
18
- // Act
19
- const result = mockRequester(jest.fn(), jest.fn());
20
-
21
- // Assert
22
- expect(result).toHaveProperty("mockOperation", expect.any(Function));
23
- });
24
-
25
- it("should provide mockOperationOnce API", () => {
26
- // Arrange
27
-
28
- // Act
29
- const result = mockRequester(jest.fn(), jest.fn());
30
-
31
- // Assert
32
- expect(result).toHaveProperty(
33
- "mockOperationOnce",
34
- expect.any(Function),
35
- );
36
- });
37
-
38
- it("should throw with helpful details formatted by operationToString if no matching mock is found", async () => {
39
- // Arrange
40
- const mockFn = mockRequester(
41
- jest.fn(),
42
- (...args: any) => `TEST FORMATTING: ${JSON.stringify(args)}`,
43
- );
44
-
45
- // Act
46
- const underTest = mockFn("any", "arguments", {we: {want: 42}});
47
-
48
- // Assert
49
- await expect(underTest).rejects.toThrowErrorMatchingInlineSnapshot(`
50
- "No matching mock response found for request:
51
- TEST FORMATTING: ["any","arguments",{"we":{"want":42}}]"
52
- `);
53
- });
54
-
55
- describe("mockOperation", () => {
56
- it("should invoke matcher with mock for a request", async () => {
57
- // Arrange
58
- const matcher = jest.fn().mockReturnValue(true);
59
- const operationToString = jest.fn();
60
- const mockFn = mockRequester(matcher, operationToString);
61
-
62
- // Act
63
- mockFn.mockOperation(
64
- "THE MOCK DESCRIPTION",
65
- RespondWith.text("TADA!"),
66
- );
67
- await mockFn("any", "arguments", {we: {want: 42}});
68
-
69
- // Assert
70
- expect(matcher).toHaveBeenCalledWith(
71
- "THE MOCK DESCRIPTION",
72
- "any",
73
- "arguments",
74
- {
75
- we: {want: 42},
76
- },
77
- );
78
- });
79
-
80
- it("should return mocked operation response if matcher returns true", async () => {
81
- // Arrange
82
- const matcher = jest.fn().mockReturnValue(true);
83
- const operationToString = jest.fn();
84
- const mockFn = mockRequester(matcher, operationToString);
85
-
86
- // Act
87
- mockFn.mockOperation(
88
- "THE MOCK DESCRIPTION",
89
- RespondWith.text("TADA!"),
90
- );
91
- const response = await mockFn("DO SOMETHING");
92
- const result = response.text();
93
-
94
- // Assert
95
- await expect(result).resolves.toBe("TADA!");
96
- });
97
-
98
- it("should skip mock if matcher returns false and try more mocks", async () => {
99
- // Arrange
100
- const matcher = jest
101
- .fn()
102
- .mockReturnValueOnce(false)
103
- .mockReturnValueOnce(true);
104
- const operationToString = jest.fn();
105
- const mockFn = mockRequester(matcher, operationToString);
106
-
107
- // Act
108
- mockFn.mockOperation(
109
- "THE MOCK DESCRIPTION 1",
110
- RespondWith.text("ONE"),
111
- );
112
- mockFn.mockOperation(
113
- "THE MOCK DESCRIPTION 2",
114
- RespondWith.text("TWO"),
115
- );
116
- const response = await mockFn("DO SOMETHING");
117
- const result = response.text();
118
-
119
- // Assert
120
- await expect(result).resolves.toBe("TWO");
121
- });
122
- });
123
-
124
- describe("mockOperationOnce", () => {
125
- it("should invoke matcher with mock for a request", async () => {
126
- // Arrange
127
- const matcher = jest.fn().mockReturnValue(true);
128
- const operationToString = jest.fn();
129
- const mockFn = mockRequester(matcher, operationToString);
130
-
131
- // Act
132
- mockFn.mockOperationOnce(
133
- "THE MOCK DESCRIPTION",
134
- RespondWith.text("TADA!"),
135
- );
136
- await mockFn("any", "arguments", {we: {want: 42}});
137
-
138
- // Assert
139
- expect(matcher).toHaveBeenCalledWith(
140
- "THE MOCK DESCRIPTION",
141
- "any",
142
- "arguments",
143
- {
144
- we: {want: 42},
145
- },
146
- );
147
- });
148
-
149
- it("should match once", async () => {
150
- // Arrange
151
- const matcher = jest.fn().mockReturnValue(true);
152
- const operationToString = jest.fn();
153
- const mockFn = mockRequester(matcher, operationToString);
154
-
155
- // Act
156
- mockFn.mockOperationOnce(
157
- "THE MOCK DESCRIPTION",
158
- RespondWith.text("TADA!"),
159
- );
160
- const response = await mockFn("DO SOMETHING");
161
- const result = response.text();
162
-
163
- // Assert
164
- await expect(result).resolves.toBe("TADA!");
165
- });
166
-
167
- it("should only match once", async () => {
168
- // Arrange
169
- const matcher = jest.fn().mockReturnValue(true);
170
- const operationToString = jest.fn();
171
- const mockFn = mockRequester(matcher, operationToString);
172
-
173
- // Act
174
- mockFn.mockOperationOnce(
175
- "THE MOCK DESCRIPTION",
176
- RespondWith.text("TADA!"),
177
- );
178
- const result = Promise.all([
179
- mockFn("DO SOMETHING"),
180
- mockFn("DO SOMETHING"),
181
- ]);
182
-
183
- // Assert
184
- await expect(result).rejects.toThrowError();
185
- });
186
-
187
- it("should skip mock if matcher returns false and try more mocks", async () => {
188
- // Arrange
189
- const matcher = jest
190
- .fn()
191
- .mockReturnValueOnce(false)
192
- .mockReturnValueOnce(true);
193
- const operationToString = jest.fn();
194
- const mockFn = mockRequester(matcher, operationToString);
195
-
196
- // Act
197
- mockFn.mockOperationOnce(
198
- "THE MOCK DESCRIPTION 1",
199
- RespondWith.text("ONE"),
200
- );
201
- mockFn.mockOperationOnce(
202
- "THE MOCK DESCRIPTION 2",
203
- RespondWith.text("TWO"),
204
- );
205
- const response = await mockFn("DO SOMETHING");
206
- const result = response.text();
207
-
208
- // Assert
209
- await expect(result).resolves.toBe("TWO");
210
- });
211
- });
212
- });
@@ -1,48 +0,0 @@
1
- import {useLocation} from "react-router-dom";
2
-
3
- import {hookHarness} from "../harness/hook-harness";
4
- import {renderHookStatic} from "../render-hook-static";
5
-
6
- describe("renderHookStatic", () => {
7
- it("should return the result of rendering the hook", () => {
8
- // Arrange
9
- const useTestHook = () => "Hello, World!";
10
-
11
- // Act
12
- const result = renderHookStatic(useTestHook);
13
-
14
- // Assert
15
- expect(result).toStrictEqual({result: {current: "Hello, World!"}});
16
- });
17
-
18
- it("should render the hook with the given initialProps", () => {
19
- // Arrange
20
- const useTestHook = (initialProps?: string) =>
21
- initialProps ?? "BAD RESULT";
22
-
23
- // Act
24
- const result = renderHookStatic(useTestHook, {
25
- initialProps: "Hello, World!",
26
- });
27
-
28
- // Assert
29
- expect(result).toStrictEqual({result: {current: "Hello, World!"}});
30
- });
31
-
32
- it("should return the result of rendering the hook inside the wrapper", () => {
33
- // Arrange
34
- const Wrapper = hookHarness({
35
- router: "/test/route",
36
- });
37
- const useTestHook = () => {
38
- const location = useLocation();
39
- return location.pathname;
40
- };
41
-
42
- // Act
43
- const result = renderHookStatic(useTestHook, {wrapper: Wrapper});
44
-
45
- // Assert
46
- expect(result).toStrictEqual({result: {current: "/test/route"}});
47
- });
48
- });