@shopware/composables 0.0.0-canary-20250116155626

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +172 -0
  3. package/composables/index.ts +1 -0
  4. package/dist/index.d.mts +2239 -0
  5. package/dist/index.d.ts +2239 -0
  6. package/dist/index.mjs +2686 -0
  7. package/nuxt.config.ts +14 -0
  8. package/package.json +83 -0
  9. package/src/_test/index.ts +1 -0
  10. package/src/_test/useSetup.ts +43 -0
  11. package/src/cms/README.md +9 -0
  12. package/src/cms/cmsAssociations.ts +28 -0
  13. package/src/cms/index.ts +2 -0
  14. package/src/cms/useCmsElementConfig.test.ts +145 -0
  15. package/src/cms/useCmsElementConfig.ts +29 -0
  16. package/src/cms/useCmsElementImage.test.ts +179 -0
  17. package/src/cms/useCmsElementImage.ts +103 -0
  18. package/src/cms/useCmsElementProductBox.test.ts +15 -0
  19. package/src/cms/useCmsElementProductBox.ts +25 -0
  20. package/src/createShopwareContext.test.ts +23 -0
  21. package/src/createShopwareContext.ts +43 -0
  22. package/src/devtools/plugin.ts +201 -0
  23. package/src/helpers/ContextError.spec.ts +20 -0
  24. package/src/helpers/ContextError.ts +6 -0
  25. package/src/index.test.ts +54 -0
  26. package/src/index.ts +111 -0
  27. package/src/mocks/Cart.ts +774 -0
  28. package/src/mocks/CmsPage.ts +544 -0
  29. package/src/mocks/CmsProductBox.ts +377 -0
  30. package/src/mocks/Configurator.ts +123 -0
  31. package/src/mocks/Country.ts +184 -0
  32. package/src/mocks/CrossSellingResponse.ts +552 -0
  33. package/src/mocks/LandingPage.ts +62 -0
  34. package/src/mocks/Menu.ts +598 -0
  35. package/src/mocks/Order.ts +1151 -0
  36. package/src/mocks/Product.ts +473 -0
  37. package/src/mocks/ProductTierPrices.ts +771 -0
  38. package/src/mocks/Salutations.ts +51 -0
  39. package/src/mocks/Search.ts +518 -0
  40. package/src/types/cmsBlockTypes.ts +80 -0
  41. package/src/types/cmsElementTypes.ts +381 -0
  42. package/src/types/cmsSectionTypes.ts +15 -0
  43. package/src/types/index.ts +52 -0
  44. package/src/useAddToCart/useAddToCart.test.ts +39 -0
  45. package/src/useAddToCart/useAddToCart.ts +90 -0
  46. package/src/useAddress/useAddress.test.ts +134 -0
  47. package/src/useAddress/useAddress.ts +202 -0
  48. package/src/useB2bQuoteManagement/useB2bQuoteManagement.test.ts +137 -0
  49. package/src/useB2bQuoteManagement/useB2bQuoteManagement.ts +225 -0
  50. package/src/useBreadcrumbs/useBreadcrumbs.test.ts +71 -0
  51. package/src/useBreadcrumbs/useBreadcrumbs.ts +89 -0
  52. package/src/useCart/useCart.test.ts +251 -0
  53. package/src/useCart/useCart.ts +314 -0
  54. package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.test.ts +122 -0
  55. package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.ts +80 -0
  56. package/src/useCartItem/useCartItem.test.ts +168 -0
  57. package/src/useCartItem/useCartItem.ts +157 -0
  58. package/src/useCartNotification/useCartNotification.test.ts +129 -0
  59. package/src/useCartNotification/useCartNotification.ts +63 -0
  60. package/src/useCategory/useCategory.test.ts +14 -0
  61. package/src/useCategory/useCategory.ts +32 -0
  62. package/src/useCategorySearch/useCategorySearch.test.ts +98 -0
  63. package/src/useCategorySearch/useCategorySearch.ts +81 -0
  64. package/src/useCheckout/useCheckout.test.ts +104 -0
  65. package/src/useCheckout/useCheckout.ts +163 -0
  66. package/src/useCmsBlock/useCmsBlock.test.ts +25 -0
  67. package/src/useCmsBlock/useCmsBlock.ts +36 -0
  68. package/src/useCmsMeta/useCmsMeta.test.ts +17 -0
  69. package/src/useCmsMeta/useCmsMeta.ts +46 -0
  70. package/src/useCmsSection/useCmsSection.test.ts +18 -0
  71. package/src/useCmsSection/useCmsSection.ts +39 -0
  72. package/src/useCmsTranslations/useCmsTranslations.test.ts +10 -0
  73. package/src/useCmsTranslations/useCmsTranslations.ts +9 -0
  74. package/src/useContext/useContext.ts +36 -0
  75. package/src/useCountries/useCountries.test.ts +64 -0
  76. package/src/useCountries/useCountries.ts +64 -0
  77. package/src/useCustomerOrders/useCustomerOrders.test.ts +47 -0
  78. package/src/useCustomerOrders/useCustomerOrders.ts +89 -0
  79. package/src/useCustomerPassword/useCustomerPassword.test.ts +164 -0
  80. package/src/useCustomerPassword/useCustomerPassword.ts +57 -0
  81. package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +41 -0
  82. package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.ts +45 -0
  83. package/src/useInternationalization/useInternationalization.test.ts +155 -0
  84. package/src/useInternationalization/useInternationalization.ts +159 -0
  85. package/src/useLandingSearch/useLandingSearch.test.ts +30 -0
  86. package/src/useLandingSearch/useLandingSearch.ts +58 -0
  87. package/src/useListing/useListing.test.ts +249 -0
  88. package/src/useListing/useListing.ts +592 -0
  89. package/src/useLocalWishlist/useLocalWishlist.test.ts +54 -0
  90. package/src/useLocalWishlist/useLocalWishlist.ts +95 -0
  91. package/src/useNavigation/useNavigation.test.ts +68 -0
  92. package/src/useNavigation/useNavigation.ts +83 -0
  93. package/src/useNavigationContext/useNavigationContext.test.ts +48 -0
  94. package/src/useNavigationContext/useNavigationContext.ts +39 -0
  95. package/src/useNavigationSearch/useNavigationSearch.test.ts +110 -0
  96. package/src/useNavigationSearch/useNavigationSearch.ts +60 -0
  97. package/src/useNewsletter/useNewsletter.test.ts +55 -0
  98. package/src/useNewsletter/useNewsletter.ts +109 -0
  99. package/src/useNotifications/useNotifications.test.ts +56 -0
  100. package/src/useNotifications/useNotifications.ts +119 -0
  101. package/src/useOrderDetails/useOrderDetails.test.ts +224 -0
  102. package/src/useOrderDetails/useOrderDetails.ts +329 -0
  103. package/src/useOrderPayment/useOrderPayment.test.ts +80 -0
  104. package/src/useOrderPayment/useOrderPayment.ts +119 -0
  105. package/src/usePrice/usePrice.test.ts +97 -0
  106. package/src/usePrice/usePrice.ts +106 -0
  107. package/src/useProduct/useProduct.test.ts +36 -0
  108. package/src/useProduct/useProduct.ts +56 -0
  109. package/src/useProductAssociations/useProductAssociations.spec.ts +105 -0
  110. package/src/useProductAssociations/useProductAssociations.ts +107 -0
  111. package/src/useProductConfigurator/useProductConfigurator.spec.ts +77 -0
  112. package/src/useProductConfigurator/useProductConfigurator.ts +134 -0
  113. package/src/useProductPrice/useProductPrice.test.ts +126 -0
  114. package/src/useProductPrice/useProductPrice.ts +131 -0
  115. package/src/useProductReviews/useProductReviews.test.ts +45 -0
  116. package/src/useProductReviews/useProductReviews.ts +80 -0
  117. package/src/useProductSearch/useProductSearch.test.ts +37 -0
  118. package/src/useProductSearch/useProductSearch.ts +58 -0
  119. package/src/useProductSearchSuggest/useProductSearchSuggest.test.ts +19 -0
  120. package/src/useProductSearchSuggest/useProductSearchSuggest.ts +67 -0
  121. package/src/useProductWishlist/useProductWishlist.test.ts +63 -0
  122. package/src/useProductWishlist/useProductWishlist.ts +74 -0
  123. package/src/useSalutations/useSalutations.spec.ts +37 -0
  124. package/src/useSalutations/useSalutations.ts +55 -0
  125. package/src/useSessionContext/useSessionContext.test.ts +221 -0
  126. package/src/useSessionContext/useSessionContext.ts +281 -0
  127. package/src/useShopwareContext/useShopwareContext.test.ts +8 -0
  128. package/src/useShopwareContext/useShopwareContext.ts +31 -0
  129. package/src/useSyncWishlist/useSyncWishlist.test.ts +117 -0
  130. package/src/useSyncWishlist/useSyncWishlist.ts +117 -0
  131. package/src/useUrlResolver/useUrlResolver.test.ts +25 -0
  132. package/src/useUrlResolver/useUrlResolver.ts +31 -0
  133. package/src/useUser/useUser.test.ts +290 -0
  134. package/src/useUser/useUser.ts +321 -0
  135. package/src/useWishlist/useWishlist.test.ts +178 -0
  136. package/src/useWishlist/useWishlist.ts +128 -0
  137. package/types/api-types.ts +11 -0
