graphql-data-generator 0.3.0-alpha.4 → 0.3.0-alpha.6

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