@shopware/composables 1.9.0 → 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.
Files changed (34) hide show
  1. package/README.md +30 -6
  2. package/dist/index.d.mts +99 -29
  3. package/dist/index.d.ts +99 -29
  4. package/dist/index.mjs +104 -59
  5. package/package.json +10 -10
  6. package/src/cms/cmsAssociations.ts +5 -1
  7. package/src/cms/useCmsElementConfig.ts +1 -1
  8. package/src/types/cmsBlockTypes.ts +2 -0
  9. package/src/types/cmsElementTypes.ts +25 -19
  10. package/src/useCart/useCart.test.ts +12 -0
  11. package/src/useCart/useCart.ts +17 -0
  12. package/src/useCountries/useCountries.test.ts +41 -0
  13. package/src/useCountries/useCountries.ts +16 -0
  14. package/src/useCustomerOrders/useCustomerOrders.ts +1 -1
  15. package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +19 -21
  16. package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.ts +23 -30
  17. package/src/useLandingSearch/useLandingSearch.test.ts +1 -1
  18. package/src/useLandingSearch/useLandingSearch.ts +1 -1
  19. package/src/useListing/useListing.test.ts +73 -0
  20. package/src/useListing/useListing.ts +10 -3
  21. package/src/useNavigationSearch/useNavigationSearch.test.ts +21 -0
  22. package/src/useNavigationSearch/useNavigationSearch.ts +6 -3
  23. package/src/useNewsletter/useNewsletter.ts +13 -0
  24. package/src/useOrderDetails/useOrderDetails.ts +5 -11
  25. package/src/useOrderPayment/useOrderPayment.ts +1 -0
  26. package/src/usePrice/usePrice.test.ts +3 -3
  27. package/src/useProductWishlist/useProductWishlist.ts +2 -2
  28. package/src/useSessionContext/useSessionContext.ts +1 -1
  29. package/src/useSyncWishlist/useSyncWishlist.test.ts +34 -17
  30. package/src/useSyncWishlist/useSyncWishlist.ts +27 -8
  31. package/src/useUser/useUser.test.ts +126 -3
  32. package/src/useUser/useUser.ts +6 -1
  33. package/src/useWishlist/useWishlist.test.ts +9 -0
  34. package/src/useWishlist/useWishlist.ts +22 -6
@@ -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
  }