@jobber/hooks 2.17.4 → 2.18.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 (148) hide show
  1. package/dist/index.cjs +49 -0
  2. package/dist/index.mjs +23 -0
  3. package/dist/isObjectLike-cjs.js +228 -0
  4. package/dist/isObjectLike-es.js +219 -0
  5. package/dist/useBool/index.cjs +8 -0
  6. package/dist/useBool/index.mjs +2 -0
  7. package/dist/useBool-cjs.js +13 -0
  8. package/dist/useBool-es.js +11 -0
  9. package/dist/useBreakpoints/index.cjs +11 -0
  10. package/dist/useBreakpoints/index.mjs +3 -0
  11. package/dist/useBreakpoints/mockViewportWidth/{mockViewportWidth.js → index.cjs} +5 -4
  12. package/dist/useBreakpoints/mockViewportWidth/index.mjs +66 -0
  13. package/dist/useBreakpoints-cjs.js +48 -0
  14. package/dist/useBreakpoints-es.js +45 -0
  15. package/dist/useCallbackRef/index.cjs +8 -0
  16. package/dist/useCallbackRef/index.mjs +2 -0
  17. package/dist/useCallbackRef-cjs.js +17 -0
  18. package/dist/useCallbackRef-es.js +15 -0
  19. package/dist/useCollectionQuery/index.cjs +13 -0
  20. package/dist/useCollectionQuery/index.mjs +7 -0
  21. package/dist/useCollectionQuery/useCollectionQuery.d.ts +1 -1
  22. package/dist/useCollectionQuery-cjs.js +2842 -0
  23. package/dist/useCollectionQuery-es.js +2821 -0
  24. package/dist/useDebounce/index.cjs +9 -0
  25. package/dist/useDebounce/index.mjs +3 -0
  26. package/dist/useDebounce-cjs.js +92 -0
  27. package/dist/useDebounce-es.js +90 -0
  28. package/dist/useFocusTrap/index.cjs +8 -0
  29. package/dist/useFocusTrap/index.mjs +2 -0
  30. package/dist/{useFocusTrap/useFocusTrap.js → useFocusTrap-cjs.js} +8 -6
  31. package/dist/useFocusTrap-es.js +68 -0
  32. package/dist/useFormState/index.cjs +8 -0
  33. package/dist/useFormState/index.mjs +2 -0
  34. package/dist/useFormState-cjs.js +13 -0
  35. package/dist/useFormState-es.js +11 -0
  36. package/dist/useInView/index.cjs +8 -0
  37. package/dist/useInView/index.mjs +2 -0
  38. package/dist/{useInView/useInView.js → useInView-cjs.js} +10 -8
  39. package/dist/useInView-es.js +19 -0
  40. package/dist/useIsMounted/index.cjs +9 -0
  41. package/dist/useIsMounted/index.mjs +3 -0
  42. package/dist/{useIsMounted/useIsMounted.js → useIsMounted-cjs.js} +9 -7
  43. package/dist/useIsMounted-es.js +34 -0
  44. package/dist/useLiveAnnounce/index.cjs +8 -0
  45. package/dist/useLiveAnnounce/index.mjs +2 -0
  46. package/dist/{useLiveAnnounce/useLiveAnnounce.js → useLiveAnnounce-cjs.js} +8 -6
  47. package/dist/useLiveAnnounce-es.js +41 -0
  48. package/dist/useOnKeyDown/index.cjs +8 -0
  49. package/dist/useOnKeyDown/index.mjs +2 -0
  50. package/dist/{useOnKeyDown/useOnKeyDown.js → useOnKeyDown-cjs.js} +7 -5
  51. package/dist/useOnKeyDown-es.js +34 -0
  52. package/dist/useOnMount/index.cjs +9 -0
  53. package/dist/useOnMount/index.mjs +3 -0
  54. package/dist/{useOnMount/useOnMount.js → useOnMount-cjs.js} +9 -8
  55. package/dist/useOnMount-es.js +19 -0
  56. package/dist/useRefocusOnActivator/index.cjs +8 -0
  57. package/dist/useRefocusOnActivator/index.mjs +2 -0
  58. package/dist/{useRefocusOnActivator/useRefocusOnActivator.js → useRefocusOnActivator-cjs.js} +7 -5
  59. package/dist/useRefocusOnActivator-es.js +26 -0
  60. package/dist/useResizeObserver/index.cjs +10 -0
  61. package/dist/useResizeObserver/index.mjs +3 -0
  62. package/dist/useResizeObserver-cjs.js +1519 -0
  63. package/dist/useResizeObserver-es.js +1516 -0
  64. package/dist/useSafeLayoutEffect/index.cjs +8 -0
  65. package/dist/useSafeLayoutEffect/index.mjs +2 -0
  66. package/dist/useSafeLayoutEffect-cjs.js +9 -0
  67. package/dist/useSafeLayoutEffect-es.js +7 -0
  68. package/dist/useShowClear/{useShowClear.js → index.cjs} +4 -3
  69. package/dist/useShowClear/index.mjs +17 -0
  70. package/dist/useStepper/index.cjs +8 -0
  71. package/dist/useStepper/index.mjs +2 -0
  72. package/dist/{useStepper/useStepper.js → useStepper-cjs.js} +26 -14
  73. package/dist/useStepper-es.js +51 -0
  74. package/dist/useWindowDimensions/index.cjs +8 -0
  75. package/dist/useWindowDimensions/index.mjs +2 -0
  76. package/dist/{useWindowDimensions/useWindowDimensions.js → useWindowDimensions-cjs.js} +8 -6
  77. package/dist/useWindowDimensions-es.js +28 -0
  78. package/package.json +109 -7
  79. package/dist/index.js +0 -34
  80. package/dist/useBool/index.js +0 -5
  81. package/dist/useBool/useBool.js +0 -11
  82. package/dist/useBool/useBool.test.d.ts +0 -1
  83. package/dist/useBool/useBool.test.js +0 -28
  84. package/dist/useBreakpoints/index.js +0 -20
  85. package/dist/useBreakpoints/mockViewportWidth/index.js +0 -17
  86. package/dist/useBreakpoints/mockViewportWidth/mockViewportWidth.test.d.ts +0 -1
  87. package/dist/useBreakpoints/mockViewportWidth/mockViewportWidth.test.js +0 -17
  88. package/dist/useBreakpoints/useBreakpoints.js +0 -28
  89. package/dist/useBreakpoints/useBreakpoints.test.d.ts +0 -1
  90. package/dist/useBreakpoints/useBreakpoints.test.js +0 -142
  91. package/dist/useBreakpoints/useMediaQuery.js +0 -22
  92. package/dist/useCallbackRef/index.js +0 -5
  93. package/dist/useCallbackRef/useCallbackRef.js +0 -15
  94. package/dist/useCallbackRef/useCallbackRef.test.d.ts +0 -1
  95. package/dist/useCallbackRef/useCallbackRef.test.js +0 -29
  96. package/dist/useCollectionQuery/index.js +0 -5
  97. package/dist/useCollectionQuery/mdxUtils.js +0 -152
  98. package/dist/useCollectionQuery/test-utilities/index.d.ts +0 -3
  99. package/dist/useCollectionQuery/test-utilities/index.js +0 -19
  100. package/dist/useCollectionQuery/test-utilities/mocks.d.ts +0 -106
  101. package/dist/useCollectionQuery/test-utilities/mocks.js +0 -131
  102. package/dist/useCollectionQuery/test-utilities/queries.d.ts +0 -37
  103. package/dist/useCollectionQuery/test-utilities/queries.js +0 -59
  104. package/dist/useCollectionQuery/test-utilities/utils.d.ts +0 -1
  105. package/dist/useCollectionQuery/test-utilities/utils.js +0 -17
  106. package/dist/useCollectionQuery/uniqueEdges.js +0 -18
  107. package/dist/useCollectionQuery/uniqueNodes.js +0 -10
  108. package/dist/useCollectionQuery/useCollectionQuery.js +0 -194
  109. package/dist/useCollectionQuery/useCollectionQuery.test.d.ts +0 -1
  110. package/dist/useCollectionQuery/useCollectionQuery.test.js +0 -357
  111. package/dist/useDebounce/index.js +0 -5
  112. package/dist/useDebounce/useDebounce.js +0 -29
  113. package/dist/useDebounce/useDebounce.test.d.ts +0 -1
  114. package/dist/useDebounce/useDebounce.test.js +0 -190
  115. package/dist/useFocusTrap/index.js +0 -5
  116. package/dist/useFocusTrap/useFocusTrap.test.d.ts +0 -1
  117. package/dist/useFocusTrap/useFocusTrap.test.js +0 -61
  118. package/dist/useFormState/index.js +0 -5
  119. package/dist/useFormState/useFormState.js +0 -11
  120. package/dist/useInView/index.js +0 -17
  121. package/dist/useInView/useInView.test.d.ts +0 -1
  122. package/dist/useInView/useInView.test.js +0 -29
  123. package/dist/useIsMounted/index.js +0 -5
  124. package/dist/useIsMounted/useIsMounted.test.d.ts +0 -1
  125. package/dist/useIsMounted/useIsMounted.test.js +0 -15
  126. package/dist/useLiveAnnounce/index.js +0 -5
  127. package/dist/useLiveAnnounce/useLiveAnnounce.test.d.ts +0 -1
  128. package/dist/useLiveAnnounce/useLiveAnnounce.test.js +0 -60
  129. package/dist/useOnKeyDown/index.js +0 -5
  130. package/dist/useOnKeyDown/useOnKeyDown.test.d.ts +0 -1
  131. package/dist/useOnKeyDown/useOnKeyDown.test.js +0 -23
  132. package/dist/useOnMount/index.js +0 -5
  133. package/dist/useOnMount/useOnMount.test.d.ts +0 -1
  134. package/dist/useOnMount/useOnMount.test.js +0 -18
  135. package/dist/useRefocusOnActivator/index.js +0 -5
  136. package/dist/useResizeObserver/index.js +0 -17
  137. package/dist/useResizeObserver/useResizeObserver.js +0 -68
  138. package/dist/useSafeLayoutEffect/index.js +0 -5
  139. package/dist/useSafeLayoutEffect/useSafeLayoutEffect.js +0 -7
  140. package/dist/useShowClear/index.js +0 -5
  141. package/dist/useShowClear/useShowClear.test.d.ts +0 -1
  142. package/dist/useShowClear/useShowClear.test.js +0 -210
  143. package/dist/useStepper/index.js +0 -5
  144. package/dist/useStepper/useStepper.test.d.ts +0 -1
  145. package/dist/useStepper/useStepper.test.js +0 -79
  146. package/dist/useWindowDimensions/index.js +0 -5
  147. package/dist/useWindowDimensions/useWIndowDimensions.test.d.ts +0 -1
  148. package/dist/useWindowDimensions/useWIndowDimensions.test.js +0 -23
