@jobber/hooks 2.17.1 → 2.17.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/dist/index.js +18 -34
- package/dist/useBool/index.js +1 -5
- package/dist/useBool/useBool.js +6 -9
- package/dist/useBool/useBool.test.js +9 -11
- package/dist/useBreakpoints/index.js +2 -20
- package/dist/useBreakpoints/mockViewportWidth/index.js +1 -17
- package/dist/useBreakpoints/mockViewportWidth/mockViewportWidth.js +2 -6
- package/dist/useBreakpoints/mockViewportWidth/mockViewportWidth.test.js +4 -6
- package/dist/useBreakpoints/useBreakpoints.js +9 -13
- package/dist/useBreakpoints/useBreakpoints.test.js +30 -35
- package/dist/useBreakpoints/useMediaQuery.js +5 -9
- package/dist/useCallbackRef/index.js +1 -5
- package/dist/useCallbackRef/useCallbackRef.js +5 -8
- package/dist/useCallbackRef/useCallbackRef.test.js +4 -6
- package/dist/useCollectionQuery/index.js +1 -5
- package/dist/useCollectionQuery/mdxUtils.js +8 -12
- package/dist/useCollectionQuery/test-utilities/index.js +3 -19
- package/dist/useCollectionQuery/test-utilities/mocks.js +19 -30
- package/dist/useCollectionQuery/test-utilities/queries.js +4 -7
- package/dist/useCollectionQuery/test-utilities/utils.js +1 -4
- package/dist/useCollectionQuery/uniqueEdges.js +2 -6
- package/dist/useCollectionQuery/uniqueNodes.js +1 -4
- package/dist/useCollectionQuery/useCollectionQuery.js +27 -34
- package/dist/useCollectionQuery/useCollectionQuery.test.js +126 -128
- package/dist/useDebounce/index.js +1 -5
- package/dist/useDebounce/useDebounce.js +8 -11
- package/dist/useDebounce/useDebounce.test.js +42 -70
- package/dist/useFocusTrap/index.js +1 -5
- package/dist/useFocusTrap/useFocusTrap.js +4 -7
- package/dist/useFocusTrap/useFocusTrap.test.js +26 -31
- package/dist/useFormState/index.js +1 -5
- package/dist/useFormState/useFormState.js +3 -6
- package/dist/useInView/index.js +1 -17
- package/dist/useInView/useInView.js +6 -9
- package/dist/useInView/useInView.test.js +12 -17
- package/dist/useIsMounted/index.js +1 -5
- package/dist/useIsMounted/useIsMounted.js +5 -8
- package/dist/useIsMounted/useIsMounted.test.js +4 -6
- package/dist/useLiveAnnounce/index.js +1 -5
- package/dist/useLiveAnnounce/useLiveAnnounce.js +4 -7
- package/dist/useLiveAnnounce/useLiveAnnounce.test.js +19 -24
- package/dist/useOnKeyDown/index.js +1 -5
- package/dist/useOnKeyDown/useOnKeyDown.js +3 -6
- package/dist/useOnKeyDown/useOnKeyDown.test.js +7 -12
- package/dist/useOnMount/index.js +1 -5
- package/dist/useOnMount/useOnMount.js +6 -10
- package/dist/useOnMount/useOnMount.test.js +4 -6
- package/dist/useRefocusOnActivator/index.js +1 -5
- package/dist/useRefocusOnActivator/useRefocusOnActivator.js +3 -6
- package/dist/useResizeObserver/index.js +1 -17
- package/dist/useResizeObserver/useResizeObserver.js +9 -16
- package/dist/useSafeLayoutEffect/index.js +1 -5
- package/dist/useSafeLayoutEffect/useSafeLayoutEffect.js +4 -7
- package/dist/useShowClear/index.js +1 -5
- package/dist/useShowClear/useShowClear.js +1 -4
- package/dist/useShowClear/useShowClear.test.js +6 -8
- package/dist/useStepper/index.js +1 -5
- package/dist/useStepper/useStepper.js +9 -15
- package/dist/useStepper/useStepper.test.js +15 -17
- package/dist/useWindowDimensions/index.js +1 -5
- package/dist/useWindowDimensions/useWIndowDimensions.test.js +6 -8
- package/dist/useWindowDimensions/useWindowDimensions.js +4 -7
- package/package.json +102 -5
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,18 +7,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
8
|
});
|
|
10
9
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const test_utilities_1 = require("./test-utilities");
|
|
10
|
+
import { act, renderHook } from "@testing-library/react-hooks";
|
|
11
|
+
import { waitFor } from "@testing-library/react";
|
|
12
|
+
import { useCollectionQuery } from "./useCollectionQuery";
|
|
13
|
+
import { LIST_QUERY, LIST_QUERY_WITH_TOTAL_COUNT, SUBSCRIPTION_QUERY, buildListRequestMock, buildListRequestMockForNextPage, buildSubscriptionRequestMock, listQueryResponseMock, listQueryWithTotalCountResponseMock, setListQueryMockHasNextPage, subscriptionQueryMock, wait, wrapper, } from "./test-utilities";
|
|
16
14
|
beforeEach(() => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
setListQueryMockHasNextPage(true);
|
|
16
|
+
listQueryResponseMock.mockClear();
|
|
17
|
+
subscriptionQueryMock.mockClear();
|
|
20
18
|
});
|
|
21
19
|
function useCollectionQueryHook(query) {
|
|
22
|
-
return
|
|
20
|
+
return useCollectionQuery({
|
|
23
21
|
query: query,
|
|
24
22
|
getCollectionByPath(data) {
|
|
25
23
|
var _a;
|
|
@@ -28,14 +26,14 @@ function useCollectionQueryHook(query) {
|
|
|
28
26
|
});
|
|
29
27
|
}
|
|
30
28
|
function useCollectionQueryHookWithSubscription(query) {
|
|
31
|
-
return
|
|
29
|
+
return useCollectionQuery({
|
|
32
30
|
query: query,
|
|
33
31
|
getCollectionByPath(data) {
|
|
34
32
|
var _a;
|
|
35
33
|
return (_a = data === null || data === void 0 ? void 0 : data.conversation) === null || _a === void 0 ? void 0 : _a.smsMessages;
|
|
36
34
|
},
|
|
37
35
|
subscription: {
|
|
38
|
-
document:
|
|
36
|
+
document: SUBSCRIPTION_QUERY,
|
|
39
37
|
getNodeByPath(conversationData) {
|
|
40
38
|
var _a;
|
|
41
39
|
return (_a = conversationData === null || conversationData === void 0 ? void 0 : conversationData.conversationMessage) === null || _a === void 0 ? void 0 : _a.smsMessage;
|
|
@@ -44,7 +42,7 @@ function useCollectionQueryHookWithSubscription(query) {
|
|
|
44
42
|
});
|
|
45
43
|
}
|
|
46
44
|
function useCollectionQueryHookWithSubscriptionAndSearch(query, searchTerm) {
|
|
47
|
-
return
|
|
45
|
+
return useCollectionQuery({
|
|
48
46
|
query: query,
|
|
49
47
|
queryOptions: {
|
|
50
48
|
variables: {
|
|
@@ -56,7 +54,7 @@ function useCollectionQueryHookWithSubscriptionAndSearch(query, searchTerm) {
|
|
|
56
54
|
return (_a = data === null || data === void 0 ? void 0 : data.conversation) === null || _a === void 0 ? void 0 : _a.smsMessages;
|
|
57
55
|
},
|
|
58
56
|
subscription: {
|
|
59
|
-
document:
|
|
57
|
+
document: SUBSCRIPTION_QUERY,
|
|
60
58
|
getNodeByPath(conversationData) {
|
|
61
59
|
var _a;
|
|
62
60
|
return (_a = conversationData === null || conversationData === void 0 ? void 0 : conversationData.conversationMessage) === null || _a === void 0 ? void 0 : _a.smsMessage;
|
|
@@ -67,77 +65,77 @@ function useCollectionQueryHookWithSubscriptionAndSearch(query, searchTerm) {
|
|
|
67
65
|
describe("useCollectionQuery", () => {
|
|
68
66
|
describe.each `
|
|
69
67
|
testCase | query | responseMock
|
|
70
|
-
${"excludes totalCount"} | ${
|
|
71
|
-
${"includes totalCount"} | ${
|
|
68
|
+
${"excludes totalCount"} | ${LIST_QUERY} | ${listQueryResponseMock}
|
|
69
|
+
${"includes totalCount"} | ${LIST_QUERY_WITH_TOTAL_COUNT} | ${listQueryWithTotalCountResponseMock}
|
|
72
70
|
`("when the query run $testCase", ({ query, responseMock }) => {
|
|
73
71
|
describe("when useCollectionQuery is first called", () => {
|
|
74
72
|
describe("when nextPage is called while it's still loading initial content", () => {
|
|
75
73
|
it("should not trigger a the next page to be fetched", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
76
|
-
const { result } =
|
|
77
|
-
wrapper:
|
|
78
|
-
|
|
79
|
-
|
|
74
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
75
|
+
wrapper: wrapper([
|
|
76
|
+
buildListRequestMock(query, responseMock),
|
|
77
|
+
buildListRequestMockForNextPage(query, responseMock),
|
|
80
78
|
]),
|
|
81
79
|
});
|
|
82
|
-
|
|
80
|
+
act(() => {
|
|
83
81
|
result.current.nextPage();
|
|
84
82
|
});
|
|
85
|
-
yield
|
|
83
|
+
yield act(wait);
|
|
86
84
|
expect(responseMock).toHaveBeenCalledTimes(1);
|
|
87
85
|
}));
|
|
88
86
|
});
|
|
89
87
|
describe("when refresh is called while it's still loading initial content", () => {
|
|
90
88
|
it("should not trigger a refresh", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
91
|
-
const { result } =
|
|
92
|
-
wrapper:
|
|
93
|
-
|
|
94
|
-
|
|
89
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
90
|
+
wrapper: wrapper([
|
|
91
|
+
buildListRequestMock(query, responseMock),
|
|
92
|
+
buildListRequestMock(query, responseMock),
|
|
95
93
|
]),
|
|
96
94
|
});
|
|
97
|
-
|
|
95
|
+
act(() => {
|
|
98
96
|
result.current.refresh();
|
|
99
97
|
});
|
|
100
|
-
yield
|
|
98
|
+
yield act(wait);
|
|
101
99
|
expect(responseMock).toHaveBeenCalledTimes(1);
|
|
102
100
|
}));
|
|
103
101
|
});
|
|
104
102
|
describe("when there is no error", () => {
|
|
105
103
|
it("should update data", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
106
104
|
var _a, _b, _c, _d;
|
|
107
|
-
const { result } =
|
|
108
|
-
wrapper:
|
|
105
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
106
|
+
wrapper: wrapper([buildListRequestMock(query, responseMock)]),
|
|
109
107
|
});
|
|
110
|
-
yield
|
|
108
|
+
yield act(wait);
|
|
111
109
|
expect((_d = (_c = (_b = (_a = result.current.data) === null || _a === void 0 ? void 0 : _a.conversation) === null || _b === void 0 ? void 0 : _b.smsMessages) === null || _c === void 0 ? void 0 : _c.edges) === null || _d === void 0 ? void 0 : _d.length).toBe(1);
|
|
112
110
|
}));
|
|
113
111
|
it("should set initialLoading while loading data", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
114
|
-
const { result } =
|
|
115
|
-
wrapper:
|
|
112
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
113
|
+
wrapper: wrapper([buildListRequestMock(query, responseMock)]),
|
|
116
114
|
});
|
|
117
115
|
expect(result.current.loadingInitialContent).toBe(true);
|
|
118
|
-
yield
|
|
116
|
+
yield act(wait);
|
|
119
117
|
}));
|
|
120
118
|
});
|
|
121
119
|
});
|
|
122
120
|
describe("#nextPage", () => {
|
|
123
121
|
describe("when nextPage is called while it's still loadingNextPage", () => {
|
|
124
122
|
it("should not trigger a nextPage", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
125
|
-
const { result } =
|
|
126
|
-
wrapper:
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
123
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
124
|
+
wrapper: wrapper([
|
|
125
|
+
buildListRequestMock(query, responseMock),
|
|
126
|
+
buildListRequestMockForNextPage(query, responseMock),
|
|
127
|
+
buildListRequestMockForNextPage(query, responseMock),
|
|
130
128
|
]),
|
|
131
129
|
});
|
|
132
|
-
yield
|
|
133
|
-
|
|
130
|
+
yield act(wait);
|
|
131
|
+
act(() => {
|
|
134
132
|
result.current.nextPage();
|
|
135
133
|
});
|
|
136
|
-
|
|
134
|
+
act(() => {
|
|
137
135
|
result.current.nextPage();
|
|
138
136
|
});
|
|
139
|
-
yield
|
|
140
|
-
yield
|
|
137
|
+
yield act(wait);
|
|
138
|
+
yield waitFor(() => {
|
|
141
139
|
var _a, _b, _c;
|
|
142
140
|
expect((_c = (_b = (_a = result.current.data) === null || _a === void 0 ? void 0 : _a.conversation) === null || _b === void 0 ? void 0 : _b.smsMessages) === null || _c === void 0 ? void 0 : _c.edges).toHaveLength(2);
|
|
143
141
|
});
|
|
@@ -145,80 +143,80 @@ describe("useCollectionQuery", () => {
|
|
|
145
143
|
});
|
|
146
144
|
describe("when refresh is called while it's still loadingNextPage", () => {
|
|
147
145
|
it("should trigger a refresh", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
148
|
-
const { result } =
|
|
149
|
-
wrapper:
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
146
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
147
|
+
wrapper: wrapper([
|
|
148
|
+
buildListRequestMock(query, responseMock),
|
|
149
|
+
buildListRequestMock(query, responseMock),
|
|
150
|
+
buildListRequestMockForNextPage(query, responseMock),
|
|
153
151
|
]),
|
|
154
152
|
});
|
|
155
|
-
yield
|
|
156
|
-
|
|
153
|
+
yield act(wait);
|
|
154
|
+
act(() => {
|
|
157
155
|
result.current.nextPage();
|
|
158
156
|
});
|
|
159
|
-
|
|
157
|
+
act(() => {
|
|
160
158
|
result.current.refresh();
|
|
161
159
|
});
|
|
162
160
|
expect(result.current.loadingRefresh).toBe(true);
|
|
163
|
-
yield
|
|
161
|
+
yield act(wait);
|
|
164
162
|
}));
|
|
165
163
|
});
|
|
166
164
|
describe("when there is no more data to fetch", () => {
|
|
167
165
|
it("should not fetch more data", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
168
|
-
|
|
169
|
-
const { result } =
|
|
170
|
-
wrapper:
|
|
171
|
-
|
|
172
|
-
|
|
166
|
+
setListQueryMockHasNextPage(false);
|
|
167
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
168
|
+
wrapper: wrapper([
|
|
169
|
+
buildListRequestMock(query, responseMock),
|
|
170
|
+
buildListRequestMockForNextPage(query, responseMock),
|
|
173
171
|
]),
|
|
174
172
|
});
|
|
175
|
-
yield
|
|
176
|
-
|
|
173
|
+
yield act(wait);
|
|
174
|
+
act(() => {
|
|
177
175
|
result.current.nextPage();
|
|
178
176
|
});
|
|
179
|
-
yield
|
|
177
|
+
yield act(wait);
|
|
180
178
|
expect(responseMock).toHaveBeenCalledTimes(1);
|
|
181
179
|
}));
|
|
182
180
|
});
|
|
183
181
|
describe("when there is no error", () => {
|
|
184
182
|
it("should update data", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
185
|
-
const { result } =
|
|
186
|
-
wrapper:
|
|
187
|
-
|
|
188
|
-
|
|
183
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
184
|
+
wrapper: wrapper([
|
|
185
|
+
buildListRequestMock(query, responseMock),
|
|
186
|
+
buildListRequestMockForNextPage(query, responseMock),
|
|
189
187
|
]),
|
|
190
188
|
});
|
|
191
|
-
yield
|
|
192
|
-
|
|
189
|
+
yield act(wait);
|
|
190
|
+
act(() => {
|
|
193
191
|
result.current.nextPage();
|
|
194
192
|
});
|
|
195
|
-
yield
|
|
196
|
-
yield
|
|
193
|
+
yield act(wait);
|
|
194
|
+
yield waitFor(() => {
|
|
197
195
|
var _a, _b, _c;
|
|
198
196
|
expect((_c = (_b = (_a = result.current.data) === null || _a === void 0 ? void 0 : _a.conversation) === null || _b === void 0 ? void 0 : _b.smsMessages) === null || _c === void 0 ? void 0 : _c.edges).toHaveLength(2);
|
|
199
197
|
});
|
|
200
198
|
}));
|
|
201
199
|
it("should set loadingNextPage while loading data", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
202
|
-
const { result } =
|
|
203
|
-
wrapper:
|
|
204
|
-
|
|
205
|
-
|
|
200
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
201
|
+
wrapper: wrapper([
|
|
202
|
+
buildListRequestMock(query, responseMock),
|
|
203
|
+
buildListRequestMockForNextPage(query, responseMock),
|
|
206
204
|
]),
|
|
207
205
|
});
|
|
208
|
-
yield
|
|
209
|
-
|
|
206
|
+
yield act(wait);
|
|
207
|
+
act(() => {
|
|
210
208
|
result.current.nextPage();
|
|
211
209
|
});
|
|
212
210
|
expect(result.current.loadingNextPage).toBe(true);
|
|
213
|
-
yield
|
|
211
|
+
yield act(wait);
|
|
214
212
|
}));
|
|
215
213
|
});
|
|
216
214
|
describe("when there is an error", () => {
|
|
217
215
|
it("should update the error state", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
218
216
|
const mockError = new Error("Failed to fetch more items");
|
|
219
|
-
const { result } =
|
|
220
|
-
wrapper:
|
|
221
|
-
|
|
217
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
218
|
+
wrapper: wrapper([
|
|
219
|
+
buildListRequestMock(query, responseMock),
|
|
222
220
|
{
|
|
223
221
|
request: {
|
|
224
222
|
query: query,
|
|
@@ -226,20 +224,20 @@ describe("useCollectionQuery", () => {
|
|
|
226
224
|
},
|
|
227
225
|
error: mockError,
|
|
228
226
|
},
|
|
229
|
-
|
|
227
|
+
buildListRequestMockForNextPage(query, responseMock),
|
|
230
228
|
]),
|
|
231
229
|
});
|
|
232
|
-
yield
|
|
233
|
-
|
|
230
|
+
yield act(wait);
|
|
231
|
+
act(() => {
|
|
234
232
|
result.current.nextPage();
|
|
235
233
|
});
|
|
236
|
-
yield
|
|
234
|
+
yield act(wait);
|
|
237
235
|
expect(result.current.error).toEqual(mockError);
|
|
238
236
|
// should clear the error after a successful fetch
|
|
239
|
-
|
|
237
|
+
act(() => {
|
|
240
238
|
result.current.nextPage();
|
|
241
239
|
});
|
|
242
|
-
yield
|
|
240
|
+
yield act(wait);
|
|
243
241
|
expect(result.current.error).toBeUndefined();
|
|
244
242
|
}));
|
|
245
243
|
});
|
|
@@ -247,58 +245,58 @@ describe("useCollectionQuery", () => {
|
|
|
247
245
|
describe("#refresh", () => {
|
|
248
246
|
describe("when refresh is called while it's still loadingRefresh", () => {
|
|
249
247
|
it("should not trigger a refresh", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
250
|
-
const { result } =
|
|
251
|
-
wrapper:
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
248
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
249
|
+
wrapper: wrapper([
|
|
250
|
+
buildListRequestMock(query, responseMock),
|
|
251
|
+
buildListRequestMock(query, responseMock),
|
|
252
|
+
buildListRequestMock(query, responseMock),
|
|
255
253
|
]),
|
|
256
254
|
});
|
|
257
|
-
yield
|
|
258
|
-
|
|
255
|
+
yield act(wait);
|
|
256
|
+
act(() => {
|
|
259
257
|
result.current.refresh();
|
|
260
258
|
});
|
|
261
|
-
|
|
259
|
+
act(() => {
|
|
262
260
|
result.current.refresh();
|
|
263
261
|
});
|
|
264
|
-
yield
|
|
262
|
+
yield act(wait);
|
|
265
263
|
expect(responseMock).toHaveBeenCalledTimes(2);
|
|
266
264
|
}));
|
|
267
265
|
});
|
|
268
266
|
describe("when nextPage is called while it's still loadingRefresh", () => {
|
|
269
267
|
it("should not trigger a nextPage", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
270
|
-
const { result } =
|
|
271
|
-
wrapper:
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
268
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
269
|
+
wrapper: wrapper([
|
|
270
|
+
buildListRequestMock(query, responseMock),
|
|
271
|
+
buildListRequestMock(query, responseMock),
|
|
272
|
+
buildListRequestMockForNextPage(query, responseMock),
|
|
275
273
|
]),
|
|
276
274
|
});
|
|
277
|
-
yield
|
|
278
|
-
|
|
275
|
+
yield act(wait);
|
|
276
|
+
act(() => {
|
|
279
277
|
result.current.refresh();
|
|
280
278
|
});
|
|
281
|
-
|
|
279
|
+
act(() => {
|
|
282
280
|
result.current.nextPage();
|
|
283
281
|
});
|
|
284
282
|
expect(result.current.loadingNextPage).toBe(false);
|
|
285
|
-
yield
|
|
283
|
+
yield act(wait);
|
|
286
284
|
}));
|
|
287
285
|
});
|
|
288
286
|
describe("when there is no error", () => {
|
|
289
287
|
it("should set loadingRefresh while loading data", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
290
|
-
const { result } =
|
|
291
|
-
wrapper:
|
|
292
|
-
|
|
293
|
-
|
|
288
|
+
const { result } = renderHook(() => useCollectionQueryHook(query), {
|
|
289
|
+
wrapper: wrapper([
|
|
290
|
+
buildListRequestMock(query, responseMock),
|
|
291
|
+
buildListRequestMock(query, responseMock),
|
|
294
292
|
]),
|
|
295
293
|
});
|
|
296
|
-
yield
|
|
297
|
-
|
|
294
|
+
yield act(wait);
|
|
295
|
+
act(() => {
|
|
298
296
|
result.current.refresh();
|
|
299
297
|
});
|
|
300
298
|
expect(result.current.loadingRefresh).toBe(true);
|
|
301
|
-
yield
|
|
299
|
+
yield act(wait);
|
|
302
300
|
}));
|
|
303
301
|
});
|
|
304
302
|
});
|
|
@@ -306,32 +304,32 @@ describe("useCollectionQuery", () => {
|
|
|
306
304
|
describe("when hook receives update from item not in collection", () => {
|
|
307
305
|
it("should add new item to collection", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
308
306
|
var _a, _b, _c, _d, _e;
|
|
309
|
-
const { result, waitForNextUpdate } =
|
|
310
|
-
wrapper:
|
|
311
|
-
|
|
312
|
-
|
|
307
|
+
const { result, waitForNextUpdate } = renderHook(() => useCollectionQueryHookWithSubscription(query), {
|
|
308
|
+
wrapper: wrapper([
|
|
309
|
+
buildListRequestMock(query, responseMock, "1"),
|
|
310
|
+
buildSubscriptionRequestMock("2"),
|
|
313
311
|
]),
|
|
314
312
|
});
|
|
315
313
|
// Wait for initial load
|
|
316
|
-
yield
|
|
314
|
+
yield act(waitForNextUpdate);
|
|
317
315
|
// Wait for subscription
|
|
318
|
-
yield
|
|
316
|
+
yield act(waitForNextUpdate);
|
|
319
317
|
expect((_e = (_d = (_c = (_b = (_a = result === null || result === void 0 ? void 0 : result.current) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.conversation) === null || _c === void 0 ? void 0 : _c.smsMessages) === null || _d === void 0 ? void 0 : _d.edges) === null || _e === void 0 ? void 0 : _e.length).toBe(2);
|
|
320
318
|
}));
|
|
321
319
|
});
|
|
322
320
|
describe("when hook receives update from item already in collection", () => {
|
|
323
321
|
it("should return the existing collection", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
324
322
|
var _a, _b, _c, _d, _e;
|
|
325
|
-
const { result, waitForNextUpdate } =
|
|
326
|
-
wrapper:
|
|
327
|
-
|
|
328
|
-
|
|
323
|
+
const { result, waitForNextUpdate } = renderHook(() => useCollectionQueryHookWithSubscription(query), {
|
|
324
|
+
wrapper: wrapper([
|
|
325
|
+
buildListRequestMock(query, responseMock, "1"),
|
|
326
|
+
buildSubscriptionRequestMock("1"),
|
|
329
327
|
]),
|
|
330
328
|
});
|
|
331
329
|
// Wait for initial load
|
|
332
|
-
yield
|
|
330
|
+
yield act(waitForNextUpdate);
|
|
333
331
|
// Wait for subscription
|
|
334
|
-
yield
|
|
332
|
+
yield act(() => wait(200));
|
|
335
333
|
expect((_e = (_d = (_c = (_b = (_a = result === null || result === void 0 ? void 0 : result.current) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.conversation) === null || _c === void 0 ? void 0 : _c.smsMessages) === null || _d === void 0 ? void 0 : _d.edges) === null || _e === void 0 ? void 0 : _e.length).toBe(1);
|
|
336
334
|
}));
|
|
337
335
|
});
|
|
@@ -339,16 +337,16 @@ describe("useCollectionQuery", () => {
|
|
|
339
337
|
it("should return the existing collection without adding the subscribed content", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
340
338
|
var _a, _b, _c, _d, _e;
|
|
341
339
|
const searchTerm = "FooBar";
|
|
342
|
-
const { result, waitForNextUpdate } =
|
|
343
|
-
wrapper:
|
|
344
|
-
|
|
345
|
-
|
|
340
|
+
const { result, waitForNextUpdate } = renderHook(() => useCollectionQueryHookWithSubscriptionAndSearch(query, searchTerm), {
|
|
341
|
+
wrapper: wrapper([
|
|
342
|
+
buildListRequestMock(query, responseMock, "1", searchTerm),
|
|
343
|
+
buildSubscriptionRequestMock("1"),
|
|
346
344
|
]),
|
|
347
345
|
});
|
|
348
346
|
// Wait for initial load
|
|
349
|
-
yield
|
|
347
|
+
yield act(waitForNextUpdate);
|
|
350
348
|
// Wait for subscription
|
|
351
|
-
yield
|
|
349
|
+
yield act(() => wait(200));
|
|
352
350
|
expect((_e = (_d = (_c = (_b = (_a = result === null || result === void 0 ? void 0 : result.current) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.conversation) === null || _c === void 0 ? void 0 : _c.smsMessages) === null || _d === void 0 ? void 0 : _d.edges) === null || _e === void 0 ? void 0 : _e.length).toBe(1);
|
|
353
351
|
}));
|
|
354
352
|
});
|
|
@@ -1,5 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useDebounce = void 0;
|
|
4
|
-
var useDebounce_1 = require("./useDebounce");
|
|
5
|
-
Object.defineProperty(exports, "useDebounce", { enumerable: true, get: function () { return useDebounce_1.useDebounce; } });
|
|
1
|
+
export { useDebounce } from "./useDebounce";
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
const es_toolkit_1 = require("es-toolkit");
|
|
6
|
-
const useCallbackRef_1 = require("../useCallbackRef");
|
|
1
|
+
import { useEffect, useMemo } from "react";
|
|
2
|
+
import { debounce } from "es-toolkit";
|
|
3
|
+
import { useCallbackRef } from "../useCallbackRef";
|
|
7
4
|
/**
|
|
8
5
|
* A hook to easily manage debounced functions, including their cleanup.
|
|
9
6
|
* @param func The function to debounce.
|
|
@@ -11,17 +8,17 @@ const useCallbackRef_1 = require("../useCallbackRef");
|
|
|
11
8
|
* @param options Optional debounce settings.
|
|
12
9
|
* @returns The debounced function.
|
|
13
10
|
*/
|
|
14
|
-
function useDebounce(func, wait, options) {
|
|
15
|
-
const funcRef =
|
|
11
|
+
export function useDebounce(func, wait, options) {
|
|
12
|
+
const funcRef = useCallbackRef(func);
|
|
16
13
|
// Note: do not add additional dependencies! There is currently no use case where we would change
|
|
17
14
|
// the wait delay or options between renders. By not tracking as dependencies, this allows
|
|
18
15
|
// consumers of useDebounce to provide a raw object for options rather than forcing them to
|
|
19
16
|
// memoize that param. Same with the func they provide, we're using a ref to keep that up
|
|
20
17
|
// to date and to avoid forcing the consumer to memoize it.
|
|
21
|
-
const debounced =
|
|
22
|
-
return
|
|
18
|
+
const debounced = useMemo(() => {
|
|
19
|
+
return debounce((...args) => funcRef(...args), wait, options);
|
|
23
20
|
}, [funcRef]);
|
|
24
|
-
|
|
21
|
+
useEffect(() => {
|
|
25
22
|
// If the debounced function is recreated (or unmounted), cancel the last call.
|
|
26
23
|
return () => debounced.cancel();
|
|
27
24
|
}, [debounced]);
|