graphql-data-generator 0.3.0-alpha.5 → 0.3.0-alpha.7

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/esm/jest.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as dntShim from "./_dnt.shims.js";
2
2
  import React, { useMemo } from "react";
3
3
  import { ApolloLink, useApolloClient, } from "@apollo/client";
4
- import { ErrorLink } from "@apollo/client/link/error";
4
+ import { onError } from "@apollo/client/link/error";
5
5
  import { MockedProvider, MockLink, } from "@apollo/client/testing";
6
6
  import { waitFor } from "@testing-library/dom";
7
7
  import { Kind, print } from "graphql";
@@ -59,9 +59,18 @@ let _failRefetchWarnings = false;
59
59
  * In older versions of `@apollo/client`, refetches with missing mocks trigger
60
60
  * warnings instead of being treated as standard missing mocks.
61
61
  * This utility converts those warnings into failures and ensures watch queries
62
- * are installed for queries with `watch: true`.
62
+ * are installed for queries with `watch: true`. This must be set when
63
+ * `MockProvider` is mounted.
64
+ *
65
+ * This is not required on modern version of `@apollo/client`.
63
66
  */
64
67
  export const failRefetchWarnings = (value = true) => _failRefetchWarnings = value;
68
+ let _allowMissingMocks = false;
69
+ /**
70
+ * Allows missing mocks, resulting in tests passing. Usage is intended to ease
71
+ * migration.
72
+ */
73
+ export const allowMissingMocks = (value) => _allowMissingMocks = value;
65
74
  const AutoWatch = ({ mocks }) => {
66
75
  const client = useApolloClient();
67
76
  for (const mock of mocks)
@@ -69,7 +78,12 @@ const AutoWatch = ({ mocks }) => {
69
78
  client.watchQuery(mock.request);
70
79
  return null;
71
80
  };
72
- export const MockProvider = ({ mocks, stack: renderStack, children }) => {
81
+ /**
82
+ * A wrapper for `@apollo/client/testing`, this component will assert all
83
+ * requests have matching mocks and all defined mocks are used unless marked
84
+ * `optional`.
85
+ */
86
+ export const MockProvider = ({ mocks, stack: renderStack, children, link: passedLink, ...rest }) => {
73
87
  const observableMocks = useMemo(() => {
74
88
  const observableMocks = mocks.flatMap((m) => [
75
89
  {
@@ -90,7 +104,7 @@ export const MockProvider = ({ mocks, stack: renderStack, children }) => {
90
104
  if (currentSpecResult.failedExpectations.length)
91
105
  return;
92
106
  for (const mock of observableMocks) {
93
- if ("optional" in mock && mock.optional || mock.error)
107
+ if (mock.optional || mock.error)
94
108
  continue;
95
109
  await waitFor(() => {
96
110
  if (currentSpecResult.failedExpectations.length)
@@ -114,8 +128,9 @@ export const MockProvider = ({ mocks, stack: renderStack, children }) => {
114
128
  const link = useMemo(() => {
115
129
  const mockLink = new MockLink(observableMocks);
116
130
  mockLink.showWarnings = false;
117
- const errorLoggingLink = new ErrorLink(({ networkError, operation }) => {
118
- if (!networkError?.message.includes("No more mocked responses"))
131
+ const errorLoggingLink = onError(({ networkError, operation }) => {
132
+ if (_allowMissingMocks ||
133
+ !networkError?.message.includes("No more mocked responses"))
119
134
  return;
120
135
  const { message, stack: altStack } = getErrorMessage(operation, mockLink);
121
136
  try {
@@ -129,8 +144,11 @@ export const MockProvider = ({ mocks, stack: renderStack, children }) => {
129
144
  // fail both throws and marks the test as failed in jest; we only need the latter
130
145
  }
131
146
  });
132
- // @ts-ignore It's fine
133
- return ApolloLink.from([errorLoggingLink, mockLink]);
147
+ return ApolloLink.from([
148
+ errorLoggingLink,
149
+ mockLink,
150
+ ...(passedLink ? [passedLink] : []),
151
+ ]);
134
152
  }, [observableMocks, renderStack]);
135
153
  if (_failRefetchWarnings) {
136
154
  const oldWarn = console.warn.bind(console.warn);
@@ -154,7 +172,7 @@ export const MockProvider = ({ mocks, stack: renderStack, children }) => {
154
172
  console.warn = oldWarn;
155
173
  });
156
174
  }
157
- return (React.createElement(MockedProvider, { link: link },
175
+ return (React.createElement(MockedProvider, { ...rest, link: link },
158
176
  React.createElement(React.Fragment, null,
159
177
  React.createElement(AutoWatch, { mocks: observableMocks }),
160
178
  children)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-data-generator",
3
- "version": "0.3.0-alpha.5",
3
+ "version": "0.3.0-alpha.7",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/voces/graphql-data-generator.git"
package/script/jest.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.MockProvider = exports.failRefetchWarnings = void 0;
26
+ exports.MockProvider = exports.allowMissingMocks = exports.failRefetchWarnings = void 0;
27
27
  const dntShim = __importStar(require("./_dnt.shims.js"));
28
28
  const react_1 = __importStar(require("react"));
29
29
  const client_1 = require("@apollo/client");
@@ -85,10 +85,20 @@ let _failRefetchWarnings = false;
85
85
  * In older versions of `@apollo/client`, refetches with missing mocks trigger
86
86
  * warnings instead of being treated as standard missing mocks.
87
87
  * This utility converts those warnings into failures and ensures watch queries
88
- * are installed for queries with `watch: true`.
88
+ * are installed for queries with `watch: true`. This must be set when
89
+ * `MockProvider` is mounted.
90
+ *
91
+ * This is not required on modern version of `@apollo/client`.
89
92
  */
90
93
  const failRefetchWarnings = (value = true) => _failRefetchWarnings = value;
91
94
  exports.failRefetchWarnings = failRefetchWarnings;
95
+ let _allowMissingMocks = false;
96
+ /**
97
+ * Allows missing mocks, resulting in tests passing. Usage is intended to ease
98
+ * migration.
99
+ */
100
+ const allowMissingMocks = (value) => _allowMissingMocks = value;
101
+ exports.allowMissingMocks = allowMissingMocks;
92
102
  const AutoWatch = ({ mocks }) => {
93
103
  const client = (0, client_1.useApolloClient)();
94
104
  for (const mock of mocks)
@@ -96,7 +106,12 @@ const AutoWatch = ({ mocks }) => {
96
106
  client.watchQuery(mock.request);
97
107
  return null;
98
108
  };
99
- const MockProvider = ({ mocks, stack: renderStack, children }) => {
109
+ /**
110
+ * A wrapper for `@apollo/client/testing`, this component will assert all
111
+ * requests have matching mocks and all defined mocks are used unless marked
112
+ * `optional`.
113
+ */
114
+ const MockProvider = ({ mocks, stack: renderStack, children, link: passedLink, ...rest }) => {
100
115
  const observableMocks = (0, react_1.useMemo)(() => {
101
116
  const observableMocks = mocks.flatMap((m) => [
102
117
  {
@@ -117,7 +132,7 @@ const MockProvider = ({ mocks, stack: renderStack, children }) => {
117
132
  if (currentSpecResult.failedExpectations.length)
118
133
  return;
119
134
  for (const mock of observableMocks) {
120
- if ("optional" in mock && mock.optional || mock.error)
135
+ if (mock.optional || mock.error)
121
136
  continue;
122
137
  await (0, dom_1.waitFor)(() => {
123
138
  if (currentSpecResult.failedExpectations.length)
@@ -141,8 +156,9 @@ const MockProvider = ({ mocks, stack: renderStack, children }) => {
141
156
  const link = (0, react_1.useMemo)(() => {
142
157
  const mockLink = new testing_1.MockLink(observableMocks);
143
158
  mockLink.showWarnings = false;
144
- const errorLoggingLink = new error_1.ErrorLink(({ networkError, operation }) => {
145
- if (!networkError?.message.includes("No more mocked responses"))
159
+ const errorLoggingLink = (0, error_1.onError)(({ networkError, operation }) => {
160
+ if (_allowMissingMocks ||
161
+ !networkError?.message.includes("No more mocked responses"))
146
162
  return;
147
163
  const { message, stack: altStack } = getErrorMessage(operation, mockLink);
148
164
  try {
@@ -156,8 +172,11 @@ const MockProvider = ({ mocks, stack: renderStack, children }) => {
156
172
  // fail both throws and marks the test as failed in jest; we only need the latter
157
173
  }
158
174
  });
159
- // @ts-ignore It's fine
160
- return client_1.ApolloLink.from([errorLoggingLink, mockLink]);
175
+ return client_1.ApolloLink.from([
176
+ errorLoggingLink,
177
+ mockLink,
178
+ ...(passedLink ? [passedLink] : []),
179
+ ]);
161
180
  }, [observableMocks, renderStack]);
162
181
  if (_failRefetchWarnings) {
163
182
  const oldWarn = console.warn.bind(console.warn);
@@ -181,7 +200,7 @@ const MockProvider = ({ mocks, stack: renderStack, children }) => {
181
200
  console.warn = oldWarn;
182
201
  });
183
202
  }
184
- return (react_1.default.createElement(testing_1.MockedProvider, { link: link },
203
+ return (react_1.default.createElement(testing_1.MockedProvider, { ...rest, link: link },
185
204
  react_1.default.createElement(react_1.default.Fragment, null,
186
205
  react_1.default.createElement(AutoWatch, { mocks: observableMocks }),
187
206
  children)));
package/types/jest.d.ts CHANGED
@@ -1,14 +1,27 @@
1
1
  import React from "react";
2
+ import { MockedProviderProps } from "@apollo/client/testing";
2
3
  import type { OperationMock } from "./types.js";
3
4
  /**
4
5
  * In older versions of `@apollo/client`, refetches with missing mocks trigger
5
6
  * warnings instead of being treated as standard missing mocks.
6
7
  * This utility converts those warnings into failures and ensures watch queries
7
- * are installed for queries with `watch: true`.
8
+ * are installed for queries with `watch: true`. This must be set when
9
+ * `MockProvider` is mounted.
10
+ *
11
+ * This is not required on modern version of `@apollo/client`.
8
12
  */
9
13
  export declare const failRefetchWarnings: (value?: boolean) => boolean;
10
- export declare const MockProvider: ({ mocks, stack: renderStack, children }: {
14
+ /**
15
+ * Allows missing mocks, resulting in tests passing. Usage is intended to ease
16
+ * migration.
17
+ */
18
+ export declare const allowMissingMocks: (value: true) => true;
19
+ /**
20
+ * A wrapper for `@apollo/client/testing`, this component will assert all
21
+ * requests have matching mocks and all defined mocks are used unless marked
22
+ * `optional`.
23
+ */
24
+ export declare const MockProvider: ({ mocks, stack: renderStack, children, link: passedLink, ...rest }: Omit<MockedProviderProps, "mocks"> & {
11
25
  mocks: OperationMock[];
12
26
  stack?: string;
13
- children?: React.ReactNode;
14
27
  }) => React.JSX.Element;
package/types/types.d.ts CHANGED
@@ -29,12 +29,15 @@ export type OperationMock<Data extends Record<string, unknown> = Record<string,
29
29
  error?: Error;
30
30
  stack?: string;
31
31
  watch?: boolean;
32
+ optional?: boolean;
32
33
  };
33
34
  export type SimpleOperationMock<Data extends Record<string, unknown> = Record<string, unknown>, Variables = Record<string, unknown> | never> = {
34
35
  data: Data;
35
36
  variables?: Variables;
36
37
  error?: Error;
37
38
  errors?: GraphQLError[];
39
+ watch?: boolean;
40
+ optional?: boolean;
38
41
  };
39
42
  export type SimpleOperationPatch<Data extends Record<string, unknown> = Record<string, unknown>, Variables = Record<string, unknown> | never> = Patch<{
40
43
  data: Data;
@@ -42,5 +45,7 @@ export type SimpleOperationPatch<Data extends Record<string, unknown> = Record<s
42
45
  }> & {
43
46
  error?: Error;
44
47
  errors?: GraphQLError[];
48
+ watch?: boolean;
49
+ optional?: boolean;
45
50
  };
46
51
  export {};