@webiny/app 6.3.0 → 6.4.0-beta.1
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/App.js +70 -77
- package/App.js.map +1 -1
- package/AppContainer.js +3 -6
- package/AppContainer.js.map +1 -1
- package/apollo-client/InMemoryCache.js +11 -13
- package/apollo-client/InMemoryCache.js.map +1 -1
- package/apollo-client/IntrospectionFragmentMatcher.js +26 -38
- package/apollo-client/IntrospectionFragmentMatcher.js.map +1 -1
- package/components/Image.js +11 -17
- package/components/Image.js.map +1 -1
- package/components/index.js +0 -2
- package/config/RouterConfig/Route.js +23 -28
- package/config/RouterConfig/Route.js.map +1 -1
- package/config/RouterConfig.js +11 -10
- package/config/RouterConfig.js.map +1 -1
- package/config.js +19 -20
- package/config.js.map +1 -1
- package/contexts/Ui/index.js +26 -28
- package/contexts/Ui/index.js.map +1 -1
- package/core/Plugin.js +7 -9
- package/core/Plugin.js.map +1 -1
- package/core/Plugins.js +15 -34
- package/core/Plugins.js.map +1 -1
- package/core/Provider.js +5 -13
- package/core/Provider.js.map +1 -1
- package/core/createProvider.js +3 -6
- package/core/createProvider.js.map +1 -1
- package/core/createProviderPlugin.js +8 -13
- package/core/createProviderPlugin.js.map +1 -1
- package/errors/AuthenticationErrorEvent.js +8 -5
- package/errors/AuthenticationErrorEvent.js.map +1 -1
- package/errors/NetworkErrorEvent.js +8 -5
- package/errors/NetworkErrorEvent.js.map +1 -1
- package/errors/abstractions.js +3 -2
- package/errors/abstractions.js.map +1 -1
- package/errors/index.js +1 -4
- package/exports/admin/env-config.js +0 -2
- package/exports/admin/graphql-client.js +0 -2
- package/exports/admin/local-storage.js +0 -2
- package/exports/admin/router.js +0 -2
- package/exports/admin/security.js +0 -2
- package/exports/admin.js +0 -2
- package/features/envConfig/EnvConfig.js +12 -13
- package/features/envConfig/EnvConfig.js.map +1 -1
- package/features/envConfig/abstractions.js +2 -1
- package/features/envConfig/abstractions.js.map +1 -1
- package/features/envConfig/feature.js +9 -8
- package/features/envConfig/feature.js.map +1 -1
- package/features/envConfig/index.js +0 -2
- package/features/eventPublisher/EventPublisher.js +13 -21
- package/features/eventPublisher/EventPublisher.js.map +1 -1
- package/features/eventPublisher/abstractions.js +7 -9
- package/features/eventPublisher/abstractions.js.map +1 -1
- package/features/eventPublisher/feature.js +13 -12
- package/features/eventPublisher/feature.js.map +1 -1
- package/features/eventPublisher/index.js +1 -3
- package/features/graphqlClient/AuthenticationErrorPublishing.js +26 -26
- package/features/graphqlClient/AuthenticationErrorPublishing.js.map +1 -1
- package/features/graphqlClient/BatchingGraphQLClient.js +104 -149
- package/features/graphqlClient/BatchingGraphQLClient.js.map +1 -1
- package/features/graphqlClient/FetchGraphQLClient.js +41 -48
- package/features/graphqlClient/FetchGraphQLClient.js.map +1 -1
- package/features/graphqlClient/NetworkErrorPublishing.js +35 -42
- package/features/graphqlClient/NetworkErrorPublishing.js.map +1 -1
- package/features/graphqlClient/RequestValue.js +42 -41
- package/features/graphqlClient/RequestValue.js.map +1 -1
- package/features/graphqlClient/RetryGraphQLClient.js +36 -45
- package/features/graphqlClient/RetryGraphQLClient.js.map +1 -1
- package/features/graphqlClient/__tests__/GraphQLClient.test.js +383 -356
- package/features/graphqlClient/__tests__/GraphQLClient.test.js.map +1 -1
- package/features/graphqlClient/abstractions.js +2 -1
- package/features/graphqlClient/abstractions.js.map +1 -1
- package/features/graphqlClient/feature.js +14 -20
- package/features/graphqlClient/feature.js.map +1 -1
- package/features/graphqlClient/index.js +0 -2
- package/features/graphqlClient/types.js +0 -3
- package/features/localStorage/BrowserLocalStorageGateway.js +56 -63
- package/features/localStorage/BrowserLocalStorageGateway.js.map +1 -1
- package/features/localStorage/LocalStorage.js +27 -24
- package/features/localStorage/LocalStorage.js.map +1 -1
- package/features/localStorage/LocalStorageRepository.js +75 -88
- package/features/localStorage/LocalStorageRepository.js.map +1 -1
- package/features/localStorage/abstractions.js +5 -16
- package/features/localStorage/abstractions.js.map +1 -1
- package/features/localStorage/feature.js +17 -28
- package/features/localStorage/feature.js.map +1 -1
- package/features/localStorage/index.js +1 -3
- package/features/mainGraphQLClient/MainGraphQLClient.js +18 -14
- package/features/mainGraphQLClient/MainGraphQLClient.js.map +1 -1
- package/features/mainGraphQLClient/abstractions.js +2 -1
- package/features/mainGraphQLClient/abstractions.js.map +1 -1
- package/features/mainGraphQLClient/feature.js +11 -10
- package/features/mainGraphQLClient/feature.js.map +1 -1
- package/features/mainGraphQLClient/index.js +0 -2
- package/features/router/HistoryRouterGateway.js +75 -105
- package/features/router/HistoryRouterGateway.js.map +1 -1
- package/features/router/HistoryRouterGateway.test.js +184 -193
- package/features/router/HistoryRouterGateway.test.js.map +1 -1
- package/features/router/Route.js +38 -54
- package/features/router/Route.js.map +1 -1
- package/features/router/RouteUrl.js +84 -217
- package/features/router/RouteUrl.js.map +1 -1
- package/features/router/Router.js +67 -124
- package/features/router/Router.js.map +1 -1
- package/features/router/RouterPresenter.js +44 -49
- package/features/router/RouterPresenter.js.map +1 -1
- package/features/router/RouterRepository.js +103 -111
- package/features/router/RouterRepository.js.map +1 -1
- package/features/router/RouterRepository.test.js +119 -150
- package/features/router/RouterRepository.test.js.map +1 -1
- package/features/router/abstractions.js +4 -14
- package/features/router/abstractions.js.map +1 -1
- package/features/router/feature.js +13 -12
- package/features/router/feature.js.map +1 -1
- package/features/router/index.js +1 -3
- package/helpers/InterfaceGenerator/date.js +0 -3
- package/helpers/InterfaceGenerator/id.js +0 -3
- package/helpers/InterfaceGenerator/identity.js +0 -3
- package/helpers/InterfaceGenerator/index.js +0 -3
- package/helpers/InterfaceGenerator/numeric.js +0 -3
- package/helpers/InterfaceGenerator/truthful.js +0 -3
- package/hooks/useAutocomplete/index.js +0 -2
- package/hooks/useAutocomplete/useAutocomplete.js +12 -15
- package/hooks/useAutocomplete/useAutocomplete.js.map +1 -1
- package/hooks/useDataList/functions/getData.js +2 -1
- package/hooks/useDataList/functions/getData.js.map +1 -1
- package/hooks/useDataList/functions/getError.js +2 -1
- package/hooks/useDataList/functions/getError.js.map +1 -1
- package/hooks/useDataList/functions/getMeta.js +2 -1
- package/hooks/useDataList/functions/getMeta.js.map +1 -1
- package/hooks/useDataList/functions/index.js +0 -2
- package/hooks/useDataList/functions/searchDataByKey.js +9 -14
- package/hooks/useDataList/functions/searchDataByKey.js.map +1 -1
- package/hooks/useDataList/index.js +0 -2
- package/hooks/useDataList/useDataList.js +131 -152
- package/hooks/useDataList/useDataList.js.map +1 -1
- package/hooks/useDataList/utils/index.js +0 -2
- package/hooks/useDataList/utils/prepareLoadListParams.js +25 -34
- package/hooks/useDataList/utils/prepareLoadListParams.js.map +1 -1
- package/hooks/useHandler.js +12 -16
- package/hooks/useHandler.js.map +1 -1
- package/hooks/useHandlers.js +18 -20
- package/hooks/useHandlers.js.map +1 -1
- package/hooks/useRegisterLegacyPlugin.js +7 -6
- package/hooks/useRegisterLegacyPlugin.js.map +1 -1
- package/hooks/useUi.js +2 -3
- package/hooks/useUi.js.map +1 -1
- package/i18n/i18n.js +8 -4
- package/i18n/i18n.js.map +1 -1
- package/i18n/index.js +0 -2
- package/index.js +6 -11
- package/package.json +14 -16
- package/plugins/AddQuerySelectionPlugin.js +36 -61
- package/plugins/AddQuerySelectionPlugin.js.map +1 -1
- package/plugins/ApolloCacheObjectIdPlugin.js +12 -11
- package/plugins/ApolloCacheObjectIdPlugin.js.map +1 -1
- package/plugins/ApolloDynamicLink.js +20 -22
- package/plugins/ApolloDynamicLink.js.map +1 -1
- package/plugins/ApolloLinkPlugin.js +16 -15
- package/plugins/ApolloLinkPlugin.js.map +1 -1
- package/plugins/ConsoleLinkPlugin.js +19 -21
- package/plugins/ConsoleLinkPlugin.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/ErrorOverlay.js +39 -45
- package/plugins/NetworkErrorLinkPlugin/ErrorOverlay.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/GqlErrorOverlay.js +18 -22
- package/plugins/NetworkErrorLinkPlugin/GqlErrorOverlay.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/LocalAwsLambdaTimeoutMessage.js +21 -29
- package/plugins/NetworkErrorLinkPlugin/LocalAwsLambdaTimeoutMessage.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/StyledComponents.js +6 -15
- package/plugins/NetworkErrorLinkPlugin/StyledComponents.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/Typography.js +9 -14
- package/plugins/NetworkErrorLinkPlugin/Typography.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin/assets/close_24px.js +19 -0
- package/plugins/NetworkErrorLinkPlugin/assets/close_24px.js.map +1 -0
- package/plugins/NetworkErrorLinkPlugin/createErrorOverlay.js +11 -18
- package/plugins/NetworkErrorLinkPlugin/createErrorOverlay.js.map +1 -1
- package/plugins/NetworkErrorLinkPlugin.js +41 -58
- package/plugins/NetworkErrorLinkPlugin.js.map +1 -1
- package/plugins/OmitTypenameLinkPlugin.js +9 -14
- package/plugins/OmitTypenameLinkPlugin.js.map +1 -1
- package/plugins/TenantHeaderLinkPlugin.js +23 -32
- package/plugins/TenantHeaderLinkPlugin.js.map +1 -1
- package/plugins/components/Image.js +8 -15
- package/plugins/components/Image.js.map +1 -1
- package/plugins/image.js +94 -133
- package/plugins/image.js.map +1 -1
- package/plugins/index.js +35 -59
- package/plugins/index.js.map +1 -1
- package/presentation/envConfig/useEnvConfig.js +4 -8
- package/presentation/envConfig/useEnvConfig.js.map +1 -1
- package/presentation/localStorage/index.js +0 -2
- package/presentation/localStorage/useLocalStorage.js +10 -16
- package/presentation/localStorage/useLocalStorage.js.map +1 -1
- package/presentation/localStorage/useLocalStorageValue.js +15 -13
- package/presentation/localStorage/useLocalStorageValue.js.map +1 -1
- package/presentation/localStorage/useLocalStorageValues.js +23 -30
- package/presentation/localStorage/useLocalStorageValues.js.map +1 -1
- package/presentation/router/RouteElementRegistry.js +22 -25
- package/presentation/router/RouteElementRegistry.js.map +1 -1
- package/presentation/router/abstractions.js +2 -1
- package/presentation/router/abstractions.js.map +1 -1
- package/presentation/router/components/Redirect.js +8 -10
- package/presentation/router/components/Redirect.js.map +1 -1
- package/presentation/router/components/RouteContent.js +14 -17
- package/presentation/router/components/RouteContent.js.map +1 -1
- package/presentation/router/components/RouteLink.js +11 -16
- package/presentation/router/components/RouteLink.js.map +1 -1
- package/presentation/router/components/SimpleLink.js +18 -24
- package/presentation/router/components/SimpleLink.js.map +1 -1
- package/presentation/router/hooks/useRoute.js +15 -23
- package/presentation/router/hooks/useRoute.js.map +1 -1
- package/presentation/router/hooks/useRouter.js +22 -23
- package/presentation/router/hooks/useRouter.js.map +1 -1
- package/presentation/router/index.js +0 -2
- package/presentation/router/types.js +0 -3
- package/renderApp.js +7 -6
- package/renderApp.js.map +1 -1
- package/router.js +0 -3
- package/rslib-runtime.js +14 -0
- package/rslib-runtime.js.map +1 -0
- package/shared/di/DiContainerProvider.js +8 -12
- package/shared/di/DiContainerProvider.js.map +1 -1
- package/shared/di/createFeature.js +7 -8
- package/shared/di/createFeature.js.map +1 -1
- package/shared/di/useFeature.js +7 -3
- package/shared/di/useFeature.js.map +1 -1
- package/static/svg/close_24px.33adaadc.svg +1 -0
- package/types.js +0 -3
- package/utils/createGenericContext.js +17 -21
- package/utils/createGenericContext.js.map +1 -1
- package/utils/createHashing.js +13 -16
- package/utils/createHashing.js.map +1 -1
- package/utils/index.js +0 -2
- package/utils/legacyPluginToReactComponent.js +11 -10
- package/utils/legacyPluginToReactComponent.js.map +1 -1
- package/components/index.js.map +0 -1
- package/errors/index.js.map +0 -1
- package/exports/admin/env-config.js.map +0 -1
- package/exports/admin/graphql-client.js.map +0 -1
- package/exports/admin/local-storage.js.map +0 -1
- package/exports/admin/router.js.map +0 -1
- package/exports/admin/security.js.map +0 -1
- package/exports/admin.js.map +0 -1
- package/features/envConfig/index.js.map +0 -1
- package/features/eventPublisher/index.js.map +0 -1
- package/features/graphqlClient/index.js.map +0 -1
- package/features/graphqlClient/types.js.map +0 -1
- package/features/localStorage/index.js.map +0 -1
- package/features/mainGraphQLClient/index.js.map +0 -1
- package/features/router/index.js.map +0 -1
- package/helpers/InterfaceGenerator/date.js.map +0 -1
- package/helpers/InterfaceGenerator/id.js.map +0 -1
- package/helpers/InterfaceGenerator/identity.js.map +0 -1
- package/helpers/InterfaceGenerator/index.js.map +0 -1
- package/helpers/InterfaceGenerator/numeric.js.map +0 -1
- package/helpers/InterfaceGenerator/truthful.js.map +0 -1
- package/hooks/useAutocomplete/index.js.map +0 -1
- package/hooks/useDataList/functions/index.js.map +0 -1
- package/hooks/useDataList/index.js.map +0 -1
- package/hooks/useDataList/utils/index.js.map +0 -1
- package/i18n/index.js.map +0 -1
- package/index.js.map +0 -1
- package/presentation/localStorage/index.js.map +0 -1
- package/presentation/router/index.js.map +0 -1
- package/presentation/router/types.js.map +0 -1
- package/react-butterfiles/Files.d.ts +0 -69
- package/react-butterfiles/Files.js +0 -227
- package/react-butterfiles/Files.js.map +0 -1
- package/react-butterfiles/file.todo +0 -1
- package/react-butterfiles/index.d.ts +0 -2
- package/react-butterfiles/index.js +0 -4
- package/react-butterfiles/index.js.map +0 -1
- package/react-butterfiles/utils/generateId.d.ts +0 -1
- package/react-butterfiles/utils/generateId.js +0 -5
- package/react-butterfiles/utils/generateId.js.map +0 -1
- package/react-butterfiles/utils/readFileContent.d.ts +0 -1
- package/react-butterfiles/utils/readFileContent.js +0 -15
- package/react-butterfiles/utils/readFileContent.js.map +0 -1
- package/router.js.map +0 -1
- package/types.js.map +0 -1
- package/utils/index.js.map +0 -1
|
@@ -1,204 +1,195 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
2
|
import { createMemoryHistory } from "history";
|
|
3
3
|
import { HistoryRouterGateway } from "./HistoryRouterGateway.js";
|
|
4
4
|
import { RouteUrl } from "./RouteUrl.js";
|
|
5
|
-
const wait = ()
|
|
6
|
-
describe("Router Gateway", ()
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
});
|
|
10
|
-
it("should execute onMatch when history changes", async () => {
|
|
11
|
-
const spyHome = vi.fn();
|
|
12
|
-
const spyLogin = vi.fn();
|
|
13
|
-
const spyDynamic = vi.fn();
|
|
14
|
-
|
|
15
|
-
// Create `history` and set the initial pathname to an unknown route
|
|
16
|
-
const history = createMemoryHistory();
|
|
17
|
-
history.replace("/__unknown__");
|
|
18
|
-
const gateway = new HistoryRouterGateway(history, "");
|
|
19
|
-
gateway.setRoutes([{
|
|
20
|
-
name: "home",
|
|
21
|
-
path: "/",
|
|
22
|
-
onMatch: spyHome
|
|
23
|
-
}, {
|
|
24
|
-
name: "login",
|
|
25
|
-
path: "/login",
|
|
26
|
-
onMatch: spyLogin
|
|
27
|
-
}, {
|
|
28
|
-
name: "test",
|
|
29
|
-
path: "/dynamic-route/:name",
|
|
30
|
-
onMatch: spyDynamic
|
|
31
|
-
}]);
|
|
32
|
-
history.push("/login");
|
|
33
|
-
await wait();
|
|
34
|
-
history.push("/");
|
|
35
|
-
await wait();
|
|
36
|
-
history.push("/dynamic-route/cars");
|
|
37
|
-
await wait();
|
|
38
|
-
history.push("/login");
|
|
39
|
-
await wait();
|
|
40
|
-
history.push("/dynamic-route/blogs");
|
|
41
|
-
await wait();
|
|
42
|
-
history.push("/dynamic-route/blogs?search=123");
|
|
43
|
-
await wait();
|
|
44
|
-
expect(spyLogin).toHaveBeenCalledTimes(2);
|
|
45
|
-
expect(spyLogin).toHaveBeenNthCalledWith(1, {
|
|
46
|
-
name: "login",
|
|
47
|
-
path: "/login",
|
|
48
|
-
pathname: "/login",
|
|
49
|
-
params: {}
|
|
50
|
-
});
|
|
51
|
-
expect(spyLogin).toHaveBeenNthCalledWith(2, {
|
|
52
|
-
name: "login",
|
|
53
|
-
path: "/login",
|
|
54
|
-
pathname: "/login",
|
|
55
|
-
params: {}
|
|
5
|
+
const wait = ()=>new Promise((resolve)=>setTimeout(resolve, 10));
|
|
6
|
+
describe("Router Gateway", ()=>{
|
|
7
|
+
beforeEach(()=>{
|
|
8
|
+
vi.clearAllMocks();
|
|
56
9
|
});
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
10
|
+
it("should execute onMatch when history changes", async ()=>{
|
|
11
|
+
const spyHome = vi.fn();
|
|
12
|
+
const spyLogin = vi.fn();
|
|
13
|
+
const spyDynamic = vi.fn();
|
|
14
|
+
const history = createMemoryHistory();
|
|
15
|
+
history.replace("/__unknown__");
|
|
16
|
+
const gateway = new HistoryRouterGateway(history, "");
|
|
17
|
+
gateway.setRoutes([
|
|
18
|
+
{
|
|
19
|
+
name: "home",
|
|
20
|
+
path: "/",
|
|
21
|
+
onMatch: spyHome
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "login",
|
|
25
|
+
path: "/login",
|
|
26
|
+
onMatch: spyLogin
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: "test",
|
|
30
|
+
path: "/dynamic-route/:name",
|
|
31
|
+
onMatch: spyDynamic
|
|
32
|
+
}
|
|
33
|
+
]);
|
|
34
|
+
history.push("/login");
|
|
35
|
+
await wait();
|
|
36
|
+
history.push("/");
|
|
37
|
+
await wait();
|
|
38
|
+
history.push("/dynamic-route/cars");
|
|
39
|
+
await wait();
|
|
40
|
+
history.push("/login");
|
|
41
|
+
await wait();
|
|
42
|
+
history.push("/dynamic-route/blogs");
|
|
43
|
+
await wait();
|
|
44
|
+
history.push("/dynamic-route/blogs?search=123");
|
|
45
|
+
await wait();
|
|
46
|
+
expect(spyLogin).toHaveBeenCalledTimes(2);
|
|
47
|
+
expect(spyLogin).toHaveBeenNthCalledWith(1, {
|
|
48
|
+
name: "login",
|
|
49
|
+
path: "/login",
|
|
50
|
+
pathname: "/login",
|
|
51
|
+
params: {}
|
|
52
|
+
});
|
|
53
|
+
expect(spyLogin).toHaveBeenNthCalledWith(2, {
|
|
54
|
+
name: "login",
|
|
55
|
+
path: "/login",
|
|
56
|
+
pathname: "/login",
|
|
57
|
+
params: {}
|
|
58
|
+
});
|
|
59
|
+
expect(spyHome).toHaveBeenCalledTimes(1);
|
|
60
|
+
expect(spyHome).toHaveBeenLastCalledWith({
|
|
61
|
+
name: "home",
|
|
62
|
+
path: "/",
|
|
63
|
+
pathname: "/",
|
|
64
|
+
params: {}
|
|
65
|
+
});
|
|
66
|
+
expect(spyDynamic).toHaveBeenCalledTimes(3);
|
|
67
|
+
expect(spyDynamic).toHaveBeenNthCalledWith(1, {
|
|
68
|
+
name: "test",
|
|
69
|
+
path: "/dynamic-route/:name",
|
|
70
|
+
pathname: "/dynamic-route/cars",
|
|
71
|
+
params: {
|
|
72
|
+
name: "cars"
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
expect(spyDynamic).toHaveBeenNthCalledWith(2, {
|
|
76
|
+
name: "test",
|
|
77
|
+
path: "/dynamic-route/:name",
|
|
78
|
+
pathname: "/dynamic-route/blogs",
|
|
79
|
+
params: {
|
|
80
|
+
name: "blogs"
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
expect(spyDynamic).toHaveBeenNthCalledWith(3, {
|
|
84
|
+
name: "test",
|
|
85
|
+
path: "/dynamic-route/:name",
|
|
86
|
+
pathname: "/dynamic-route/blogs",
|
|
87
|
+
params: {
|
|
88
|
+
name: "blogs",
|
|
89
|
+
search: "123"
|
|
90
|
+
}
|
|
91
|
+
});
|
|
63
92
|
});
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
93
|
+
it("should generate route URLs", async ()=>{
|
|
94
|
+
const urls = [
|
|
95
|
+
RouteUrl.fromPattern("/"),
|
|
96
|
+
RouteUrl.fromPattern("/login", {}),
|
|
97
|
+
RouteUrl.fromPattern("/login", {
|
|
98
|
+
redirect: "/",
|
|
99
|
+
reason: "login"
|
|
100
|
+
}),
|
|
101
|
+
RouteUrl.fromPattern("/dynamic-route/:name", {
|
|
102
|
+
name: "cars"
|
|
103
|
+
}),
|
|
104
|
+
RouteUrl.fromPattern("/dynamic-route", {
|
|
105
|
+
folderId: "696556831e485d00027a1a0b#0001"
|
|
106
|
+
})
|
|
107
|
+
];
|
|
108
|
+
expect(urls).toEqual([
|
|
109
|
+
"/",
|
|
110
|
+
"/login",
|
|
111
|
+
"/login?redirect=%2F&reason=login",
|
|
112
|
+
"/dynamic-route/cars",
|
|
113
|
+
"/dynamic-route?folderId=696556831e485d00027a1a0b%230001"
|
|
114
|
+
]);
|
|
72
115
|
});
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
116
|
+
it("should handle baseUrl for route matching and URL generation", async ()=>{
|
|
117
|
+
const spyFileManager = vi.fn();
|
|
118
|
+
const spyHome = vi.fn();
|
|
119
|
+
const history = createMemoryHistory();
|
|
120
|
+
history.replace("/tenant123/__unknown__");
|
|
121
|
+
const gateway = new HistoryRouterGateway(history, "/tenant123");
|
|
122
|
+
gateway.setRoutes([
|
|
123
|
+
{
|
|
124
|
+
name: "home",
|
|
125
|
+
path: "/",
|
|
126
|
+
onMatch: spyHome
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
name: "fileManager",
|
|
130
|
+
path: "/file-manager",
|
|
131
|
+
onMatch: spyFileManager
|
|
132
|
+
}
|
|
133
|
+
]);
|
|
134
|
+
history.push("/tenant123/file-manager");
|
|
135
|
+
await wait();
|
|
136
|
+
expect(spyFileManager).toHaveBeenCalledTimes(1);
|
|
137
|
+
expect(spyFileManager).toHaveBeenCalledWith({
|
|
138
|
+
name: "fileManager",
|
|
139
|
+
path: "/file-manager",
|
|
140
|
+
pathname: "/tenant123/file-manager",
|
|
141
|
+
params: {}
|
|
142
|
+
});
|
|
143
|
+
const urlWithBase = RouteUrl.fromPattern("/file-manager", {}, "/tenant123");
|
|
144
|
+
expect(urlWithBase).toBe("/tenant123/file-manager");
|
|
145
|
+
const urlWithBaseAndParams = RouteUrl.fromPattern("/file-manager", {
|
|
146
|
+
folder: "abc"
|
|
147
|
+
}, "/tenant123");
|
|
148
|
+
expect(urlWithBaseAndParams).toBe("/tenant123/file-manager?folder=abc");
|
|
80
149
|
});
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
150
|
+
it("should properly sort routes with wildcards always at the bottom", async ()=>{
|
|
151
|
+
const spyWildcard = vi.fn();
|
|
152
|
+
const spySpecific = vi.fn();
|
|
153
|
+
const spyHome = vi.fn();
|
|
154
|
+
const history = createMemoryHistory();
|
|
155
|
+
history.replace("/initial");
|
|
156
|
+
const gateway = new HistoryRouterGateway(history, "");
|
|
157
|
+
gateway.setRoutes([
|
|
158
|
+
{
|
|
159
|
+
name: "wildcard",
|
|
160
|
+
path: "*",
|
|
161
|
+
onMatch: spyWildcard
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
name: "home",
|
|
165
|
+
path: "/",
|
|
166
|
+
onMatch: spyHome
|
|
167
|
+
}
|
|
168
|
+
]);
|
|
169
|
+
expect(spyWildcard).toHaveBeenCalledTimes(1);
|
|
170
|
+
vi.clearAllMocks();
|
|
171
|
+
gateway.setRoutes([
|
|
172
|
+
{
|
|
173
|
+
name: "specific",
|
|
174
|
+
path: "/specific-route",
|
|
175
|
+
onMatch: spySpecific
|
|
176
|
+
}
|
|
177
|
+
]);
|
|
178
|
+
expect(spyWildcard).toHaveBeenCalledTimes(1);
|
|
179
|
+
vi.clearAllMocks();
|
|
180
|
+
history.push("/specific-route");
|
|
181
|
+
await wait();
|
|
182
|
+
expect(spySpecific).toHaveBeenCalledTimes(1);
|
|
183
|
+
expect(spyWildcard).toHaveBeenCalledTimes(0);
|
|
184
|
+
expect(spyHome).toHaveBeenCalledTimes(0);
|
|
185
|
+
history.push("/");
|
|
186
|
+
await wait();
|
|
187
|
+
expect(spyHome).toHaveBeenCalledTimes(1);
|
|
188
|
+
expect(spyWildcard).toHaveBeenCalledTimes(0);
|
|
189
|
+
history.push("/unknown");
|
|
190
|
+
await wait();
|
|
191
|
+
expect(spyWildcard).toHaveBeenCalledTimes(1);
|
|
89
192
|
});
|
|
90
|
-
});
|
|
91
|
-
it("should generate route URLs", async () => {
|
|
92
|
-
const urls = [RouteUrl.fromPattern("/"), RouteUrl.fromPattern("/login", {}), RouteUrl.fromPattern("/login", {
|
|
93
|
-
redirect: "/",
|
|
94
|
-
reason: "login"
|
|
95
|
-
}), RouteUrl.fromPattern("/dynamic-route/:name", {
|
|
96
|
-
name: "cars"
|
|
97
|
-
}), RouteUrl.fromPattern("/dynamic-route", {
|
|
98
|
-
folderId: "696556831e485d00027a1a0b#0001"
|
|
99
|
-
})];
|
|
100
|
-
expect(urls).toEqual(["/", "/login", "/login?redirect=%2F&reason=login", "/dynamic-route/cars", "/dynamic-route?folderId=696556831e485d00027a1a0b%230001"]);
|
|
101
|
-
});
|
|
102
|
-
it("should handle baseUrl for route matching and URL generation", async () => {
|
|
103
|
-
const spyFileManager = vi.fn();
|
|
104
|
-
const spyHome = vi.fn();
|
|
105
|
-
|
|
106
|
-
// Create history with a tenant prefix
|
|
107
|
-
const history = createMemoryHistory();
|
|
108
|
-
history.replace("/tenant123/__unknown__");
|
|
109
|
-
const gateway = new HistoryRouterGateway(history, "/tenant123");
|
|
110
|
-
gateway.setRoutes([{
|
|
111
|
-
name: "home",
|
|
112
|
-
path: "/",
|
|
113
|
-
onMatch: spyHome
|
|
114
|
-
}, {
|
|
115
|
-
name: "fileManager",
|
|
116
|
-
path: "/file-manager",
|
|
117
|
-
onMatch: spyFileManager
|
|
118
|
-
}]);
|
|
119
|
-
|
|
120
|
-
// Navigate to /tenant123/file-manager
|
|
121
|
-
history.push("/tenant123/file-manager");
|
|
122
|
-
await wait();
|
|
123
|
-
|
|
124
|
-
// Should match the /file-manager route
|
|
125
|
-
expect(spyFileManager).toHaveBeenCalledTimes(1);
|
|
126
|
-
expect(spyFileManager).toHaveBeenCalledWith({
|
|
127
|
-
name: "fileManager",
|
|
128
|
-
path: "/file-manager",
|
|
129
|
-
pathname: "/tenant123/file-manager",
|
|
130
|
-
params: {}
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
// Test URL generation with baseUrl
|
|
134
|
-
const urlWithBase = RouteUrl.fromPattern("/file-manager", {}, "/tenant123");
|
|
135
|
-
expect(urlWithBase).toBe("/tenant123/file-manager");
|
|
136
|
-
const urlWithBaseAndParams = RouteUrl.fromPattern("/file-manager", {
|
|
137
|
-
folder: "abc"
|
|
138
|
-
}, "/tenant123");
|
|
139
|
-
expect(urlWithBaseAndParams).toBe("/tenant123/file-manager?folder=abc");
|
|
140
|
-
});
|
|
141
|
-
it("should properly sort routes with wildcards always at the bottom", async () => {
|
|
142
|
-
const spyWildcard = vi.fn();
|
|
143
|
-
const spySpecific = vi.fn();
|
|
144
|
-
const spyHome = vi.fn();
|
|
145
|
-
const history = createMemoryHistory();
|
|
146
|
-
// Start at a non-matching path to avoid initial route resolution
|
|
147
|
-
history.replace("/initial");
|
|
148
|
-
const gateway = new HistoryRouterGateway(history, "");
|
|
149
|
-
|
|
150
|
-
// Add wildcard first, then home route
|
|
151
|
-
// Note: setRoutes will trigger route resolution for current path (/initial)
|
|
152
|
-
gateway.setRoutes([{
|
|
153
|
-
name: "wildcard",
|
|
154
|
-
path: "*",
|
|
155
|
-
onMatch: spyWildcard
|
|
156
|
-
}, {
|
|
157
|
-
name: "home",
|
|
158
|
-
path: "/",
|
|
159
|
-
onMatch: spyHome
|
|
160
|
-
}]);
|
|
161
|
-
|
|
162
|
-
// Wildcard should have matched /initial
|
|
163
|
-
expect(spyWildcard).toHaveBeenCalledTimes(1);
|
|
164
|
-
vi.clearAllMocks();
|
|
165
|
-
|
|
166
|
-
// Now add a specific route after the wildcard - this should re-sort and keep wildcard at bottom
|
|
167
|
-
// Note: setRoutes will trigger route resolution again for /initial, matching wildcard again
|
|
168
|
-
gateway.setRoutes([{
|
|
169
|
-
name: "specific",
|
|
170
|
-
path: "/specific-route",
|
|
171
|
-
onMatch: spySpecific
|
|
172
|
-
}]);
|
|
173
|
-
|
|
174
|
-
// Wildcard should match /initial again after re-sorting
|
|
175
|
-
expect(spyWildcard).toHaveBeenCalledTimes(1);
|
|
176
|
-
vi.clearAllMocks();
|
|
177
|
-
|
|
178
|
-
// Navigate to the specific route
|
|
179
|
-
history.push("/specific-route");
|
|
180
|
-
await wait();
|
|
181
|
-
|
|
182
|
-
// Should match the specific route, not the wildcard
|
|
183
|
-
expect(spySpecific).toHaveBeenCalledTimes(1);
|
|
184
|
-
expect(spyWildcard).toHaveBeenCalledTimes(0);
|
|
185
|
-
expect(spyHome).toHaveBeenCalledTimes(0);
|
|
186
|
-
|
|
187
|
-
// Navigate to home
|
|
188
|
-
history.push("/");
|
|
189
|
-
await wait();
|
|
190
|
-
|
|
191
|
-
// Should match home route
|
|
192
|
-
expect(spyHome).toHaveBeenCalledTimes(1);
|
|
193
|
-
expect(spyWildcard).toHaveBeenCalledTimes(0);
|
|
194
|
-
|
|
195
|
-
// Navigate to unknown route
|
|
196
|
-
history.push("/unknown");
|
|
197
|
-
await wait();
|
|
198
|
-
|
|
199
|
-
// Should match wildcard
|
|
200
|
-
expect(spyWildcard).toHaveBeenCalledTimes(1);
|
|
201
|
-
});
|
|
202
193
|
});
|
|
203
194
|
|
|
204
195
|
//# sourceMappingURL=HistoryRouterGateway.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["describe","it","beforeEach","expect","vi","createMemoryHistory","HistoryRouterGateway","RouteUrl","wait","Promise","resolve","setTimeout","clearAllMocks","spyHome","fn","spyLogin","spyDynamic","history","replace","gateway","setRoutes","name","path","onMatch","push","toHaveBeenCalledTimes","toHaveBeenNthCalledWith","pathname","params","toHaveBeenLastCalledWith","search","urls","fromPattern","redirect","reason","folderId","toEqual","spyFileManager","toHaveBeenCalledWith","urlWithBase","toBe","urlWithBaseAndParams","folder","spyWildcard","spySpecific"],"sources":["HistoryRouterGateway.test.ts"],"sourcesContent":["import { describe, it, beforeEach, expect, vi } from \"vitest\";\nimport { createMemoryHistory } from \"history\";\nimport { HistoryRouterGateway } from \"./HistoryRouterGateway.js\";\nimport { RouteUrl } from \"./RouteUrl.js\";\n\nconst wait = () => new Promise(resolve => setTimeout(resolve, 10));\n\ndescribe(\"Router Gateway\", () => {\n beforeEach(() => {\n vi.clearAllMocks();\n });\n\n it(\"should execute onMatch when history changes\", async () => {\n const spyHome = vi.fn();\n const spyLogin = vi.fn();\n const spyDynamic = vi.fn();\n\n // Create `history` and set the initial pathname to an unknown route\n const history = createMemoryHistory();\n history.replace(\"/__unknown__\");\n\n const gateway = new HistoryRouterGateway(history, \"\");\n gateway.setRoutes([\n { name: \"home\", path: \"/\", onMatch: spyHome },\n { name: \"login\", path: \"/login\", onMatch: spyLogin },\n { name: \"test\", path: \"/dynamic-route/:name\", onMatch: spyDynamic }\n ]);\n\n history.push(\"/login\");\n await wait();\n history.push(\"/\");\n await wait();\n history.push(\"/dynamic-route/cars\");\n await wait();\n history.push(\"/login\");\n await wait();\n history.push(\"/dynamic-route/blogs\");\n await wait();\n history.push(\"/dynamic-route/blogs?search=123\");\n await wait();\n\n expect(spyLogin).toHaveBeenCalledTimes(2);\n expect(spyLogin).toHaveBeenNthCalledWith(1, {\n name: \"login\",\n path: \"/login\",\n pathname: \"/login\",\n params: {}\n });\n expect(spyLogin).toHaveBeenNthCalledWith(2, {\n name: \"login\",\n path: \"/login\",\n pathname: \"/login\",\n params: {}\n });\n expect(spyHome).toHaveBeenCalledTimes(1);\n expect(spyHome).toHaveBeenLastCalledWith({\n name: \"home\",\n path: \"/\",\n pathname: \"/\",\n params: {}\n });\n expect(spyDynamic).toHaveBeenCalledTimes(3);\n expect(spyDynamic).toHaveBeenNthCalledWith(1, {\n name: \"test\",\n path: \"/dynamic-route/:name\",\n pathname: \"/dynamic-route/cars\",\n params: {\n name: \"cars\"\n }\n });\n expect(spyDynamic).toHaveBeenNthCalledWith(2, {\n name: \"test\",\n path: \"/dynamic-route/:name\",\n pathname: \"/dynamic-route/blogs\",\n params: {\n name: \"blogs\"\n }\n });\n expect(spyDynamic).toHaveBeenNthCalledWith(3, {\n name: \"test\",\n path: \"/dynamic-route/:name\",\n pathname: \"/dynamic-route/blogs\",\n params: {\n name: \"blogs\",\n search: \"123\"\n }\n });\n });\n\n it(\"should generate route URLs\", async () => {\n const urls = [\n RouteUrl.fromPattern(\"/\"),\n RouteUrl.fromPattern(\"/login\", {}),\n RouteUrl.fromPattern(\"/login\", { redirect: \"/\", reason: \"login\" }),\n RouteUrl.fromPattern(\"/dynamic-route/:name\", { name: \"cars\" }),\n RouteUrl.fromPattern(\"/dynamic-route\", { folderId: \"696556831e485d00027a1a0b#0001\" })\n ];\n expect(urls).toEqual([\n \"/\",\n \"/login\",\n \"/login?redirect=%2F&reason=login\",\n \"/dynamic-route/cars\",\n \"/dynamic-route?folderId=696556831e485d00027a1a0b%230001\"\n ]);\n });\n\n it(\"should handle baseUrl for route matching and URL generation\", async () => {\n const spyFileManager = vi.fn();\n const spyHome = vi.fn();\n\n // Create history with a tenant prefix\n const history = createMemoryHistory();\n history.replace(\"/tenant123/__unknown__\");\n\n const gateway = new HistoryRouterGateway(history, \"/tenant123\");\n gateway.setRoutes([\n { name: \"home\", path: \"/\", onMatch: spyHome },\n { name: \"fileManager\", path: \"/file-manager\", onMatch: spyFileManager }\n ]);\n\n // Navigate to /tenant123/file-manager\n history.push(\"/tenant123/file-manager\");\n await wait();\n\n // Should match the /file-manager route\n expect(spyFileManager).toHaveBeenCalledTimes(1);\n expect(spyFileManager).toHaveBeenCalledWith({\n name: \"fileManager\",\n path: \"/file-manager\",\n pathname: \"/tenant123/file-manager\",\n params: {}\n });\n\n // Test URL generation with baseUrl\n const urlWithBase = RouteUrl.fromPattern(\"/file-manager\", {}, \"/tenant123\");\n expect(urlWithBase).toBe(\"/tenant123/file-manager\");\n\n const urlWithBaseAndParams = RouteUrl.fromPattern(\n \"/file-manager\",\n { folder: \"abc\" },\n \"/tenant123\"\n );\n expect(urlWithBaseAndParams).toBe(\"/tenant123/file-manager?folder=abc\");\n });\n\n it(\"should properly sort routes with wildcards always at the bottom\", async () => {\n const spyWildcard = vi.fn();\n const spySpecific = vi.fn();\n const spyHome = vi.fn();\n\n const history = createMemoryHistory();\n // Start at a non-matching path to avoid initial route resolution\n history.replace(\"/initial\");\n\n const gateway = new HistoryRouterGateway(history, \"\");\n\n // Add wildcard first, then home route\n // Note: setRoutes will trigger route resolution for current path (/initial)\n gateway.setRoutes([\n { name: \"wildcard\", path: \"*\", onMatch: spyWildcard },\n { name: \"home\", path: \"/\", onMatch: spyHome }\n ]);\n\n // Wildcard should have matched /initial\n expect(spyWildcard).toHaveBeenCalledTimes(1);\n vi.clearAllMocks();\n\n // Now add a specific route after the wildcard - this should re-sort and keep wildcard at bottom\n // Note: setRoutes will trigger route resolution again for /initial, matching wildcard again\n gateway.setRoutes([{ name: \"specific\", path: \"/specific-route\", onMatch: spySpecific }]);\n\n // Wildcard should match /initial again after re-sorting\n expect(spyWildcard).toHaveBeenCalledTimes(1);\n vi.clearAllMocks();\n\n // Navigate to the specific route\n history.push(\"/specific-route\");\n await wait();\n\n // Should match the specific route, not the wildcard\n expect(spySpecific).toHaveBeenCalledTimes(1);\n expect(spyWildcard).toHaveBeenCalledTimes(0);\n expect(spyHome).toHaveBeenCalledTimes(0);\n\n // Navigate to home\n history.push(\"/\");\n await wait();\n\n // Should match home route\n expect(spyHome).toHaveBeenCalledTimes(1);\n expect(spyWildcard).toHaveBeenCalledTimes(0);\n\n // Navigate to unknown route\n history.push(\"/unknown\");\n await wait();\n\n // Should match wildcard\n expect(spyWildcard).toHaveBeenCalledTimes(1);\n });\n});\n"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,EAAE,EAAEC,UAAU,EAAEC,MAAM,EAAEC,EAAE,QAAQ,QAAQ;AAC7D,SAASC,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CAACY,qBAAqB,CAAC,CAAC,CAAC;IACxCtB,MAAM,CAACwC,WAAW,CAAC,CAAClB,qBAAqB,CAAC,CAAC,CAAC;;IAE5C;IACAR,OAAO,CAACO,IAAI,CAAC,UAAU,CAAC;IACxB,MAAMhB,IAAI,CAAC,CAAC;;IAEZ;IACAL,MAAM,CAACwC,WAAW,CAAC,CAAClB,qBAAqB,CAAC,CAAC,CAAC;EAChD,CAAC,CAAC;AACN,CAAC,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"features/router/HistoryRouterGateway.test.js","sources":["../../../src/features/router/HistoryRouterGateway.test.ts"],"sourcesContent":["import { describe, it, beforeEach, expect, vi } from \"vitest\";\nimport { createMemoryHistory } from \"history\";\nimport { HistoryRouterGateway } from \"./HistoryRouterGateway.js\";\nimport { RouteUrl } from \"./RouteUrl.js\";\n\nconst wait = () => new Promise(resolve => setTimeout(resolve, 10));\n\ndescribe(\"Router Gateway\", () => {\n beforeEach(() => {\n vi.clearAllMocks();\n });\n\n it(\"should execute onMatch when history changes\", async () => {\n const spyHome = vi.fn();\n const spyLogin = vi.fn();\n const spyDynamic = vi.fn();\n\n // Create `history` and set the initial pathname to an unknown route\n const history = createMemoryHistory();\n history.replace(\"/__unknown__\");\n\n const gateway = new HistoryRouterGateway(history, \"\");\n gateway.setRoutes([\n { name: \"home\", path: \"/\", onMatch: spyHome },\n { name: \"login\", path: \"/login\", onMatch: spyLogin },\n { name: \"test\", path: \"/dynamic-route/:name\", onMatch: spyDynamic }\n ]);\n\n history.push(\"/login\");\n await wait();\n history.push(\"/\");\n await wait();\n history.push(\"/dynamic-route/cars\");\n await wait();\n history.push(\"/login\");\n await wait();\n history.push(\"/dynamic-route/blogs\");\n await wait();\n history.push(\"/dynamic-route/blogs?search=123\");\n await wait();\n\n expect(spyLogin).toHaveBeenCalledTimes(2);\n expect(spyLogin).toHaveBeenNthCalledWith(1, {\n name: \"login\",\n path: \"/login\",\n pathname: \"/login\",\n params: {}\n });\n expect(spyLogin).toHaveBeenNthCalledWith(2, {\n name: \"login\",\n path: \"/login\",\n pathname: \"/login\",\n params: {}\n });\n expect(spyHome).toHaveBeenCalledTimes(1);\n expect(spyHome).toHaveBeenLastCalledWith({\n name: \"home\",\n path: \"/\",\n pathname: \"/\",\n params: {}\n });\n expect(spyDynamic).toHaveBeenCalledTimes(3);\n expect(spyDynamic).toHaveBeenNthCalledWith(1, {\n name: \"test\",\n path: \"/dynamic-route/:name\",\n pathname: \"/dynamic-route/cars\",\n params: {\n name: \"cars\"\n }\n });\n expect(spyDynamic).toHaveBeenNthCalledWith(2, {\n name: \"test\",\n path: \"/dynamic-route/:name\",\n pathname: \"/dynamic-route/blogs\",\n params: {\n name: \"blogs\"\n }\n });\n expect(spyDynamic).toHaveBeenNthCalledWith(3, {\n name: \"test\",\n path: \"/dynamic-route/:name\",\n pathname: \"/dynamic-route/blogs\",\n params: {\n name: \"blogs\",\n search: \"123\"\n }\n });\n });\n\n it(\"should generate route URLs\", async () => {\n const urls = [\n RouteUrl.fromPattern(\"/\"),\n RouteUrl.fromPattern(\"/login\", {}),\n RouteUrl.fromPattern(\"/login\", { redirect: \"/\", reason: \"login\" }),\n RouteUrl.fromPattern(\"/dynamic-route/:name\", { name: \"cars\" }),\n RouteUrl.fromPattern(\"/dynamic-route\", { folderId: \"696556831e485d00027a1a0b#0001\" })\n ];\n expect(urls).toEqual([\n \"/\",\n \"/login\",\n \"/login?redirect=%2F&reason=login\",\n \"/dynamic-route/cars\",\n \"/dynamic-route?folderId=696556831e485d00027a1a0b%230001\"\n ]);\n });\n\n it(\"should handle baseUrl for route matching and URL generation\", async () => {\n const spyFileManager = vi.fn();\n const spyHome = vi.fn();\n\n // Create history with a tenant prefix\n const history = createMemoryHistory();\n history.replace(\"/tenant123/__unknown__\");\n\n const gateway = new HistoryRouterGateway(history, \"/tenant123\");\n gateway.setRoutes([\n { name: \"home\", path: \"/\", onMatch: spyHome },\n { name: \"fileManager\", path: \"/file-manager\", onMatch: spyFileManager }\n ]);\n\n // Navigate to /tenant123/file-manager\n history.push(\"/tenant123/file-manager\");\n await wait();\n\n // Should match the /file-manager route\n expect(spyFileManager).toHaveBeenCalledTimes(1);\n expect(spyFileManager).toHaveBeenCalledWith({\n name: \"fileManager\",\n path: \"/file-manager\",\n pathname: \"/tenant123/file-manager\",\n params: {}\n });\n\n // Test URL generation with baseUrl\n const urlWithBase = RouteUrl.fromPattern(\"/file-manager\", {}, \"/tenant123\");\n expect(urlWithBase).toBe(\"/tenant123/file-manager\");\n\n const urlWithBaseAndParams = RouteUrl.fromPattern(\n \"/file-manager\",\n { folder: \"abc\" },\n \"/tenant123\"\n );\n expect(urlWithBaseAndParams).toBe(\"/tenant123/file-manager?folder=abc\");\n });\n\n it(\"should properly sort routes with wildcards always at the bottom\", async () => {\n const spyWildcard = vi.fn();\n const spySpecific = vi.fn();\n const spyHome = vi.fn();\n\n const history = createMemoryHistory();\n // Start at a non-matching path to avoid initial route resolution\n history.replace(\"/initial\");\n\n const gateway = new HistoryRouterGateway(history, \"\");\n\n // Add wildcard first, then home route\n // Note: setRoutes will trigger route resolution for current path (/initial)\n gateway.setRoutes([\n { name: \"wildcard\", path: \"*\", onMatch: spyWildcard },\n { name: \"home\", path: \"/\", onMatch: spyHome }\n ]);\n\n // Wildcard should have matched /initial\n expect(spyWildcard).toHaveBeenCalledTimes(1);\n vi.clearAllMocks();\n\n // Now add a specific route after the wildcard - this should re-sort and keep wildcard at bottom\n // Note: setRoutes will trigger route resolution again for /initial, matching wildcard again\n gateway.setRoutes([{ name: \"specific\", path: \"/specific-route\", onMatch: spySpecific }]);\n\n // Wildcard should match /initial again after re-sorting\n expect(spyWildcard).toHaveBeenCalledTimes(1);\n vi.clearAllMocks();\n\n // Navigate to the specific route\n history.push(\"/specific-route\");\n await wait();\n\n // Should match the specific route, not the wildcard\n expect(spySpecific).toHaveBeenCalledTimes(1);\n expect(spyWildcard).toHaveBeenCalledTimes(0);\n expect(spyHome).toHaveBeenCalledTimes(0);\n\n // Navigate to home\n history.push(\"/\");\n await wait();\n\n // Should match home route\n expect(spyHome).toHaveBeenCalledTimes(1);\n expect(spyWildcard).toHaveBeenCalledTimes(0);\n\n // Navigate to unknown route\n history.push(\"/unknown\");\n await wait();\n\n // Should match wildcard\n expect(spyWildcard).toHaveBeenCalledTimes(1);\n });\n});\n"],"names":["wait","Promise","resolve","setTimeout","describe","beforeEach","vi","it","spyHome","spyLogin","spyDynamic","history","createMemoryHistory","gateway","HistoryRouterGateway","expect","urls","RouteUrl","spyFileManager","urlWithBase","urlWithBaseAndParams","spyWildcard","spySpecific"],"mappings":";;;;AAKA,MAAMA,OAAO,IAAM,IAAIC,QAAQC,CAAAA,UAAWC,WAAWD,SAAS;AAE9DE,SAAS,kBAAkB;IACvBC,WAAW;QACPC,GAAG,aAAa;IACpB;IAEAC,GAAG,+CAA+C;QAC9C,MAAMC,UAAUF,GAAG,EAAE;QACrB,MAAMG,WAAWH,GAAG,EAAE;QACtB,MAAMI,aAAaJ,GAAG,EAAE;QAGxB,MAAMK,UAAUC;QAChBD,QAAQ,OAAO,CAAC;QAEhB,MAAME,UAAU,IAAIC,qBAAqBH,SAAS;QAClDE,QAAQ,SAAS,CAAC;YACd;gBAAE,MAAM;gBAAQ,MAAM;gBAAK,SAASL;YAAQ;YAC5C;gBAAE,MAAM;gBAAS,MAAM;gBAAU,SAASC;YAAS;YACnD;gBAAE,MAAM;gBAAQ,MAAM;gBAAwB,SAASC;YAAW;SACrE;QAEDC,QAAQ,IAAI,CAAC;QACb,MAAMX;QACNW,QAAQ,IAAI,CAAC;QACb,MAAMX;QACNW,QAAQ,IAAI,CAAC;QACb,MAAMX;QACNW,QAAQ,IAAI,CAAC;QACb,MAAMX;QACNW,QAAQ,IAAI,CAAC;QACb,MAAMX;QACNW,QAAQ,IAAI,CAAC;QACb,MAAMX;QAENe,OAAON,UAAU,qBAAqB,CAAC;QACvCM,OAAON,UAAU,uBAAuB,CAAC,GAAG;YACxC,MAAM;YACN,MAA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C;QACtCO,OAAOM,aAAa,qBAAqB,CAAC;QAG1CV,QAAQ,IAAI,CAAC;QACb,MAAMX;QAGNe,OAAOM,aAAa,qBAAqB,CAAC;IAC9C;AACJ"}
|
package/features/router/Route.js
CHANGED
|
@@ -1,59 +1,43 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
base = z.coerce.string();
|
|
40
|
-
} else if (base instanceof z.ZodNumber) {
|
|
41
|
-
base = z.coerce.number();
|
|
42
|
-
} else if (base instanceof z.ZodBoolean) {
|
|
43
|
-
base = z.coerce.boolean();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// rewrap optional/nullable
|
|
47
|
-
if (isNullable) {
|
|
48
|
-
base = base.nullable();
|
|
49
|
-
}
|
|
50
|
-
if (isOptional) {
|
|
51
|
-
base = base.optional();
|
|
52
|
-
}
|
|
53
|
-
result[key] = base;
|
|
2
|
+
class Route {
|
|
3
|
+
constructor(route){
|
|
4
|
+
this.route = route;
|
|
5
|
+
const paramsSchema = route.params ? route.params(z) : void 0;
|
|
6
|
+
this.schema = paramsSchema ? z.looseObject(this.coerceParams(paramsSchema)) : void 0;
|
|
7
|
+
}
|
|
8
|
+
get name() {
|
|
9
|
+
return this.route.name;
|
|
10
|
+
}
|
|
11
|
+
get path() {
|
|
12
|
+
return this.route.path;
|
|
13
|
+
}
|
|
14
|
+
get params() {
|
|
15
|
+
return this.schema;
|
|
16
|
+
}
|
|
17
|
+
coerceParams(shape) {
|
|
18
|
+
const result = {};
|
|
19
|
+
for (const [key, schema] of Object.entries(shape)){
|
|
20
|
+
let base = schema;
|
|
21
|
+
let isOptional = false;
|
|
22
|
+
let isNullable = false;
|
|
23
|
+
if (base instanceof z.ZodOptional) {
|
|
24
|
+
isOptional = true;
|
|
25
|
+
base = base.unwrap();
|
|
26
|
+
}
|
|
27
|
+
if (base instanceof z.ZodNullable) {
|
|
28
|
+
isNullable = true;
|
|
29
|
+
base = base.unwrap();
|
|
30
|
+
}
|
|
31
|
+
if (base instanceof z.ZodString) base = z.coerce.string();
|
|
32
|
+
else if (base instanceof z.ZodNumber) base = z.coerce.number();
|
|
33
|
+
else if (base instanceof z.ZodBoolean) base = z.coerce.boolean();
|
|
34
|
+
if (isNullable) base = base.nullable();
|
|
35
|
+
if (isOptional) base = base.optional();
|
|
36
|
+
result[key] = base;
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
54
39
|
}
|
|
55
|
-
return result;
|
|
56
|
-
}
|
|
57
40
|
}
|
|
41
|
+
export { Route };
|
|
58
42
|
|
|
59
43
|
//# sourceMappingURL=Route.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"features/router/Route.js","sources":["../../../src/features/router/Route.ts"],"sourcesContent":["import { z } from \"zod\";\n\nexport type RouteParamsDefinition = Record<string, z.ZodTypeAny>;\n\nexport type RouteParamsInfer<T extends RouteParamsDefinition | undefined> =\n T extends RouteParamsDefinition ? z.infer<z.ZodObject<T> & { [k: string]: any }> : never;\n\nexport interface RouteParams<TParams extends RouteParamsDefinition | undefined> {\n name: string;\n path: `/${string}` | `*`;\n params?: (zod: typeof z) => TParams;\n}\n\nexport class Route<TParams extends RouteParamsDefinition | undefined = undefined> {\n private readonly route: RouteParams<TParams>;\n private readonly schema: TParams extends RouteParamsDefinition\n ? RouteParamsInfer<TParams>\n : undefined;\n\n constructor(route: RouteParams<TParams>) {\n this.route = route;\n const paramsSchema = route.params ? route.params(z) : undefined;\n // @ts-expect-error\n this.schema = paramsSchema ? z.looseObject(this.coerceParams(paramsSchema)) : undefined;\n }\n\n get name() {\n return this.route.name;\n }\n\n get path() {\n return this.route.path;\n }\n\n get params(): TParams extends RouteParamsDefinition ? RouteParamsInfer<TParams> : undefined {\n return this.schema;\n }\n\n private coerceParams<T extends Record<string, z.ZodTypeAny>>(shape: T) {\n const result: Record<string, z.ZodTypeAny> = {};\n for (const [key, schema] of Object.entries(shape)) {\n let base = schema as z.ZodTypeAny;\n let isOptional = false;\n let isNullable = false;\n\n // unwrap optional\n if (base instanceof z.ZodOptional) {\n isOptional = true;\n base = base.unwrap() as z.ZodTypeAny;\n }\n\n // unwrap nullable\n if (base instanceof z.ZodNullable) {\n isNullable = true;\n base = base.unwrap() as z.ZodTypeAny;\n }\n\n // replace with coerced\n if (base instanceof z.ZodString) {\n base = z.coerce.string();\n } else if (base instanceof z.ZodNumber) {\n base = z.coerce.number();\n } else if (base instanceof z.ZodBoolean) {\n base = z.coerce.boolean();\n }\n\n // rewrap optional/nullable\n if (isNullable) {\n base = base.nullable();\n }\n if (isOptional) {\n base = base.optional();\n }\n\n result[key] = base;\n }\n return result;\n }\n}\n"],"names":["Route","route","paramsSchema","z","undefined","shape","result","key","schema","Object","base","isOptional","isNullable"],"mappings":";AAaO,MAAMA;IAMT,YAAYC,KAA2B,CAAE;QACrC,IAAI,CAAC,KAAK,GAAGA;QACb,MAAMC,eAAeD,MAAM,MAAM,GAAGA,MAAM,MAAM,CAACE,KAAKC;QAEtD,IAAI,CAAC,MAAM,GAAGF,eAAeC,EAAE,WAAW,CAAC,IAAI,CAAC,YAAY,CAACD,iBAAiBE;IAClF;IAEA,IAAI,OAAO;QACP,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI;IAC1B;IAEA,IAAI,OAAO;QACP,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI;IAC1B;IAEA,IAAI,SAAwF;QACxF,OAAO,IAAI,CAAC,MAAM;IACtB;IAEQ,aAAqDC,KAAQ,EAAE;QACnE,MAAMC,SAAuC,CAAC;QAC9C,KAAK,MAAM,CAACC,KAAKC,OAAO,IAAIC,OAAO,OAAO,CAACJ,OAAQ;YAC/C,IAAIK,OAAOF;YACX,IAAIG,aAAa;YACjB,IAAIC,aAAa;YAGjB,IAAIF,gBAAgBP,EAAE,WAAW,EAAE;gBAC/BQ,aAAa;gBACbD,OAAOA,KAAK,MAAM;YACtB;YAGA,IAAIA,gBAAgBP,EAAE,WAAW,EAAE;gBAC/BS,aAAa;gBACbF,OAAOA,KAAK,MAAM;YACtB;YAGA,IAAIA,gBAAgBP,EAAE,SAAS,EAC3BO,OAAOP,EAAE,MAAM,CAAC,MAAM;iBACnB,IAAIO,gBAAgBP,EAAE,SAAS,EAClCO,OAAOP,EAAE,MAAM,CAAC,MAAM;iBACnB,IAAIO,gBAAgBP,EAAE,UAAU,EACnCO,OAAOP,EAAE,MAAM,CAAC,OAAO;YAI3B,IAAIS,YACAF,OAAOA,KAAK,QAAQ;YAExB,IAAIC,YACAD,OAAOA,KAAK,QAAQ;YAGxBJ,MAAM,CAACC,IAAI,GAAGG;QAClB;QACA,OAAOJ;IACX;AACJ"}
|