@jskit-ai/http-web 0.2.57 → 0.2.59
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/CHANGELOG.md +2 -0
- package/locales/en.json +1 -1
- package/package.json +7 -7
- package/patterns/crud-screen-set/example/books/BookEditPage.vue +0 -1
- package/patterns/crud-screen-set/example/books/BookListPage.vue +0 -1
- package/patterns/crud-screen-set/example/books/BookViewPage.vue +0 -1
- package/src/client/composables/crud/crudLookupFieldRuntime.js +3 -0
- package/src/client/composables/records/useAddEdit.js +4 -4
- package/src/client/composables/records/useList.js +11 -6
- package/src/client/composables/records/useView.js +4 -3
- package/src/client/composables/support/errorMessageHelpers.js +8 -3
- package/src/client/composables/support/resourceLoadStateHelpers.js +20 -1
- package/src/client/composables/useCrudListFilterLookups.js +4 -0
- package/src/client/composables/useCrudListParentTitle.js +3 -0
- package/src/client/composables/useCrudListScreen.js +1 -1
- package/src/client/composables/useCrudViewScreen.js +1 -1
- package/src/client/types.js +10 -6
- package/test/crudLookupFieldRuntime.test.js +12 -1
- package/test/errorMessageHelpers.test.js +7 -0
- package/test/loadRetryAction.test.js +113 -13
- package/test/translation.test.js +2 -2
- package/test/useCrudListFilterLookups.test.js +21 -0
- package/test/useCrudListParentTitle.test.js +21 -0
- package/test/useListFirstPage.test.js +167 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- **Breaking:** `useList()`, `useView()` and `useAddEdit()` (and so `useCrudList()`, `useCrudView()`, `useCrudAddEdit()` and the three CRUD screens) leave shell-web's request recovery banner out of their read unless the caller passes `requestRecovery`: the default was on, and is now off (`meta.jskit.requestRecovery` is `false`). Fix: a read whose screen shows no `loadError` (a lookup feeding a select, a section that hides its read) passes `requestRecovery: true` (or a label, or an options object) to keep the banner; a screen that shows the read's `loadError` needs no change. Why: these reads show their failure where it happened, as `loadError` with Retry, and report it once as `resource-load`, while the banner reported the same network failure again with its own Retry, so the page showed it twice. Where the old shape shows, the read is refused: a `requestRecoveryLabel` given without `requestRecovery: true`, which named the banner before, is a `TypeError` at setup naming both fixes (pass `requestRecovery: true`, or drop the label where the screen shows `loadError`), and the CRUD screens no longer default a label of their own (the catalog's `jskit.http.crud.records`, which only that default used, is gone; no dependent project uses it). The rule lives in one helper, `resolveScreenReadRequestRecovery`. JSKIT's own reads that show no load error ask for the banner: the CRUD lookup fields' and list filters' options and `useCrudListParentTitle()`'s parent read; the crud-screen-set pattern's Book screens no longer pass a label. `useEndpointResource()` and `useListCore()` keep request recovery on by default, as before.
|
|
6
|
+
- `toUiErrorMessage` and `toQueryErrorMessage` no longer show the browser's "signal timed out" for a request past its deadline as if the server had said it: the caller's fallback copy shows, and without one "The server took too long to answer. Try again." (`jskit.http.feedback.timedOut`). Before, a read that took longer than 30 seconds showed "signal timed out" on the page.
|
|
5
7
|
- The CRUD screens' command buttons keep keyboard focus while they run, through shell-web's `pending` and `v-pending`: the form's Save while it saves or the record reloads after it (`addEdit.isSubmitPending`, new on `useAddEdit()` and the runtimes built on it, alongside `isSubmitDisabled`, which still includes it), the list's Refresh and "Load more", the Retry of a list, record or form that failed to load, and the delete dialog's Delete. Each reads as unavailable and ignores a second press meanwhile, where it was `disabled` and dropped focus to the page body (the dialog's Delete kept focus but never read as unavailable). A save the server refuses with a field error moves focus to that field. When the last page of "Load more" takes the button away, focus moves to the first record that page added.
|
|
6
8
|
- A `useCommand()` run refused because the person lacks its `runPermissions` (`canRun` false) is no longer silent: it shows, through the command's feedback (its `message` and `messageType`, and the shell's action feedback), the notice the server's 403 for the same request shows (`ACTION_PERMISSION_DENIED`, "Forbidden." in the person's language). It still sends nothing and resolves `null`, so callers are unchanged, and `onRunError` does not run, as nothing was sent. Before, `run()` answered null with no request and no notice, so a tap did nothing visible. A run while the command is already running still answers null without a notice.
|
|
7
9
|
- A list's, a record's or a form's load notice offers Retry only when trying again can help: after a network failure (no status), a timeout (408), a rate limit (429) or a server error (5xx). Before, it offered Retry on every failed load, also when the server had refused it (401, 403, 404, 422 and the other 4xx), where Retry only failed again; such a notice now has no action. One helper decides it for `useView()`, `useList()` and `useAddEdit()` (whose notice still offers nothing while `readEnabled` is false, and whose `canRetryLoad` and `refresh()` are unchanged).
|
package/locales/en.json
CHANGED
|
@@ -80,7 +80,6 @@
|
|
|
80
80
|
},
|
|
81
81
|
"record": "Record",
|
|
82
82
|
"recordNotFound": "Record not found.",
|
|
83
|
-
"records": "Records",
|
|
84
83
|
"view": {
|
|
85
84
|
"loadFailed": "Unable to load record.",
|
|
86
85
|
"notFound": "This {resource} could not be found."
|
|
@@ -94,6 +93,7 @@
|
|
|
94
93
|
"requestFailed": "Request failed.",
|
|
95
94
|
"saveFailed": "Unable to save.",
|
|
96
95
|
"saved": "Saved.",
|
|
96
|
+
"timedOut": "The server took too long to answer. Try again.",
|
|
97
97
|
"validationFailed": "Validation failed."
|
|
98
98
|
},
|
|
99
99
|
"list": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/http-web",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.59",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -47,20 +47,20 @@
|
|
|
47
47
|
"./locales/en.json": "./locales/en.json"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
50
|
+
"@jskit-ai/resource-crud-core": "0.1.226"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
54
|
-
"@jskit-ai/kernel": "0.2.
|
|
55
|
-
"@jskit-ai/realtime": "0.2.
|
|
56
|
-
"@jskit-ai/shell-web": "0.2.
|
|
53
|
+
"@jskit-ai/http-runtime": "0.2.34",
|
|
54
|
+
"@jskit-ai/kernel": "0.2.33",
|
|
55
|
+
"@jskit-ai/realtime": "0.2.53",
|
|
56
|
+
"@jskit-ai/shell-web": "0.2.50",
|
|
57
57
|
"@tanstack/vue-query": "^5.90.5",
|
|
58
58
|
"vue": "^3.5.13",
|
|
59
59
|
"vue-router": "^5.0.4",
|
|
60
60
|
"vuetify": "^4.3.0"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@jskit-ai/material-web": "0.1.
|
|
63
|
+
"@jskit-ai/material-web": "0.1.10"
|
|
64
64
|
},
|
|
65
65
|
"description": "Neutral Vue request, command, resource, and CRUD UI runtime for JSKIT web applications.",
|
|
66
66
|
"jskit": {
|
|
@@ -25,7 +25,6 @@ const screen = useCrudAddEditScreen({
|
|
|
25
25
|
placementSource: "crud.books.edit",
|
|
26
26
|
queryKeyFactory: (surfaceId = "", scope = "") => ["crud", "books", surfaceId, scope, routeRecordId.value],
|
|
27
27
|
writeMethod: "PATCH",
|
|
28
|
-
requestRecoveryLabel: "Book",
|
|
29
28
|
fallbackLoadError: "Unable to load this book.",
|
|
30
29
|
fallbackSaveError: "Unable to save this book.",
|
|
31
30
|
recordIdParam: "bookId",
|
|
@@ -115,9 +115,12 @@ function createCrudLookupFieldRuntime({
|
|
|
115
115
|
continue;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
+
// A lookup field shows no load error of its own, so a network failure of its options shows as the shell's
|
|
119
|
+
// request recovery banner.
|
|
118
120
|
const runtime = useList({
|
|
119
121
|
...(relationSurfaceId ? { surfaceId: relationSurfaceId } : {}),
|
|
120
122
|
apiSuffix: apiPath,
|
|
123
|
+
requestRecovery: true,
|
|
121
124
|
...(transport ? { transport } : {}),
|
|
122
125
|
queryKeyFactory: (surfaceId = "", scopeParamValue = "") => [
|
|
123
126
|
...normalizedQueryKeyPrefix,
|
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
import {
|
|
16
16
|
resolveResourceMessages
|
|
17
17
|
} from "../support/scopeHelpers.js";
|
|
18
|
-
import { resolveLoadErrorStatus } from "../support/resourceLoadStateHelpers.js";
|
|
18
|
+
import { resolveLoadErrorStatus, resolveScreenReadRequestRecovery } from "../support/resourceLoadStateHelpers.js";
|
|
19
19
|
import { toLoadRefusalMessage } from "../support/errorMessageHelpers.js";
|
|
20
20
|
import { createRequestQueryRuntime } from "../support/requestQueryRuntimeSupport.js";
|
|
21
21
|
import { resolveRouteParamNamesInOrder } from "../support/routeTemplateHelpers.js";
|
|
@@ -39,7 +39,7 @@ function useAddEdit({
|
|
|
39
39
|
client,
|
|
40
40
|
transport,
|
|
41
41
|
requestRecovery,
|
|
42
|
-
requestRecoveryLabel
|
|
42
|
+
requestRecoveryLabel,
|
|
43
43
|
placementSource = "http-web.add-edit",
|
|
44
44
|
fallbackLoadError = t("jskit.http.resource.loadFailed"),
|
|
45
45
|
fallbackSaveError = "",
|
|
@@ -137,8 +137,8 @@ function useAddEdit({
|
|
|
137
137
|
writeMethod,
|
|
138
138
|
readQuery: requestQueryRuntime.requestQuery,
|
|
139
139
|
transport,
|
|
140
|
-
requestRecovery,
|
|
141
|
-
requestRecoveryLabel,
|
|
140
|
+
requestRecovery: resolveScreenReadRequestRecovery(requestRecovery, { caller: "useAddEdit", requestRecoveryLabel }),
|
|
141
|
+
requestRecoveryLabel: requestRecoveryLabel ?? t("jskit.http.resource.label"),
|
|
142
142
|
fallbackLoadError,
|
|
143
143
|
fallbackSaveError: effectiveMessages.saveError
|
|
144
144
|
});
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
} from "../runtime/operationUiHelpers.js";
|
|
15
15
|
import { createListUiRuntime } from "../runtime/listUiRuntime.js";
|
|
16
16
|
import { asPlainObject } from "../support/scopeHelpers.js";
|
|
17
|
-
import { resolveLoadErrorStatus } from "../support/resourceLoadStateHelpers.js";
|
|
17
|
+
import { resolveLoadErrorStatus, resolveScreenReadRequestRecovery } from "../support/resourceLoadStateHelpers.js";
|
|
18
18
|
import { toLoadRefusalMessage } from "../support/errorMessageHelpers.js";
|
|
19
19
|
import {
|
|
20
20
|
buildRequestQueryObject,
|
|
@@ -63,7 +63,7 @@ function useList({
|
|
|
63
63
|
requestOptions,
|
|
64
64
|
queryOptions,
|
|
65
65
|
requestRecovery,
|
|
66
|
-
requestRecoveryLabel
|
|
66
|
+
requestRecoveryLabel,
|
|
67
67
|
realtime,
|
|
68
68
|
recordIdParam = "recordId",
|
|
69
69
|
recordIdSelector,
|
|
@@ -253,8 +253,8 @@ function useList({
|
|
|
253
253
|
selectItems,
|
|
254
254
|
requestOptions: listRequestOptions,
|
|
255
255
|
queryOptions,
|
|
256
|
-
requestRecovery,
|
|
257
|
-
requestRecoveryLabel,
|
|
256
|
+
requestRecovery: resolveScreenReadRequestRecovery(requestRecovery, { caller: "useList", requestRecoveryLabel }),
|
|
257
|
+
requestRecoveryLabel: requestRecoveryLabel ?? t("jskit.http.list.label"),
|
|
258
258
|
fallbackLoadError
|
|
259
259
|
});
|
|
260
260
|
const routeSyncApplying = ref(false);
|
|
@@ -410,6 +410,10 @@ function useList({
|
|
|
410
410
|
);
|
|
411
411
|
}
|
|
412
412
|
|
|
413
|
+
// Changing the search or a filter starts the list again at its first page: the new key's cached pages are trimmed to
|
|
414
|
+
// the first. The trim runs at the change itself (flush "sync"), before vue-query's own watcher on the query's
|
|
415
|
+
// options sees the new key. In the same pre-flush, the two watchers run in the order they were notified, and when
|
|
416
|
+
// vue-query's runs first, its refetch of the new key takes every cached page and puts them all back over the trim.
|
|
413
417
|
watch(activeSearchQuery, (nextValue, previousValue) => {
|
|
414
418
|
if (!querySearchEnabled.value) {
|
|
415
419
|
return;
|
|
@@ -419,7 +423,7 @@ function useList({
|
|
|
419
423
|
}
|
|
420
424
|
|
|
421
425
|
list.trimToFirstPage();
|
|
422
|
-
});
|
|
426
|
+
}, { flush: "sync" });
|
|
423
427
|
watch(
|
|
424
428
|
[
|
|
425
429
|
activeQueryParamsToken,
|
|
@@ -428,7 +432,8 @@ function useList({
|
|
|
428
432
|
],
|
|
429
433
|
() => {
|
|
430
434
|
list.trimToFirstPage();
|
|
431
|
-
}
|
|
435
|
+
},
|
|
436
|
+
{ flush: "sync" }
|
|
432
437
|
);
|
|
433
438
|
const listItems = computed(() => (Array.isArray(list.items.value) ? list.items.value : []));
|
|
434
439
|
|
|
@@ -16,6 +16,7 @@ import { createViewUiRuntime } from "../runtime/viewUiRuntime.js";
|
|
|
16
16
|
import { createRequestQueryRuntime } from "../support/requestQueryRuntimeSupport.js";
|
|
17
17
|
import { resolveRouteParamNamesInOrder } from "../support/routeTemplateHelpers.js";
|
|
18
18
|
import { resolveOperationRealtimeOptions } from "../useRealtimeQueryInvalidation.js";
|
|
19
|
+
import { resolveScreenReadRequestRecovery } from "../support/resourceLoadStateHelpers.js";
|
|
19
20
|
import { useJskitText } from "../../i18n.js";
|
|
20
21
|
|
|
21
22
|
const { t } = useJskitText();
|
|
@@ -34,7 +35,7 @@ function useView({
|
|
|
34
35
|
client,
|
|
35
36
|
transport,
|
|
36
37
|
requestRecovery,
|
|
37
|
-
requestRecoveryLabel
|
|
38
|
+
requestRecoveryLabel,
|
|
38
39
|
placementSource = "http-web.view",
|
|
39
40
|
fallbackLoadError = t("jskit.http.resource.loadFailed"),
|
|
40
41
|
notFoundStatuses = [404],
|
|
@@ -116,8 +117,8 @@ function useView({
|
|
|
116
117
|
readMethod,
|
|
117
118
|
readQuery: requestQueryRuntime.requestQuery,
|
|
118
119
|
transport,
|
|
119
|
-
requestRecovery,
|
|
120
|
-
requestRecoveryLabel,
|
|
120
|
+
requestRecovery: resolveScreenReadRequestRecovery(requestRecovery, { caller: "useView", requestRecoveryLabel }),
|
|
121
|
+
requestRecoveryLabel: requestRecoveryLabel ?? t("jskit.http.resource.label"),
|
|
121
122
|
refreshOnPull: true,
|
|
122
123
|
fallbackLoadError
|
|
123
124
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isGenericHttpErrorMessage, resolveFieldErrors } from "@jskit-ai/http-runtime/client";
|
|
1
|
+
import { isGenericHttpErrorMessage, isRequestTimeoutError, resolveFieldErrors } from "@jskit-ai/http-runtime/client";
|
|
2
2
|
import { isRetryableLoadStatus, resolveLoadErrorStatus } from "./resourceLoadStateHelpers.js";
|
|
3
3
|
import { useJskitText } from "../../i18n.js";
|
|
4
4
|
|
|
@@ -6,8 +6,10 @@ const { t, text, serverMessage, fieldErrorMessages } = useJskitText();
|
|
|
6
6
|
|
|
7
7
|
// An error's message in the user's language: its code or messageCode when the
|
|
8
8
|
// English it carries is JSKIT's English for that code, otherwise as sent. A
|
|
9
|
-
// caller's specific fallback copy still wins over generic transport wording
|
|
10
|
-
//
|
|
9
|
+
// caller's specific fallback copy still wins over generic transport wording;
|
|
10
|
+
// without one, a request past its deadline says so in words, never the
|
|
11
|
+
// browser's own "signal timed out". Fallbacks may be message keys, rendered in
|
|
12
|
+
// the language active when shown.
|
|
11
13
|
function toUiErrorMessage(error, fallbackMessage = "", defaultMessage = "") {
|
|
12
14
|
const normalizedFallback = text(fallbackMessage).trim();
|
|
13
15
|
const normalizedMessage = String(error?.message || "").trim();
|
|
@@ -17,6 +19,9 @@ function toUiErrorMessage(error, fallbackMessage = "", defaultMessage = "") {
|
|
|
17
19
|
if (normalizedFallback) {
|
|
18
20
|
return normalizedFallback;
|
|
19
21
|
}
|
|
22
|
+
if (isRequestTimeoutError(error)) {
|
|
23
|
+
return t("jskit.http.feedback.timedOut");
|
|
24
|
+
}
|
|
20
25
|
if (normalizedMessage) {
|
|
21
26
|
return serverMessage(error).trim();
|
|
22
27
|
}
|
|
@@ -125,11 +125,30 @@ function mergeRequestRecoveryQueryMeta(queryOptions = null, requestRecovery = nu
|
|
|
125
125
|
return Object.keys(meta).length > 0 ? mergeQueryMeta(sourceOptions, meta) : sourceOptions;
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
+
// useList(), useView() and useAddEdit() report their read's failure once, for their screen: `loadError` (shown where
|
|
129
|
+
// the read failed, with Retry when trying again can help) and the shell's one `resource-load` notice, whose channel the
|
|
130
|
+
// app's error policy picks. So shell-web's request recovery, which would report a network failure again as its own
|
|
131
|
+
// banner, leaves their reads out unless the caller asks for it with `requestRecovery`, for a read no screen shows the
|
|
132
|
+
// failure of (a lookup's options). The one rule for the three. A requestRecoveryLabel the caller gives while the banner
|
|
133
|
+
// stays out names nothing, so it is refused: such a read relied on the banner before this default, or its label is a
|
|
134
|
+
// leftover.
|
|
135
|
+
function resolveScreenReadRequestRecovery(requestRecovery = null, { caller, requestRecoveryLabel } = {}) {
|
|
136
|
+
const resolved = requestRecovery ?? false;
|
|
137
|
+
if (requestRecoveryLabel != null && (resolved === false || resolved?.enabled === false)) {
|
|
138
|
+
throw new TypeError(`${caller}() was given requestRecoveryLabel, which names the shell's request recovery banner, ` +
|
|
139
|
+
"but its read leaves the banner out unless requestRecovery is true: its screen shows the failure as loadError. " +
|
|
140
|
+
"Pass requestRecovery: true for a read whose screen shows no loadError (a lookup's options), or drop " +
|
|
141
|
+
"requestRecoveryLabel where the screen shows loadError (a CRUD screen always does).");
|
|
142
|
+
}
|
|
143
|
+
return resolved;
|
|
144
|
+
}
|
|
145
|
+
|
|
128
146
|
export {
|
|
129
147
|
buildRequestRecoveryMeta,
|
|
130
148
|
hasResolvedQueryData,
|
|
131
149
|
isRetryableLoadStatus,
|
|
132
150
|
mergeQueryMeta,
|
|
133
151
|
mergeRequestRecoveryQueryMeta,
|
|
134
|
-
resolveLoadErrorStatus
|
|
152
|
+
resolveLoadErrorStatus,
|
|
153
|
+
resolveScreenReadRequestRecovery
|
|
135
154
|
};
|
|
@@ -77,9 +77,12 @@ function useCrudListFilterLookups(
|
|
|
77
77
|
}
|
|
78
78
|
};
|
|
79
79
|
|
|
80
|
+
// A filter's options show no load error of their own, so a network failure of them shows as the shell's request
|
|
81
|
+
// recovery banner, as does the read of the selected values' labels below.
|
|
80
82
|
const runtime = useList({
|
|
81
83
|
apiSuffix: filter.lookup.apiSuffix,
|
|
82
84
|
...(transport ? { transport } : {}),
|
|
85
|
+
requestRecovery: true,
|
|
83
86
|
readEnabled: optionsWanted,
|
|
84
87
|
queryKeyFactory: queryKeyFactory(""),
|
|
85
88
|
search: {
|
|
@@ -114,6 +117,7 @@ function useCrudListFilterLookups(
|
|
|
114
117
|
? useList({
|
|
115
118
|
apiSuffix: filter.lookup.apiSuffix,
|
|
116
119
|
...(transport ? { transport } : {}),
|
|
120
|
+
requestRecovery: true,
|
|
117
121
|
readEnabled: computed(() => unlabeledValues.value.length > 0),
|
|
118
122
|
queryKeyFactory: queryKeyFactory("__selected__"),
|
|
119
123
|
requestQueryParams: (context) => ({
|
|
@@ -95,8 +95,11 @@ function useCrudListParentTitle({
|
|
|
95
95
|
return !listTitle;
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
+
// The title falls back to the entity's label and shows no load error, so a network failure of the parent's read shows
|
|
99
|
+
// as the shell's request recovery banner.
|
|
98
100
|
const parentView = viewRuntimeFactory({
|
|
99
101
|
apiUrlTemplate: normalizeText(initialParentDescriptor.apiUrlTemplate),
|
|
102
|
+
requestRecovery: true,
|
|
100
103
|
transport: parentTransport,
|
|
101
104
|
readEnabled: shouldLoadParentRecord,
|
|
102
105
|
recordIdParam: normalizeText(initialParentDescriptor.routeParamKey) || "recordId",
|
|
@@ -134,7 +134,7 @@ function useCrudListScreen({
|
|
|
134
134
|
requestQueryParams,
|
|
135
135
|
requestFieldsets,
|
|
136
136
|
readEnabled = true,
|
|
137
|
-
requestRecoveryLabel
|
|
137
|
+
requestRecoveryLabel,
|
|
138
138
|
fallbackLoadError = t("jskit.http.crud.list.loadFailed")
|
|
139
139
|
}) {
|
|
140
140
|
const crudHttpClient = resolveCrudHttpClient(resource, { client });
|
|
@@ -20,7 +20,7 @@ function useCrudViewScreen({
|
|
|
20
20
|
requestFieldsets,
|
|
21
21
|
readEnabled = true,
|
|
22
22
|
queryKeyFactory = null,
|
|
23
|
-
requestRecoveryLabel
|
|
23
|
+
requestRecoveryLabel,
|
|
24
24
|
fallbackLoadError = t("jskit.http.crud.view.loadFailed"),
|
|
25
25
|
notFoundMessage = t("jskit.http.crud.recordNotFound")
|
|
26
26
|
}) {
|
package/src/client/types.js
CHANGED
|
@@ -22,7 +22,13 @@
|
|
|
22
22
|
/** @typedef {string | ((context: Record<string, any>) => string) | import("vue").Ref<string>} ApiSuffix */
|
|
23
23
|
/** @typedef {boolean | import("vue").Ref<boolean> | ((context: Record<string, any>) => boolean)} ReadEnabled */
|
|
24
24
|
/** @typedef {(surfaceId: string, scopeParamValue?: string, ownershipFilter?: string) => unknown[]} QueryKeyFactory */
|
|
25
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Whether shell-web's request recovery reports a network failure of the read as its banner, with a Retry: `false` off,
|
|
27
|
+
* `true` on, a label string or an options object on with those hints. `null` takes the composable's default: on for
|
|
28
|
+
* useEndpointResource() and useListCore(); off for useList(), useView() and useAddEdit(), whose screen shows the failure
|
|
29
|
+
* as `loadError` with its own Retry, so a caller whose screen shows no load error passes `true`.
|
|
30
|
+
* @typedef {boolean | string | { enabled?: boolean, label?: string, source?: string, dedupeKey?: string, method?: string, dedupeWindowMs?: number } | null} RequestRecovery
|
|
31
|
+
*/
|
|
26
32
|
/** @typedef {Record<string, any> | import("vue").Ref<Record<string, any>> | ((context: Record<string, any>) => Record<string, any>) | null} RequestQueryParams */
|
|
27
33
|
/** @typedef {(model: any, payload: any, context: Record<string, any>) => void} MapLoadedToModel */
|
|
28
34
|
|
|
@@ -278,7 +284,7 @@
|
|
|
278
284
|
* @property {HttpClient} [client]
|
|
279
285
|
* @property {Record<string, any> | null} [transport]
|
|
280
286
|
* @property {RequestRecovery} [requestRecovery]
|
|
281
|
-
* @property {string} [requestRecoveryLabel]
|
|
287
|
+
* @property {string} [requestRecoveryLabel] The request recovery banner's label: only with `requestRecovery: true`, refused (a TypeError) without it.
|
|
282
288
|
* @property {string} [placementSource]
|
|
283
289
|
* @property {string} [fallbackLoadError]
|
|
284
290
|
* @property {string} [fallbackSaveError]
|
|
@@ -350,7 +356,7 @@
|
|
|
350
356
|
* @property {HttpClient} [client]
|
|
351
357
|
* @property {Record<string, any> | null} [transport]
|
|
352
358
|
* @property {RequestRecovery} [requestRecovery]
|
|
353
|
-
* @property {string} [requestRecoveryLabel]
|
|
359
|
+
* @property {string} [requestRecoveryLabel] The request recovery banner's label: only with `requestRecovery: true`, refused (a TypeError) without it.
|
|
354
360
|
* @property {string} [placementSource]
|
|
355
361
|
* @property {string} [fallbackLoadError]
|
|
356
362
|
* @property {number[]} [notFoundStatuses]
|
|
@@ -397,7 +403,7 @@
|
|
|
397
403
|
* @property {Record<string, any>} [requestOptions]
|
|
398
404
|
* @property {Record<string, any>} [queryOptions]
|
|
399
405
|
* @property {RequestRecovery} [requestRecovery]
|
|
400
|
-
* @property {string} [requestRecoveryLabel]
|
|
406
|
+
* @property {string} [requestRecoveryLabel] The request recovery banner's label: only with `requestRecovery: true`, refused (a TypeError) without it.
|
|
401
407
|
* @property {OperationRealtime | false | null} [realtime]
|
|
402
408
|
* @property {string} [recordIdParam]
|
|
403
409
|
* @property {(record: Record<string, any>) => unknown} [recordIdSelector]
|
|
@@ -561,7 +567,6 @@
|
|
|
561
567
|
* @property {RequestQueryParams} [requestFieldsets]
|
|
562
568
|
* @property {ReadEnabled} [readEnabled]
|
|
563
569
|
* @property {QueryKeyFactory} [queryKeyFactory]
|
|
564
|
-
* @property {string} [requestRecoveryLabel]
|
|
565
570
|
* @property {string} [fallbackLoadError]
|
|
566
571
|
* @property {string} [notFoundMessage]
|
|
567
572
|
*/
|
|
@@ -589,7 +594,6 @@
|
|
|
589
594
|
* @property {RequestQueryParams} [requestQueryParams]
|
|
590
595
|
* @property {RequestQueryParams} [requestFieldsets]
|
|
591
596
|
* @property {ReadEnabled} [readEnabled]
|
|
592
|
-
* @property {string} [requestRecoveryLabel]
|
|
593
597
|
* @property {string} [fallbackLoadError]
|
|
594
598
|
*/
|
|
595
599
|
|
|
@@ -15,7 +15,8 @@ const hooks = registerHooks({
|
|
|
15
15
|
return {
|
|
16
16
|
format: "module",
|
|
17
17
|
shortCircuit: true,
|
|
18
|
-
source: "export const useList = () =>
|
|
18
|
+
source: "export const useList = (options) => { (globalThis.lookupUseListCalls ||= []).push(options); " +
|
|
19
|
+
"return { items: [{ id: 7, name: 'Bath' }] }; };"
|
|
19
20
|
};
|
|
20
21
|
}
|
|
21
22
|
});
|
|
@@ -304,3 +305,13 @@ test("lookup items never title the selected value with its raw id", () => {
|
|
|
304
305
|
[{ value: "53", label: "Daycare - One Day", record: { id: 53, name: "Daycare - One Day" } }, ...listed]
|
|
305
306
|
);
|
|
306
307
|
});
|
|
308
|
+
|
|
309
|
+
// A lookup field shows no load error of its own, so its options keep the shell's request recovery banner, which
|
|
310
|
+
// useList leaves out for a screen that shows its own.
|
|
311
|
+
test("a lookup field's options read asks for the shell's request recovery", () => {
|
|
312
|
+
globalThis.lookupUseListCalls = [];
|
|
313
|
+
createCrudLookupFieldRuntime({ formFields: serviceFormFields });
|
|
314
|
+
|
|
315
|
+
assert.equal(globalThis.lookupUseListCalls.length, 1);
|
|
316
|
+
assert.equal(globalThis.lookupUseListCalls[0].requestRecovery, true);
|
|
317
|
+
});
|
|
@@ -37,3 +37,10 @@ test("toUiErrorMessage uses fallback copy for generic transport errors", () => {
|
|
|
37
37
|
);
|
|
38
38
|
assert.equal(toUiErrorMessage({}, "Unable to save."), "Unable to save.");
|
|
39
39
|
});
|
|
40
|
+
|
|
41
|
+
test("a request past its deadline reads as transport wording, never the browser's \"signal timed out\"", () => {
|
|
42
|
+
const timedOut = new DOMException("signal timed out", "TimeoutError");
|
|
43
|
+
assert.equal(toQueryErrorMessage(timedOut, "Unable to load list."), "Unable to load list.");
|
|
44
|
+
assert.equal(toUiErrorMessage(timedOut, "Unable to save."), "Unable to save.");
|
|
45
|
+
assert.equal(toUiErrorMessage(timedOut), "The server took too long to answer. Try again.");
|
|
46
|
+
});
|
|
@@ -4,6 +4,8 @@ import { QueryClient, VueQueryPlugin } from "@tanstack/vue-query";
|
|
|
4
4
|
import { createRenderer, h, reactive } from "vue";
|
|
5
5
|
import { createMemoryHistory, createRouter } from "vue-router";
|
|
6
6
|
import { SHELL_WEB_ERROR_RUNTIME_KEY } from "@jskit-ai/shell-web/client/error";
|
|
7
|
+
import { silentLogger } from "@jskit-ai/kernel/shared/support/silentLogger";
|
|
8
|
+
import { createShellRequestRecoveryRuntime } from "../../shell-web/src/client/requestRecovery/runtime.js";
|
|
7
9
|
import { useAddEdit } from "../src/client/composables/records/useAddEdit.js";
|
|
8
10
|
import { useCrudList } from "../src/client/composables/records/useCrudList.js";
|
|
9
11
|
import { useList } from "../src/client/composables/records/useList.js";
|
|
@@ -59,10 +61,12 @@ async function waitFor(check, message) {
|
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
// Mounts one runtime whose every read fails with `status` (and `message`, by default one of the server's own, and the
|
|
62
|
-
// server's `code` when given), and records what it reports to the shell.
|
|
64
|
+
// server's `code` when given), and records what it reports to the shell. With `shellRequestRecovery`, shell-web's
|
|
65
|
+
// request recovery watches the same queries and reports to the same shell, as an app's shell has it.
|
|
63
66
|
async function mountFailingLoad(t, status, useRuntime, {
|
|
64
67
|
message = `Contacts refused the read (${describeStatus(status)}).`,
|
|
65
|
-
code
|
|
68
|
+
code,
|
|
69
|
+
shellRequestRecovery = false
|
|
66
70
|
} = {}) {
|
|
67
71
|
const requests = [];
|
|
68
72
|
const client = {
|
|
@@ -95,15 +99,21 @@ async function mountFailingLoad(t, status, useRuntime, {
|
|
|
95
99
|
});
|
|
96
100
|
app.use(router);
|
|
97
101
|
app.use(VueQueryPlugin, { queryClient });
|
|
98
|
-
|
|
102
|
+
const shell = {
|
|
99
103
|
report(event) {
|
|
100
104
|
reports.push(event);
|
|
101
105
|
return { presentationId: `presentation-${reports.length}` };
|
|
102
106
|
},
|
|
103
107
|
dismiss() {}
|
|
104
|
-
}
|
|
108
|
+
};
|
|
109
|
+
app.provide(SHELL_WEB_ERROR_RUNTIME_KEY, shell);
|
|
110
|
+
const requestRecovery = shellRequestRecovery
|
|
111
|
+
? createShellRequestRecoveryRuntime({ queryClient, errorRuntime: shell, logger: silentLogger })
|
|
112
|
+
: null;
|
|
113
|
+
requestRecovery?.install();
|
|
105
114
|
app.mount(node("root"));
|
|
106
115
|
t.after(() => {
|
|
116
|
+
requestRecovery?.dispose();
|
|
107
117
|
app.unmount();
|
|
108
118
|
queryClient.clear();
|
|
109
119
|
});
|
|
@@ -225,15 +235,9 @@ const LOAD_REFUSALS = Object.freeze([
|
|
|
225
235
|
}
|
|
226
236
|
]);
|
|
227
237
|
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
apiSuffix: "/contacts/7",
|
|
232
|
-
ownershipFilter: "public",
|
|
233
|
-
client,
|
|
234
|
-
queryKeyFactory: () => ["contacts", "7"]
|
|
235
|
-
}),
|
|
236
|
-
useAddEdit: (client) => useAddEdit({
|
|
238
|
+
const REFUSAL_RUNTIMES_OPTIONS = Object.freeze({
|
|
239
|
+
view: (client) => ({ apiSuffix: "/contacts/7", ownershipFilter: "public", client, queryKeyFactory: () => ["contacts", "7"] }),
|
|
240
|
+
addEdit: (client) => ({
|
|
237
241
|
apiSuffix: "/contacts/7",
|
|
238
242
|
ownershipFilter: "public",
|
|
239
243
|
client,
|
|
@@ -241,6 +245,11 @@ const REFUSAL_RUNTIMES = Object.freeze({
|
|
|
241
245
|
model: reactive({ name: "" })
|
|
242
246
|
})
|
|
243
247
|
});
|
|
248
|
+
const REFUSAL_RUNTIMES = Object.freeze({
|
|
249
|
+
useList: (client) => useList(listOptions(client)),
|
|
250
|
+
useView: (client) => useView(REFUSAL_RUNTIMES_OPTIONS.view(client)),
|
|
251
|
+
useAddEdit: (client) => useAddEdit(REFUSAL_RUNTIMES_OPTIONS.addEdit(client))
|
|
252
|
+
});
|
|
244
253
|
|
|
245
254
|
for (const [name, useRuntime] of Object.entries(REFUSAL_RUNTIMES)) {
|
|
246
255
|
for (const { label, status, message, refusal } of LOAD_REFUSALS) {
|
|
@@ -268,3 +277,94 @@ test("useCrudList: a 400 for a query key the list does not take says the server'
|
|
|
268
277
|
assert.equal(mounted.runtime.errorStatus, 400);
|
|
269
278
|
assert.equal(mounted.runtime.loadRefusalMessage, sentence);
|
|
270
279
|
});
|
|
280
|
+
|
|
281
|
+
// A network failure of a screen's read shows once: the screen shows the runtime's `loadError` with Retry, and the shell
|
|
282
|
+
// gets its one load notice, whose channel the app's error policy picks. shell-web's request recovery, which reports a
|
|
283
|
+
// network failure of a read as its own banner with its own Retry, leaves these reads out.
|
|
284
|
+
const SCREEN_READS = Object.freeze({
|
|
285
|
+
...REFUSAL_RUNTIMES,
|
|
286
|
+
useCrudList: (client) => useCrudList(listOptions(client))
|
|
287
|
+
});
|
|
288
|
+
const NETWORK_FAILURE = "Failed to fetch";
|
|
289
|
+
|
|
290
|
+
for (const [name, useRuntime] of Object.entries(SCREEN_READS)) {
|
|
291
|
+
test(`${name}: a network failure shows once, as its own load notice, not again as the shell's request recovery banner`, async (t) => {
|
|
292
|
+
const mounted = await mountFailingLoad(t, undefined, useRuntime, {
|
|
293
|
+
message: NETWORK_FAILURE,
|
|
294
|
+
shellRequestRecovery: true
|
|
295
|
+
});
|
|
296
|
+
// Long enough for the request recovery observer, which reports from the query cache's events, to have spoken.
|
|
297
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
298
|
+
|
|
299
|
+
assert.deepEqual(mounted.reports.map((event) => event.intent), ["resource-load"]);
|
|
300
|
+
assertRetryOffer(mounted, { status: undefined, retry: true });
|
|
301
|
+
assert.ok(mounted.runtime.loadError);
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// The errors a reader's setup throws, as the app's error handler receives them.
|
|
306
|
+
async function setupErrors(t, useRuntime) {
|
|
307
|
+
const router = createRouter({
|
|
308
|
+
history: createMemoryHistory(),
|
|
309
|
+
routes: [{ path: "/contacts/:recordId?", component: { render: () => h("div") } }]
|
|
310
|
+
});
|
|
311
|
+
await router.push("/contacts/7");
|
|
312
|
+
await router.isReady();
|
|
313
|
+
const errors = [];
|
|
314
|
+
const app = renderer.createApp({
|
|
315
|
+
setup() {
|
|
316
|
+
useRuntime({ async request() { return {}; } });
|
|
317
|
+
return () => h("div");
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
app.config.errorHandler = (error) => errors.push(error);
|
|
321
|
+
app.use(router);
|
|
322
|
+
app.use(VueQueryPlugin, { queryClient: new QueryClient() });
|
|
323
|
+
app.provide(SHELL_WEB_ERROR_RUNTIME_KEY, { report: () => ({}), dismiss() {} });
|
|
324
|
+
app.mount(node("root"));
|
|
325
|
+
t.after(() => app.unmount());
|
|
326
|
+
return errors;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// A label names the banner, which a screen read leaves out: given without requestRecovery: true it names nothing, as
|
|
330
|
+
// in an application written before this default, so the read is refused and the error says what to change.
|
|
331
|
+
const LABELLED_SCREEN_READS = Object.freeze([
|
|
332
|
+
["useList", "useList", (client) => useList({ ...listOptions(client), requestRecoveryLabel: "Contacts" })],
|
|
333
|
+
["useList with requestRecovery: false", "useList", (client) => useList({
|
|
334
|
+
...listOptions(client),
|
|
335
|
+
requestRecovery: false,
|
|
336
|
+
requestRecoveryLabel: "Contacts"
|
|
337
|
+
})],
|
|
338
|
+
["useView", "useView", (client) => useView({ ...REFUSAL_RUNTIMES_OPTIONS.view(client), requestRecoveryLabel: "Contact" })],
|
|
339
|
+
["useAddEdit", "useAddEdit", (client) => useAddEdit({ ...REFUSAL_RUNTIMES_OPTIONS.addEdit(client), requestRecoveryLabel: "Contact" })],
|
|
340
|
+
["useCrudList", "useList", (client) => useCrudList({ ...listOptions(client), requestRecoveryLabel: "Contacts" })]
|
|
341
|
+
]);
|
|
342
|
+
|
|
343
|
+
for (const [name, caller, useRuntime] of LABELLED_SCREEN_READS) {
|
|
344
|
+
test(`${name}: a requestRecoveryLabel without requestRecovery: true is refused, naming the fix`, async (t) => {
|
|
345
|
+
const errors = await setupErrors(t, useRuntime);
|
|
346
|
+
assert.equal(errors.length, 1);
|
|
347
|
+
assert.ok(errors[0] instanceof TypeError);
|
|
348
|
+
assert.equal(errors[0].message, `${caller}() was given requestRecoveryLabel, which names the shell's request ` +
|
|
349
|
+
"recovery banner, but its read leaves the banner out unless requestRecovery is true: its screen shows the failure " +
|
|
350
|
+
"as loadError. Pass requestRecovery: true for a read whose screen shows no loadError (a lookup's options), or " +
|
|
351
|
+
"drop requestRecoveryLabel where the screen shows loadError (a CRUD screen always does).");
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// A read whose screen shows no load error (a lookup's options) asks for the banner, and gets it with its Retry.
|
|
356
|
+
test("useList with requestRecovery: a network failure also shows as the shell's request recovery banner", async (t) => {
|
|
357
|
+
const mounted = await mountFailingLoad(t, undefined, (client) => useList({
|
|
358
|
+
...listOptions(client),
|
|
359
|
+
requestRecovery: true,
|
|
360
|
+
requestRecoveryLabel: "Contacts"
|
|
361
|
+
}), { message: NETWORK_FAILURE, shellRequestRecovery: true });
|
|
362
|
+
|
|
363
|
+
await waitFor(
|
|
364
|
+
() => mounted.reports.some((event) => event.intent === "app-recoverable"),
|
|
365
|
+
"Expected the shell's request recovery banner."
|
|
366
|
+
);
|
|
367
|
+
const banner = mounted.reports.find((event) => event.intent === "app-recoverable");
|
|
368
|
+
assert.equal(banner.message, "Contacts could not reach the server or network. Check the connection and try again.");
|
|
369
|
+
assert.equal(banner.action.label, "Retry");
|
|
370
|
+
});
|
package/test/translation.test.js
CHANGED
|
@@ -160,12 +160,12 @@ test("CRUD list chrome is English by default and follows the active language", a
|
|
|
160
160
|
});
|
|
161
161
|
|
|
162
162
|
test("date filters format with the app's active language", async () => {
|
|
163
|
-
const filter = { type: "dateRange", label: "jskit.http.crud.
|
|
163
|
+
const filter = { type: "dateRange", label: "jskit.http.crud.record" };
|
|
164
164
|
await withTestLanguage(() => {
|
|
165
165
|
assert.equal(formatDateOnlyDisplay("2026-04-18"), "18 Apr 2026");
|
|
166
166
|
assert.equal(
|
|
167
167
|
formatCrudListDateFilterChipLabel(filter, { from: "2026-05-04", to: "2026-05-10" }),
|
|
168
|
-
"[xx] [xx]
|
|
168
|
+
"[xx] [xx] Record: 4 May 2026 to 10 May 2026"
|
|
169
169
|
);
|
|
170
170
|
}, "en-GB");
|
|
171
171
|
});
|
|
@@ -232,3 +232,24 @@ test("a lookup filter reads nothing until its options are shown, and labels a se
|
|
|
232
232
|
assert.equal(lookup.options[0].label, "Farm 01");
|
|
233
233
|
assert.equal(lookup.options[44].label, "Farm 45");
|
|
234
234
|
});
|
|
235
|
+
|
|
236
|
+
// A filter's options show no load error of their own, so both of its reads keep the shell's request recovery banner,
|
|
237
|
+
// which useList leaves out for a screen that shows its own: their queries carry the recovery hints, never `false`.
|
|
238
|
+
test("a lookup filter's reads ask for the shell's request recovery", async (t) => {
|
|
239
|
+
const values = reactive({ supplierContactId: ["44"] });
|
|
240
|
+
const { app, filterLookups, queryClient } = await mountSupplierLookup(values);
|
|
241
|
+
t.after(() => {
|
|
242
|
+
app.unmount();
|
|
243
|
+
queryClient.clear();
|
|
244
|
+
configureHttpWebClient({});
|
|
245
|
+
});
|
|
246
|
+
await settle(() => filterLookups.resolveLookupLabel("supplierContactId", "44", "Supplier") === "Farm 44");
|
|
247
|
+
|
|
248
|
+
// The options' query and the selected values' query, one per key they have had.
|
|
249
|
+
const recoveries = queryClient.getQueryCache().getAll().map((query) => query.meta?.jskit || {});
|
|
250
|
+
assert.ok(recoveries.length >= 2);
|
|
251
|
+
for (const recovery of recoveries) {
|
|
252
|
+
assert.notEqual(recovery.requestRecovery, false);
|
|
253
|
+
assert.equal(recovery.requestRecoveryMethod, "GET");
|
|
254
|
+
}
|
|
255
|
+
});
|
|
@@ -260,3 +260,24 @@ test("useCrudListParentTitle requests the parent through JSON:API record transpo
|
|
|
260
260
|
responseKind: "record"
|
|
261
261
|
});
|
|
262
262
|
});
|
|
263
|
+
|
|
264
|
+
// The title falls back to the entity's label and shows no load error, so the parent's read keeps the shell's request
|
|
265
|
+
// recovery banner, which useView leaves out for a screen that shows its own.
|
|
266
|
+
test("useCrudListParentTitle's parent read asks for the shell's request recovery", () => {
|
|
267
|
+
let capturedOptions = null;
|
|
268
|
+
useCrudListParentTitle({
|
|
269
|
+
listRuntime: { items: [], isInitialLoading: false, loadError: "" },
|
|
270
|
+
resource: contactChildResource,
|
|
271
|
+
recordIdParam: "availabilityRuleId",
|
|
272
|
+
route: {
|
|
273
|
+
matched: [{ path: "/w/:workspaceSlug/admin/contacts/:contactId/availabilities" }],
|
|
274
|
+
params: { workspaceSlug: "harbor", contactId: "538779" }
|
|
275
|
+
},
|
|
276
|
+
viewRuntimeFactory: (options = {}) => {
|
|
277
|
+
capturedOptions = options;
|
|
278
|
+
return { record: null, isLoading: false, loadError: "" };
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
assert.equal(capturedOptions.requestRecovery, true);
|
|
283
|
+
});
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { QueryClient, VueQueryPlugin } from "@tanstack/vue-query";
|
|
4
|
+
import { createRenderer, h, nextTick, ref } from "vue";
|
|
5
|
+
import { createMemoryHistory, createRouter } from "vue-router";
|
|
6
|
+
import { useList } from "../src/client/composables/records/useList.js";
|
|
7
|
+
import { useCrudListFilters } from "../src/client/composables/useCrudListFilters.js";
|
|
8
|
+
|
|
9
|
+
// Changing or removing a filter, or changing the search, starts the list again at its first page. Two watchers of the
|
|
10
|
+
// list's component react to such a change in the same flush: vue-query's own, on the query's options, which starts the
|
|
11
|
+
// refetch of the new key's cached pages (all of them, as they are when it starts), and the list's, which trims that
|
|
12
|
+
// cache to its first page. They run in the order they were notified, which shifts with render timing, so these tests
|
|
13
|
+
// make vue-query's run first: something else the query reads (its gcTime) changes first in the same tick.
|
|
14
|
+
|
|
15
|
+
const PAGE_SIZE = 20;
|
|
16
|
+
const SEARCH_DEBOUNCE_MS = 50;
|
|
17
|
+
const REGION_FILTER = Object.freeze({
|
|
18
|
+
region: Object.freeze({
|
|
19
|
+
type: "enum",
|
|
20
|
+
options: Object.freeze([
|
|
21
|
+
Object.freeze({ label: "Far", value: "far" }),
|
|
22
|
+
Object.freeze({ label: "Near", value: "near" })
|
|
23
|
+
])
|
|
24
|
+
})
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const node = (type, text = "") => ({ type, text, props: {}, children: [], parent: null });
|
|
28
|
+
const renderer = createRenderer({
|
|
29
|
+
createElement: node,
|
|
30
|
+
createText: (text) => node("text", text),
|
|
31
|
+
createComment: () => node("comment"),
|
|
32
|
+
setText: (item, text) => { item.text = text; },
|
|
33
|
+
setElementText: (item, text) => { item.text = text; item.children = []; },
|
|
34
|
+
patchProp: (item, key, _previous, value) => { item.props[key] = value; },
|
|
35
|
+
parentNode: (item) => item.parent,
|
|
36
|
+
nextSibling: (item) => item.parent?.children[item.parent.children.indexOf(item) + 1] ?? null,
|
|
37
|
+
insert(item, parent, anchor) {
|
|
38
|
+
if (item.parent) item.parent.children.splice(item.parent.children.indexOf(item), 1);
|
|
39
|
+
item.parent = parent;
|
|
40
|
+
const index = anchor ? parent.children.indexOf(anchor) : -1;
|
|
41
|
+
if (index < 0) parent.children.push(item); else parent.children.splice(index, 0, item);
|
|
42
|
+
},
|
|
43
|
+
remove(item) {
|
|
44
|
+
if (item.parent) item.parent.children.splice(item.parent.children.indexOf(item), 1);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
async function waitFor(check, message) {
|
|
49
|
+
for (let attempt = 0; attempt < 500; attempt += 1) {
|
|
50
|
+
if (check()) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await new Promise((resolve) => setTimeout(resolve, 2));
|
|
54
|
+
}
|
|
55
|
+
assert.fail(message);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Three pages of 20 projects; a region filter or a search finds one.
|
|
59
|
+
function projectsPage(query = {}) {
|
|
60
|
+
if (query.region || query.q) {
|
|
61
|
+
return { items: [{ id: "far" }], nextCursor: null };
|
|
62
|
+
}
|
|
63
|
+
const page = Number(query.cursor ?? 0);
|
|
64
|
+
return {
|
|
65
|
+
items: Array.from({ length: PAGE_SIZE }, (_, index) => ({ id: `p${page * PAGE_SIZE + index}` })),
|
|
66
|
+
nextCursor: page < 2 ? String(page + 1) : null
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Mounts a list of projects with a region filter (and the search, when given) and loads its first two pages.
|
|
71
|
+
async function mountProjectList(t, { search = null } = {}) {
|
|
72
|
+
const requests = [];
|
|
73
|
+
const client = {
|
|
74
|
+
async request(_path, options) {
|
|
75
|
+
requests.push(options.query);
|
|
76
|
+
return projectsPage(options.query);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const router = createRouter({
|
|
80
|
+
history: createMemoryHistory(),
|
|
81
|
+
routes: [{ path: "/projects", component: { render: () => h("div") } }]
|
|
82
|
+
});
|
|
83
|
+
await router.push("/projects");
|
|
84
|
+
await router.isReady();
|
|
85
|
+
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
|
86
|
+
const gcTime = ref(5 * 60 * 1000);
|
|
87
|
+
let filters = null;
|
|
88
|
+
let list = null;
|
|
89
|
+
const app = renderer.createApp({
|
|
90
|
+
setup() {
|
|
91
|
+
filters = useCrudListFilters(REGION_FILTER);
|
|
92
|
+
list = useList({
|
|
93
|
+
apiSuffix: "/projects",
|
|
94
|
+
ownershipFilter: "public",
|
|
95
|
+
client,
|
|
96
|
+
queryKeyFactory: () => ["projects"],
|
|
97
|
+
queryParams: filters.queryParams,
|
|
98
|
+
search,
|
|
99
|
+
queryOptions: { gcTime }
|
|
100
|
+
});
|
|
101
|
+
return () => h("div");
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
app.use(router);
|
|
105
|
+
app.use(VueQueryPlugin, { queryClient });
|
|
106
|
+
app.mount(node("root"));
|
|
107
|
+
t.after(() => {
|
|
108
|
+
app.unmount();
|
|
109
|
+
queryClient.clear();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
await waitFor(() => list.items.length === PAGE_SIZE && !list.isFetching, "The list loads its first page.");
|
|
113
|
+
await list.loadMore();
|
|
114
|
+
await waitFor(() => list.items.length === PAGE_SIZE * 2 && !list.isFetching, "The list loads its second page.");
|
|
115
|
+
return { list, filters, requests, gcTime };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Queues vue-query's own watcher on the list's query options before any watcher the next change notifies.
|
|
119
|
+
function queueVueQueryFirst(gcTime) {
|
|
120
|
+
gcTime.value += 1;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function waitForReload(list, requests, requestsBefore, message) {
|
|
124
|
+
await waitFor(() => requests.length > requestsBefore && !list.isFetching, message);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
test("removing a filter starts the list again at its first page when vue-query's watcher runs first", async (t) => {
|
|
128
|
+
const { list, filters, requests, gcTime } = await mountProjectList(t);
|
|
129
|
+
filters.values.region = "far";
|
|
130
|
+
await waitFor(() => list.items.length === 1 && !list.isFetching, "The region filter finds one project.");
|
|
131
|
+
|
|
132
|
+
const requestsBefore = requests.length;
|
|
133
|
+
queueVueQueryFirst(gcTime);
|
|
134
|
+
filters.clearFilter("region");
|
|
135
|
+
await waitForReload(list, requests, requestsBefore, "The list loads again without the filter.");
|
|
136
|
+
|
|
137
|
+
assert.equal(list.items.length, PAGE_SIZE);
|
|
138
|
+
assert.deepEqual(requests.slice(requestsBefore), [{ limit: PAGE_SIZE }]);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("clearing the search starts the list again at its first page when vue-query's watcher runs first", async (t) => {
|
|
142
|
+
const { list, requests, gcTime } = await mountProjectList(t, {
|
|
143
|
+
search: { enabled: true, debounceMs: SEARCH_DEBOUNCE_MS }
|
|
144
|
+
});
|
|
145
|
+
list.searchQuery = "far";
|
|
146
|
+
await waitFor(
|
|
147
|
+
() => list.items.length === 1 && !list.isFetching && !list.isSearchDebouncing,
|
|
148
|
+
"The search finds one project."
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const requestsBefore = requests.length;
|
|
152
|
+
// The search's debounce ends on a mocked clock, so it ends in the same tick as the change that queues vue-query's
|
|
153
|
+
// watcher first. Nothing else starts a timer meanwhile.
|
|
154
|
+
t.mock.timers.enable({ apis: ["setTimeout"] });
|
|
155
|
+
try {
|
|
156
|
+
list.searchQuery = "";
|
|
157
|
+
await nextTick();
|
|
158
|
+
queueVueQueryFirst(gcTime);
|
|
159
|
+
t.mock.timers.tick(SEARCH_DEBOUNCE_MS);
|
|
160
|
+
} finally {
|
|
161
|
+
t.mock.timers.reset();
|
|
162
|
+
}
|
|
163
|
+
await waitForReload(list, requests, requestsBefore, "The list loads again without the search.");
|
|
164
|
+
|
|
165
|
+
assert.equal(list.items.length, PAGE_SIZE);
|
|
166
|
+
assert.deepEqual(requests.slice(requestsBefore), [{ limit: PAGE_SIZE }]);
|
|
167
|
+
});
|