@vtex/faststore-plugin-buyer-portal 2.0.49 → 2.0.50
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/package.json +1 -1
- package/src/features/accounting-fields/hooks/useListAccountingFields.ts +2 -1
- package/src/features/addresses/components/AddressLine/AddressLine.tsx +3 -6
- package/src/features/contracts/components/ContractListItem/ContractListItem.tsx +0 -5
- package/src/features/contracts/components/index.ts +0 -2
- package/src/features/contracts/hooks/useSwitchContract.ts +13 -5
- package/src/features/contracts/layouts/ContractListingLayout/contract-listing-layout.scss +0 -1
- package/src/features/org-units/components/CreateOrgUnitDrawer/CreateOrgUnitDrawer.tsx +4 -2
- package/src/features/org-units/components/DeleteOrgUnitDrawer/DeleteOrgUnitDrawer.tsx +6 -2
- package/src/features/org-units/components/OrgUnitBreadcrumb/OrgUnitBreadcrumbLink.tsx +3 -4
- package/src/features/org-units/components/OrgUnitDetailsNavbar/OrgUnitDetailsNavbar.tsx +82 -3
- package/src/features/org-units/components/OrgUnitHeaderNav/OrgUnitHeaderNav.tsx +13 -2
- package/src/features/org-units/components/OrgUnitsHierarchyTree/OrgUnitsHierarchyTree.tsx +18 -10
- package/src/features/org-units/components/UpdateOrgUnitDrawer/UpdateOrgUnitDrawer.tsx +4 -2
- package/src/features/org-units/hooks/useOrgUnitByUser.ts +2 -0
- package/src/features/shared/components/BasicCard/BasicCard.tsx +6 -3
- package/src/features/shared/components/BasicCard/BasicCardLine.tsx +3 -5
- package/src/features/shared/components/BuyerPortalProvider/BuyerPortalProvider.tsx +2 -0
- package/src/features/shared/components/HeaderInside/HeaderInside.tsx +2 -5
- package/src/features/shared/components/InternalTopbar/InternalTopbar.tsx +3 -4
- package/src/features/shared/components/PageLoadingIndicator/PageLoadingIndicator.tsx +129 -0
- package/src/features/shared/components/PageLoadingIndicator/page-loading-indicator.scss +49 -0
- package/src/features/shared/components/Table/TableRow/TableRow.tsx +2 -5
- package/src/features/shared/components/VerticalNav/VerticalNavLink.tsx +11 -10
- package/src/features/shared/hooks/index.ts +2 -0
- package/src/features/shared/hooks/useDisplayedOrgUnitName.ts +12 -0
- package/src/features/shared/hooks/useDrawerProps.ts +17 -4
- package/src/features/shared/hooks/usePageItems.ts +7 -1
- package/src/features/shared/hooks/usePageLoading.ts +11 -0
- package/src/features/shared/hooks/useQuery.ts +62 -20
- package/src/features/shared/layouts/GlobalLayout/global-layout.scss +1 -0
- package/src/features/shared/layouts/LoadingTabsLayout/LoadingTabsLayout.tsx +5 -2
- package/src/features/shared/layouts/SumaPageLayout/SumaSidebarDrawer.tsx +20 -2
- package/src/features/shared/layouts/SumaPageLayout/suma-sidebar-drawer.scss +14 -0
- package/src/features/shared/utils/__tests__/getRouteKey.test.ts +29 -0
- package/src/features/shared/utils/constants.ts +1 -1
- package/src/features/shared/utils/getRouteKey.ts +4 -0
- package/src/features/shared/utils/pageLoading/__tests__/keptSidebarDrawer.test.ts +170 -0
- package/src/features/shared/utils/pageLoading/__tests__/navigationLevel.test.ts +107 -0
- package/src/features/shared/utils/pageLoading/__tests__/orgUnitNames.test.ts +60 -0
- package/src/features/shared/utils/pageLoading/__tests__/pageLoadingStore.test.ts +266 -0
- package/src/features/shared/utils/pageLoading/keptSidebarDrawer.ts +133 -0
- package/src/features/shared/utils/pageLoading/navigationLevel.ts +89 -0
- package/src/features/shared/utils/pageLoading/orgUnitNames.ts +49 -0
- package/src/features/shared/utils/pageLoading/pageLoadingStore.ts +277 -0
- package/src/features/shared/utils/withProviders.tsx +5 -1
- package/src/features/shared/utils/withRouteKey.ts +23 -0
- package/src/features/contracts/components/ContractSwitchOverlay/ContractSwitchOverlay.tsx +0 -47
- package/src/features/contracts/components/ContractSwitchOverlay/contract-switch-overlay.scss +0 -7
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
KEPT_SIDEBAR_DRAWER_MAX_AGE,
|
|
5
|
+
KEPT_SIDEBAR_DRAWER_STORAGE_KEY,
|
|
6
|
+
forgetSidebarDrawerKeptAcrossLoad,
|
|
7
|
+
isFullLoadEnd,
|
|
8
|
+
isNavigationStart,
|
|
9
|
+
isSidebarDrawerKept,
|
|
10
|
+
keepSidebarDrawer,
|
|
11
|
+
releaseSidebarDrawer,
|
|
12
|
+
takeSidebarDrawerKeptAcrossLoad,
|
|
13
|
+
} from "../keptSidebarDrawer";
|
|
14
|
+
|
|
15
|
+
const BASE = "/pvt/organization-account";
|
|
16
|
+
const PROFILE = `${BASE}/profile/u1/c1`;
|
|
17
|
+
const idle = { phase: "idle", targetPath: null, fullLoad: false } as const;
|
|
18
|
+
const toRoles = {
|
|
19
|
+
phase: "navigating",
|
|
20
|
+
targetPath: `${BASE}/roles/u1`,
|
|
21
|
+
fullLoad: false,
|
|
22
|
+
} as const;
|
|
23
|
+
const toUsers = {
|
|
24
|
+
phase: "navigating",
|
|
25
|
+
targetPath: `${BASE}/users/u1`,
|
|
26
|
+
fullLoad: false,
|
|
27
|
+
} as const;
|
|
28
|
+
const toProfile = {
|
|
29
|
+
phase: "navigating",
|
|
30
|
+
targetPath: PROFILE,
|
|
31
|
+
fullLoad: true,
|
|
32
|
+
} as const;
|
|
33
|
+
|
|
34
|
+
const createStorage = () => {
|
|
35
|
+
const items = new Map<string, string>();
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
getItem: (key: string) => items.get(key) ?? null,
|
|
39
|
+
setItem: (key: string, value: string) => void items.set(key, value),
|
|
40
|
+
removeItem: (key: string) => void items.delete(key),
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
describe("keptSidebarDrawer", () => {
|
|
45
|
+
let storage: ReturnType<typeof createStorage>;
|
|
46
|
+
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
storage = createStorage();
|
|
49
|
+
vi.stubGlobal("window", { sessionStorage: storage });
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterEach(() => {
|
|
53
|
+
releaseSidebarDrawer();
|
|
54
|
+
vi.unstubAllGlobals();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("keeps the drawer from when it's opened until it's released", () => {
|
|
58
|
+
expect(isSidebarDrawerKept()).toBe(false);
|
|
59
|
+
|
|
60
|
+
keepSidebarDrawer();
|
|
61
|
+
expect(isSidebarDrawerKept()).toBe(true);
|
|
62
|
+
|
|
63
|
+
releaseSidebarDrawer();
|
|
64
|
+
expect(isSidebarDrawerKept()).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("keeps a drawer opened during a full load for the page it opens", () => {
|
|
68
|
+
keepSidebarDrawer(toProfile, 1000);
|
|
69
|
+
|
|
70
|
+
expect(takeSidebarDrawerKeptAcrossLoad(PROFILE, 2000)).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("compares the page without its locale or query", () => {
|
|
74
|
+
keepSidebarDrawer({ ...toProfile, targetPath: `${PROFILE}?tab=1` }, 1000);
|
|
75
|
+
|
|
76
|
+
expect(takeSidebarDrawerKeptAcrossLoad(`/it-IT${PROFILE}`, 2000)).toBe(
|
|
77
|
+
true
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("restores it once: reloading the page doesn't", () => {
|
|
82
|
+
keepSidebarDrawer(toProfile, 1000);
|
|
83
|
+
takeSidebarDrawerKeptAcrossLoad(PROFILE, 2000);
|
|
84
|
+
|
|
85
|
+
expect(takeSidebarDrawerKeptAcrossLoad(PROFILE, 3000)).toBe(false);
|
|
86
|
+
expect(storage.getItem(KEPT_SIDEBAR_DRAWER_STORAGE_KEY)).toBeNull();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("doesn't keep it across loads outside a full load", () => {
|
|
90
|
+
keepSidebarDrawer(undefined, 1000);
|
|
91
|
+
keepSidebarDrawer(idle, 1000);
|
|
92
|
+
keepSidebarDrawer(toRoles, 1000);
|
|
93
|
+
keepSidebarDrawer({ ...toProfile, phase: "mounting" }, 1000);
|
|
94
|
+
|
|
95
|
+
expect(storage.getItem(KEPT_SIDEBAR_DRAWER_STORAGE_KEY)).toBeNull();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("doesn't restore it on another page or after too long", () => {
|
|
99
|
+
keepSidebarDrawer(toProfile, 1000);
|
|
100
|
+
expect(takeSidebarDrawerKeptAcrossLoad(`${BASE}/roles/u1`, 2000)).toBe(
|
|
101
|
+
false
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
keepSidebarDrawer(toProfile, 1000);
|
|
105
|
+
expect(
|
|
106
|
+
takeSidebarDrawerKeptAcrossLoad(
|
|
107
|
+
PROFILE,
|
|
108
|
+
1000 + KEPT_SIDEBAR_DRAWER_MAX_AGE + 1
|
|
109
|
+
)
|
|
110
|
+
).toBe(false);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("forgets it when the buyer closes it or a navigation starts", () => {
|
|
114
|
+
keepSidebarDrawer(toProfile, 1000);
|
|
115
|
+
releaseSidebarDrawer();
|
|
116
|
+
|
|
117
|
+
expect(takeSidebarDrawerKeptAcrossLoad(PROFILE, 2000)).toBe(false);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("forgets it for the next page only when the full load ends", () => {
|
|
121
|
+
keepSidebarDrawer(toProfile, 1000);
|
|
122
|
+
forgetSidebarDrawerKeptAcrossLoad();
|
|
123
|
+
|
|
124
|
+
expect(isSidebarDrawerKept()).toBe(true);
|
|
125
|
+
expect(takeSidebarDrawerKeptAcrossLoad(PROFILE, 2000)).toBe(false);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("ignores a broken or missing storage", () => {
|
|
129
|
+
storage.setItem(KEPT_SIDEBAR_DRAWER_STORAGE_KEY, "{");
|
|
130
|
+
expect(takeSidebarDrawerKeptAcrossLoad(PROFILE, 2000)).toBe(false);
|
|
131
|
+
|
|
132
|
+
vi.stubGlobal("window", {
|
|
133
|
+
get sessionStorage() {
|
|
134
|
+
throw new Error("blocked");
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
expect(() => keepSidebarDrawer(toProfile, 1000)).not.toThrow();
|
|
138
|
+
expect(takeSidebarDrawerKeptAcrossLoad(PROFILE, 2000)).toBe(false);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe("isNavigationStart", () => {
|
|
143
|
+
it("is a start when a navigation begins or changes its destination", () => {
|
|
144
|
+
expect(isNavigationStart(idle, toRoles)).toBe(true);
|
|
145
|
+
expect(isNavigationStart({ ...toRoles, phase: "mounting" }, toUsers)).toBe(
|
|
146
|
+
true
|
|
147
|
+
);
|
|
148
|
+
expect(isNavigationStart(toRoles, toUsers)).toBe(true);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("isn't a start while the same page loads or shows", () => {
|
|
152
|
+
expect(isNavigationStart(toRoles, toRoles)).toBe(false);
|
|
153
|
+
expect(isNavigationStart(toRoles, { ...toRoles, phase: "mounting" })).toBe(
|
|
154
|
+
false
|
|
155
|
+
);
|
|
156
|
+
expect(isNavigationStart(toRoles, idle)).toBe(false);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe("isFullLoadEnd", () => {
|
|
161
|
+
it("is an end when a full load goes back to idle on the same page", () => {
|
|
162
|
+
expect(isFullLoadEnd(toProfile, idle)).toBe(true);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("isn't an end for other navigations or while loading", () => {
|
|
166
|
+
expect(isFullLoadEnd(toRoles, idle)).toBe(false);
|
|
167
|
+
expect(isFullLoadEnd(toProfile, toProfile)).toBe(false);
|
|
168
|
+
expect(isFullLoadEnd(idle, idle)).toBe(false);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
getLoadingRegion,
|
|
5
|
+
getNavigationLocation,
|
|
6
|
+
removePathPrefix,
|
|
7
|
+
} from "../navigationLevel";
|
|
8
|
+
|
|
9
|
+
const BASE = "/pvt/organization-account";
|
|
10
|
+
|
|
11
|
+
describe("removePathPrefix", () => {
|
|
12
|
+
it("removes the base path and locale before the Organization Account", () => {
|
|
13
|
+
expect(removePathPrefix(`/store/it-IT${BASE}/users/u1?page=2`)).toBe(
|
|
14
|
+
`${BASE}/users/u1?page=2`
|
|
15
|
+
);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("keeps paths without a prefix or outside the Organization Account", () => {
|
|
19
|
+
expect(removePathPrefix(`${BASE}/users/u1`)).toBe(`${BASE}/users/u1`);
|
|
20
|
+
expect(removePathPrefix(`/login?returnUrl=${BASE}/users`)).toBe(
|
|
21
|
+
`/login?returnUrl=${BASE}/users`
|
|
22
|
+
);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("getNavigationLocation", () => {
|
|
27
|
+
it("returns null outside the Organization Account", () => {
|
|
28
|
+
expect(getNavigationLocation("/account/orders")).toBeNull();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("places the unit pages at level 0", () => {
|
|
32
|
+
expect(getNavigationLocation(`${BASE}/users/unit-a?contractId=c1`)).toEqual(
|
|
33
|
+
{ level: 0, page: "users", orgUnitId: "unit-a", contractId: undefined }
|
|
34
|
+
);
|
|
35
|
+
expect(getNavigationLocation(`${BASE}/budgets/unit-a/c1`)?.level).toBe(0);
|
|
36
|
+
expect(
|
|
37
|
+
getNavigationLocation(`${BASE}/buying-policies/unit-a/c1`)?.level
|
|
38
|
+
).toBe(0);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("places the contract pages at level 1", () => {
|
|
42
|
+
expect(getNavigationLocation(`${BASE}/addresses/unit-a/c1`)).toEqual({
|
|
43
|
+
level: 1,
|
|
44
|
+
page: "addresses",
|
|
45
|
+
orgUnitId: "unit-a",
|
|
46
|
+
contractId: "c1",
|
|
47
|
+
});
|
|
48
|
+
expect(getNavigationLocation(`${BASE}/profile/unit-a/c1`)?.level).toBe(1);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("places a budget's details at level 0, like the budgets listing", () => {
|
|
52
|
+
expect(getNavigationLocation(`${BASE}/budgets/unit-a/c1/b1`)?.level).toBe(
|
|
53
|
+
0
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("getLoadingRegion", () => {
|
|
59
|
+
it("loads the content between pages of the unit menu", () => {
|
|
60
|
+
expect(
|
|
61
|
+
getLoadingRegion(`${BASE}/users/unit-a`, `${BASE}/roles/unit-a`)
|
|
62
|
+
).toBe("content");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("loads the content when opening a budget from the listing", () => {
|
|
66
|
+
expect(
|
|
67
|
+
getLoadingRegion(
|
|
68
|
+
`${BASE}/budgets/unit-a/c1`,
|
|
69
|
+
`${BASE}/budgets/unit-a/c1/b1`
|
|
70
|
+
)
|
|
71
|
+
).toBe("content");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("loads the content between pages of the contract menu", () => {
|
|
75
|
+
expect(
|
|
76
|
+
getLoadingRegion(
|
|
77
|
+
`${BASE}/addresses/unit-a/c1`,
|
|
78
|
+
`${BASE}/credit-cards/unit-a/c1`
|
|
79
|
+
)
|
|
80
|
+
).toBe("content");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("loads the body when opening a contract or going back", () => {
|
|
84
|
+
expect(
|
|
85
|
+
getLoadingRegion(`${BASE}/contracts/unit-a`, `${BASE}/profile/unit-a/c1`)
|
|
86
|
+
).toBe("body");
|
|
87
|
+
expect(
|
|
88
|
+
getLoadingRegion(`${BASE}/profile/unit-a/c1`, `${BASE}/contracts/unit-a`)
|
|
89
|
+
).toBe("body");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("loads the body when the unit or the contract changes", () => {
|
|
93
|
+
expect(
|
|
94
|
+
getLoadingRegion(`${BASE}/users/unit-a`, `${BASE}/users/unit-b`)
|
|
95
|
+
).toBe("body");
|
|
96
|
+
expect(
|
|
97
|
+
getLoadingRegion(
|
|
98
|
+
`${BASE}/addresses/unit-a/c1`,
|
|
99
|
+
`${BASE}/addresses/unit-a/c2`
|
|
100
|
+
)
|
|
101
|
+
).toBe("body");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("loads the body when leaving the Organization Account", () => {
|
|
105
|
+
expect(getLoadingRegion(`${BASE}/users/unit-a`, "/")).toBe("body");
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
forgetOrgUnitNames,
|
|
5
|
+
getDisplayedOrgUnitName,
|
|
6
|
+
rememberOrgUnitNames,
|
|
7
|
+
rememberOrgUnitPath,
|
|
8
|
+
} from "../orgUnitNames";
|
|
9
|
+
|
|
10
|
+
const BASE = "/pvt/organization-account";
|
|
11
|
+
const current = { id: "unit-a", name: "Unit A" };
|
|
12
|
+
|
|
13
|
+
describe("getDisplayedOrgUnitName", () => {
|
|
14
|
+
afterEach(forgetOrgUnitNames);
|
|
15
|
+
|
|
16
|
+
it("shows the current unit when idle", () => {
|
|
17
|
+
rememberOrgUnitNames([{ id: "unit-b", name: "Unit B" }]);
|
|
18
|
+
|
|
19
|
+
expect(
|
|
20
|
+
getDisplayedOrgUnitName(current, { phase: "idle", targetPath: null })
|
|
21
|
+
).toBe("Unit A");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("shows the destination unit while navigating to it", () => {
|
|
25
|
+
rememberOrgUnitNames([{ id: "unit-b", name: "Unit B" }]);
|
|
26
|
+
|
|
27
|
+
expect(
|
|
28
|
+
getDisplayedOrgUnitName(current, {
|
|
29
|
+
phase: "navigating",
|
|
30
|
+
targetPath: `${BASE}/contracts/unit-b?contractId=c1`,
|
|
31
|
+
})
|
|
32
|
+
).toBe("Unit B");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("keeps the current unit when the destination is unknown or the same", () => {
|
|
36
|
+
expect(
|
|
37
|
+
getDisplayedOrgUnitName(current, {
|
|
38
|
+
phase: "navigating",
|
|
39
|
+
targetPath: `${BASE}/contracts/unit-c`,
|
|
40
|
+
})
|
|
41
|
+
).toBe("Unit A");
|
|
42
|
+
expect(
|
|
43
|
+
getDisplayedOrgUnitName(current, {
|
|
44
|
+
phase: "navigating",
|
|
45
|
+
targetPath: `${BASE}/users/unit-a`,
|
|
46
|
+
})
|
|
47
|
+
).toBe("Unit A");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("knows the units of a unit's path", () => {
|
|
51
|
+
rememberOrgUnitPath({ ids: "/root/unit-a", names: "Root/Unit A" });
|
|
52
|
+
|
|
53
|
+
expect(
|
|
54
|
+
getDisplayedOrgUnitName(current, {
|
|
55
|
+
phase: "navigating",
|
|
56
|
+
targetPath: `${BASE}/contracts/root`,
|
|
57
|
+
})
|
|
58
|
+
).toBe("Root");
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
REVEAL_TIMEOUT,
|
|
5
|
+
SETTLE_DELAY,
|
|
6
|
+
createPageLoadingStore,
|
|
7
|
+
type PageLoadingStore,
|
|
8
|
+
} from "../pageLoadingStore";
|
|
9
|
+
|
|
10
|
+
const USERS = "/pvt/organization-account/users/unit-a";
|
|
11
|
+
const ROLES = "/pvt/organization-account/roles/unit-a";
|
|
12
|
+
const PROFILE = "/pvt/organization-account/profile/unit-a/c1";
|
|
13
|
+
|
|
14
|
+
type Timer = { at: number; callback: () => void };
|
|
15
|
+
|
|
16
|
+
const createEnvironment = () => {
|
|
17
|
+
let now = 0;
|
|
18
|
+
let sidebarVariant: string | null = "unit";
|
|
19
|
+
const timers = new Set<Timer>();
|
|
20
|
+
|
|
21
|
+
const advance = (ms: number) => {
|
|
22
|
+
const target = now + ms;
|
|
23
|
+
|
|
24
|
+
for (;;) {
|
|
25
|
+
const next = [...timers]
|
|
26
|
+
.filter((timer) => timer.at <= target)
|
|
27
|
+
.sort((a, b) => a.at - b.at)[0];
|
|
28
|
+
|
|
29
|
+
if (!next) break;
|
|
30
|
+
|
|
31
|
+
timers.delete(next);
|
|
32
|
+
now = next.at;
|
|
33
|
+
next.callback();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
now = target;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
advance,
|
|
41
|
+
setSidebarVariant: (variant: string | null) => {
|
|
42
|
+
sidebarVariant = variant;
|
|
43
|
+
},
|
|
44
|
+
env: {
|
|
45
|
+
now: () => now,
|
|
46
|
+
setTimeout: (callback: () => void, delay: number) => {
|
|
47
|
+
const timer = { at: now + delay, callback };
|
|
48
|
+
timers.add(timer);
|
|
49
|
+
return timer;
|
|
50
|
+
},
|
|
51
|
+
clearTimeout: (timer: unknown) => {
|
|
52
|
+
timers.delete(timer as Timer);
|
|
53
|
+
},
|
|
54
|
+
hasShell: () => true,
|
|
55
|
+
getSidebarVariant: () => sidebarVariant,
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
describe("pageLoadingStore", () => {
|
|
61
|
+
let environment: ReturnType<typeof createEnvironment>;
|
|
62
|
+
let store: PageLoadingStore;
|
|
63
|
+
|
|
64
|
+
const navigate = (from: string, to: string) => {
|
|
65
|
+
store.start(to, from);
|
|
66
|
+
store.complete();
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
beforeEach(() => {
|
|
70
|
+
environment = createEnvironment();
|
|
71
|
+
store = createPageLoadingStore(environment.env);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("starts hidden, so a hard load shows the page once it's ready", () => {
|
|
75
|
+
expect(store.getSnapshot()).toMatchObject({
|
|
76
|
+
phase: "mounting",
|
|
77
|
+
region: "body",
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const end = store.track();
|
|
81
|
+
store.init();
|
|
82
|
+
environment.advance(SETTLE_DELAY);
|
|
83
|
+
expect(store.getSnapshot().phase).toBe("mounting");
|
|
84
|
+
|
|
85
|
+
end();
|
|
86
|
+
environment.advance(SETTLE_DELAY);
|
|
87
|
+
expect(store.getSnapshot().phase).toBe("idle");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("after the first page shows", () => {
|
|
91
|
+
beforeEach(() => {
|
|
92
|
+
store.init();
|
|
93
|
+
environment.advance(SETTLE_DELAY);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("keeps the target path and the region while navigating", () => {
|
|
97
|
+
store.start(`${ROLES}?contractId=c1`, USERS);
|
|
98
|
+
|
|
99
|
+
expect(store.getSnapshot()).toMatchObject({
|
|
100
|
+
phase: "navigating",
|
|
101
|
+
region: "content",
|
|
102
|
+
targetPath: `${ROLES}?contractId=c1`,
|
|
103
|
+
fullLoad: false,
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("ignores changes to the query string only", () => {
|
|
108
|
+
store.start(`${USERS}?page=2`, USERS);
|
|
109
|
+
|
|
110
|
+
expect(store.getSnapshot().phase).toBe("idle");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("ignores the router URL's locale prefix", () => {
|
|
114
|
+
store.start(`/it-IT${USERS}?page=2`, USERS);
|
|
115
|
+
expect(store.getSnapshot().phase).toBe("idle");
|
|
116
|
+
|
|
117
|
+
store.start(`/it-IT${ROLES}`, USERS);
|
|
118
|
+
expect(store.getSnapshot()).toMatchObject({
|
|
119
|
+
phase: "navigating",
|
|
120
|
+
targetPath: ROLES,
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("waits for the requests of the next page", () => {
|
|
125
|
+
store.start(ROLES, USERS);
|
|
126
|
+
const end = store.track();
|
|
127
|
+
store.complete();
|
|
128
|
+
|
|
129
|
+
environment.advance(SETTLE_DELAY * 4);
|
|
130
|
+
expect(store.getSnapshot().phase).toBe("mounting");
|
|
131
|
+
|
|
132
|
+
end();
|
|
133
|
+
environment.advance(SETTLE_DELAY);
|
|
134
|
+
expect(store.getSnapshot()).toMatchObject({
|
|
135
|
+
phase: "idle",
|
|
136
|
+
targetPath: null,
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("shows the page after a timeout", () => {
|
|
141
|
+
store.start(ROLES, USERS);
|
|
142
|
+
store.track();
|
|
143
|
+
store.complete();
|
|
144
|
+
|
|
145
|
+
environment.advance(REVEAL_TIMEOUT);
|
|
146
|
+
expect(store.getSnapshot().phase).toBe("idle");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("doesn't count requests of a previous navigation", () => {
|
|
150
|
+
store.start(ROLES, USERS);
|
|
151
|
+
const staleEnd = store.track();
|
|
152
|
+
store.start(PROFILE, USERS);
|
|
153
|
+
const end = store.track();
|
|
154
|
+
store.complete();
|
|
155
|
+
|
|
156
|
+
staleEnd();
|
|
157
|
+
environment.advance(SETTLE_DELAY);
|
|
158
|
+
expect(store.getSnapshot().phase).toBe("mounting");
|
|
159
|
+
|
|
160
|
+
end();
|
|
161
|
+
end();
|
|
162
|
+
environment.advance(SETTLE_DELAY);
|
|
163
|
+
expect(store.getSnapshot().phase).toBe("idle");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("keeps the bar's start on a click while loading", () => {
|
|
167
|
+
environment.advance(100);
|
|
168
|
+
store.start(ROLES, USERS);
|
|
169
|
+
environment.advance(100);
|
|
170
|
+
store.start(PROFILE, USERS);
|
|
171
|
+
|
|
172
|
+
expect(store.getSnapshot()).toMatchObject({
|
|
173
|
+
startedAt: 100 + SETTLE_DELAY,
|
|
174
|
+
region: "body",
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("doesn't track requests made after the page shows", () => {
|
|
179
|
+
const end = store.track();
|
|
180
|
+
navigate(USERS, ROLES);
|
|
181
|
+
end();
|
|
182
|
+
environment.advance(SETTLE_DELAY);
|
|
183
|
+
|
|
184
|
+
expect(store.getSnapshot().phase).toBe("idle");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("shows the current page again when the navigation fails", () => {
|
|
188
|
+
store.start(ROLES, USERS);
|
|
189
|
+
store.fail(Object.assign(new Error("failed"), { cancelled: false }));
|
|
190
|
+
|
|
191
|
+
expect(store.getSnapshot().phase).toBe("idle");
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("keeps loading when the navigation is cancelled by another one", () => {
|
|
195
|
+
store.start(ROLES, USERS);
|
|
196
|
+
store.fail({ cancelled: true });
|
|
197
|
+
|
|
198
|
+
expect(store.getSnapshot().phase).toBe("navigating");
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("hides the body during a full page load", () => {
|
|
202
|
+
store.startFullLoad(PROFILE);
|
|
203
|
+
|
|
204
|
+
expect(store.getSnapshot()).toMatchObject({
|
|
205
|
+
phase: "navigating",
|
|
206
|
+
region: "body",
|
|
207
|
+
targetPath: PROFILE,
|
|
208
|
+
fullLoad: true,
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("shows the current page again when a full page load fails", () => {
|
|
213
|
+
store.startFullLoad(PROFILE);
|
|
214
|
+
store.fail();
|
|
215
|
+
|
|
216
|
+
expect(store.getSnapshot()).toMatchObject({
|
|
217
|
+
phase: "idle",
|
|
218
|
+
fullLoad: false,
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("hides the body when the next page has another sidebar", () => {
|
|
223
|
+
store.start(ROLES, USERS);
|
|
224
|
+
environment.setSidebarVariant("contract");
|
|
225
|
+
store.complete();
|
|
226
|
+
|
|
227
|
+
expect(store.getSnapshot().region).toBe("body");
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("listens to the router once, ignoring shallow changes", () => {
|
|
231
|
+
const handlers: Record<string, (...args: never[]) => void> = {};
|
|
232
|
+
const events = {
|
|
233
|
+
on: (event: string, handler: (...args: never[]) => void) => {
|
|
234
|
+
handlers[event] = handler;
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
const call = (event: string, ...args: unknown[]) =>
|
|
238
|
+
(handlers[event] as (...args: unknown[]) => void)(...args);
|
|
239
|
+
|
|
240
|
+
store.attach(events, () => USERS);
|
|
241
|
+
store.attach({ on: () => {} }, () => USERS);
|
|
242
|
+
|
|
243
|
+
call("routeChangeStart", ROLES, { shallow: true });
|
|
244
|
+
expect(store.getSnapshot().phase).toBe("idle");
|
|
245
|
+
|
|
246
|
+
call("routeChangeStart", ROLES, { shallow: false });
|
|
247
|
+
expect(store.getSnapshot().phase).toBe("navigating");
|
|
248
|
+
|
|
249
|
+
call("routeChangeComplete", ROLES);
|
|
250
|
+
expect(store.getSnapshot().phase).toBe("mounting");
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("notifies its subscribers", () => {
|
|
254
|
+
let calls = 0;
|
|
255
|
+
const unsubscribe = store.subscribe(() => {
|
|
256
|
+
calls += 1;
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
store.start(ROLES, USERS);
|
|
260
|
+
unsubscribe();
|
|
261
|
+
store.complete();
|
|
262
|
+
|
|
263
|
+
expect(calls).toBe(1);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
});
|