@shopware/composables 1.10.0 → 1.11.1

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 (46) hide show
  1. package/README.md +11 -30
  2. package/dist/index.d.mts +26 -9
  3. package/dist/index.d.ts +26 -9
  4. package/dist/index.mjs +37 -23
  5. package/package.json +16 -14
  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/useNavigationSearch/useNavigationSearch.test.ts +95 -2
  24. package/src/useNavigationSearch/useNavigationSearch.ts +18 -8
  25. package/src/useNewsletter/useNewsletter.test.ts +20 -1
  26. package/src/useNewsletter/useNewsletter.ts +7 -4
  27. package/src/useOrderDetails/useOrderDetails.test.ts +92 -3
  28. package/src/useOrderDetails/useOrderDetails.ts +7 -3
  29. package/src/useOrderPayment/useOrderPayment.test.ts +55 -1
  30. package/src/usePrice/usePrice.test.ts +10 -0
  31. package/src/usePrice/usePrice.ts +2 -7
  32. package/src/useProduct/useProduct.test.ts +14 -1
  33. package/src/useProductAssociations/useProductAssociations.spec.ts +1 -0
  34. package/src/useProductConfigurator/useProductConfigurator.spec.ts +38 -0
  35. package/src/useProductConfigurator/useProductConfigurator.ts +1 -1
  36. package/src/useProductPrice/useProductPrice.test.ts +7 -7
  37. package/src/useProductPrice/useProductPrice.ts +12 -4
  38. package/src/useProductReviews/useProductReviews.test.ts +15 -3
  39. package/src/useSalutations/useSalutations.spec.ts +13 -0
  40. package/src/useSessionContext/useSessionContext.test.ts +122 -4
  41. package/src/useSessionContext/useSessionContext.ts +13 -1
  42. package/src/useSyncWishlist/useSyncWishlist.test.ts +60 -8
  43. package/src/useUrlResolver/useUrlResolver.test.ts +6 -12
  44. package/src/useUrlResolver/useUrlResolver.ts +4 -6
  45. package/src/useUser/useUser.test.ts +107 -15
  46. package/src/useWishlist/useWishlist.test.ts +124 -26
@@ -2,16 +2,16 @@ const Configurator = [
2
2
  {
3
3
  translated: {
4
4
  name: "Colour",
5
- description: null,
5
+ description: undefined,
6
6
  position: 1,
7
7
  customFields: {},
8
8
  },
9
9
  createdAt: "2020-08-06T06:26:29.726+00:00",
10
- updatedAt: null,
10
+ updatedAt: undefined,
11
11
  name: "Colour",
12
12
  displayType: "text",
13
13
  sortingType: "alphanumeric",
14
- description: null,
14
+ description: undefined,
15
15
  position: 1,
16
16
  filterable: true,
17
17
  visibleOnProductDetailPage: true,
@@ -23,47 +23,47 @@ const Configurator = [
23
23
  groupId: "15de997512de4e229d2db1b72aca16f5",
24
24
  name: "red",
25
25
  position: 13,
26
- colorHexCode: null,
27
- mediaId: null,
28
- group: null,
29
- media: null,
30
- customFields: null,
26
+ colorHexCode: undefined,
27
+ mediaId: undefined,
28
+ group: undefined,
29
+ media: undefined,
30
+ customFields: undefined,
31
31
  id: "cc02c6cf39ad43d5856a25f8928490bf",
32
32
  apiAlias: "property_group_option",
33
33
  },
34
34
  {
35
35
  translated: { name: "blue", position: 17, customFields: {} },
36
36
  createdAt: "2020-08-06T06:26:29.667+00:00",
37
- updatedAt: null,
37
+ updatedAt: undefined,
38
38
  groupId: "15de997512de4e229d2db1b72aca16f5",
39
39
  name: "blue",
40
40
  position: 17,
41
- colorHexCode: null,
42
- mediaId: null,
43
- group: null,
44
- media: null,
45
- customFields: null,
41
+ colorHexCode: undefined,
42
+ mediaId: undefined,
43
+ group: undefined,
44
+ media: undefined,
45
+ customFields: undefined,
46
46
  id: "862a4c3c1326423a874b25b6d9ae2101",
47
47
  apiAlias: "property_group_option",
48
48
  },
49
49
  ],
50
- customFields: null,
50
+ customFields: undefined,
51
51
  id: "15de997512de4e229d2db1b72aca16f5",
52
52
  apiAlias: "property_group",
53
53
  },
54
54
  {
55
55
  translated: {
56
56
  name: "Size",
57
- description: null,
57
+ description: undefined,
58
58
  position: 1,
59
59
  customFields: {},
60
60
  },
61
61
  createdAt: "2020-08-06T06:26:29.930+00:00",
62
- updatedAt: null,
62
+ updatedAt: undefined,
63
63
  name: "Size",
64
64
  displayType: "text",
65
65
  sortingType: "alphanumeric",
66
- description: null,
66
+ description: undefined,
67
67
  position: 1,
68
68
  filterable: true,
69
69
  visibleOnProductDetailPage: true,
@@ -75,46 +75,46 @@ const Configurator = [
75
75
  groupId: "6d71c64e67d64876b9c7fbb97fcba3c6",
76
76
  name: "L",
77
77
  position: 24,
78
- colorHexCode: null,
79
- mediaId: null,
80
- group: null,
81
- media: null,
82
- customFields: null,
78
+ colorHexCode: undefined,
79
+ mediaId: undefined,
80
+ group: undefined,
81
+ media: undefined,
82
+ customFields: undefined,
83
83
  id: "f13d254355cc41e28637e3428e23f279",
84
84
  apiAlias: "property_group_option",
85
85
  },
86
86
  {
87
87
  translated: { name: "M", position: 23, customFields: {} },
88
88
  createdAt: "2020-08-06T06:26:29.877+00:00",
89
- updatedAt: null,
89
+ updatedAt: undefined,
90
90
  groupId: "6d71c64e67d64876b9c7fbb97fcba3c6",
91
91
  name: "M",
92
92
  position: 23,
93
- colorHexCode: null,
94
- mediaId: null,
95
- group: null,
96
- media: null,
97
- customFields: null,
93
+ colorHexCode: undefined,
94
+ mediaId: undefined,
95
+ group: undefined,
96
+ media: undefined,
97
+ customFields: undefined,
98
98
  id: "24495f5441004fb9bcde8e1645714063",
99
99
  apiAlias: "property_group_option",
100
100
  },
101
101
  {
102
102
  translated: { name: "S", position: 22, customFields: {} },
103
103
  createdAt: "2020-08-06T06:26:29.923+00:00",
104
- updatedAt: null,
104
+ updatedAt: undefined,
105
105
  groupId: "6d71c64e67d64876b9c7fbb97fcba3c6",
106
106
  name: "S",
107
107
  position: 22,
108
- colorHexCode: null,
109
- mediaId: null,
110
- group: null,
111
- media: null,
112
- customFields: null,
108
+ colorHexCode: undefined,
109
+ mediaId: undefined,
110
+ group: undefined,
111
+ media: undefined,
112
+ customFields: undefined,
113
113
  id: "dcd3a971b122413785ed56a5782ed5b7",
114
114
  apiAlias: "property_group_option",
115
115
  },
116
116
  ],
117
- customFields: null,
117
+ customFields: undefined,
118
118
  id: "6d71c64e67d64876b9c7fbb97fcba3c6",
119
119
  apiAlias: "property_group",
120
120
  },
@@ -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
  });