@shopware/composables 1.9.1 → 1.10.0

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.
@@ -89,27 +89,44 @@ describe("useSyncWishlist", () => {
89
89
 
90
90
  describe("getWishlistProducts - error", () => {
91
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
- ],
92
+ const { vm, injections } = useSetup(() => useSyncWishlist());
93
+ // First, set up a successful call to populate state
94
+ injections.apiClient.invoke.mockResolvedValueOnce({
95
+ data: {
96
+ products: {
97
+ total: 9,
98
+ page: 1,
99
+ elements: [
100
+ {
101
+ id: "test-id",
103
102
  },
104
- } as unknown as FetchResponse<{
105
- errors: Array<{ title: string }>;
106
- }>);
107
- }),
103
+ ],
104
+ },
108
105
  },
109
106
  });
110
- vm.getWishlistProducts();
111
- expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
107
+ await vm.getWishlistProducts();
112
108
  expect(vm.count).toBe(9);
109
+ expect(vm.items.length).toBe(1);
110
+
111
+ // Now test error handling - state should be cleared on error
112
+ injections.apiClient.invoke.mockImplementation(() => {
113
+ throw new ApiClientError({
114
+ status: 500,
115
+ _data: {
116
+ errors: [
117
+ {
118
+ title: "Test error",
119
+ },
120
+ ],
121
+ },
122
+ } as unknown as FetchResponse<{
123
+ errors: Array<{ title: string }>;
124
+ }>);
125
+ });
126
+ await vm.getWishlistProducts();
127
+ expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
128
+ // State should be cleared when error occurs
129
+ expect(vm.count).toBe(0);
113
130
  expect(vm.items.length).toBe(0);
114
131
  });
115
132
  });
@@ -1,4 +1,3 @@
1
- import { ApiClientError } from "@shopware/api-client";
2
1
  import { computed, ref } from "vue";
3
2
  import type { ComputedRef, Ref } from "vue";
4
3
  import { useShopwareContext } from "#imports";
@@ -35,12 +34,26 @@ export type UseSyncWishlistReturn = {
35
34
  * Current page number
36
35
  */
37
36
  currentPage: ComputedRef<number>;
37
+ /**
38
+ * Limit number of products per page
39
+ */
40
+ limit: ComputedRef<number>;
41
+ /**
42
+ * Wishlist products
43
+ */
44
+ products: ComputedRef<Schemas["Product"][]>;
45
+ /**
46
+ * Indicates if the wishlist is loading
47
+ */
48
+ isLoading: Ref<boolean>;
38
49
  };
39
50
 
40
51
  const _wishlistItems: Ref<string[]> = ref([]);
52
+ const _wishlistProducts: Ref<Schemas["Product"][]> = ref([]);
41
53
  const _currentPage: Ref<number> = ref(1);
54
+ const _limit: Ref<number> = ref(15);
42
55
  const totalWishlistItemsCount: Ref<number> = ref(0);
