@shopware/composables 1.10.0 → 1.11.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 (44) hide show
  1. package/README.md +7 -28
  2. package/dist/index.d.mts +23 -7
  3. package/dist/index.d.ts +23 -7
  4. package/dist/index.mjs +24 -19
  5. package/package.json +12 -11
  6. package/src/cms/useCmsElementConfig.test.ts +8 -0
  7. package/src/cms/useCmsElementConfig.ts +3 -0
  8. package/src/cms/useCmsElementImage.test.ts +23 -0
  9. package/src/cms/useCmsElementProductBox.test.ts +21 -0
  10. package/src/devtools/plugin.ts +1 -2
  11. package/src/mocks/Configurator.ts +36 -36
  12. package/src/useAddToCart/useAddToCart.test.ts +18 -1
  13. package/src/useAddress/useAddress.test.ts +41 -2
  14. package/src/useAddress/useAddress.ts +1 -1
  15. package/src/useCart/useCart.test.ts +30 -0
  16. package/src/useCartNotification/useCartNotification.test.ts +30 -0
  17. package/src/useCheckout/useCheckout.test.ts +43 -1
  18. package/src/useCmsMeta/useCmsMeta.test.ts +26 -0
  19. package/src/useCountries/useCountries.test.ts +14 -0
  20. package/src/useLocalWishlist/useLocalWishlist.test.ts +44 -5
  21. package/src/useLocalWishlist/useLocalWishlist.ts +3 -2
  22. package/src/useNavigationContext/useNavigationContext.test.ts +25 -24
  23. package/src/useNewsletter/useNewsletter.test.ts +16 -0
  24. package/src/useNewsletter/useNewsletter.ts +3 -3
  25. package/src/useOrderDetails/useOrderDetails.test.ts +92 -3
  26. package/src/useOrderDetails/useOrderDetails.ts +7 -3
  27. package/src/useOrderPayment/useOrderPayment.test.ts +55 -1
  28. package/src/usePrice/usePrice.test.ts +10 -0
  29. package/src/usePrice/usePrice.ts +2 -7
  30. package/src/useProduct/useProduct.test.ts +14 -1
  31. package/src/useProductAssociations/useProductAssociations.spec.ts +1 -0
  32. package/src/useProductConfigurator/useProductConfigurator.spec.ts +38 -0
  33. package/src/useProductConfigurator/useProductConfigurator.ts +1 -1
  34. package/src/useProductPrice/useProductPrice.test.ts +7 -7
  35. package/src/useProductPrice/useProductPrice.ts +12 -4
  36. package/src/useProductReviews/useProductReviews.test.ts +15 -3
  37. package/src/useSalutations/useSalutations.spec.ts +13 -0
  38. package/src/useSessionContext/useSessionContext.test.ts +122 -4
  39. package/src/useSessionContext/useSessionContext.ts +13 -1
  40. package/src/useSyncWishlist/useSyncWishlist.test.ts +60 -8
  41. package/src/useUrlResolver/useUrlResolver.test.ts +6 -12
  42. package/src/useUrlResolver/useUrlResolver.ts +4 -6
  43. package/src/useUser/useUser.test.ts +107 -15
  44. package/src/useWishlist/useWishlist.test.ts +124 -26
@@ -4,7 +4,7 @@ import { useSetup } from "../_test";
4
4
  import ProductMocked from "../mocks/Product";
5
5
  import { useAddToCart } from "./useAddToCart";
6
6
 
