@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,117 @@
1
+ import { ApiClientError } from "@shopware/api-client";
2
+ import type { FetchResponse } from "ofetch";
3
+ import { beforeEach, describe, expect, it, vi } from "vitest";
4
+ import { useSetup } from "../_test";
5
+ import { useSyncWishlist } from "./useSyncWishlist";
6
+
7
+ describe("useSyncWishlist", () => {
8
+ beforeEach(() => {
9
+ vi.clearAllMocks();
10
+ });
11
+
12
+ const consoleErrorSpy = vi.spyOn(console, "error");
13
+ consoleErrorSpy.mockImplementation(() => {});
14
+ describe("methods", () => {
15
+ describe("addToWishlist", () => {
16
+ it("should add product to the wishlist", async () => {
17
+ const { vm } = useSetup(() => useSyncWishlist());
18
+
19
+ await expect(vm.addToWishlistSync("some-id")).resolves.toBe(undefined);
20
+ });
21
+ });
22
+
23
+ describe("removeFromWishlist", () => {
24
+ it("should remove product from the wishlist", () => {
25
+ const { vm } = useSetup(() => useSyncWishlist());
26
+
27
+ vm.getWishlistProducts();
28
+ vm.removeFromWishlistSync("some-id");
29
+ // Mocked value
30
+ expect(vm.count).toBe(vm.count);
31
+ });
32
+ });
33
+
34
+ describe("mergeWishlistProducts", () => {
35
+ it("should sync wishlist", async () => {
36
+ const { vm } = useSetup(() => useSyncWishlist());
37
+
38
+ await expect(
39
+ vm.mergeWishlistProducts(["test1", "test2"]),
40
+ ).resolves.toEqual(undefined);
41
+ });
42
+ });
43
+
44
+ describe("getWishlistProducts", () => {
45
+ it("should get wishlist products", async () => {
46
+ const { vm, injections } = useSetup(() => useSyncWishlist());
47
+ injections.apiClient.invoke.mockResolvedValue({
48
+ data: {
49
+ products: {
50
+ elements: [],
51
+ },
52
+ },
53
+ });
54
+ await vm.getWishlistProducts();
55
+
56
+ expect(vm.count).toBe(0);
57
+ expect(vm.items.length).toBe(0);
58
+ });
59
+
60
+ it("getWishlistProducts", async () => {
61
+ const { vm, injections } = useSetup(() => useSyncWishlist());
62
+ injections.apiClient.invoke.mockResolvedValue({
63
+ data: {
64
+ products: {
65
+ total: 9,
66
+ page: 1,
67
+ elements: [
68
+ {
69
+ id: "test-id",
70
+ },
71
+ ],
72
+ },
73
+ },
74
+ });
75
+ await vm.getWishlistProducts();
76
+
77
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
78
+ expect.stringContaining("readCustomerWishlist"),
79
+ expect.objectContaining({
80
+ body: {
81
+ "total-count-mode": "exact",
82
+ },
83
+ }),
84
+ );
85
+ expect(vm.count).toBe(9);
86
+ expect(vm.items.length).toBe(1);
87
+ });
88
+ });
89
+
90
+ describe("getWishlistProducts - error", () => {
91
+ it("should handle error after fetching product wishlist", async () => {
92
+ const { vm } = useSetup(() => useSyncWishlist(), {
93
+ apiClient: {
94
+ invoke: vi.fn().mockImplementation(() => {
95
+ throw new ApiClientError({
96
+ status: 500,
97
+ _data: {
98
+ errors: [
99
+ {
100
+ title: "Test error",
101
+ },
102
+ ],
103
+ },
104
+ } as unknown as FetchResponse<{
105
+ errors: Array<{ title: string }>;
106
+ }>);
107
+ }),
108
+ },
109
+ });
110
+ vm.getWishlistProducts();
111
+ expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
112
+ expect(vm.count).toBe(9);
113
+ expect(vm.items.length).toBe(0);
114
+ });
115
+ });
116
+ });
117
+ });
@@ -0,0 +1,117 @@
1
+ import { ApiClientError } from "@shopware/api-client";
2
+ import { computed, ref } from "vue";
3
+ import type { ComputedRef, Ref } from "vue";
4
+ import { useShopwareContext } from "#imports";
5
+ import type { Schemas } from "#shopware";
6
+
7
+ export type UseSyncWishlistReturn = {
8
+ /**
9
+ * Get products from wishlist
10
+ */
11
+ getWishlistProducts(
12
+ defaultSearchCriteria?: Schemas["Criteria"],
13
+ ): Promise<void>;
14
+ /**
15
+ * Merge products with wishlist already existing in API wishlist
16
+ */
17
+ mergeWishlistProducts(itemsToMerge: string[]): void;
18
+ /**
19
+ * Add product to wishlist
20
+ */
21
+ addToWishlistSync(id: string): void;
22
+ /**
23
+ * Remove product from wishlist
24
+ */
25
+ removeFromWishlistSync(id: string): void;
26
+ /**
27
+ * Wishlist items (Product IDs)
28
+ */
29
+ items: ComputedRef<string[]>;
30
+ /**
31
+ * Wishlist items count
32
+ */
33
+ count: ComputedRef<number>;
34
+ /**
35
+ * Current page number
36
+ */
37
+ currentPage: ComputedRef<number>;
38
+ };
39
+
40
+ const _wishlistItems: Ref<string[]> = ref([]);
41
+ const _currentPage: Ref<number> = ref(1);
42
+ const totalWishlistItemsCount: Ref<number> = ref(0);
43
+
44
+ /**
45
+ * Composable to manage wishlist via API
46
+ * @public
47
+ * @category Wishlist
48
+ */
49
+ export function useSyncWishlist(): UseSyncWishlistReturn {
50
+ const { apiClient } = useShopwareContext();
51
+ async function addToWishlistSync(id: string) {
52
+ await apiClient.invoke(
53
+ "addProductOnWishlist post /customer/wishlist/add/{productId}",
54
+ {
55
+ pathParams: { productId: id },
56
+ },
57
+ );
58
+ }
59
+
60
+ async function removeFromWishlistSync(id: string) {
61
+ await apiClient.invoke(
62
+ "deleteProductOnWishlist delete /customer/wishlist/delete/{productId}",
63
+ {
64
+ pathParams: { productId: id },
65
+ },
66
+ );
67
+ }
68
+
69
+ /**
70
+ * Fetch wishlist items
71
+ * Only for logged-in users
72
+ */
73
+ async function getWishlistProducts(
74
+ defaultSearchCriteria?: Schemas["Criteria"],
75
+ ) {
76
+ try {
77
+ const response = await apiClient.invoke(
78
+ "readCustomerWishlist post /customer/wishlist",
79
+ { body: { ...defaultSearchCriteria, "total-count-mode": "exact" } },
80
+ );
81
+ _wishlistItems.value = [
82
+ ...response.data.products.elements.map((element) => element.id),
83
+ ];
84
+ totalWishlistItemsCount.value = response.data.products.total ?? 0;
85
+ _currentPage.value = response.data.products.page ?? 1;
86
+ } catch (e) {
87
+ if (e instanceof ApiClientError) {
88
+ // If 404 ignore printing error and reset wishlist
89
+ if (e.status !== 404) console.error(e);
90
+ _wishlistItems.value = [];
91
+ }
92
+ }
93
+ }
94
+
95
+ async function mergeWishlistProducts(productIds: string[]) {
96
+ await apiClient.invoke(
97
+ "mergeProductOnWishlist post /customer/wishlist/merge",
98
+ {
99
+ body: { productIds },
100
+ },
101
+ );
102
+ }
103
+
104
+ const items = computed(() => _wishlistItems.value);
105
+ const count = computed(() => totalWishlistItemsCount.value);
106
+ const currentPage = computed(() => _currentPage.value);
107
+
108
+ return {
109
+ getWishlistProducts,
110
+ addToWishlistSync,
111
+ removeFromWishlistSync,
112
+ mergeWishlistProducts,
113
+ items,
114
+ count,
115
+ currentPage,
116
+ };
117
+ }
@@ -0,0 +1,25 @@
1
+ import { buildUrlPrefix } from "@shopware-pwa/helpers-next";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import { useSetup } from "../_test";
4
+ import { useUrlResolver } from "./useUrlResolver";
5
+
6
+ vi.mock("@shopware-pwa/helpers-next", async () => {
7
+ return {
8
+ buildUrlPrefix: vi.fn(),
9
+ };
10
+ });
11
+
12
+ describe("useUrlResolver", () => {
13
+ it("resolve url", async () => {
14
+ const { vm } = await useSetup(useUrlResolver);
15
+ expect(vm.resolveUrl("/test-url/abc")).toBe("/test-url/abc");
16
+ expect(() => vm.resolveUrl("x".repeat(2084))).toThrow();
17
+ });
18
+
19
+ it("resolve url with navigation pattern", async () => {
20
+ const { vm } = await useSetup(useUrlResolver);
21
+ vm.resolveUrl("/de/navigation/123");
22
+
23
+ expect(buildUrlPrefix).toHaveBeenCalled();
24
+ });
25
+ });
@@ -0,0 +1,31 @@
1
+ import { buildUrlPrefix } from "@shopware-pwa/helpers-next";
2
+ import { inject } from "vue";
3
+
4
+ export function useUrlResolver() {
5
+ const getUrlPrefix = () => inject("urlPrefix", "");
6
+
7
+ const resolveUrl = (url: string) => {
8
+ // @see: https://codeql.github.com/codeql-query-help/javascript/js-polynomial-redos/
9
+ if (url.length > 2083) {
10
+ throw new Error("URL Input too long");
11
+ }
12
+
13
+ const navigationPattern = /[a-zA-Z0-9]+\/navigation\/[a-zA-Z0-9]+/;
14
+ const urlPrefix = getUrlPrefix();
15
+
16
+ if (navigationPattern.test(url)) {
17
+ const newUrl = url.split("/").slice(1);
18
+
19
+ if (newUrl.length > 0) {
20
+ return `${buildUrlPrefix(newUrl.join("/"), urlPrefix)}`;
21
+ }
22
+ }
23
+
24
+ return url;
25
+ };
26
+
27
+ return {
28
+ resolveUrl,
29
+ getUrlPrefix,
30
+ };
31
+ }
@@ -0,0 +1,290 @@
1
+ import type { operations } from "@shopware/api-client/api-types";
2
+ import { describe, expect, it, vi } from "vitest";
3
+ import { ref } from "vue";
4
+ import { useSetup } from "../_test";
5
+ import { useUser } from "./useUser";
6
+
7
+ const refreshCartSpy = vi.fn();
8
+ vi.mock("../useCart/useCart.ts", async () => {
9
+ return {
10
+ useCart: () => {
11
+ return {
12
+ refreshCart: refreshCartSpy,
13
+ };
14
+ },
15
+ };
16
+ });
17
+
18
+ const refreshSessionContextSpy = vi.fn();
19
+ vi.mock("../useSessionContext/useSessionContext.ts", async () => {
20
+ return {
21
+ useSessionContext: () => {
22
+ return {
23
+ refreshSessionContext: refreshSessionContextSpy,
24
+ userFromContext: ref(),
25
+ };
26
+ },
27
+ };
28
+ });
29
+
30
+ const REGISTRATION_DATA: Omit<
31
+ operations["register post /account/register"]["body"],
32
+ "storefrontUrl"
33
+ > = {
34
+ acceptedDataProtection: true,
35
+ accountType: "private",
36
+ salutationId: "d5e543063dd642b48ef94b02d68e5785",
37
+ firstName: "test",
38
+ lastName: "test",
39
+ email: "test@test.testwwww",
40
+ password: "ZAQ!2wsx",
41
+ billingAddress: {
42
+ street: "asasa",
43
+ zipcode: "12-123",
44
+ city: "sadasdas",
45
+ countryId: "2de9ecc24e7b43d283302abba082b7ce",
46
+ countryStateId: "",
47
+ customerId: "",
48
+ id: "",
49
+ firstName: "test",
50
+ lastName: "test",
51
+ },
52
+ };
53
+
54
+ describe("useUser", () => {
55
+ it("login function", async () => {
56
+ const { vm, injections } = useSetup(() => useUser());
57
+
58
+ await vm.login({ username: "test@test.zzz", password: "test" });
59
+
60
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
61
+ expect.stringContaining("login"),
62
+ expect.anything(),
63
+ );
64
+
65
+ expect(refreshCartSpy).toHaveBeenCalled();
66
+ expect(refreshSessionContextSpy).toHaveBeenCalled();
67
+ });
68
+
69
+ it("register function", async () => {
70
+ const { vm, injections } = useSetup(() => useUser());
71
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
72
+ await vm.register(REGISTRATION_DATA);
73
+
74
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
75
+ expect.stringContaining("register"),
76
+ expect.objectContaining({
77
+ body: {
78
+ ...REGISTRATION_DATA,
79
+ storefrontUrl: "http://localhost:3000", // This is the default value from the useInternationalization
80
+ },
81
+ }),
82
+ );
83
+ });
84
+
85
+ it("register function with refresh", async () => {
86
+ const { vm, injections } = useSetup(() => useUser());
87
+ injections.apiClient.invoke.mockResolvedValue({
88
+ data: {
89
+ active: true,
90
+ id: "test123",
91
+ guest: false,
92
+ },
93
+ });
94
+
95
+ await vm.register(REGISTRATION_DATA);
96
+
97
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
98
+ expect.stringContaining("register"),
99
+ expect.objectContaining({
100
+ body: {
101
+ ...REGISTRATION_DATA,
102
+ storefrontUrl: "http://localhost:3000", // This is the default value from the useInternationalization
103
+ },
104
+ }),
105
+ );
106
+ expect(vm.isLoggedIn).toBe(true);
107
+ });
108
+
109
+ it("register function with double opt-in option should not automatically log user in", async () => {
110
+ const { vm, injections } = useSetup(() => useUser());
111
+ injections.apiClient.invoke.mockResolvedValue({
112
+ data: {
113
+ active: true,
114
+ doubleOptInRegistration: true,
115
+ id: "test123",
116
+ guest: false,
117
+ },
118
+ });
119
+
120
+ await vm.register(REGISTRATION_DATA);
121
+
122
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
123
+ expect.stringContaining("register"),
124
+ expect.objectContaining({
125
+ body: {
126
+ ...REGISTRATION_DATA,
127
+ storefrontUrl: "http://localhost:3000", // This is the default value from the useInternationalization
128
+ },
129
+ }),
130
+ );
131
+ expect(vm.isLoggedIn).toBe(false);
132
+ });
133
+
134
+ it("logout", async () => {
135
+ const { vm, injections } = useSetup(() => useUser());
136
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
137
+ await vm.logout();
138
+
139
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
140
+ expect.stringContaining("logoutCustomer"),
141
+ );
142
+ });
143
+
144
+ it("refreshUser", async () => {
145
+ const { vm, injections } = useSetup(() => useUser());
146
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
147
+ await vm.refreshUser();
148
+
149
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
150
+ expect.stringContaining("readCustomer"),
151
+ expect.anything(),
152
+ );
153
+ });
154
+
155
+ it("refreshUser - error", async () => {
156
+ const { vm } = useSetup(() => useUser(), {
157
+ apiClient: {
158
+ invoke: vi.fn().mockImplementation(() => {
159
+ throw new Error("error test");
160
+ }),
161
+ },
162
+ });
163
+
164
+ await expect(async () => {
165
+ await vm.refreshUser();
166
+ }).rejects.toThrowError();
167
+ });
168
+
169
+ it("loadCountry", async () => {
170
+ const countryId = "2de9ecc24e7b43d283302abba082b7ce";
171
+ const { vm, injections } = useSetup(() => useUser());
172
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
173
+ await vm.loadCountry(countryId);
174
+
175
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
176
+ expect.stringContaining("readCountry"),
177
+ expect.objectContaining({
178
+ body: {
179
+ filter: [
180
+ {
181
+ field: "id",
182
+ type: "equals",
183
+ value: countryId,
184
+ },
185
+ ],
186
+ },
187
+ }),
188
+ );
189
+
190
+ injections.apiClient.invoke.mockResolvedValue({
191
+ data: {
192
+ elements: [
193
+ {
194
+ name: "Poland",
195
+ id: "2de9ecc24e",
196
+ },
197
+ ],
198
+ },
199
+ });
200
+ await vm.loadCountry(countryId);
201
+ expect(vm.country).toEqual({ name: "Poland", id: "2de9ecc24e" });
202
+ });
203
+
204
+ it("updatePersonalInfo", async () => {
205
+ const { vm, injections } = useSetup(() => useUser());
206
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
207
+ await vm.updatePersonalInfo({
208
+ firstName: "test",
209
+ lastName: "test",
210
+ salutationId: "d5e543063dd642b48ef94b02d68e5785",
211
+ title: "",
212
+ });
213
+
214
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
215
+ expect.stringContaining("changeProfile"),
216
+ expect.objectContaining({
217
+ body: {
218
+ firstName: "test",
219
+ lastName: "test",
220
+ salutationId: "d5e543063dd642b48ef94b02d68e5785",
221
+ title: "",
222
+ },
223
+ }),
224
+ );
225
+ });
226
+
227
+ it("updateEmail", () => {
228
+ const { vm, injections } = useSetup(() => useUser());
229
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
230
+ vm.updateEmail({
231
+ email: "test@test.test",
232
+ emailConfirmation: "test@test.test",
233
+ password: "test",
234
+ });
235
+
236
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
237
+ expect.stringContaining("changeEmail"),
238
+ expect.objectContaining({
239
+ body: {
240
+ email: "test@test.test",
241
+ emailConfirmation: "test@test.test",
242
+ password: "test",
243
+ },
244
+ }),
245
+ );
246
+ });
247
+
248
+ it("setDefaultPaymentMethod", async () => {
249
+ const { vm, injections } = useSetup(() => useUser());
250
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
251
+ vm.setDefaultPaymentMethod("test");
252
+
253
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
254
+ expect.stringContaining("changePaymentMethod"),
255
+ expect.objectContaining({
256
+ pathParams: {
257
+ paymentMethodId: "test",
258
+ },
259
+ }),
260
+ );
261
+ });
262
+
263
+ it("loadSalutation", async () => {
264
+ const { vm, injections } = useSetup(() => useUser());
265
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
266
+ await vm.loadSalutation("test");
267
+
268
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
269
+ expect.stringContaining("readSalutation"),
270
+ expect.objectContaining({
271
+ body: {
272
+ filter: [
273
+ {
274
+ field: "id",
275
+ type: "equals",
276
+ value: "test",
277
+ },
278
+ ],
279
+ },
280
+ }),
281
+ );
282
+
283
+ injections.apiClient.invoke.mockResolvedValue({
284
+ data: { elements: [{ id: "test", name: "test" }] },
285
+ });
286
+ await vm.loadSalutation("test");
287
+
288
+ expect(vm.salutation).toEqual({ id: "test", name: "test" });
289
+ });
290
+ });