43
-
56
+ const isLoading: Ref<boolean> = ref(false);
44
57
  /**
45
58
  * Composable to manage wishlist via API
46
59
  * @public
@@ -81,14 +94,15 @@ export function useSyncWishlist(): UseSyncWishlistReturn {
81
94
  _wishlistItems.value = [
82
95
  ...response.data.products.elements.map((element) => element.id),
83
96
  ];
97
+ _wishlistProducts.value = response.data.products.elements;
84
98
  totalWishlistItemsCount.value = response.data.products.total ?? 0;
85
99
  _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
- }
100
+ _limit.value = response.data.products.limit ?? 15;
101
+ } catch (error) {
102
+ console.error("[useSyncWishlist][getWishlistProducts][error]:", error);
103
+ _wishlistItems.value = [];
104
+ _wishlistProducts.value = [];
105
+ totalWishlistItemsCount.value = 0;
92
106
  }
93
107
  }
94
108
 
@@ -104,14 +118,19 @@ export function useSyncWishlist(): UseSyncWishlistReturn {
104
118
  const items = computed(() => _wishlistItems.value);
105
119
  const count = computed(() => totalWishlistItemsCount.value);
106
120
  const currentPage = computed(() => _currentPage.value);
121
+ const products = computed(() => _wishlistProducts.value);
122
+ const limit = computed(() => _limit.value);
107
123
 
108
124
  return {
109
125
  getWishlistProducts,
110
126
  addToWishlistSync,
111
127
  removeFromWishlistSync,
112
128
  mergeWishlistProducts,
129
+ products,
113
130
  items,
114
131
  count,
115
132
  currentPage,
133
+ isLoading,
134
+ limit,
116
135
  };
117
136
  }
@@ -1,5 +1,5 @@
1
1
  import type { operations } from "@shopware/api-client/api-types";
2
- import { describe, expect, it, vi } from "vitest";
2
+ import { afterEach, describe, expect, it, vi } from "vitest";
3
3
  import { ref } from "vue";
4
4
  import { useSetup } from "../_test";
5
5
  import { useUser } from "./useUser";
@@ -16,12 +16,13 @@ vi.mock("../useCart/useCart.ts", async () => {
16
16
  });
17
17
 
18
18
  const refreshSessionContextSpy = vi.fn();
19
+ const userFromContextRef = ref();
19
20
  vi.mock("../useSessionContext/useSessionContext.ts", async () => {
20
21
  return {
21
22
  useSessionContext: () => {
22
23
  return {
23
24
  refreshSessionContext: refreshSessionContextSpy,
24
- userFromContext: ref(),
25
+ userFromContext: userFromContextRef,
25
26
  };
26
27
  },
27
28
  };
@@ -52,6 +53,10 @@ const REGISTRATION_DATA: Omit<
52
53
  };
53
54
 
54
55
  describe("useUser", () => {
56
+ afterEach(() => {
57
+ userFromContextRef.value = undefined;
58
+ });
59
+
55
60
  it("login function", async () => {
56
61
  const { vm, injections } = useSetup(() => useUser());
57
62
 
@@ -201,14 +206,43 @@ describe("useUser", () => {
201
206
  expect(vm.country).toEqual({ name: "Poland", id: "2de9ecc24e" });
202
207
  });
203
208
 
204
- it("updatePersonalInfo", async () => {
209
+ it("updatePersonalInfo - business", async () => {
205
210
  const { vm, injections } = useSetup(() => useUser());
206
211
  injections.apiClient.invoke.mockResolvedValue({ data: {} });
212
+
213
+ await vm.updatePersonalInfo({
214
+ firstName: "test",
215
+ lastName: "test",
216
+ salutationId: "d5e543063dd642b48ef94b02d68e5785",
217
+ title: "",
218
+ accountType: "business",
219
+ company: "test",
220
+ vatIds: ["1234567890"],
221
+ });
222
+
223
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
224
+ expect.stringContaining("changeProfile"),
225
+ expect.objectContaining({
226
+ body: {
227
+ firstName: "test",
228
+ lastName: "test",
229
+ salutationId: "d5e543063dd642b48ef94b02d68e5785",
230
+ title: "",
231
+ accountType: "business",
232
+ company: "test",
233
+ vatIds: ["1234567890"],
234
+ },
235
+ }),
236
+ );
237
+
207
238
  await vm.updatePersonalInfo({
208
239
  firstName: "test",
209
240
  lastName: "test",
210
241
  salutationId: "d5e543063dd642b48ef94b02d68e5785",
211
242
  title: "",
243
+ accountType: "business",
244
+ company: "test",
245
+ vatIds: "1234567890" as unknown as [string, ...string[]],
212
246
  });
213
247
 
214
248
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
@@ -219,6 +253,34 @@ describe("useUser", () => {
219
253
  lastName: "test",
220
254
  salutationId: "d5e543063dd642b48ef94b02d68e5785",
221
255
  title: "",
256
+ accountType: "business",
257
+ company: "test",
258
+ vatIds: ["1234567890"],
259
+ },
260
+ }),
261
+ );
262
+ });
263
+
264
+ it("updatePersonalInfo - private", async () => {
265
+ const { vm, injections } = useSetup(() => useUser());
266
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
267
+ await vm.updatePersonalInfo({
268
+ firstName: "test",
269
+ lastName: "test",
270
+ salutationId: "d5e543063dd642b48ef94b02d68e5785",
271
+ title: "",
272
+ accountType: "private",
273
+ });
274
+
275
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
276
+ expect.stringContaining("changeProfile"),
277
+ expect.objectContaining({
278
+ body: {
279
+ firstName: "test",
280
+ lastName: "test",
281
+ salutationId: "d5e543063dd642b48ef94b02d68e5785",
282
+ title: "",
283
+ accountType: "private",
222
284
  },
223
285
  }),
224
286
  );
@@ -287,4 +349,65 @@ describe("useUser", () => {
287
349
 
288
350
  expect(vm.salutation).toEqual({ id: "test", name: "test" });
289
351
  });
352
+
353
+ it("userDefaultPaymentMethod - should fallback to legacy defaultPaymentMethod when lastPaymentMethod is not available", () => {
354
+ const { vm } = useSetup(() => useUser());
355
+
356
+ // Mock the sessionContext to return a user with only legacy defaultPaymentMethod
357
+ const mockUser = {
358
+ // No lastPaymentMethod property (undefined)
359
+ defaultPaymentMethod: {
360
+ translated: { name: "Legacy Payment Method" },
361
+ },
362
+ };
363
+
364
+ // Set the userFromContext ref that the composable uses
365
+ userFromContextRef.value = mockUser;
366
+
367
+ expect(vm.userDefaultPaymentMethod).toEqual({
368
+ name: "Legacy Payment Method",
369
+ });
370
+
371
+ // Clean up
372
+ userFromContextRef.value = undefined;
373
+ });
374
+
375
+ it("userDefaultPaymentMethod - should return null when no payment methods are available", () => {
376
+ const { vm } = useSetup(() => useUser());
377
+
378
+ // Mock user with no payment methods at all
379
+ const mockUser = {
380
+ // No lastPaymentMethod and no defaultPaymentMethod
381
+ };
382
+
383
+ userFromContextRef.value = mockUser;
384
+
385
+ expect(vm.userDefaultPaymentMethod).toBeNull();
386
+
387
+ // Clean up
388
+ userFromContextRef.value = undefined;
389
+ });
390
+
391
+ it("userDefaultPaymentMethod - should fallback when lastPaymentMethod.translated is falsy", () => {
392
+ const { vm } = useSetup(() => useUser());
393
+
394
+ // Mock user with falsy lastPaymentMethod.translated but valid defaultPaymentMethod
395
+ const mockUser = {
396
+ lastPaymentMethod: {
397
+ translated: null, // falsy translated
398
+ },
399
+ defaultPaymentMethod: {
400
+ translated: { name: "Fallback Payment Method" },
401
+ },
402
+ };
403
+
404
+ userFromContextRef.value = mockUser;
405
+
406
+ expect(vm.userDefaultPaymentMethod).toEqual({
407
+ name: "Fallback Payment Method",
408
+ });
409
+
410
+ // Clean up
411
+ userFromContextRef.value = undefined;
412
+ });
290
413
  });
@@ -141,7 +141,11 @@ export function useUser(): UseUserReturn {
141
141
  const { refreshCart } = useCart();
142
142
 
143
143
  const userDefaultPaymentMethod = computed(
144
- () => user.value?.defaultPaymentMethod?.translated || null,
144
+ () =>
145
+ user.value?.lastPaymentMethod?.translated ||
146
+ // @ts-expect-error TODO: [MAJOR] Removed since 6.7
147
+ user.value?.defaultPaymentMethod?.translated ||
148
+ null,
145
149
  );
146
150
  const userDefaultBillingAddress = computed(
147
151
  () => user.value?.defaultBillingAddress || null,
@@ -277,6 +281,7 @@ export function useUser(): UseUserReturn {
277
281
  paymentMethodId: string,
278
282
  ): Promise<void> {
279
283
  await apiClient.invoke(
284
+ // @ts-expect-error TODO: [MAJOR] Removed since 6.7
280
285
  "changePaymentMethod post /account/change-payment-method/{paymentMethodId}",
281
286
  {
282
287
  pathParams: { paymentMethodId },
@@ -55,6 +55,9 @@ describe("useWishlist - not logged in user", () => {
55
55
  removeFromWishlistSync: () => undefined,
56
56
  count: computed(() => 17),
57
57
  currentPage: computed(() => 2),
58
+ limit: computed(() => 15),
59
+ products: computed((): Schemas["Product"][] => []),
60
+ isLoading: computed(() => false),
58
61
  });
59
62
  const { vm } = useSetup(() => useWishlist());
60
63
 
@@ -77,6 +80,9 @@ describe("useWishlist - not logged in user", () => {
77
80
  removeFromWishlistSync: () => undefined,
78
81
  count: computed(() => 15),
79
82
  currentPage: computed(() => 2),
83
+ limit: computed(() => 15),
84
+ products: computed((): Schemas["Product"][] => []),
85
+ isLoading: computed(() => false),
80
86
  });
81
87
  const { vm } = useSetup(() => useWishlist());
82
88
 
@@ -141,6 +147,9 @@ describe("useWishlist - logged in user", () => {
141
147
  removeFromWishlistSync: () => undefined,
142
148
  count: computed(() => 15),
143
149
  currentPage: computed(() => 1),
150
+ limit: computed(() => 15),
151
+ products: computed((): Schemas["Product"][] => []),
152
+ isLoading: computed(() => false),
144
153
  });
145
154
  const { vm } = useSetup(() => useWishlist());
146
155
 
@@ -40,6 +40,14 @@ export type UseWishlistReturn = {
40
40
  * Indicates if the wishlist can be synced
41
41
  */
