@dhis2/app-service-data 3.11.3 → 3.12.0-alpha.2
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/build/cjs/__tests__/integration.test.js +6 -15
- package/build/cjs/__tests__/mutations.test.js +4 -11
- package/build/cjs/engine/DataEngine.js +4 -14
- package/build/cjs/engine/DataEngine.test.js +0 -2
- package/build/cjs/engine/helpers/getMutationFetchType.js +0 -2
- package/build/cjs/engine/helpers/getMutationFetchType.test.js +0 -1
- package/build/cjs/engine/helpers/resolveDynamicQuery.js +0 -2
- package/build/cjs/engine/helpers/resolveDynamicQuery.test.js +3 -4
- package/build/cjs/engine/helpers/validate.js +0 -21
- package/build/cjs/engine/helpers/validate.test.js +0 -1
- package/build/cjs/engine/index.js +0 -18
- package/build/cjs/engine/types/FetchError.js +3 -8
- package/build/cjs/engine/types/FetchError.test.js +0 -1
- package/build/cjs/engine/types/InvalidQueryError.js +3 -8
- package/build/cjs/index.js +0 -2
- package/build/cjs/links/CustomDataLink.js +3 -17
- package/build/cjs/links/CustomDataLink.test.js +0 -1
- package/build/cjs/links/ErrorLink.js +3 -7
- package/build/cjs/links/RestAPILink/fetchData.js +12 -22
- package/build/cjs/links/RestAPILink/fetchData.test.js +0 -2
- package/build/cjs/links/RestAPILink/metadataResources.js +9 -7
- package/build/cjs/links/RestAPILink/path.js +0 -3
- package/build/cjs/links/RestAPILink/path.test.js +0 -1
- package/build/cjs/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.js +9 -14
- package/build/cjs/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.test.js +0 -1
- package/build/cjs/links/RestAPILink/queryToRequestOptions/requestContentType.js +4 -31
- package/build/cjs/links/RestAPILink/queryToRequestOptions/requestContentType.test.js +0 -1
- package/build/cjs/links/RestAPILink/queryToRequestOptions/textPlainMatchers.js +22 -39
- package/build/cjs/links/RestAPILink/queryToRequestOptions/textPlainMatchers.test.js +0 -1
- package/build/cjs/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.js +0 -2
- package/build/cjs/links/RestAPILink/queryToRequestOptions/xWwwFormUrlencodedMatchers.test.js +0 -1
- package/build/cjs/links/RestAPILink/queryToRequestOptions.js +0 -9
- package/build/cjs/links/RestAPILink/queryToRequestOptions.test.js +0 -1
- package/build/cjs/links/RestAPILink/queryToResourcePath.js +3 -24
- package/build/cjs/links/RestAPILink/queryToResourcePath.test.js +2 -4
- package/build/cjs/links/RestAPILink/validateQuery.js +4 -18
- package/build/cjs/links/RestAPILink/validateQuery.test.js +0 -1
- package/build/cjs/links/RestAPILink.js +3 -14
- package/build/cjs/links/RestAPILink.test.js +0 -2
- package/build/cjs/links/index.js +0 -6
- package/build/cjs/react/components/CustomDataProvider.js +3 -12
- package/build/cjs/react/components/DataMutation.js +1 -4
- package/build/cjs/react/components/DataProvider.js +11 -16
- package/build/cjs/react/components/DataProvider.test.js +2 -9
- package/build/cjs/react/components/DataQuery.js +1 -4
- package/build/cjs/react/context/DataContext.js +2 -8
- package/build/cjs/react/context/defaultContext.js +2 -6
- package/build/cjs/react/context/defaultContext.test.js +0 -1
- package/build/cjs/react/hooks/mergeAndCompareVariables.js +4 -6
- package/build/cjs/react/hooks/mergeAndCompareVariables.test.js +2 -3
- package/build/cjs/react/hooks/stableVariablesHash.js +11 -16
- package/build/cjs/react/hooks/stableVariablesHash.test.js +0 -1
- package/build/cjs/react/hooks/useDataEngine.js +0 -4
- package/build/cjs/react/hooks/useDataMutation.js +0 -7
- package/build/cjs/react/hooks/useDataMutation.test.js +42 -71
- package/build/cjs/react/hooks/useDataQuery.js +26 -44
- package/build/cjs/react/hooks/useDataQuery.test.js +318 -374
- package/build/cjs/react/hooks/useQueryExecutor.js +6 -14
- package/build/cjs/react/hooks/useQueryExecutor.test.js +42 -45
- package/build/cjs/react/hooks/useStaticInput.js +0 -3
- package/build/cjs/react/hooks/useStaticInput.test.js +8 -10
- package/build/cjs/react/index.js +0 -11
- package/build/cjs/setupRTL.js +1 -2
- package/build/es/__tests__/integration.test.js +4 -8
- package/build/es/__tests__/mutations.test.js +2 -4
- package/build/es/engine/DataEngine.js +3 -8
- package/build/es/engine/DataEngine.test.js +0 -1
- package/build/es/engine/helpers/resolveDynamicQuery.test.js +3 -3
- package/build/es/engine/helpers/validate.js +0 -13
- package/build/es/engine/types/FetchError.js +3 -6
- package/build/es/engine/types/InvalidQueryError.js +3 -6
- package/build/es/links/CustomDataLink.js +3 -15
- package/build/es/links/ErrorLink.js +3 -5
- package/build/es/links/RestAPILink/fetchData.js +12 -16
- package/build/es/links/RestAPILink/metadataResources.js +8 -3
- package/build/es/links/RestAPILink/path.js +0 -1
- package/build/es/links/RestAPILink/queryToRequestOptions/multipartFormDataMatchers.js +9 -4
- package/build/es/links/RestAPILink/queryToRequestOptions/requestContentType.js +2 -19
- package/build/es/links/RestAPILink/queryToRequestOptions/textPlainMatchers.js +22 -19
- package/build/es/links/RestAPILink/queryToRequestOptions.js +0 -7
- package/build/es/links/RestAPILink/queryToResourcePath.js +3 -21
- package/build/es/links/RestAPILink/queryToResourcePath.test.js +2 -3
- package/build/es/links/RestAPILink/validateQuery.js +4 -16
- package/build/es/links/RestAPILink.js +3 -8
- package/build/es/react/components/CustomDataProvider.js +2 -2
- package/build/es/react/components/DataMutation.js +1 -1
- package/build/es/react/components/DataProvider.js +9 -3
- package/build/es/react/components/DataProvider.test.js +1 -1
- package/build/es/react/components/DataQuery.js +1 -1
- package/build/es/react/hooks/mergeAndCompareVariables.js +4 -3
- package/build/es/react/hooks/mergeAndCompareVariables.test.js +2 -1
- package/build/es/react/hooks/stableVariablesHash.js +11 -14
- package/build/es/react/hooks/useDataMutation.test.js +17 -39
- package/build/es/react/hooks/useDataQuery.js +26 -37
- package/build/es/react/hooks/useDataQuery.test.js +268 -318
- package/build/es/react/hooks/useQueryExecutor.js +6 -9
- package/build/es/react/hooks/useQueryExecutor.test.js +33 -34
- package/build/es/react/hooks/useStaticInput.test.js +6 -6
- package/build/es/setupRTL.js +1 -1
- package/build/types/engine/types/ExecuteOptions.d.ts +1 -1
- package/build/types/engine/types/FetchError.d.ts +2 -2
- package/build/types/engine/types/JsonValue.d.ts +1 -1
- package/build/types/engine/types/Mutation.d.ts +2 -2
- package/build/types/engine/types/PossiblyDynamic.d.ts +1 -1
- package/build/types/engine/types/Query.d.ts +3 -3
- package/build/types/engine/types/QueryParameters.d.ts +4 -4
- package/build/types/links/CustomDataLink.d.ts +2 -2
- package/build/types/links/RestAPILink/queryToRequestOptions/requestContentType.d.ts +1 -1
- package/build/types/links/RestAPILink/queryToRequestOptions.d.ts +1 -1
- package/build/types/react/components/DataProvider.d.ts +2 -11
- package/build/types/react/hooks/mergeAndCompareVariables.d.ts +1 -1
- package/build/types/types.d.ts +6 -6
- package/package.json +5 -5
|
@@ -1,19 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
|
|
3
|
+
var _react = require("@testing-library/react");
|
|
5
4
|
var React = _interopRequireWildcard(require("react"));
|
|
6
|
-
|
|
7
5
|
var _CustomDataProvider = require("../components/CustomDataProvider");
|
|
8
|
-
|
|
9
6
|
var _useDataEngine = require("./useDataEngine");
|
|
10
|
-
|
|
11
7
|
var _useDataMutation = require("./useDataMutation");
|
|
12
|
-
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
16
|
-
|
|
8
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
9
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
17
10
|
describe('useDataMutation', () => {
|
|
18
11
|
it('should render without failing', async () => {
|
|
19
12
|
const mutation = {
|
|
@@ -26,8 +19,7 @@ describe('useDataMutation', () => {
|
|
|
26
19
|
const data = {
|
|
27
20
|
answer: 42
|
|
28
21
|
};
|
|
29
|
-
|
|
30
|
-
const wrapper = (_ref) => {
|
|
22
|
+
const wrapper = _ref => {
|
|
31
23
|
let {
|
|
32
24
|
children
|
|
33
25
|
} = _ref;
|
|
@@ -35,11 +27,9 @@ describe('useDataMutation', () => {
|
|
|
35
27
|
data: data
|
|
36
28
|
}, children);
|
|
37
29
|
};
|
|
38
|
-
|
|
39
30
|
const {
|
|
40
|
-
result
|
|
41
|
-
|
|
42
|
-
} = (0, _reactHooks.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation), {
|
|
31
|
+
result
|
|
32
|
+
} = (0, _react.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation), {
|
|
43
33
|
wrapper
|
|
44
34
|
});
|
|
45
35
|
const [mutate, beforeMutation] = result.current;
|
|
@@ -47,17 +37,17 @@ describe('useDataMutation', () => {
|
|
|
47
37
|
loading: false,
|
|
48
38
|
called: false
|
|
49
39
|
});
|
|
50
|
-
(0,
|
|
40
|
+
(0, _react.act)(() => {
|
|
51
41
|
mutate();
|
|
52
42
|
});
|
|
53
|
-
await waitFor(() => {
|
|
43
|
+
await (0, _react.waitFor)(() => {
|
|
54
44
|
const [, duringMutation] = result.current;
|
|
55
45
|
expect(duringMutation).toMatchObject({
|
|
56
46
|
loading: true,
|
|
57
47
|
called: true
|
|
58
48
|
});
|
|
59
49
|
});
|
|
60
|
-
await waitFor(() => {
|
|
50
|
+
await (0, _react.waitFor)(() => {
|
|
61
51
|
const [, afterMutation] = result.current;
|
|
62
52
|
expect(afterMutation).toMatchObject({
|
|
63
53
|
loading: false,
|
|
@@ -77,8 +67,7 @@ describe('useDataMutation', () => {
|
|
|
77
67
|
const data = {
|
|
78
68
|
answer: 42
|
|
79
69
|
};
|
|
80
|
-
|
|
81
|
-
const wrapper = (_ref2) => {
|
|
70
|
+
const wrapper = _ref2 => {
|
|
82
71
|
let {
|
|
83
72
|
children
|
|
84
73
|
} = _ref2;
|
|
@@ -86,11 +75,9 @@ describe('useDataMutation', () => {
|
|
|
86
75
|
data: data
|
|
87
76
|
}, children);
|
|
88
77
|
};
|
|
89
|
-
|
|
90
78
|
const {
|
|
91
|
-
result
|
|
92
|
-
|
|
93
|
-
} = (0, _reactHooks.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation, {
|
|
79
|
+
result
|
|
80
|
+
} = (0, _react.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation, {
|
|
94
81
|
lazy: false
|
|
95
82
|
}), {
|
|
96
83
|
wrapper
|
|
@@ -100,7 +87,7 @@ describe('useDataMutation', () => {
|
|
|
100
87
|
loading: true,
|
|
101
88
|
called: true
|
|
102
89
|
});
|
|
103
|
-
await waitFor(() => {
|
|
90
|
+
await (0, _react.waitFor)(() => {
|
|
104
91
|
const [, afterMutation] = result.current;
|
|
105
92
|
expect(afterMutation).toMatchObject({
|
|
106
93
|
loading: false,
|
|
@@ -121,8 +108,7 @@ describe('useDataMutation', () => {
|
|
|
121
108
|
const data = {
|
|
122
109
|
answer: 42
|
|
123
110
|
};
|
|
124
|
-
|
|
125
|
-
const wrapper = (_ref3) => {
|
|
111
|
+
const wrapper = _ref3 => {
|
|
126
112
|
let {
|
|
127
113
|
children
|
|
128
114
|
} = _ref3;
|
|
@@ -130,21 +116,19 @@ describe('useDataMutation', () => {
|
|
|
130
116
|
data: data
|
|
131
117
|
}, children);
|
|
132
118
|
};
|
|
133
|
-
|
|
134
119
|
const {
|
|
135
|
-
result
|
|
136
|
-
|
|
137
|
-
} = (0, _reactHooks.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation, {
|
|
120
|
+
result
|
|
121
|
+
} = (0, _react.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation, {
|
|
138
122
|
onComplete
|
|
139
123
|
}), {
|
|
140
124
|
wrapper
|
|
141
125
|
});
|
|
142
126
|
expect(onComplete).toHaveBeenCalledTimes(0);
|
|
143
127
|
const [mutate] = result.current;
|
|
144
|
-
(0,
|
|
128
|
+
(0, _react.act)(() => {
|
|
145
129
|
mutate();
|
|
146
130
|
});
|
|
147
|
-
await waitFor(() => {
|
|
131
|
+
await (0, _react.waitFor)(() => {
|
|
148
132
|
const [, state] = result.current;
|
|
149
133
|
expect(state).toMatchObject({
|
|
150
134
|
loading: false,
|
|
@@ -170,8 +154,7 @@ describe('useDataMutation', () => {
|
|
|
170
154
|
throw error;
|
|
171
155
|
}
|
|
172
156
|
};
|
|
173
|
-
|
|
174
|
-
const wrapper = (_ref4) => {
|
|
157
|
+
const wrapper = _ref4 => {
|
|
175
158
|
let {
|
|
176
159
|
children
|
|
177
160
|
} = _ref4;
|
|
@@ -179,21 +162,19 @@ describe('useDataMutation', () => {
|
|
|
179
162
|
data: data
|
|
180
163
|
}, children);
|
|
181
164
|
};
|
|
182
|
-
|
|
183
165
|
const {
|
|
184
|
-
result
|
|
185
|
-
|
|
186
|
-
} = (0, _reactHooks.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation, {
|
|
166
|
+
result
|
|
167
|
+
} = (0, _react.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation, {
|
|
187
168
|
onError
|
|
188
169
|
}), {
|
|
189
170
|
wrapper
|
|
190
171
|
});
|
|
191
172
|
expect(onError).toHaveBeenCalledTimes(0);
|
|
192
173
|
const [mutate] = result.current;
|
|
193
|
-
(0,
|
|
174
|
+
(0, _react.act)(() => {
|
|
194
175
|
mutate();
|
|
195
176
|
});
|
|
196
|
-
await waitFor(() => {
|
|
177
|
+
await (0, _react.waitFor)(() => {
|
|
197
178
|
const [, state] = result.current;
|
|
198
179
|
expect(state).toMatchObject({
|
|
199
180
|
loading: false,
|
|
@@ -208,7 +189,7 @@ describe('useDataMutation', () => {
|
|
|
208
189
|
const mutation = {
|
|
209
190
|
type: 'update',
|
|
210
191
|
resource: 'answer',
|
|
211
|
-
id:
|
|
192
|
+
id: _ref5 => {
|
|
212
193
|
let {
|
|
213
194
|
id
|
|
214
195
|
} = _ref5;
|
|
@@ -222,8 +203,7 @@ describe('useDataMutation', () => {
|
|
|
222
203
|
const data = {
|
|
223
204
|
answer: answerSpy
|
|
224
205
|
};
|
|
225
|
-
|
|
226
|
-
const wrapper = (_ref6) => {
|
|
206
|
+
const wrapper = _ref6 => {
|
|
227
207
|
let {
|
|
228
208
|
children
|
|
229
209
|
} = _ref6;
|
|
@@ -231,11 +211,9 @@ describe('useDataMutation', () => {
|
|
|
231
211
|
data: data
|
|
232
212
|
}, children);
|
|
233
213
|
};
|
|
234
|
-
|
|
235
214
|
const {
|
|
236
|
-
result
|
|
237
|
-
|
|
238
|
-
} = (0, _reactHooks.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation, {
|
|
215
|
+
result
|
|
216
|
+
} = (0, _react.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation, {
|
|
239
217
|
lazy: false,
|
|
240
218
|
variables: {
|
|
241
219
|
id: '1'
|
|
@@ -243,18 +221,18 @@ describe('useDataMutation', () => {
|
|
|
243
221
|
}), {
|
|
244
222
|
wrapper
|
|
245
223
|
});
|
|
246
|
-
await waitFor(() => {
|
|
224
|
+
await (0, _react.waitFor)(() => {
|
|
247
225
|
expect(answerSpy).toHaveBeenLastCalledWith(expect.any(String), expect.objectContaining({
|
|
248
226
|
id: '1'
|
|
249
227
|
}), expect.any(Object));
|
|
250
228
|
});
|
|
251
229
|
const [mutate] = result.current;
|
|
252
|
-
(0,
|
|
230
|
+
(0, _react.act)(() => {
|
|
253
231
|
mutate({
|
|
254
232
|
id: '2'
|
|
255
233
|
});
|
|
256
234
|
});
|
|
257
|
-
await waitFor(() => {
|
|
235
|
+
await (0, _react.waitFor)(() => {
|
|
258
236
|
expect(answerSpy).toHaveBeenLastCalledWith(expect.any(String), expect.objectContaining({
|
|
259
237
|
id: '2'
|
|
260
238
|
}), expect.any(Object));
|
|
@@ -268,8 +246,7 @@ describe('useDataMutation', () => {
|
|
|
268
246
|
answer: '?'
|
|
269
247
|
}
|
|
270
248
|
};
|
|
271
|
-
|
|
272
|
-
const wrapper = (_ref7) => {
|
|
249
|
+
const wrapper = _ref7 => {
|
|
273
250
|
let {
|
|
274
251
|
children
|
|
275
252
|
} = _ref7;
|
|
@@ -277,18 +254,17 @@ describe('useDataMutation', () => {
|
|
|
277
254
|
data: {}
|
|
278
255
|
}, children);
|
|
279
256
|
};
|
|
280
|
-
|
|
281
|
-
const engineHook = (0, _reactHooks.renderHook)(() => (0, _useDataEngine.useDataEngine)(), {
|
|
257
|
+
const engineHook = (0, _react.renderHook)(() => (0, _useDataEngine.useDataEngine)(), {
|
|
282
258
|
wrapper
|
|
283
259
|
});
|
|
284
|
-
const mutationHook = (0,
|
|
260
|
+
const mutationHook = (0, _react.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation), {
|
|
285
261
|
wrapper
|
|
286
262
|
});
|
|
263
|
+
|
|
287
264
|
/**
|
|
288
265
|
* Ideally we'd check referential equality here with .toBe, but since
|
|
289
266
|
* both hooks run in a different context that doesn't work.
|
|
290
267
|
*/
|
|
291
|
-
|
|
292
268
|
expect(mutationHook.result.current[1].engine).toStrictEqual(engineHook.result.current);
|
|
293
269
|
});
|
|
294
270
|
it('should return a stable mutate function', async () => {
|
|
@@ -302,8 +278,7 @@ describe('useDataMutation', () => {
|
|
|
302
278
|
const data = {
|
|
303
279
|
answer: 42
|
|
304
280
|
};
|
|
305
|
-
|
|
306
|
-
const wrapper = (_ref8) => {
|
|
281
|
+
const wrapper = _ref8 => {
|
|
307
282
|
let {
|
|
308
283
|
children
|
|
309
284
|
} = _ref8;
|
|
@@ -311,14 +286,13 @@ describe('useDataMutation', () => {
|
|
|
311
286
|
data: data
|
|
312
287
|
}, children);
|
|
313
288
|
};
|
|
314
|
-
|
|
315
289
|
const {
|
|
316
290
|
result
|
|
317
|
-
} = (0,
|
|
291
|
+
} = (0, _react.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation), {
|
|
318
292
|
wrapper
|
|
319
293
|
});
|
|
320
294
|
const [firstMutate] = result.current;
|
|
321
|
-
await (0,
|
|
295
|
+
await (0, _react.act)(async () => {
|
|
322
296
|
await firstMutate({
|
|
323
297
|
variable: 'variable'
|
|
324
298
|
});
|
|
@@ -341,8 +315,7 @@ describe('useDataMutation', () => {
|
|
|
341
315
|
const data = {
|
|
342
316
|
answer: 42
|
|
343
317
|
};
|
|
344
|
-
|
|
345
|
-
const wrapper = (_ref9) => {
|
|
318
|
+
const wrapper = _ref9 => {
|
|
346
319
|
let {
|
|
347
320
|
children
|
|
348
321
|
} = _ref9;
|
|
@@ -350,19 +323,17 @@ describe('useDataMutation', () => {
|
|
|
350
323
|
data: data
|
|
351
324
|
}, children);
|
|
352
325
|
};
|
|
353
|
-
|
|
354
326
|
const {
|
|
355
|
-
result
|
|
356
|
-
|
|
357
|
-
} = (0, _reactHooks.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation), {
|
|
327
|
+
result
|
|
328
|
+
} = (0, _react.renderHook)(() => (0, _useDataMutation.useDataMutation)(mutation), {
|
|
358
329
|
wrapper
|
|
359
330
|
});
|
|
360
331
|
let mutatePromise;
|
|
361
332
|
const [mutate] = result.current;
|
|
362
|
-
(0,
|
|
333
|
+
(0, _react.act)(() => {
|
|
363
334
|
mutatePromise = mutate();
|
|
364
335
|
});
|
|
365
|
-
await waitFor(() => {
|
|
336
|
+
await (0, _react.waitFor)(() => {
|
|
366
337
|
const [, state] = result.current;
|
|
367
338
|
expect(state).toMatchObject({
|
|
368
339
|
loading: false,
|
|
@@ -4,31 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.useDataQuery = void 0;
|
|
7
|
-
|
|
7
|
+
var _reactQuery = require("@tanstack/react-query");
|
|
8
8
|
var _react = require("react");
|
|
9
|
-
|
|
10
|
-
var _reactQuery = require("react-query");
|
|
11
|
-
|
|
12
9
|
var _mergeAndCompareVariables = require("./mergeAndCompareVariables");
|
|
13
|
-
|
|
14
10
|
var _useDataEngine = require("./useDataEngine");
|
|
15
|
-
|
|
16
11
|
var _useStaticInput = require("./useStaticInput");
|
|
17
|
-
|
|
18
|
-
const noop = () => {
|
|
19
|
-
/**
|
|
20
|
-
* Used to silence the default react-query logger. Eventually we
|
|
21
|
-
* could expose the setLogger functionality and remove the call
|
|
22
|
-
* to setLogger here.
|
|
23
|
-
*/
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
(0, _reactQuery.setLogger)({
|
|
27
|
-
log: noop,
|
|
28
|
-
warn: noop,
|
|
29
|
-
error: noop
|
|
30
|
-
});
|
|
31
|
-
|
|
32
12
|
const useDataQuery = function (query) {
|
|
33
13
|
let {
|
|
34
14
|
onComplete: userOnSuccess,
|
|
@@ -47,6 +27,7 @@ const useDataQuery = function (query) {
|
|
|
47
27
|
enabled: !initialLazy,
|
|
48
28
|
refetchCallback: undefined
|
|
49
29
|
});
|
|
30
|
+
|
|
50
31
|
/**
|
|
51
32
|
* Display current query state and refetch count in React DevTools
|
|
52
33
|
*/
|
|
@@ -56,53 +37,50 @@ const useDataQuery = function (query) {
|
|
|
56
37
|
enabled: queryState.current.enabled,
|
|
57
38
|
variables: queryState.current.variables
|
|
58
39
|
}, debugValue => JSON.stringify(debugValue));
|
|
40
|
+
|
|
59
41
|
/**
|
|
60
42
|
* User callbacks and refetch handling
|
|
61
43
|
*/
|
|
62
44
|
|
|
63
45
|
const onSuccess = data => {
|
|
64
46
|
var _queryState$current$r, _queryState$current;
|
|
65
|
-
|
|
66
47
|
(_queryState$current$r = (_queryState$current = queryState.current).refetchCallback) === null || _queryState$current$r === void 0 ? void 0 : _queryState$current$r.call(_queryState$current, data);
|
|
67
48
|
queryState.current.refetchCallback = undefined;
|
|
68
|
-
|
|
69
49
|
if (userOnSuccess) {
|
|
70
50
|
userOnSuccess(data);
|
|
71
51
|
}
|
|
72
52
|
};
|
|
73
|
-
|
|
74
53
|
const onError = error => {
|
|
75
54
|
// If we'd want to reject on errors we'd call the cb with the error here
|
|
76
55
|
queryState.current.refetchCallback = undefined;
|
|
77
|
-
|
|
78
56
|
if (userOnError) {
|
|
79
57
|
userOnError(error);
|
|
80
58
|
}
|
|
81
59
|
};
|
|
60
|
+
|
|
82
61
|
/**
|
|
83
62
|
* Setting up react-query
|
|
84
63
|
*/
|
|
85
64
|
|
|
86
|
-
|
|
87
65
|
const engine = (0, _useDataEngine.useDataEngine)();
|
|
88
66
|
const queryKey = [staticQuery, queryState.current.variables];
|
|
89
|
-
|
|
90
67
|
const queryFn = () => engine.query(staticQuery, {
|
|
91
68
|
variables: queryState.current.variables
|
|
92
69
|
});
|
|
93
|
-
|
|
94
70
|
const {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
isLoading,
|
|
71
|
+
status,
|
|
72
|
+
fetchStatus,
|
|
98
73
|
error,
|
|
99
74
|
data,
|
|
100
75
|
refetch: queryRefetch
|
|
101
|
-
} = (0, _reactQuery.useQuery)(
|
|
76
|
+
} = (0, _reactQuery.useQuery)({
|
|
77
|
+
queryKey,
|
|
78
|
+
queryFn,
|
|
102
79
|
enabled: queryState.current.enabled,
|
|
103
80
|
onSuccess,
|
|
104
81
|
onError
|
|
105
82
|
});
|
|
83
|
+
|
|
106
84
|
/**
|
|
107
85
|
* Refetch allows a user to update the variables or just
|
|
108
86
|
* trigger a refetch of the query with the current variables.
|
|
@@ -118,52 +96,56 @@ const useDataQuery = function (query) {
|
|
|
118
96
|
mergedVariables,
|
|
119
97
|
mergedVariablesHash
|
|
120
98
|
} = (0, _mergeAndCompareVariables.mergeAndCompareVariables)(queryState.current.variables, newVariables, queryState.current.variablesHash);
|
|
99
|
+
|
|
121
100
|
/**
|
|
122
101
|
* If there are no updates that will trigger an automatic refetch
|
|
123
102
|
* we'll need to call react-query's refetch directly
|
|
124
103
|
*/
|
|
125
|
-
|
|
126
104
|
if (queryState.current.enabled && identical) {
|
|
127
105
|
return queryRefetch({
|
|
128
|
-
cancelRefetch: true,
|
|
129
106
|
throwOnError: false
|
|
130
|
-
}).then(
|
|
107
|
+
}).then(_ref => {
|
|
131
108
|
let {
|
|
132
109
|
data
|
|
133
110
|
} = _ref;
|
|
134
111
|
return data;
|
|
135
112
|
});
|
|
136
113
|
}
|
|
137
|
-
|
|
138
114
|
queryState.current.variables = mergedVariables;
|
|
139
115
|
queryState.current.variablesHash = mergedVariablesHash;
|
|
140
|
-
queryState.current.enabled = true;
|
|
116
|
+
queryState.current.enabled = true;
|
|
141
117
|
|
|
118
|
+
// This promise does not currently reject on errors
|
|
142
119
|
const refetchPromise = new Promise(resolve => {
|
|
143
120
|
queryState.current.refetchCallback = data => {
|
|
144
121
|
resolve(data);
|
|
145
122
|
};
|
|
146
|
-
});
|
|
123
|
+
});
|
|
147
124
|
|
|
125
|
+
// Trigger a react-query refetch by incrementing variablesUpdateCount state
|
|
148
126
|
setVariablesUpdateCount(prevCount => prevCount + 1);
|
|
149
127
|
return refetchPromise;
|
|
150
128
|
}, [queryRefetch]);
|
|
129
|
+
|
|
151
130
|
/**
|
|
152
131
|
* react-query returns null or an error, but we return undefined
|
|
153
132
|
* or an error, so this ensures consistency with the other types.
|
|
154
133
|
*/
|
|
155
|
-
|
|
156
134
|
const ourError = error || undefined;
|
|
157
135
|
return {
|
|
158
136
|
engine,
|
|
159
|
-
// A query
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
fetching
|
|
137
|
+
// A query has not been called if it is lazy (fetchStatus = 'idle') and no initial data is available (status = 'loading').
|
|
138
|
+
// https://tanstack.com/query/v4/docs/framework/react/guides/queries
|
|
139
|
+
called: !(status === 'loading' && fetchStatus === 'idle'),
|
|
140
|
+
// 'loading' should only be true when actively fetching (fetchStatus = 'fetching') while there is no data yet (status = 'loading').
|
|
141
|
+
// If there is already data for the query, then 'loading' will not become 'true' when refetching, so the previous data can still be
|
|
142
|
+
// displayed while new data is fetched in the background
|
|
143
|
+
loading: fetchStatus === 'fetching' && status === 'loading',
|
|
144
|
+
// 'fetching' reflects the fetching behavior behind the scenes
|
|
145
|
+
fetching: fetchStatus === 'fetching',
|
|
163
146
|
error: ourError,
|
|
164
147
|
data,
|
|
165
148
|
refetch
|
|
166
149
|
};
|
|
167
150
|
};
|
|
168
|
-
|
|
169
151
|
exports.useDataQuery = useDataQuery;
|