7
- describe("useB2bQuoteManagement", () => {
7
+ describe("useAddToCart", () => {
8
8
  vi.mock("../useCart/useCart.ts", () => ({
9
9
  useCart() {
10
10
  return {
@@ -29,11 +29,28 @@ describe("useB2bQuoteManagement", () => {
29
29
  expect(vm.quantity).toBe(1);
30
30
  expect(vm.count).toBe(1);
31
31
  expect(vm.isInCart).toBe(true);
32
+ expect(vm.getStock).toBe(ProductMocked.stock);
33
+ expect(vm.getAvailableStock).toBe(ProductMocked.availableStock);
32
34
  });
33
35
 
34
36
  it("add product to cart - empty cart", async () => {
35
37
  const { vm } = await useSetup(() => useAddToCart(ref()));
36
38
  expect(vm.count).toBe(0);
39
+ expect(vm.isInCart).toBe(false);
37
40
  await expect(vm.addToCart()).rejects.toThrow();
38
41
  });
42
+
43
+ it("add product to cart - product not in cart", async () => {
44
+ const productNotInCart = { ...ProductMocked, id: "other-product-id" };
45
+ const { vm } = await useSetup(() => useAddToCart(ref(productNotInCart)));
46
+ expect(vm.count).toBe(0);
47
+ expect(vm.isInCart).toBe(false);
48
+ });
49
+
50
+ it("add product to cart - quantity resets after add", async () => {
51
+ const { vm } = await useSetup(() => useAddToCart(ref(ProductMocked)));
52
+ vm.quantity = 3;
53
+ await vm.addToCart();
54
+ expect(vm.quantity).toBe(1);
55
+ });
39
56
  });
@@ -6,6 +6,7 @@ import { useSetup } from "../_test";
6
6
  import { useAddress } from "./useAddress";
7
7
 
8
8
  const MOCKED_ADDRESS = {
9
+ id: "address-id",
9
10
  countryId: "6777d83705454d078fc9a7419296c7dc",
10
11
  countryStateId: "",
11
12
  salutationId: "d5e543063dd642b48ef94b02d68e5785",
@@ -19,13 +20,17 @@ const MOCKED_ADDRESS = {
19
20
  describe("useAddress", () => {
20
21
  it("load customer address", async () => {
21
22
  const { vm, injections } = await useSetup(useAddress);
22
- injections.apiClient.invoke.mockResolvedValue({ data: {} });
23
- await vm.loadCustomerAddresses();
23
+ injections.apiClient.invoke.mockResolvedValue({
24
+ data: { elements: [MOCKED_ADDRESS] },
25
+ });
26
+ const result = await vm.loadCustomerAddresses();
24
27
 
25
28
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
26
29
  expect.stringContaining("listAddress"),
27
30
  expect.objectContaining({}),
28
31
  );
32
+ expect(result).toEqual([MOCKED_ADDRESS]);
33
+ expect(vm.customerAddresses).toEqual([MOCKED_ADDRESS]);
29
34
  });
30
35
 
31
36
  it("load customer address - error", async () => {
@@ -49,6 +54,7 @@ describe("useAddress", () => {
49
54
  });
50
55
 
51
56
  await expect(vm.loadCustomerAddresses()).rejects.toThrowError();
57
+ expect(vm.customerAddresses).toEqual([]);
52
58
  });
53
59
 
54
60
  it("create address", async () => {
@@ -111,6 +117,39 @@ describe("useAddress", () => {
111
117
  );
112
118
  });
113
119
 
120
+ it("load customer address - ApiClientError non-403", async () => {
121
+ const { vm } = await useSetup(useAddress, {
122
+ apiClient: {
123
+ invoke: vi.fn().mockImplementation(() => {
124
+ throw new ApiClientError({
125
+ status: 500,
126
+ _data: {
127
+ errors: [{ code: "SERVER", status: 500, detail: "Error" }],
128
+ },
129
+ } as unknown as FetchResponse<{
130
+ errors: Array<{ title: string }>;
131
+ }>);
132
+ }),
133
+ },
134
+ });
135
+
136
+ await expect(vm.loadCustomerAddresses()).rejects.toThrowError();
137
+ });
138
+
139
+ it("load customer address - non-ApiClientError", async () => {
140
+ const { vm } = await useSetup(useAddress, {
141
+ apiClient: {
142
+ invoke: vi.fn().mockImplementation(() => {
143
+ throw new Error("Network error");
144
+ }),
145
+ },
146
+ });
147
+
148
+ await expect(vm.loadCustomerAddresses()).rejects.toThrowError(
149
+ "Network error",
150
+ );
151
+ });
152
+
114
153
  it("build error message builder", async () => {
115
154
  const { vm } = await useSetup(useAddress);
116
155
  const message = vm.errorMessageBuilder({
@@ -190,7 +190,7 @@ export function useAddress(): UseAddressReturn {
190
190
  }
191
191
 
192
192
  return {
193
- customerAddresses: computed(() => _storeCustomerAddresses.value || []),
193
+ customerAddresses: computed(() => _storeCustomerAddresses.value),
194
194
  loadCustomerAddresses,
195
195
  createCustomerAddress,
196
196
  updateCustomerAddress,
@@ -260,4 +260,34 @@ describe("useCart", () => {
260
260
 
261
261
  expect(vm.shippingCosts.length).toEqual(0);
262
262
  });
263
+
264
+ it("should return true for isVirtualCart when all items are digital", async () => {
265
+ injections.apiClient.invoke.mockResolvedValue({
266
+ data: {
267
+ lineItems: [
268
+ {
269
+ type: "product",
270
+ good: true,
271
+ quantity: 1,
272
+ states: ["is-download"],
273
+ },
274
+ ],
275
+ },
276
+ });
277
+ await vm.refreshCart();
278
+ expect(vm.isVirtualCart).toBe(true);
279
+ });
280
+
281
+ it("consumeCartErrors returns null when no errors", async () => {
282
+ await vm.refreshCart();
283
+ expect(vm.consumeCartErrors()).toEqual(null);
284
+ });
285
+
286
+ it("isEmpty should return true when cart has no items", async () => {
287
+ injections.apiClient.invoke.mockResolvedValue({
288
+ data: { lineItems: [] },
289
+ });
290
+ await vm.refreshCart();
291
+ expect(vm.isEmpty).toBe(true);
292
+ });
263
293
  });
@@ -116,6 +116,27 @@ describe("useCartNotification", () => {
116
116
  ]);
117
117
  });
118
118
 
119
+ it("should exclude success codes from getErrorsCodes", () => {
120
+ vi.mocked(useCart).mockReturnValue({
121
+ consumeCartErrors: vi.fn(() => {
122
+ return JSON.parse(
123
+ JSON.stringify({
124
+ "promotion-discount-added-1326f60448d84aed964d77ca1c2dde90": {
125
+ message: "Discount has been added",
126
+ code: 0,
127
+ key: "promotion-discount-added-1326f60448d84aed964d77ca1c2dde90",
128
+ level: 0,
129
+ messageKey: "promotion-discount-added",
130
+ },
131
+ }),
132
+ );
133
+ }),
134
+ } as unknown as ReturnType<typeof useCart>);
135
+ const { vm } = useSetup(() => useCartNotification());
136
+
137
+ expect(vm.getErrorsCodes()).toEqual([]);
138
+ });
139
+
119
140
  it("should return empty array if errors are not present", () => {
120
141
  vi.mocked(useCart).mockReturnValue({
121
142
  consumeCartErrors: vi.fn(() => {
@@ -126,4 +147,13 @@ describe("useCartNotification", () => {
126
147
  const { vm } = useSetup(() => useCartNotification());
127
148
  expect(vm.getErrorsCodes()).toEqual([]);
128
149
  });
150
+
151
+ it("should return empty array when errors is array", () => {
152
+ vi.mocked(useCart).mockReturnValue({
153
+ consumeCartErrors: vi.fn(() => []),
154
+ } as unknown as ReturnType<typeof useCart>);
155
+
156
+ const { vm } = useSetup(() => useCartNotification());
157
+ expect(vm.getErrorsCodes()).toEqual([]);
158
+ });
129
159
  });
@@ -1,7 +1,19 @@
1
- import { describe, expect, it } from "vitest";
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { ref } from "vue";
2
3
  import { useSetup } from "../_test";
3
4
  import { useCheckout } from "./useCheckout";
4
5
 
6
+ const sessionContextRef = ref();
7
+ vi.mock("../useSessionContext/useSessionContext.ts", () => ({
8
+ useSessionContext: () => ({
9
+ sessionContext: sessionContextRef,
10
+ selectedPaymentMethod: ref(null),
11
+ selectedShippingMethod: ref(null),
12
+ setShippingMethod: vi.fn(),
13
+ setPaymentMethod: vi.fn(),
14
+ }),
15
+ }));
16
+
5
17
  describe("useCheckout", () => {
6
18
  it("getShippingMethods - empty", async () => {
7
19
  const { vm, injections } = useSetup(useCheckout);
@@ -101,4 +113,34 @@ describe("useCheckout", () => {
101
113
  expect.objectContaining({}),
102
114
  );
103
115
  });
116
+
117
+ it("createOrder with params", async () => {
118
+ const { vm, injections } = useSetup(useCheckout);
119
+ injections.apiClient.invoke.mockResolvedValue({
120
+ data: { id: "order-123" },
121
+ });
122
+
123
+ const result = await vm.createOrder({ customerComment: "test" });
124
+
125
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
126
+ expect.stringContaining("createOrder"),
127
+ expect.objectContaining({
128
+ body: { customerComment: "test" },
129
+ }),
130
+ );
131
+ expect(result).toEqual({ id: "order-123" });
132
+ });
133
+
134
+ it("shippingAddress and billingAddress from sessionContext", () => {
135
+ const shippingAddr = { street: "Test St", city: "Test City" };
136
+ const billingAddr = { street: "Billing St", city: "Billing City" };
137
+ sessionContextRef.value = {
138
+ shippingLocation: { address: shippingAddr },
139
+ customer: { activeBillingAddress: billingAddr },
140
+ };
141
+
142
+ const { vm } = useSetup(useCheckout);
143
+ expect(vm.shippingAddress).toEqual(shippingAddr);
144
+ expect(vm.billingAddress).toEqual(billingAddr);
145
+ });
104
146
  });
@@ -1,4 +1,5 @@
1
1
  import { describe, expect, it } from "vitest";
2
+ import type { Schemas } from "#shopware";
2
3
  import { useSetup } from "../_test";
3
4
  import LandingPageMock from "../mocks/LandingPage";
4
5
  import { useCmsMeta } from "./useCmsMeta";
@@ -14,4 +15,29 @@ describe("composables - useCmsMeta", () => {
14
15
  { name: "title", content: "Landing page title" },
15
16
  ]);
16
17
  });
18
+
19
+ it("should return empty meta when no keywords, description or title", () => {
20
+ const entity = {
21
+ translated: {},
22
+ name: "Entity Name",
23
+ } as Schemas["Category"];
24
+ const { vm } = useSetup(() => useCmsMeta(entity));
25
+
26
+ expect(vm.title).toEqual("Entity Name");
27
+ expect(vm.meta).toEqual([]);
28
+ });
29
+
30
+ it("should return partial meta when only some fields present", () => {
31
+ const entity = {
32
+ translated: {
33
+ metaDescription: "Only description",
34
+ },
35
+ name: "Entity Name",
36
+ } as Schemas["Category"];
37
+ const { vm } = useSetup(() => useCmsMeta(entity));
38
+
39
+ expect(vm.meta).toEqual([
40
+ { name: "description", content: "Only description" },
41
+ ]);
42
+ });
17
43
  });
@@ -1,4 +1,5 @@
1
1
  import { describe, expect, it, vi } from "vitest";
2
+ import { ref } from "vue";
2
3
  import { useSetup } from "../_test";
3
4
  import CountryMock from "../mocks/Country";
4
5
  import { useCountries } from "./useCountries";
@@ -102,4 +103,17 @@ describe("useCountries", () => {
102
103
 
103
104
  expect(vm.getCountriesOptions).toStrictEqual([]);
104
105
  });
106
+
107
+ it("should not fetch when swCountries already populated", async () => {
108
+ const preloadedCountries = ref(CountryMock.elements);
109
+ const { vm, injections } = await useSetup(useCountries, {
110
+ apiClient: { invoke: vi.fn() },
111
+ swCountries: preloadedCountries,
112
+ } as Parameters<typeof useSetup>[1]);
113
+
114
+ await vm.mountedCallback();
115
+
116
+ expect(injections.apiClient.invoke).not.toHaveBeenCalled();
117
+ expect(vm.getCountries).toStrictEqual(CountryMock.elements);
118
+ });
105
119
  });
@@ -1,4 +1,4 @@
1
- import { describe, expect, it } from "vitest";
1
+ import { describe, expect, it, vi } from "vitest";
2
2
  import { useSetup } from "../_test";
3
3
  import { useLocalWishlist } from "./useLocalWishlist";
4
4
 
@@ -10,6 +10,13 @@ describe("useLocalWishlist", () => {
10
10
  vm.addToWishlist("some-id");
11
11
  expect(vm.items.length).toBe(1);
12
12
  });
13
+
14
+ it("addToWishlist does not add duplicate", () => {
15
+ const { vm } = useSetup(useLocalWishlist);
16
+ vm.addToWishlist("some-id");
17
+ vm.addToWishlist("some-id");
18
+ expect(vm.items.length).toBe(1);
19
+ });
13
20
  });
14
21
 
15
22
  describe("removeFromWishlist", () => {
@@ -18,14 +25,23 @@ describe("useLocalWishlist", () => {
18
25
  vm.removeFromWishlist("some-id");
19
26
  expect(vm.items.length).toBe(0);
20
27
  });
28
+
29
+ it("should remove existing product from the wishlist", () => {
30
+ const { vm } = useSetup(useLocalWishlist);
31
+ vm.addToWishlist("id-to-remove");
32
+ vm.addToWishlist("id-to-keep");
33
+ vm.removeFromWishlist("id-to-remove");
34
+ expect(vm.items).toEqual(["id-to-keep"]);
35
+ });
21
36
  });
22
37
 
23
38
  describe("clearWishlist", () => {
24
- it("should clear the wishlist", () => {
39
+ it("should clear the wishlist", async () => {
25
40
  const { vm } = useSetup(useLocalWishlist);
26
- vm.addToWishlist("some-id");
41
+ await vm.clearWishlist();
42
+ await vm.addToWishlist("clear-test-id");
27
43
  expect(vm.count).toBe(1);
28
- vm.clearWishlist();
44
+ await vm.clearWishlist();
29
45
  expect(vm.count).toBe(0);
30
46
  });
31
47
  });
@@ -45,10 +61,33 @@ describe("useLocalWishlist", () => {
45
61
  });
46
62
  it("getWishlistProducts with items localStorage", () => {
47
63
  const { vm } = useSetup(useLocalWishlist);
48
- localStorage.removeItem("sw-wishlist-items");
64
+ vm.clearWishlist();
65
+ localStorage.setItem("sw-wishlist-items", JSON.stringify(["id-1"]));
49
66
  vm.getWishlistProducts();
50
67
  expect(vm.count).toBe(1);
68
+ expect(vm.items).toContain("id-1");
51
69
  });
52
70
  });
71
+
72
+ it("getWishlistProducts with missing localStorage key", () => {
73
+ const { vm } = useSetup(useLocalWishlist);
74
+ vm.clearWishlist();
75
+ localStorage.removeItem("sw-wishlist-items");
76
+ vm.getWishlistProducts();
77
+ expect(vm.count).toBe(0);
78
+ });
79
+
80
+ it("getWishlistProducts returns nothing when localStorage is unavailable", () => {
81
+ const { vm } = useSetup(useLocalWishlist);
82
+ vm.clearWishlist();
83
+ const getItemSpy = vi
84
+ .spyOn(localStorage, "getItem")
85
+ .mockImplementation(() => {
86
+ throw new Error("localStorage not available");
87
+ });
88
+ vm.getWishlistProducts();
89
+ getItemSpy.mockRestore();
90
+ expect(vm.count).toBe(0);
91
+ });
53
92
  });
54
93
  });
@@ -43,10 +43,11 @@ export function useLocalWishlist(): UseLocalWishlistReturn {
43
43
  JSON.stringify(_wishlistItems.value),
44
44
  );
45
45
  };
46
- /* istanbul ignore next */
47
46
  const getFromStorage = () => {
48
- if (typeof window !== "undefined" && localStorage) {
47
+ try {
49
48
  return JSON.parse(localStorage.getItem("sw-wishlist-items") ?? "[]");
49
+ } catch {
50
+ return undefined;
50
51
  }
51
52
  };
52
53
 
@@ -3,46 +3,47 @@ import { ref } from "vue";
3
3
  import { useSetup } from "../_test";
4
4
  import { useNavigationContext } from "./useNavigationContext";
5
5
 
6
+ const mockSeoUrl = {
7
+ createdAt: "2020-08-06T06:26:52.576+00:00",
8
+ foreignKey: "7e117859f69b4ee08947e1ae4fce1f69",
9
+ id: "dd38c5ed443544e28818e967809c8914",
10
+ isCanonical: true,
11
+ isDeleted: false,
12
+ isModified: false,
13
+ languageId: "2fbb5fe2e29a4d70aa5854ce7ce3e20b",
14
+ pathInfo: "/detail/7e117859f69b4ee08947e1ae4fce1f69",
15
+ routeName: "frontend.detail.page",
16
+ updatedAt: "2022-08-03T08:07:04.512+00:00",
17
+ salesChannelId: "98432def39fc4624b33213a56b8c944d",
18
+ seoPathInfo: "Cigar-Special-40/SW10044",
19
+ } as const;
20
+
6
21
  describe("useNavigationContext", () => {
7
22
  it("notification flow", async () => {
8
- const { vm } = useSetup(() =>
9
- useNavigationContext(
10
- ref({
11
- createdAt: "2020-08-06T06:26:52.576+00:00",
12
- foreignKey: "7e117859f69b4ee08947e1ae4fce1f69",
13
- id: "dd38c5ed443544e28818e967809c8914",
14
- isCanonical: true,
15
- isDeleted: false,
16
- isModified: false,
17
- languageId: "2fbb5fe2e29a4d70aa5854ce7ce3e20b",
18
- pathInfo: "/detail/7e117859f69b4ee08947e1ae4fce1f69",
19
- routeName: "frontend.detail.page",
20
- updatedAt: "2022-08-03T08:07:04.512+00:00",
21
- salesChannelId: "98432def39fc4624b33213a56b8c944d",
22
- seoPathInfo: "Cigar-Special-40/SW10044",
23
- }),
24
- ),
25
- );
23
+ const { vm } = useSetup(() => useNavigationContext(ref(mockSeoUrl)));
26
24
 
27
25
  expect(vm.foreignKey).toBe("7e117859f69b4ee08947e1ae4fce1f69");
28
26
  expect(vm.routeName).toBe("frontend.detail.page");
27
+ expect(vm.navigationContext).toEqual(mockSeoUrl);
29
28
  });
30
29
 
31
30
  it("should return empty string if no foreignKey", async () => {
32
31
  const { vm } = useSetup(() =>
33
32
  useNavigationContext(
34
33
  ref({
35
- createdAt: "2020-08-06T06:26:52.576+00:00",
34
+ ...mockSeoUrl,
36
35
  foreignKey: "",
37
- id: "dd38c5ed443544e28818e967809c8914",
38
- languageId: "2fbb5fe2e29a4d70aa5854ce7ce3e20b",
39
- pathInfo: "/detail/7e117859f69b4ee08947e1ae4fce1f69",
40
- routeName: "frontend.detail.page",
41
- seoPathInfo: "Cigar-Special-40/SW10044",
42
36
  }),
43
37
  ),
44
38
  );
45
39
 
46
40
  expect(vm.foreignKey).toBe("");
47
41
  });
42
+
43
+ it("should return empty string when context is null", () => {
44
+ const { vm } = useSetup(() => useNavigationContext(ref(null)));
45
+ expect(vm.foreignKey).toBe("");
46
+ expect(vm.routeName).toBeUndefined();
47
+ expect(vm.navigationContext).toBeNull();
48
+ });
48
49
  });
@@ -52,4 +52,20 @@ describe("useNewsletter", () => {
52
52
  vm.newsletterStatus = "direct";
53
53
  expect(vm.isNewsletterSubscriber).toBe(true);
54
54
  });
55
+
56
+ it("confirmationNeeded when status is notSet", async () => {
57
+ const { vm, injections } = useSetup(useNewsletter);
58
+ injections.apiClient.invoke.mockResolvedValue({
59
+ data: { status: "notSet" },
60
+ });
61
+
62
+ await vm.getNewsletterStatus();
63
+ expect(vm.confirmationNeeded).toBe(true);
64
+ });
65
+
66
+ it("returns SUBSRIBE_KEY and UNSUBSCRIBE_KEY", () => {
67
+ const { vm } = useSetup(useNewsletter);
68
+ expect(vm.SUBSCRIBE_KEY).toBe("subscribe");
69
+ expect(vm.UNSUBSCRIBE_KEY).toBe("unsubscribe");
70
+ });
55
71
  });
@@ -40,7 +40,7 @@ export type UseNewsletterReturn = {
40
40
  /**
41
41
  * Subscribe key
42
42
  */
43
- SUBSRIBE_KEY: string;
43
+ SUBSCRIBE_KEY: string;
44
44
  /**
45
45
  * Unsubscribe key
46
46
  */
@@ -53,7 +53,7 @@ export type UseNewsletterReturn = {
53
53
  * @category Customer & Account
54
54
  */
55
55
  export function useNewsletter(): UseNewsletterReturn {
56
- const SUBSRIBE_KEY = "subscribe";
56
+ const SUBSCRIBE_KEY = "subscribe";
57
57
  const UNSUBSCRIBE_KEY = "unsubscribe";
58
58
 
59
59
  const { apiClient } = useShopwareContext();
@@ -116,7 +116,7 @@ export function useNewsletter(): UseNewsletterReturn {
116
116
  getNewsletterStatus,
117
117
  newsletterStatus,
118
118
  confirmationNeeded,
119
- SUBSRIBE_KEY,
119
+ SUBSCRIBE_KEY,
120
120
  UNSUBSCRIBE_KEY,
121
121
  };
122
122
  }
@@ -6,12 +6,21 @@ import { useOrderDetails } from "./useOrderDetails";
6
6
  describe("useOrderDetails", () => {
7
7
  it("init details", async () => {
8
8
  const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
9
+
10
+ expect(vm.documents).toStrictEqual([]);
11
+
9
12
  injections.apiClient.invoke.mockResolvedValue({ data: Order });
10
13
 
11
14
  await vm.loadOrderDetails();
15
+ expect(vm.hasDocuments).toBe(false);
16
+ expect(vm.documents).toStrictEqual([]);
12
17
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
13
18
  expect.stringContaining("readOrder"),
14
- expect.objectContaining({}),
19
+ expect.objectContaining({
20
+ body: expect.objectContaining({
21
+ associations: expect.anything(),
22
+ }),
23
+ }),
15
24
  );
16
25
 
17
26
  expect(vm.personalDetails).toEqual({
@@ -26,11 +35,25 @@ describe("useOrderDetails", () => {
26
35
  id === Order.orders.elements?.[0]?.billingAddressId,
27
36
  ),
28
37
  );
38
+
39
+ expect(vm.order).toBeDefined();
40
+ expect(vm.status).toBe(
41
+ Order.orders.elements?.[0]?.stateMachineState.translated.name,
42
+ );
43
+ expect(vm.statusTechnicalName).toBe(
44
+ Order.orders.elements?.[0]?.stateMachineState.technicalName,
45
+ );
46
+ expect(vm.shippingAddress).toBeUndefined();
47
+ expect(vm.total).toBe(Order.orders.elements?.[0]?.price?.totalPrice);
48
+ expect(vm.subtotal).toBe(Order.orders.elements?.[0]?.price?.positionPrice);
49
+ expect(vm.shippingCosts).toBe(Order.orders.elements?.[0]?.shippingTotal);
29
50
  });
30
51
 
31
52
  it("should handle setting the order payment", async () => {
32
53
  const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
33
- injections.apiClient.invoke.mockResolvedValue({ data: {} });
54
+ injections.apiClient.invoke.mockResolvedValue({
55
+ data: { redirectUrl: "https://payment.example.com" },
56
+ });
34
57
  await vm.handlePayment();
35
58
 
36
59
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
@@ -43,6 +66,24 @@ describe("useOrderDetails", () => {
43
66
  },
44
67
  }),
45
68
  );
69
+ expect(vm.paymentUrl).toBe("https://payment.example.com");
70
+ });
71
+
72
+ it("should handle payment with success and error URLs", async () => {
73
+ const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
74
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
75
+ await vm.handlePayment("https://success.com", "https://error.com");
76
+
77
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
78
+ expect.stringContaining("handlePaymentMethod"),
79
+ expect.objectContaining({
80
+ body: {
81
+ orderId: "123-test",
82
+ finishUrl: "https://success.com",
83
+ errorUrl: "https://error.com",
84
+ },
85
+ }),
86
+ );
46
87
  });
47
88
 
48
89
  it("should cancel the order", async () => {
@@ -112,7 +153,7 @@ describe("useOrderDetails", () => {
112
153
  it("getPaymentMethods", async () => {
113
154
  const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
114
155
  injections.apiClient.invoke.mockResolvedValue({ data: {} });
115
- await vm.getPaymentMethods();
156
+ const result = await vm.getPaymentMethods();
116
157
 
117
158
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
118
159
  expect.stringContaining("readPaymentMethod"),
@@ -122,6 +163,18 @@ describe("useOrderDetails", () => {
122
163
  },
123
164
  }),
124
165
  );
166
+ expect(result).toEqual([]);
167
+ });
168
+
169
+ it("getPaymentMethods returns elements when available", async () => {
170
+ const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
171
+ const paymentMethods = [{ id: "1", name: "Invoice" }];
172
+ injections.apiClient.invoke.mockResolvedValue({
173
+ data: { elements: paymentMethods },
174
+ });
175
+ const result = await vm.getPaymentMethods();
176
+
177
+ expect(result).toEqual(paymentMethods);
125
178
  });
126
179
 
127
180
  it("paymentChangeable", async () => {
@@ -132,6 +185,42 @@ describe("useOrderDetails", () => {
132
185
  expect(vm.paymentChangeable).toEqual(true);
133
186
  });
134
187
 
188
+ it("loadOrderDetails with custom associations", async () => {
189
+ const customAssociations = { lineItems: { associations: { cover: {} } } };
190
+ const { vm, injections } = useSetup(() =>
191
+ useOrderDetails("123-test", customAssociations),
192
+ );
193
+ injections.apiClient.invoke.mockResolvedValue({ data: Order });
194
+ await vm.loadOrderDetails();
195
+
196
+ const call = injections.apiClient.invoke.mock.calls.at(0);
197
+ expect(call).toBeDefined();
198
+ if (call) {
199
+ const args = call[1] as { body: { associations: unknown } };
200
+ expect(call[0]).toContain("readOrder");
201
+ expect(args.body.associations).toMatchObject(customAssociations);
202
+ }
203
+ });
204
+
205
+ it("hasDocuments and documents when order has documents", async () => {
206
+ const orderElement = Order.orders.elements[0];
207
+ const orderWithDocs = {
208
+ ...orderElement,
209
+ documents: [{ id: "doc-1", deepLinkCode: "abc" }],
210
+ };
211
+ const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
212
+ injections.apiClient.invoke.mockResolvedValue({
213
+ data: {
214
+ orders: { elements: [orderWithDocs] },
215
+ paymentChangeable: { "123-test": true },
216
+ },
217
+ });
218
+ await vm.loadOrderDetails();
219
+
220
+ expect(vm.hasDocuments).toBe(true);
221
+ expect(vm.documents).toStrictEqual([{ id: "doc-1", deepLinkCode: "abc" }]);
222
+ });
223
+
135
224
  it("should return current payment method", async () => {
136
225
  const { vm, injections } = useSetup(() => useOrderDetails("123-test"));
137
226
  injections.apiClient.invoke.mockResolvedValue({