@@ -0,0 +1,155 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import type { Schemas } from "#shopware";
3
+ import { useSetup } from "../_test";
4
+ import { useInternationalization } from "./useInternationalization";
5
+ import type { RouteObject } from "./useInternationalization";
6
+
7
+ describe("useInternationalization", () => {
8
+ it("should return storefrontUrl", async () => {
9
+ const url = "http://frontend.test";
10
+ vi.stubGlobal("location", {
11
+ origin: url,
12
+ });
13
+ const { vm } = useSetup(useInternationalization);
14
+
15
+ expect(vm.getStorefrontUrl()).toBe(url);
16
+
17
+ vi.unstubAllGlobals();
18
+ });
19
+
20
+ it("should return default window storefrontUrl", async () => {
21
+ const { vm } = useSetup(useInternationalization);
22
+
23
+ expect(vm.getStorefrontUrl()).toBe("http://localhost:3000");
24
+ });
25
+
26
+ it("should invoke getting available languages", async () => {
27
+ const { vm, injections } = useSetup(useInternationalization);
28
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
29
+ vm.getAvailableLanguages();
30
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
31
+ expect.stringContaining("readLanguages"),
32
+ );
33
+ });
34
+
35
+ it("should invoke change language", async () => {
36
+ const { vm, injections } = useSetup(useInternationalization);
37
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
38
+ vm.changeLanguage("test-id");
39
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
40
+ expect.stringContaining("updateContext"),
41
+ expect.objectContaining({ body: { languageId: "test-id" } }),
42
+ );
43
+ });
44
+
45
+ it("getLanguageCodeFromId", async () => {
46
+ const { vm } = useSetup(useInternationalization);
47
+ vm.languages = [
48
+ { id: "test-id", translationCode: { code: "test-code" } },
49
+ ] as Schemas["Language"][];
50
+ expect(vm.getLanguageCodeFromId("test-id")).toBe("test-code");
51
+ });
52
+
53
+ it("getLanguageCodeFromId - no translationCode", async () => {
54
+ const { vm } = useSetup(useInternationalization);
55
+ vm.languages = [{ id: "test-id" }] as Schemas["Language"][];
56
+ expect(vm.getLanguageCodeFromId("test-id")).toBe("");
57
+ });
58
+
59
+ it("getLanguageCodeFromId - no languages", async () => {
60
+ const { vm } = useSetup(useInternationalization);
61
+ expect(() => vm.getLanguageCodeFromId("test-id")).toThrowError();
62
+ });
63
+
64
+ it("getLanguageIdFromCode", async () => {
65
+ const { vm } = useSetup(useInternationalization);
66
+ vm.languages = [
67
+ { id: "test-id", translationCode: { code: "test-code" } },
68
+ ] as Schemas["Language"][];
69
+ expect(vm.getLanguageIdFromCode("test-code")).toBe("test-id");
70
+ });
71
+
72
+ it("getLanguageIdFromCode - no id", async () => {
73
+ const { vm } = useSetup(useInternationalization);
74
+ vm.languages = [
75
+ { translationCode: { code: "test-code" } },
76
+ ] as Schemas["Language"][];
77
+ expect(vm.getLanguageIdFromCode("test-code")).toBe("");
78
+ });
79
+
80
+ it("getLanguageIdFromCode - no languages", async () => {
81
+ const { vm } = useSetup(useInternationalization);
82
+ expect(() => vm.getLanguageIdFromCode("test-code")).toThrowError();
83
+ });
84
+
85
+ it("should return the storefront url with the devStorefrontUrl", async () => {
86
+ const url = "http://frontend.test";
87
+ const { vm } = useSetup(useInternationalization);
88
+
89
+ expect(vm.replaceToDevStorefront(url)).toBe(url);
90
+ });
91
+
92
+ it("formatLink", async () => {
93
+ const { vm } = useSetup(() =>
94
+ useInternationalization((element) => element),
95
+ );
96
+ expect(vm.formatLink("test")).toBe("test");
97
+ });
98
+
99
+ it("formatLink with path", async () => {
100
+ const { vm } = useSetup(() =>
101
+ useInternationalization((element) => element),
102
+ );
103
+ expect(vm.formatLink({ path: "test" })).toStrictEqual({ path: "test" });
104
+ });
105
+
106
+ it("formatLink custom structure", async () => {
107
+ const { vm } = useSetup(() =>
108
+ useInternationalization((element) => element),
109
+ );
110
+ expect(
111
+ vm.formatLink({ custom: "test" } as unknown as RouteObject),
112
+ ).toStrictEqual({
113
+ custom: "test",
114
+ });
115
+ });
116
+
117
+ it("replaceToDevStorefront with devStorefrontUrl", async () => {
118
+ const { vm } = useSetup(
119
+ () => useInternationalization((element) => element),
120
+ {
121
+ shopware: { devStorefrontUrl: "http://dev-storefront.test" },
122
+ },
123
+ );
124
+ expect(vm.getStorefrontUrl()).toBe("http://dev-storefront.test");
125
+ expect(vm.replaceToDevStorefront("http://localhost:3000/test")).toBe(
126
+ "http://dev-storefront.test/test",
127
+ );
128
+ });
129
+
130
+ it("getStorefrontUrl without devStorefrontUrl and window", async () => {
131
+ vi.spyOn(window, "location", "get").mockImplementation(
132
+ () => ({}) as Location,
133
+ );
134
+ const { vm } = useSetup(
135
+ () => useInternationalization((element) => element),
136
+ {
137
+ shopware: { devStorefrontUrl: null },
138
+ },
139
+ );
140
+
141
+ expect(vm.getStorefrontUrl()).toBe("");
142
+ });
143
+
144
+ it("pathResolver absolute path", async () => {
145
+ const { vm } = useSetup(() =>
146
+ useInternationalization((element) => element),
147
+ );
148
+ expect(vm.formatLink("http://www.test.test")).toBe("http://www.test.test");
149
+ });
150
+
151
+ it("pathResolver without resolver", async () => {
152
+ const { vm } = useSetup(() => useInternationalization());
153
+ expect(vm.formatLink("test")).toBe("test");
154
+ });
155
+ });
@@ -0,0 +1,159 @@
1
+ import { urlIsAbsolute } from "@shopware-pwa/helpers-next";
2
+ import type { Ref } from "vue";
3
+ import { useContext, useShopwareContext } from "#imports";
4
+ import type { Schemas, operations } from "#shopware";
5
+
6
+ export type UseInternationalizationReturn = {
7
+ /**
8
+ * StorefrontUrl is needed to specify language of emails
9
+ *
10
+ * @returns {string} storefront URL
11
+ */
12
+ getStorefrontUrl(): string;
13
+ /**
14
+ * Get available languages from backend
15
+ *
16
+ * @returns {Promise<operations['readLanguages post /language']['body']>} list of languages
17
+ */
18
+ getAvailableLanguages(): Promise<
19
+ operations["readLanguages post /language"]["response"]
20
+ >;
21
+ /**
22
+ * Change current language
23
+ *
24
+ * @param {string} languageId
25
+ * @returns {Promise<Schemas['ContextTokenResponse']>} context object
26
+ */
27
+ changeLanguage(
28
+ languageId: string,
29
+ ): Promise<operations["updateContext patch /context"]["response"]>;
30
+ /**
31
+ * Get language code from backend language id
32
+ *
33
+ * @param {string} languageId
34
+ * @returns {string} language code
35
+ */
36
+ getLanguageCodeFromId(languageId: string): string;
37
+ /**
38
+ * Get backend language id from language code
39
+ *
40
+ * @param {string} languageCode
41
+ * @returns {string} language
42
+ */
43
+ getLanguageIdFromCode(languageCode: string): string;
44
+ /**
45
+ * Replace to dev url if it is set
46
+ *
47
+ * @param {string} url
48
+ * @returns {string} prefixed url
49
+ */
50
+ replaceToDevStorefront(url: string): string;
51
+ /**
52
+ * List of available languages
53
+ */
54
+ languages: Ref<Schemas["Language"][]>;
55
+ /**
56
+ * Currently used language
57
+ */
58
+ currentLanguage: Ref<string>;
59
+ /**
60
+ * Current prefix from the context
61
+ */
62
+ currentPrefix: Ref<string>;
63
+ /**
64
+ * Add prefix to the Url
65
+ * @param {string | RouteObject} link
66
+ */
67
+ formatLink(link: string | RouteObject): string | RouteObject;
68
+ };
69
+
70
+ export type RouteObject = {
71
+ path: string;
72
+ [key: string]: unknown;
73
+ };
74
+
75
+ /**
76
+ * Composable for internationalization management.
77
+ * @public
78
+ * @category Context & Language
79
+ */
80
+ export function useInternationalization(
81
+ pathResolver?: (path: string) => string,
82
+ ): UseInternationalizationReturn {
83
+ const { devStorefrontUrl } = useShopwareContext();
84
+ const { apiClient } = useShopwareContext();
85
+
86
+ const _storeLanguages = useContext<Schemas["Language"][]>("swLanguages");
87
+ const _storeCurrentLanguage = useContext<string>(
88
+ "swLanguagesCurrentLanguage",
89
+ );
90
+ const _storeCurrentPrefix = useContext<string>("swLanguagesCurrentPrefix");
91
+
92
+ function getStorefrontUrl() {
93
+ return devStorefrontUrl ?? window.location.origin ?? "";
94
+ }
95
+
96
+ async function getAvailableLanguages() {
97
+ const { data } = await apiClient.invoke("readLanguages post /language");
98
+ _storeLanguages.value = data.elements;
99
+ return data;
100
+ }
101
+
102
+ async function changeLanguage(languageId: string) {
103
+ const { data } = await apiClient.invoke("updateContext patch /context", {
104
+ body: { languageId },
105
+ });
106
+ return data;
107
+ }
108
+
109
+ function getLanguageCodeFromId(languageId: string) {
110
+ return (
111
+ _storeLanguages.value.find((element) => element?.id === languageId)
112
+ ?.translationCode?.code || ""
113
+ );
114
+ }
115
+
116
+ function getLanguageIdFromCode(languageCode: string) {
117
+ return (
118
+ _storeLanguages.value.find(
119
+ (element) => element.translationCode?.code === languageCode,
120
+ )?.id || ""
121
+ );
122
+ }
123
+
124
+ function replaceToDevStorefront(url: string) {
125
+ const current = new URL(url);
126
+ return devStorefrontUrl
127
+ ? url.replace(`${current.protocol}//${current.host}`, devStorefrontUrl)
128
+ : url;
129
+ }
130
+
131
+ function formatLink(link: string | RouteObject) {
132
+ if (!pathResolver) return link;
133
+
134
+ if (typeof link === "string") {
135
+ if (urlIsAbsolute(link)) return link;
136
+ return pathResolver(link);
137
+ }
138
+
139
+ if (link.path) {
140
+ link.path = pathResolver(link.path);
141
+ return link;
142
+ }
143
+
144
+ return link;
145
+ }
146
+
147
+ return {
148
+ getAvailableLanguages,
149
+ getStorefrontUrl,
150
+ changeLanguage,
151
+ getLanguageCodeFromId,
152
+ getLanguageIdFromCode,
153
+ replaceToDevStorefront,
154
+ formatLink,
155
+ languages: _storeLanguages,
156
+ currentLanguage: _storeCurrentLanguage,
157
+ currentPrefix: _storeCurrentPrefix,
158
+ };
159
+ }
@@ -0,0 +1,30 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { useSetup } from "../_test";
3
+ import { cmsAssociations } from "../cms/cmsAssociations";
4
+ import LandingPageMock from "../mocks/LandingPage";
5
+ import { useLandingSearch } from "./useLandingSearch";
6
+
7
+ describe("useLandingSearch", () => {
8
+ it("mergeWishlistProducts", async () => {
9
+ const { vm, injections } = useSetup(useLandingSearch);
10
+ injections.apiClient.invoke.mockResolvedValue({ data: LandingPageMock });
11
+
12
+ const result = vm.search("test");
13
+
14
+ expect(injections.apiClient.invoke).toBeCalledWith(
15
+ expect.stringContaining("readLandingPage"),
16
+ expect.objectContaining({ pathParams: { landingPageId: "test" } }),
17
+ );
18
+ await expect(result).resolves.toStrictEqual(LandingPageMock);
19
+
20
+ vm.search("test", { withCmsAssociations: true });
21
+
22
+ expect(injections.apiClient.invoke).toBeCalledWith(
23
+ expect.stringContaining("readLandingPage"),
24
+ expect.objectContaining({
25
+ pathParams: { landingPageId: "test" },
26
+ body: { associations: cmsAssociations },
27
+ }),
28
+ );
29
+ });
30
+ });
@@ -0,0 +1,58 @@
1
+ import { useShopwareContext } from "#imports";
2
+ import type { Schemas, operations } from "#shopware";
3
+ import { cmsAssociations } from "../cms/cmsAssociations";
4
+
5
+ export type UseLandingSearchReturn = {
6
+ /**
7
+ * Search for landing page by ID
8
+ * Accepts optional media associations
9
+ */
10
+ search(
11
+ navigationId: string,
12
+ options?: {
13
+ withCmsAssociations?: boolean;
14
+ },
15
+ ): Promise<Schemas["LandingPage"]>;
16
+ };
17
+
18
+ /**
19
+ * Composable for landing page search.
20
+ * @public
21
+ * @category Navigation & Routing
22
+ */
23
+ export function useLandingSearch(): {
24
+ search: (
25
+ navigationId: string,
26
+ options?: {
27
+ withCmsAssociations?: boolean;
28
+ },
29
+ ) => Promise<Schemas["LandingPage"]>;
30
+ } {
31
+ const { apiClient } = useShopwareContext();
32
+
33
+ const search = async (
34
+ navigationId: string,
35
+ options?: {
36
+ withCmsAssociations?: boolean;
37
+ },
38
+ ) => {
39
+ const associations: operations["readLandingPage post /landing-page/{landingPageId}"]["body"]["associations"] =
40
+ options?.withCmsAssociations ? cmsAssociations : {};
41
+ const result = await apiClient.invoke(
42
+ "readLandingPage post /landing-page/{landingPageId}",
43
+ {
44
+ pathParams: {
45
+ landingPageId: navigationId,
46
+ },
47
+ body: {
48
+ associations,
49
+ },
50
+ },
51
+ );
52
+ return result.data;
53
+ };
54
+
55
+ return {
56
+ search,
57
+ };
58
+ }
@@ -0,0 +1,249 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import type { Schemas } from "#shopware";
3
+ import { useSetup } from "../_test";
4
+ import ContextError from "../helpers/ContextError";
5
+ import searchMock from "../mocks/Search";
6
+ import { useCategoryListing, useListing } from "./useListing";
7
+
8
+ describe("useListing", () => {
9
+ it("should invoke search", async () => {
10
+ const { vm, injections } = await useSetup(() =>
11
+ useListing({
12
+ listingType: "productSearchListing",
13
+ }),
14
+ );
15
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
16
+ await vm.search({
17
+ search: "",
18
+ });
19
+
20
+ injections.apiClient.invoke.mockResolvedValue(searchMock);
21
+
22
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
23
+ expect.stringContaining("search"),
24
+ expect.objectContaining({}),
25
+ );
26
+
27
+ expect(vm.getCurrentPage).toBe(1);
28
+ expect(vm.getTotalPagesCount).toBe(0);
29
+ expect(vm.getLimit).toBe(10);
30
+ });
31
+
32
+ it('should invoke "readQuotes"', async () => {
33
+ const { vm, injections } = await useSetup(() =>
34
+ useListing({
35
+ listingType: "categoryListing",
36
+ categoryId: "1234",
37
+ }),
38
+ );
39
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
40
+ await vm.search({
41
+ search: "",
42
+ });
43
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
44
+ expect.stringContaining("readProductListing"),
45
+ expect.objectContaining({
46
+ body: {
47
+ search: "",
48
+ },
49
+ headers: {
50
+ "sw-include-seo-urls": true,
51
+ },
52
+ pathParams: {
53
+ categoryId: "1234",
54
+ },
55
+ }),
56
+ );
57
+ });
58
+
59
+ it("should handle context error", async () => {
60
+ let error: unknown | null = null;
61
+ try {
62
+ await useSetup(() =>
63
+ useListing({
64
+ listingType: "categoryListing",
65
+ }),
66
+ );
67
+ } catch (e) {
68
+ error = e;
69
+ }
70
+ expect(error).toEqual(new ContextError("Category"));
71
+ });
72
+
73
+ it("should set current filters", async () => {
74
+ const { vm, injections } = await useSetup(() =>
75
+ useListing({
76
+ listingType: "categoryListing",
77
+ categoryId: "1234",
78
+ }),
79
+ );
80
+
81
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
82
+ vm.setCurrentFilters([{ code: "shipping-free", value: true }]);
83
+
84
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
85
+ expect.stringContaining("readProductListing"),
86
+ expect.objectContaining({
87
+ body: {
88
+ manufacturer: undefined,
89
+ properties: undefined,
90
+ query: undefined,
91
+ "shipping-free": true,
92
+ },
93
+ headers: {
94
+ "sw-include-seo-urls": true,
95
+ },
96
+ pathParams: {
97
+ categoryId: "1234",
98
+ },
99
+ }),
100
+ );
101
+ });
102
+
103
+ it("filtersToQuery", async () => {
104
+ const { vm } = await useSetup(() =>
105
+ useListing({
106
+ listingType: "categoryListing",
107
+ categoryId: "1234",
108
+ }),
109
+ );
110
+
111
+ expect(vm.filtersToQuery({ limit: 10, page: 1 })).toEqual({
112
+ limit: 10,
113
+ page: 1,
114
+ });
115
+ });
116
+
117
+ it("resetFilters", async () => {
118
+ const { vm, injections } = await useSetup(() =>
119
+ useListing({
120
+ listingType: "categoryListing",
121
+ categoryId: "1234",
122
+ }),
123
+ );
124
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
125
+ vm.resetFilters();
126
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
127
+ expect.stringContaining("readProductListing"),
128
+ expect.objectContaining({
129
+ body: {
130
+ search: "",
131
+ },
132
+ headers: {
133
+ "sw-include-seo-urls": true,
134
+ },
135
+ pathParams: {
136
+ categoryId: "1234",
137
+ },
138
+ }),
139
+ );
140
+ });
141
+
142
+ it("useCategoryListing - error is thrown when listingContext was not provided in parent tree", async () => {
143
+ expect(() => useSetup(useCategoryListing)).toThrowError();
144
+ });
145
+
146
+ it("setInitialListing", async () => {
147
+ const { vm } = await useSetup(() =>
148
+ useListing({
149
+ listingType: "categoryListing",
150
+ categoryId: "1234",
151
+ }),
152
+ );
153
+
154
+ vm.setInitialListing({
155
+ apiAlias: "product_listing",
156
+ } as Schemas["ProductListingResult"]);
157
+
158
+ expect(vm.getInitialListing).toEqual({ apiAlias: "product_listing" });
159
+ });
160
+
161
+ it("initSearch", () => {
162
+ const { vm, injections } = useSetup(() =>
163
+ useListing({ listingType: "productSearchListing" }),
164
+ );
165
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
166
+ vm.initSearch({
167
+ search: "test",
168
+ });
169
+
170
+ expect(vm.getCurrentPage).toBe(1);
171
+ expect(vm.getTotalPagesCount).toBe(0);
172
+ expect(vm.getLimit).toBe(10);
173
+ });
174
+
175
+ it("loadMore", async () => {
176
+ const { vm, injections } = await useSetup(() =>
177
+ useListing({
178
+ listingType: "productSearchListing",
179
+ }),
180
+ );
181
+ injections.apiClient.invoke.mockResolvedValue({ data: { page: 1 } });
182
+ await vm.loadMore();
183
+
184
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
185
+ expect.stringContaining("search"),
186
+ expect.objectContaining({
187
+ body: {
188
+ p: 2,
189
+ },
190
+ headers: {
191
+ "sw-include-seo-urls": true,
192
+ },
193
+ }),
194
+ );
195
+ });
196
+
197
+ it("changeCurrentSortingOrder", async () => {
198
+ const { vm, injections } = await useSetup(() =>
199
+ useListing({
200
+ listingType: "productSearchListing",
201
+ }),
202
+ );
203
+ injections.apiClient.invoke.mockResolvedValue({ data: { page: 1 } });
204
+ vm.changeCurrentSortingOrder("test");
205
+
206
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
207
+ expect.stringContaining("search"),
208
+ expect.objectContaining({
209
+ body: {
210
+ order: "test",
211
+ },
212
+ headers: {
213
+ "sw-include-seo-urls": true,
214
+ },
215
+ }),
216
+ );
217
+ });
218
+
219
+ it("changeCurrentPage", async () => {
220
+ const { vm, injections } = await useSetup(() =>
221
+ useListing({
222
+ listingType: "productSearchListing",
223
+ }),
224
+ );
225
+ injections.apiClient.invoke.mockResolvedValue({ data: { page: 1 } });
226
+ vm.changeCurrentPage(2);
227
+
228
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
229
+ expect.stringContaining("search"),
230
+ expect.objectContaining({
231
+ body: {
232
+ page: 2,
233
+ },
234
+ headers: {
235
+ "sw-include-seo-urls": true,
236
+ },
237
+ }),
238
+ );
239
+ });
240
+
241
+ it("getAvailableFilters", async () => {
242
+ const { vm } = await useSetup(() =>
243
+ useListing({
244
+ listingType: "productSearchListing",
245
+ }),
246
+ );
247
+ expect(vm.getAvailableFilters).toEqual([]);
248
+ });
249
+ });