@@ -1,59 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SUBSCRIPTION_QUERY = exports.LIST_QUERY_WITH_TOTAL_COUNT = exports.LIST_QUERY = void 0;
4
- const client_1 = require("@apollo/client");
5
- exports.LIST_QUERY = (0, client_1.gql) `
6
- query ConversationMessages($cursor: string, $searchTerm: string) {
7
- conversation(id: "MQ==") {
8
- smsMessages(first: 1, after: $cursor, searchTerm: $searchTerm) {
9
- edges {
10
- node {
11
- __typename
12
- id
13
- }
14
- }
15
- nodes {
16
- __typename
17
- id
18
- }
19
- pageInfo {
20
- endCursor
21
- hasNextPage
22
- }
23
- }
24
- }
25
- }
26
- `;
27
- exports.LIST_QUERY_WITH_TOTAL_COUNT = (0, client_1.gql) `
28
- query ConversationMessages($cursor: string, $searchTerm: string) {
29
- conversation(id: "MQ==") {
30
- smsMessages(first: 1, after: $cursor, searchTerm: $searchTerm) {
31
- edges {
32
- node {
33
- __typename
34
- id
35
- }
36
- }
37
- nodes {
38
- __typename
39
- id
40
- }
41
- pageInfo {
42
- endCursor
43
- hasNextPage
44
- }
45
- totalCount
46
- }
47
- }
48
- }
49
- `;
50
- exports.SUBSCRIPTION_QUERY = (0, client_1.gql) `
51
- subscription ConversationMessage($conversationId: EncodedId!) {
52
- conversationMessage(conversationId: $conversationId) {
53
- smsMessage {
54
- __typename
55
- id
56
- }
57
- }
58
- }
59
- `;
@@ -1 +0,0 @@
1
- export declare function wait(milliseconds?: number): Promise<void>;
@@ -1,17 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.wait = wait;
13
- function wait() {
14
- return __awaiter(this, arguments, void 0, function* (milliseconds = 0) {
15
- yield new Promise(resolve => setTimeout(resolve, milliseconds));
16
- });
17
- }
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createEdge = createEdge;
4
- exports.uniqueEdges = uniqueEdges;
5
- function createEdge(node) {
6
- return {
7
- node: node,
8
- cursor: "",
9
- __typename: `${node.__typename}Edge`,
10
- };
11
- }
12
- function uniqueEdges(edges) {
13
- const result = new Map();
14
- edges.forEach(edge => {
15
- result.set(`${edge.__typename}-${edge.node.__typename}-${edge.node.id}`, edge);
16
- });
17
- return Array.from(result.values());
18
- }
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uniqueNodes = uniqueNodes;
4
- function uniqueNodes(nodes) {
5
- const result = new Map();
6
- nodes.forEach(node => {
7
- result.set(`${node.__typename}-${node.id}`, node);
8
- });
9
- return Array.from(result.values());
10
- }
@@ -1,194 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.useCollectionQuery = useCollectionQuery;
7
- exports.isAlreadyUpdated = isAlreadyUpdated;
8
- const client_1 = require("@apollo/client");
9
- const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
10
- const react_1 = require("react");
11
- const formatters_1 = require("@jobber/formatters");
12
- const uniqueNodes_1 = require("./uniqueNodes");
13
- const uniqueEdges_1 = require("./uniqueEdges");
14
- const useIsMounted_1 = require("../useIsMounted");
15
- function useCollectionQuery({ query, queryOptions, getCollectionByPath, subscription, }) {
16
- var _a, _b;
17
- const { data, loading, refetch, error, fetchMore, subscribeToMore } = (0, client_1.useQuery)(query, queryOptions);
18
- const isMounted = (0, useIsMounted_1.useIsMounted)();
19
- const [loadingRefresh, setLoadingRefresh] = (0, react_1.useState)(false);
20
- const [loadingNextPage, setLoadingNextPage] = (0, react_1.useState)(false);
21
- const [hookError, setHookError] = (0, react_1.useState)();
22
- const loadingInitialContent = loading && !loadingRefresh && !loadingNextPage;
23
- const isSearching = !!((_a = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.variables) === null || _a === void 0 ? void 0 : _a.searchTerm);
24
- const refresh = (0, react_1.useCallback)(() => {
25
- if (loadingInitialContent || loadingRefresh) {
26
- return;
27
- }
28
- setLoadingRefresh(true);
29
- fetchMore({
30
- // a workaround fix for the error described in this post
31
- // https://github.com/apollographql/apollo-client/issues/7491#issuecomment-767985363
32
- // These changes can be reverted once we can update to version 3.4
33
- // (the current release candidate)
34
- variables: {},
35
- updateQuery: (prev, { fetchMoreResult }) => fetchMoreResult || prev,
36
- })
37
- .catch(err => formatters_1.config.errorNotifier("Refetch Error", err))
38
- .finally(() => {
39
- if (isMounted.current) {
40
- setLoadingRefresh(false);
41
- }
42
- });
43
- }, [
44
- loadingInitialContent,
45
- loadingRefresh,
46
- setLoadingRefresh,
47
- refetch,
48
- isMounted,
49
- ]);
50
- const nextPage = (0, react_1.useCallback)(() => {
51
- var _a;
52
- if (loadingInitialContent || loadingRefresh || loadingNextPage) {
53
- return;
54
- }
55
- const pageInfo = (_a = getCollectionByPath(data)) === null || _a === void 0 ? void 0 : _a.pageInfo;
56
- if (!pageInfo || !pageInfo.hasNextPage) {
57
- return;
58
- }
59
- setLoadingNextPage(true);
60
- setHookError(undefined);
61
- fetchMore({
62
- variables: {
63
- cursor: pageInfo.endCursor,
64
- },
65
- updateQuery: (prev, { fetchMoreResult }) => fetchMoreUpdateQueryHandler(prev, fetchMoreResult, getCollectionByPath),
66
- })
67
- .catch(err => {
68
- formatters_1.config.errorNotifier("FetchMore Error", err);
69
- setHookError(err);
70
- })
71
- .finally(() => {
72
- if (isMounted.current) {
73
- setLoadingNextPage(false);
74
- }
75
- });
76
- }, [
77
- data,
78
- loadingInitialContent,
79
- loadingRefresh,
80
- fetchMore,
81
- loadingNextPage,
82
- setLoadingNextPage,
83
- getCollectionByPath,
84
- isMounted,
85
- ]);
86
- (0, react_1.useEffect)(() => {
87
- if (subscription == undefined)
88
- return;
89
- const subscriptionOptions = subscription.options || {};
90
- // Reset this state so we can handle errors from the subscription
91
- setHookError(undefined);
92
- return subscribeToMore(Object.assign(Object.assign({}, subscriptionOptions), { document: subscription.document, updateQuery: (prev, { subscriptionData }) => subscribeToMoreHandler(isSearching, prev, getCollectionByPath, subscriptionData === null || subscriptionData === void 0 ? void 0 : subscriptionData.data, subscription.getNodeByPath), onError: err => {
93
- formatters_1.config.errorNotifier("Subscribe to More Error", err);
94
- setHookError(err);
95
- } }));
96
- },
97
- // Disabling this linter so we can force this only run once. If we didn't
98
- // do this we would need to ensure subscription, subscribeToMore, and getNodeByPath
99
- // all use useCallback.
100
- [(_b = queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.variables) === null || _b === void 0 ? void 0 : _b.searchTerm]);
101
- const combinedError = error || hookError;
102
- return {
103
- data,
104
- error: combinedError,
105
- refresh,
106
- loadingRefresh,
107
- nextPage,
108
- loadingNextPage,
109
- loadingInitialContent,
110
- };
111
- }
112
- /**
113
- * The following method uses an `output` variable, and indirectly modifies it through the `outputCollection` variable.
114
- * This type of indirect modification is prone to bugs, but I couldn't think of a better way to write this code.
115
- *
116
- * Here's what I was balancing:
117
- * 1. We need to copy the structure of prev to ensure that when we're combining two objects, we're not losing properties
118
- * 2. We need to extract a key from an unknown path that's given to us by getCollectionByPath and then we need to modify
119
- * that, and ensure that it's properly set on the cloned output object.
120
- * 3. We want to keep the interface to this hook as simple and easy to use as possible
121
- *
122
- * The alternative approaches that were rejected:
123
- * 1. We replace the getCollectionByPath with a keyPath string. (Eg. "data.conversation.message") and then we use lodash
124
- * `get` and `set` which should remove all the object manipulation. But, this approach loses us the type safety that
125
- * getCollectionByPath gives us
126
- * 2. We could add a setCollection function to the list of arguments for this hook. This leaves us with type safety but
127
- * makes the `useCollectionQuery` interface more complicated by adding arguments
128
- */
129
- function fetchMoreUpdateQueryHandler(prev, fetchMoreResult, getCollectionByPath) {
130
- const nextCollection = getCollectionByPath(fetchMoreResult);
131
- const output = (0, cloneDeep_1.default)(prev);
132
- const outputCollection = getCollectionByPath(output);
133
- if (outputCollection === undefined || nextCollection === undefined) {
134
- return output;
135
- }
136
- if (outputCollection.nodes && nextCollection.nodes) {
137
- outputCollection.nodes = getUpdatedNodes(outputCollection.nodes, nextCollection.nodes);
138
- }
139
- if (outputCollection.edges && nextCollection.edges) {
140
- outputCollection.edges = getUpdatedEdges(outputCollection.edges, nextCollection.edges);
141
- }
142
- Object.assign(outputCollection, Object.assign({ pageInfo: (0, cloneDeep_1.default)(nextCollection.pageInfo) }, getTotalCount(nextCollection.totalCount)));
143
- return output;
144
- }
145
- function subscribeToMoreHandler(isSearching, prev, getCollectionByPath, subscriptionData, getNodeByPath) {
146
- const node = getNodeByPath(subscriptionData);
147
- const output = (0, cloneDeep_1.default)(prev);
148
- const outputCollection = getCollectionByPath(output);
149
- if (outputCollection == undefined || node == undefined)
150
- return output;
151
- if (isAlreadyUpdated(outputCollection, node) || isSearching) {
152
- return prev;
153
- }
154
- if (outputCollection.nodes) {
155
- outputCollection.nodes = getUpdatedNodes(outputCollection.nodes, [node], false);
156
- }
157
- if (outputCollection.edges) {
158
- outputCollection.edges = getUpdatedEdges(outputCollection.edges, [(0, uniqueEdges_1.createEdge)(node)], false);
159
- }
160
- Object.assign(outputCollection, Object.assign({ pageInfo: (0, cloneDeep_1.default)(outputCollection.pageInfo) }, getTotalCount(outputCollection.totalCount, 1)));
161
- return output;
162
- }
163
- function getTotalCount(totalCount, additionalCount = 0) {
164
- return totalCount !== undefined
165
- ? { totalCount: totalCount + additionalCount }
166
- : {};
167
- }
168
- function isAlreadyUpdated(outputCollection, newNode) {
169
- let edgesAlreadyUpdated = true;
170
- let nodesAlreadyUpdated = true;
171
- if (outputCollection.edges) {
172
- edgesAlreadyUpdated = outputCollection.edges.some(edge => {
173
- return edge.node.id === newNode.id;
174
- });
175
- }
176
- if (outputCollection.nodes) {
177
- nodesAlreadyUpdated = outputCollection.nodes.some(node => {
178
- return node.id === newNode.id;
179
- });
180
- }
181
- return edgesAlreadyUpdated && nodesAlreadyUpdated;
182
- }
183
- function getUpdatedEdges(prevEdges, nextEdges, appendToEnd = true) {
184
- const newEdges = appendToEnd
185
- ? [...prevEdges, ...nextEdges]
186
- : [...nextEdges, ...prevEdges];
187
- return (0, uniqueEdges_1.uniqueEdges)(newEdges);
188
- }
189
- function getUpdatedNodes(prevNodes, nextNodes, appendToEnd = true) {
190
- const newNodes = appendToEnd
191
- ? [...prevNodes, ...nextNodes]
192
- : [...nextNodes, ...prevNodes];
193
- return (0, uniqueNodes_1.uniqueNodes)(newNodes);
194
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,357 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const react_hooks_1 = require("@testing-library/react-hooks");
13
- const react_1 = require("@testing-library/react");
14
- const useCollectionQuery_1 = require("./useCollectionQuery");
15
- const test_utilities_1 = require("./test-utilities");
16
- beforeEach(() => {
17
- (0, test_utilities_1.setListQueryMockHasNextPage)(true);
18
- test_utilities_1.listQueryResponseMock.mockClear();
19
- test_utilities_1.subscriptionQueryMock.mockClear();
20
- });
21
- function useCollectionQueryHook(query) {
22
- return (0, useCollectionQuery_1.useCollectionQuery)({
23
- query: query,
24
- getCollectionByPath(data) {
25
- var _a;
26
- return (_a = data === null || data === void 0 ? void 0 : data.conversation) === null || _a === void 0 ? void 0 : _a.smsMessages;
27
- },
28
- });
29
- }
30
- function useCollectionQueryHookWithSubscription(query) {
31
- return (0, useCollectionQuery_1.useCollectionQuery)({
32
- query: query,
33
- getCollectionByPath(data) {
34
- var _a;
35
- return (_a = data === null || data === void 0 ? void 0 : data.conversation) === null || _a === void 0 ? void 0 : _a.smsMessages;
36
- },
37
- subscription: {
38
- document: test_utilities_1.SUBSCRIPTION_QUERY,
39
- getNodeByPath(conversationData) {
40
- var _a;
41
- return (_a = conversationData === null || conversationData === void 0 ? void 0 : conversationData.conversationMessage) === null || _a === void 0 ? void 0 : _a.smsMessage;
42
- },
43
- },
44
- });
45
- }
46
- function useCollectionQueryHookWithSubscriptionAndSearch(query, searchTerm) {
47
- return (0, useCollectionQuery_1.useCollectionQuery)({
48
- query: query,
49
- queryOptions: {
50
- variables: {
51
- searchTerm: searchTerm,
52
- },
53
- },
54
- getCollectionByPath(data) {
55
- var _a;
56
- return (_a = data === null || data === void 0 ? void 0 : data.conversation) === null || _a === void 0 ? void 0 : _a.smsMessages;
57
- },
58
- subscription: {
59
- document: test_utilities_1.SUBSCRIPTION_QUERY,
60
- getNodeByPath(conversationData) {
61
- var _a;
62
- return (_a = conversationData === null || conversationData === void 0 ? void 0 : conversationData.conversationMessage) === null || _a === void 0 ? void 0 : _a.smsMessage;
63
- },
64
- },
65
- });
66
- }
67
- describe("useCollectionQuery", () => {
68
- describe.each `
69
- testCase | query | responseMock
70
- ${"excludes totalCount"} | ${test_utilities_1.LIST_QUERY} | ${test_utilities_1.listQueryResponseMock}
71
- ${"includes totalCount"} | ${test_utilities_1.LIST_QUERY_WITH_TOTAL_COUNT} | ${test_utilities_1.listQueryWithTotalCountResponseMock}
72
- `("when the query run $testCase", ({ query, responseMock }) => {
73
- describe("when useCollectionQuery is first called", () => {
74
- describe("when nextPage is called while it's still loading initial content", () => {
75
- it("should not trigger a the next page to be fetched", () => __awaiter(void 0, void 0, void 0, function* () {
76
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
77
- wrapper: (0, test_utilities_1.wrapper)([
78
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
79
- (0, test_utilities_1.buildListRequestMockForNextPage)(query, responseMock),
80
- ]),
81
- });
82
- (0, react_hooks_1.act)(() => {
83
- result.current.nextPage();
84
- });
85
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
86
- expect(responseMock).toHaveBeenCalledTimes(1);
87
- }));
88
- });
89
- describe("when refresh is called while it's still loading initial content", () => {
90
- it("should not trigger a refresh", () => __awaiter(void 0, void 0, void 0, function* () {
91
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
92
- wrapper: (0, test_utilities_1.wrapper)([
93
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
94
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
95
- ]),
96
- });
97
- (0, react_hooks_1.act)(() => {
98
- result.current.refresh();
99
- });
100
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
101
- expect(responseMock).toHaveBeenCalledTimes(1);
102
- }));
103
- });
104
- describe("when there is no error", () => {
105
- it("should update data", () => __awaiter(void 0, void 0, void 0, function* () {
106
- var _a, _b, _c, _d;
107
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
108
- wrapper: (0, test_utilities_1.wrapper)([(0, test_utilities_1.buildListRequestMock)(query, responseMock)]),
109
- });
110
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
111
- 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
- }));
113
- it("should set initialLoading while loading data", () => __awaiter(void 0, void 0, void 0, function* () {
114
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
115
- wrapper: (0, test_utilities_1.wrapper)([(0, test_utilities_1.buildListRequestMock)(query, responseMock)]),
116
- });
117
- expect(result.current.loadingInitialContent).toBe(true);
118
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
119
- }));
120
- });
121
- });
122
- describe("#nextPage", () => {
123
- describe("when nextPage is called while it's still loadingNextPage", () => {
124
- it("should not trigger a nextPage", () => __awaiter(void 0, void 0, void 0, function* () {
125
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
126
- wrapper: (0, test_utilities_1.wrapper)([
127
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
128
- (0, test_utilities_1.buildListRequestMockForNextPage)(query, responseMock),
129
- (0, test_utilities_1.buildListRequestMockForNextPage)(query, responseMock),
130
- ]),
131
- });
132
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
133
- (0, react_hooks_1.act)(() => {
134
- result.current.nextPage();
135
- });
136
- (0, react_hooks_1.act)(() => {
137
- result.current.nextPage();
138
- });
139
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
140
- yield (0, react_1.waitFor)(() => {
141
- var _a, _b, _c;
142
- 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
- });
144
- }));
145
- });
146
- describe("when refresh is called while it's still loadingNextPage", () => {
147
- it("should trigger a refresh", () => __awaiter(void 0, void 0, void 0, function* () {
148
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
149
- wrapper: (0, test_utilities_1.wrapper)([
150
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
151
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
152
- (0, test_utilities_1.buildListRequestMockForNextPage)(query, responseMock),
153
- ]),
154
- });
155
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
156
- (0, react_hooks_1.act)(() => {
157
- result.current.nextPage();
158
- });
159
- (0, react_hooks_1.act)(() => {
160
- result.current.refresh();
161
- });
162
- expect(result.current.loadingRefresh).toBe(true);
163
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
164
- }));
165
- });
166
- describe("when there is no more data to fetch", () => {
167
- it("should not fetch more data", () => __awaiter(void 0, void 0, void 0, function* () {
168
- (0, test_utilities_1.setListQueryMockHasNextPage)(false);
169
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
170
- wrapper: (0, test_utilities_1.wrapper)([
171
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
172
- (0, test_utilities_1.buildListRequestMockForNextPage)(query, responseMock),
173
- ]),
174
- });
175
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
176
- (0, react_hooks_1.act)(() => {
177
- result.current.nextPage();
178
- });
179
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
180
- expect(responseMock).toHaveBeenCalledTimes(1);
181
- }));
182
- });
183
- describe("when there is no error", () => {
184
- it("should update data", () => __awaiter(void 0, void 0, void 0, function* () {
185
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
186
- wrapper: (0, test_utilities_1.wrapper)([
187
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
188
- (0, test_utilities_1.buildListRequestMockForNextPage)(query, responseMock),
189
- ]),
190
- });
191
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
192
- (0, react_hooks_1.act)(() => {
193
- result.current.nextPage();
194
- });
195
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
196
- yield (0, react_1.waitFor)(() => {
197
- var _a, _b, _c;
198
- 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
- });
200
- }));
201
- it("should set loadingNextPage while loading data", () => __awaiter(void 0, void 0, void 0, function* () {
202
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
203
- wrapper: (0, test_utilities_1.wrapper)([
204
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
205
- (0, test_utilities_1.buildListRequestMockForNextPage)(query, responseMock),
206
- ]),
207
- });
208
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
209
- (0, react_hooks_1.act)(() => {
210
- result.current.nextPage();
211
- });
212
- expect(result.current.loadingNextPage).toBe(true);
213
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
214
- }));
215
- });
216
- describe("when there is an error", () => {
217
- it("should update the error state", () => __awaiter(void 0, void 0, void 0, function* () {
218
- const mockError = new Error("Failed to fetch more items");
219
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
220
- wrapper: (0, test_utilities_1.wrapper)([
221
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
222
- {
223
- request: {
224
- query: query,
225
- variables: { cursor: "MZ" },
226
- },
227
- error: mockError,
228
- },
229
- (0, test_utilities_1.buildListRequestMockForNextPage)(query, responseMock),
230
- ]),
231
- });
232
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
233
- (0, react_hooks_1.act)(() => {
234
- result.current.nextPage();
235
- });
236
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
237
- expect(result.current.error).toEqual(mockError);
238
- // should clear the error after a successful fetch
239
- (0, react_hooks_1.act)(() => {
240
- result.current.nextPage();
241
- });
242
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
243
- expect(result.current.error).toBeUndefined();
244
- }));
245
- });
246
- });
247
- describe("#refresh", () => {
248
- describe("when refresh is called while it's still loadingRefresh", () => {
249
- it("should not trigger a refresh", () => __awaiter(void 0, void 0, void 0, function* () {
250
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
251
- wrapper: (0, test_utilities_1.wrapper)([
252
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
253
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
254
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
255
- ]),
256
- });
257
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
258
- (0, react_hooks_1.act)(() => {
259
- result.current.refresh();
260
- });
261
- (0, react_hooks_1.act)(() => {
262
- result.current.refresh();
263
- });
264
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
265
- expect(responseMock).toHaveBeenCalledTimes(2);
266
- }));
267
- });
268
- describe("when nextPage is called while it's still loadingRefresh", () => {
269
- it("should not trigger a nextPage", () => __awaiter(void 0, void 0, void 0, function* () {
270
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
271
- wrapper: (0, test_utilities_1.wrapper)([
272
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
273
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
274
- (0, test_utilities_1.buildListRequestMockForNextPage)(query, responseMock),
275
- ]),
276
- });
277
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
278
- (0, react_hooks_1.act)(() => {
279
- result.current.refresh();
280
- });
281
- (0, react_hooks_1.act)(() => {
282
- result.current.nextPage();
283
- });
284
- expect(result.current.loadingNextPage).toBe(false);
285
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
286
- }));
287
- });
288
- describe("when there is no error", () => {
289
- it("should set loadingRefresh while loading data", () => __awaiter(void 0, void 0, void 0, function* () {
290
- const { result } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHook(query), {
291
- wrapper: (0, test_utilities_1.wrapper)([
292
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
293
- (0, test_utilities_1.buildListRequestMock)(query, responseMock),
294
- ]),
295
- });
296
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
297
- (0, react_hooks_1.act)(() => {
298
- result.current.refresh();
299
- });
300
- expect(result.current.loadingRefresh).toBe(true);
301
- yield (0, react_hooks_1.act)(test_utilities_1.wait);
302
- }));
303
- });
304
- });
305
- describe("#subscribeToMore", () => {
306
- describe("when hook receives update from item not in collection", () => {
307
- it("should add new item to collection", () => __awaiter(void 0, void 0, void 0, function* () {
308
- var _a, _b, _c, _d, _e;
309
- const { result, waitForNextUpdate } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHookWithSubscription(query), {
310
- wrapper: (0, test_utilities_1.wrapper)([
311
- (0, test_utilities_1.buildListRequestMock)(query, responseMock, "1"),
312
- (0, test_utilities_1.buildSubscriptionRequestMock)("2"),
313
- ]),
314
- });
315
- // Wait for initial load
316
- yield (0, react_hooks_1.act)(waitForNextUpdate);
317
- // Wait for subscription
318
- yield (0, react_hooks_1.act)(waitForNextUpdate);
319
- 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
- }));
321
- });
322
- describe("when hook receives update from item already in collection", () => {
323
- it("should return the existing collection", () => __awaiter(void 0, void 0, void 0, function* () {
324
- var _a, _b, _c, _d, _e;
325
- const { result, waitForNextUpdate } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHookWithSubscription(query), {
326
- wrapper: (0, test_utilities_1.wrapper)([
327
- (0, test_utilities_1.buildListRequestMock)(query, responseMock, "1"),
328
- (0, test_utilities_1.buildSubscriptionRequestMock)("1"),
329
- ]),
330
- });
331
- // Wait for initial load
332
- yield (0, react_hooks_1.act)(waitForNextUpdate);
333
- // Wait for subscription
334
- yield (0, react_hooks_1.act)(() => (0, test_utilities_1.wait)(200));
335
- 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
- }));
337
- });
338
- describe("when hook receives `update` but is currently searching a collection", () => {
339
- it("should return the existing collection without adding the subscribed content", () => __awaiter(void 0, void 0, void 0, function* () {
340
- var _a, _b, _c, _d, _e;
341
- const searchTerm = "FooBar";
342
- const { result, waitForNextUpdate } = (0, react_hooks_1.renderHook)(() => useCollectionQueryHookWithSubscriptionAndSearch(query, searchTerm), {
343
- wrapper: (0, test_utilities_1.wrapper)([
344
- (0, test_utilities_1.buildListRequestMock)(query, responseMock, "1", searchTerm),
345
- (0, test_utilities_1.buildSubscriptionRequestMock)("1"),
346
- ]),
347
- });
348
- // Wait for initial load
349
- yield (0, react_hooks_1.act)(waitForNextUpdate);
350
- // Wait for subscription
351
- yield (0, react_hooks_1.act)(() => (0, test_utilities_1.wait)(200));
352
- 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
- }));
354
- });
355
- });
356
- });
357
- });