42
42
  canSyncWishlist: ComputedRef<boolean>;
43
+ /**
44
+ * Wishlist products
45
+ */
46
+ products: ComputedRef<Schemas["Product"][]>;
47
+ /**
48
+ * Limit number of products per page
49
+ */
50
+ limit: ComputedRef<number>;
43
51
  };
44
52
 
45
53
  /**
@@ -65,21 +73,27 @@ export function useWishlist(): UseWishlistReturn {
65
73
  removeFromWishlistSync,
66
74
  count: countSync,
67
75
  currentPage,
76
+ products,
77
+ limit: limitSync,
68
78
  } = useSyncWishlist();
69
79
 
70
80
  const limit = ref<number>(15);
71
81
 
72
82
  const getWishlistProducts = async (query?: Schemas["Criteria"]) => {
73
- const wishlistQuery = {} as Schemas["Criteria"];
83
+ let wishlistQuery = {
84
+ limit: limit.value,
85
+ } as Schemas["Criteria"];
86
+
74
87
  if (query) {
75
- defu(wishlistQuery, query);
76
- }
77
- if (query?.limit) {
78
- limit.value = query.limit;
88
+ wishlistQuery = defu(query, wishlistQuery);
89
+
90
+ if (query.limit) {
91
+ limit.value = query.limit;
92
+ }
79
93
  }
80
94
 
81
95
  if (canSyncWishlist.value) {
82
- await getWishlistProductsSync(query);
96
+ await getWishlistProductsSync(wishlistQuery);
83
97
  } else {
84
98
  getWishlistProductsLocal();
85
99
  }
@@ -124,5 +138,7 @@ export function useWishlist(): UseWishlistReturn {
124
138
  totalPagesCount,
125
139
  count,
126
140
  canSyncWishlist,
141
+ products,
142
+ limit: limitSync,
127
143
  };
128
144
  }