@shopbite-de/storefront 1.23.3 → 1.25.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 (204) hide show
  1. package/.env.example +22 -0
  2. package/.github/workflows/build.yaml +1 -1
  3. package/.github/workflows/ci.yaml +6 -3
  4. package/CLAUDE.md +48 -3
  5. package/README.md +139 -40
  6. package/api-gen.config.json +9 -0
  7. package/api-types/storeApiSchema.shopbite.overrides.json +35 -0
  8. package/api-types/storeApiTypes.d.ts +95 -81
  9. package/app/app.config.ts +3 -2
  10. package/app/app.vue +40 -53
  11. package/app/assets/css/main.css +36 -4
  12. package/app/components/AddToWishlist.vue +13 -6
  13. package/app/components/Address/Fields.vue +2 -21
  14. package/app/components/AnimatedSection.vue +9 -4
  15. package/app/components/BottomNavi.vue +9 -2
  16. package/app/components/Cart/Bar.vue +102 -0
  17. package/app/components/Cart/Item.vue +21 -10
  18. package/app/components/Cart/QuickView.vue +4 -0
  19. package/app/components/Cart/Upsell.vue +106 -0
  20. package/app/components/Category/Header.vue +9 -3
  21. package/app/components/Category/Listing.vue +62 -45
  22. package/app/components/Checkout/PaymentAndDelivery.vue +19 -3
  23. package/app/components/Checkout/Summary.vue +70 -2
  24. package/app/components/Cta.vue +4 -2
  25. package/app/components/DemoBanner.vue +13 -0
  26. package/app/components/Feature/Card.vue +121 -0
  27. package/app/components/Features.vue +14 -9
  28. package/app/components/Food/MarqueeItem.vue +20 -8
  29. package/app/components/Footer/Contact.vue +82 -0
  30. package/app/components/Footer/StoreStatus.vue +30 -0
  31. package/app/components/Footer.vue +2 -1
  32. package/app/components/Header/Body.vue +1 -1
  33. package/app/components/Header/Right.vue +60 -17
  34. package/app/components/Header/Title.vue +1 -0
  35. package/app/components/Header.vue +5 -15
  36. package/app/components/Hero.vue +169 -36
  37. package/app/components/ImageGallery.vue +11 -5
  38. package/app/components/Product/Card.vue +104 -46
  39. package/app/components/Product/CardCompact.vue +72 -0
  40. package/app/components/Product/CardDietBadges.vue +38 -31
  41. package/app/components/Product/CardIngredients.vue +35 -0
  42. package/app/components/Product/CardSkeleton.vue +10 -18
  43. package/app/components/Product/Category.vue +9 -2
  44. package/app/components/Product/Configurator.vue +11 -7
  45. package/app/components/Product/CrossSelling.vue +45 -34
  46. package/app/components/Product/DeselectIngredient.vue +37 -31
  47. package/app/components/Product/Detail.vue +73 -13
  48. package/app/components/Product/QuickView.vue +77 -0
  49. package/app/components/Topseller.vue +44 -34
  50. package/app/components/User/RegistrationForm.vue +0 -1
  51. package/app/components/Wishlist.vue +2 -15
  52. package/app/composables/useAddToCart.ts +13 -40
  53. package/app/composables/useAddressAutocomplete.ts +12 -19
  54. package/app/composables/useAddressSuggestionArea.ts +16 -0
  55. package/app/composables/useAddressValidation.ts +3 -19
  56. package/app/composables/useCartMutations.ts +168 -0
  57. package/app/composables/useCartQuickView.ts +19 -0
  58. package/app/composables/useCartUpsell.ts +100 -0
  59. package/app/composables/{useCategory.ts → useCategoryById.ts} +1 -1
  60. package/app/composables/useCategorySeo.ts +27 -29
  61. package/app/composables/useCheckoutMethodGuard.ts +201 -0
  62. package/app/composables/useMatomo.ts +25 -0
  63. package/app/composables/useMatomoConfig.ts +12 -0
  64. package/app/composables/useMenuSectionSchema.ts +55 -0
  65. package/app/composables/useNavigation.ts +12 -9
  66. package/app/composables/usePageSeo.ts +50 -0
  67. package/app/composables/useProductCartQuantity.ts +15 -0
  68. package/app/composables/useProductCrossSelling.ts +1 -0
  69. package/app/composables/useProductDetail.ts +15 -1
  70. package/app/composables/useProductEvents.ts +30 -0
  71. package/app/composables/useProductQuickView.ts +65 -0
  72. package/app/composables/useRestaurantSchema.ts +64 -0
  73. package/app/composables/useScrollAnimation.ts +39 -8
  74. package/app/composables/useSearchTracking.ts +33 -0
  75. package/app/composables/useSeoUrlRoute.ts +58 -0
  76. package/app/composables/useSortingSelection.ts +28 -0
  77. package/app/composables/useStoreStatus.ts +48 -0
  78. package/app/composables/useTopSellers.ts +22 -0
  79. package/app/composables/useTrackEvent.ts +36 -23
  80. package/app/pages/[...all].vue +4 -4
  81. package/app/pages/account/recover/password/index.vue +1 -0
  82. package/app/pages/anmelden.vue +4 -0
  83. package/app/pages/bestellung/[id]/erfolg.vue +1 -5
  84. package/app/pages/bestellung/[id]/fehler.vue +2 -2
  85. package/app/pages/bestellung/[id]/index.vue +9 -3
  86. package/app/pages/bestellung/bestaetigen.vue +1 -5
  87. package/app/pages/bestellung/warenkorb.vue +1 -5
  88. package/app/pages/bestellung/zahlung-versand.vue +1 -5
  89. package/app/pages/c/[...all].vue +2 -35
  90. package/app/pages/index.vue +28 -14
  91. package/app/pages/kontakt.vue +4 -0
  92. package/app/pages/konto/adressen.vue +4 -0
  93. package/app/pages/konto/bestellung/[id].vue +4 -0
  94. package/app/pages/konto/bestellungen.vue +4 -0
  95. package/app/pages/konto/index.vue +4 -0
  96. package/app/pages/konto/profil.vue +4 -0
  97. package/app/pages/maintenance.vue +6 -0
  98. package/app/pages/merkliste.vue +1 -7
  99. package/app/pages/passwort-vergessen.vue +1 -0
  100. package/app/pages/registrierung/bestaetigen.vue +4 -0
  101. package/app/pages/registrierung/index.vue +4 -0
  102. package/app/pages/speisekarte/[...all].vue +2 -36
  103. package/app/pages/suche.vue +42 -26
  104. package/app/plugins/matomo.ts +35 -0
  105. package/app/types/Association.d.ts +1 -0
  106. package/app/utils/hydrateWhenVisible.ts +20 -0
  107. package/app/utils/media.ts +33 -0
  108. package/app/utils/openingHours.ts +90 -0
  109. package/app/utils/product.ts +31 -0
  110. package/app/utils/schema.ts +235 -0
  111. package/app/utils/seo.ts +56 -0
  112. package/app/utils/seoPath.ts +57 -0
  113. package/content/index.yml +26 -20
  114. package/content.config.ts +16 -0
  115. package/docs/notes/2026-07-05-issue-259-matomo-broken.md +7 -0
  116. package/docs/notes/2026-09-08-issue-240-shipping-method-blocked.md +83 -0
  117. package/docs/notes/2026-09-08-issue-241-cart-lock-conflicts.md +63 -0
  118. package/docs/notes/2026-09-08-issue-244-category-url-trailing-slash.md +65 -0
  119. package/docs/notes/2026-09-08-issue-276-payment-method-blocked.md +39 -0
  120. package/docs/notes/2026-09-08-issue-277-typecheck.md +99 -0
  121. package/docs/notes/2026-09-10-issue-250-animated-section.md +23 -0
  122. package/docs/notes/2026-09-10-issue-251-shop-data-in-base-layer.md +30 -0
  123. package/docs/notes/2026-09-10-issue-272-restaurant-schema.md +27 -0
  124. package/docs/notes/2026-09-10-issue-273-performance-media.md +64 -0
  125. package/docs/notes/2026-09-10-issue-274-seo-basics.md +49 -0
  126. package/docs/notes/2026-09-10-issue-275-accessibility.md +46 -0
  127. package/docs/notes/2026-09-10-issue-290-footer-nap.md +28 -0
  128. package/docs/notes/2026-09-10-issue-291-seo-url-trailing-slash.md +24 -0
  129. package/docs/notes/2026-09-10-issue-294-matomo-without-config.md +33 -0
  130. package/docs/notes/2026-09-10-issue-295-search-hydration-mismatch.md +25 -0
  131. package/docs/notes/2026-09-10-issue-296-track-direct-search.md +19 -0
  132. package/docs/notes/2026-09-10-issue-312-store-api-criteria-post.md +64 -0
  133. package/docs/notes/2026-09-10-issue-313-wishlist-scoped-tailwind.md +29 -0
  134. package/docs/notes/2026-09-10-issue-314-mobile-js-cost.md +119 -0
  135. package/docs/notes/2026-09-12-issue-319-stylesheet-cost.md +154 -0
  136. package/docs/notes/2026-09-12-issue-325-product-card.md +100 -0
  137. package/docs/notes/2026-09-12-issue-327-home-hero.md +64 -0
  138. package/docs/notes/2026-09-13-demo-banner.md +32 -0
  139. package/docs/notes/2026-09-13-issue-332-home-info-cards.md +46 -0
  140. package/docs/notes/2026-09-13-quick-view-flicker.md +65 -0
  141. package/docs/notes/2026-09-14-cart-upsell.md +105 -0
  142. package/docs/notes/2026-09-14-demo-noindex.md +57 -0
  143. package/node.dockerfile +5 -2
  144. package/nuxt.config.ts +98 -15
  145. package/package.json +5 -3
  146. package/patches/@shopware__nuxt-module@1.5.1.patch +32 -0
  147. package/pnpm-workspace.yaml +2 -0
  148. package/public/hero-poster.webp +0 -0
  149. package/scripts/perf/first-render.mjs +30 -0
  150. package/scripts/perf/h2-proxy.cjs +49 -0
  151. package/scripts/perf/lighthouse.sh +22 -0
  152. package/scripts/perf/quick-view-layout.mjs +126 -0
  153. package/server/api/__sitemap__/urls.get.ts +49 -0
  154. package/server/api/category/[categoryId].get.ts +20 -9
  155. package/server/api/content/home.get.ts +14 -0
  156. package/server/api/content/page.get.ts +22 -0
  157. package/server/api/listing/[categoryId].get.ts +14 -18
  158. package/server/api/product/[productId]/cross-selling.get.ts +3 -9
  159. package/server/api/product/[productId].get.ts +4 -8
  160. package/server/api/search.get.ts +2 -0
  161. package/server/plugins/site-url.ts +13 -0
  162. package/server/plugins/sitemap-trailing-slash.ts +26 -0
  163. package/server/utils/sitemap.ts +60 -0
  164. package/server/utils/storeApi.ts +34 -0
  165. package/shopware.d.ts +35 -11
  166. package/test/e2e/simple-checkout-as-recurring-customer.test.ts +13 -17
  167. package/test/nuxt/AddressFields.test.ts +2 -73
  168. package/test/nuxt/AnimatedSection.test.ts +67 -0
  169. package/test/nuxt/CartBar.test.ts +110 -0
  170. package/test/nuxt/CartItem.test.ts +95 -0
  171. package/test/nuxt/CartUpsell.test.ts +125 -0
  172. package/test/nuxt/DemoBanner.test.ts +12 -0
  173. package/test/nuxt/FooterContact.test.ts +102 -0
  174. package/test/nuxt/FooterStoreStatus.test.ts +48 -0
  175. package/test/nuxt/HeaderRight.test.ts +14 -10
  176. package/test/nuxt/Hero.test.ts +68 -0
  177. package/test/nuxt/PaymentAndDelivery.test.ts +67 -8
  178. package/test/nuxt/RegistrationForm.test.ts +3 -0
  179. package/test/nuxt/useAddToCart.test.ts +37 -17
  180. package/test/nuxt/useAddressAutocomplete.test.ts +2 -38
  181. package/test/nuxt/useAddressSuggestionArea.test.ts +27 -0
  182. package/test/nuxt/useBusinessHours.test.ts +1 -1
  183. package/test/nuxt/useCartMutations.test.ts +225 -0
  184. package/test/nuxt/useCartUpsell.test.ts +126 -0
  185. package/test/nuxt/useCheckoutMethodGuard.test.ts +386 -0
  186. package/test/nuxt/useScrollAnimation.test.ts +55 -49
  187. package/test/nuxt/useSearchTracking.test.ts +71 -0
  188. package/test/nuxt/useSeoUrlRoute.test.ts +93 -0
  189. package/test/nuxt/useStoreStatus.test.ts +77 -0
  190. package/test/nuxt/useTrackEvent.test.ts +127 -0
  191. package/test/unit/media.spec.ts +49 -0
  192. package/test/unit/openingHours.spec.ts +90 -0
  193. package/test/unit/product.spec.ts +72 -0
  194. package/test/unit/schema.spec.ts +243 -0
  195. package/test/unit/seo.spec.ts +107 -0
  196. package/test/unit/seoPath.spec.ts +116 -0
  197. package/test/unit/sitemap.spec.ts +88 -0
  198. package/test/unit/useCategorySeo.spec.ts +42 -31
  199. package/test/unit/useSortingSelection.spec.ts +39 -0
  200. package/test/visual/playwright.config.ts +33 -0
  201. package/test/visual/visual.spec.ts +87 -0
  202. package/app/components/Product/CardDescription.vue +0 -27
  203. package/app/components/Product/CardFooter.vue +0 -51
  204. package/app/composables/useValidCitiesForDelivery.ts +0 -12
@@ -9,33 +9,31 @@ import { nextTick } from "vue";
9
9
  const {
10
10
  mockAddProducts,
11
11
  mockRefreshCart,
12
- mockToastAdd,
13
- mockTriggerProductAdded,
12
+ mockTriggerCartItemAdded,
14
13
  mockTrackEvent,
15
14
  } = vi.hoisted(() => ({
16
15
  mockAddProducts: vi.fn(),
17
16
  mockRefreshCart: vi.fn(),
18
- mockToastAdd: vi.fn(),
19
- mockTriggerProductAdded: vi.fn(),
17
+ mockTriggerCartItemAdded: vi.fn(),
20
18
  mockTrackEvent: vi.fn(),
21
19
  }));
22
20
 
23
- mockNuxtImport("useCart", () => {
21
+ mockNuxtImport("useCartMutations", () => {
24
22
  return () => ({
25
- addProducts: mockAddProducts,
26
- refreshCart: mockRefreshCart,
23
+ addLineItems: mockAddProducts,
27
24
  });
28
25
  });
29
26
 
30
- mockNuxtImport("useToast", () => {
27
+ mockNuxtImport("useCart", () => {
31
28
  return () => ({
32
- add: mockToastAdd,
29
+ addProducts: mockAddProducts,
30
+ refreshCart: mockRefreshCart,
33
31
  });
34
32
  });
35
33
 
36
34
  mockNuxtImport("useProductEvents", () => {
37
35
  return () => ({
38
- triggerProductAdded: mockTriggerProductAdded,
36
+ triggerCartItemAdded: mockTriggerCartItemAdded,
39
37
  });
40
38
  });
41
39
 
@@ -46,15 +44,15 @@ mockNuxtImport("useTrackEvent", () => {
46
44
  });
47
45
 
48
46
  // Provide the mocks globally or in a way that they are picked up
47
+ vi.stubGlobal("useCartMutations", () => ({
48
+ addLineItems: mockAddProducts,
49
+ }));
49
50
  vi.stubGlobal("useCart", () => ({
50
51
  addProducts: mockAddProducts,
51
52
  refreshCart: mockRefreshCart,
52
53
  }));
53
- vi.stubGlobal("useToast", () => ({
54
- add: mockToastAdd,
55
- }));
56
54
  vi.stubGlobal("useProductEvents", () => ({
57
- triggerProductAdded: mockTriggerProductAdded,
55
+ triggerCartItemAdded: mockTriggerCartItemAdded,
58
56
  }));
59
57
  vi.stubGlobal("useTrackEvent", () => ({
60
58
  trackAddToCart: mockTrackEvent,
@@ -133,12 +131,34 @@ describe("useAddToCart", () => {
133
131
  type: "product",
134
132
  },
135
133
  ]);
136
- expect(mockRefreshCart).toHaveBeenCalled();
137
- expect(mockToastAdd).toHaveBeenCalled();
138
- expect(mockTriggerProductAdded).toHaveBeenCalled();
134
+ expect(mockTriggerCartItemAdded).toHaveBeenCalledWith(mockProduct, 1);
139
135
  expect(mockTrackEvent).toHaveBeenCalledWith(mockProduct, 1);
140
136
  });
141
137
 
138
+ it("should not report success when adding to cart failed", async () => {
139
+ const { setSelectedProduct, addToCart, isLoading } = useAddToCart();
140
+ setSelectedProduct(mockProduct);
141
+
142
+ // useCartMutations reports the failure itself and resolves undefined
143
+ mockAddProducts.mockResolvedValue(undefined);
144
+
145
+ await addToCart();
146
+
147
+ expect(isLoading.value).toBe(false);
148
+ expect(mockTriggerCartItemAdded).not.toHaveBeenCalled();
149
+ expect(mockTrackEvent).not.toHaveBeenCalled();
150
+ });
151
+
152
+ it("should reset isLoading when the mutation throws", async () => {
153
+ const { setSelectedProduct, addToCart, isLoading } = useAddToCart();
154
+ setSelectedProduct(mockProduct);
155
+
156
+ mockAddProducts.mockRejectedValue(new Error("boom"));
157
+
158
+ await expect(addToCart()).rejects.toThrow("boom");
159
+ expect(isLoading.value).toBe(false);
160
+ });
161
+
142
162
  it("should add product with extras to cart as container", async () => {
143
163
  const { setSelectedProduct, setSelectedExtras, addToCart } = useAddToCart();
144
164
  setSelectedProduct(mockProduct);
@@ -16,14 +16,13 @@ mockNuxtImport("useRuntimeConfig", () => () => ({
16
16
  geoapifyApiKey: "test-api-key",
17
17
  }));
18
18
 
19
- // Mock useValidCitiesForDelivery
19
+ // Mock useAddressSuggestionArea
20
20
  const { mockBoundingBoxCoordinates } = vi.hoisted(() => ({
21
21
  mockBoundingBoxCoordinates: {
22
22
  value: "8.822251,50.055026,8.899077,50.104327",
23
23
  },
24
24
  }));
25
- mockNuxtImport("useValidCitiesForDelivery", () => () => ({
26
- validCities: ref(["Obertshausen", "Lämmerspiel", "Hausen"]),
25
+ mockNuxtImport("useAddressSuggestionArea", () => () => ({
27
26
  boundingBoxCoordinates: mockBoundingBoxCoordinates,
28
27
  }));
29
28
 
@@ -103,41 +102,6 @@ describe("useAddressAutocomplete", () => {
103
102
  expect(consoleSpy).toHaveBeenCalled();
104
103
  consoleSpy.mockRestore();
105
104
  });
106
- it("should filter suggestions based on valid cities", async () => {
107
- const mockResponse = {
108
- features: [
109
- {
110
- properties: {
111
- street: "Main St",
112
- housenumber: "1",
113
- city: "Obertshausen",
114
- postcode: "63179",
115
- formatted: "Main St 1, 63179 Obertshausen, Germany",
116
- },
117
- },
118
- {
119
- properties: {
120
- street: "Other St",
121
- housenumber: "2",
122
- city: "Berlin",
123
- postcode: "10115",
124
- formatted: "Other St 2, 10115 Berlin, Germany",
125
- },
126
- },
127
- ],
128
- };
129
-
130
- mockUseFetch.mockResolvedValue({
131
- data: ref(mockResponse),
132
- });
133
-
134
- const { getSuggestions } = useAddressAutocomplete();
135
- const suggestions = await getSuggestions("Main St 1");
136
-
137
- expect(suggestions).toHaveLength(1);
138
- expect(suggestions[0]?.city).toBe("Obertshausen");
139
- });
140
-
141
105
  it("should not include filter when boundingBoxCoordinates is empty", async () => {
142
106
  mockBoundingBoxCoordinates.value = "";
143
107
  mockUseFetch.mockResolvedValue({
@@ -0,0 +1,27 @@
1
+ import { describe, it, expect, beforeEach } from "vitest";
2
+ import { useAddressSuggestionArea } from "../../app/composables/useAddressSuggestionArea";
3
+
4
+ // Setting the real runtime config instead of mocking useRuntimeConfig, see
5
+ // FooterContact.test.ts.
6
+ function setBoundingBox(boundingBox: string) {
7
+ useRuntimeConfig().public.shopBite.addressAutocomplete.boundingBox =
8
+ boundingBox;
9
+ }
10
+
11
+ describe("useAddressSuggestionArea", () => {
12
+ beforeEach(() => {
13
+ setBoundingBox("");
14
+ });
15
+
16
+ it("has no bounding box without configuration", () => {
17
+ expect(useAddressSuggestionArea().boundingBoxCoordinates.value).toBe("");
18
+ });
19
+
20
+ it("returns the trimmed bounding box", () => {
21
+ setBoundingBox(" 8.822251,50.055026,8.899077,50.104327 ");
22
+
23
+ expect(useAddressSuggestionArea().boundingBoxCoordinates.value).toBe(
24
+ "8.822251,50.055026,8.899077,50.104327",
25
+ );
26
+ });
27
+ });
@@ -6,7 +6,7 @@ import { useBusinessHours } from "~/composables/useBusinessHours";
6
6
  // Mock useAsyncData
7
7
  mockNuxtImport("useAsyncData", () => {
8
8
  return (key: string, handler: () => Promise<unknown>) => {
9
- const data = ref(null);
9
+ const data = ref<unknown>(null);
10
10
  const pending = ref(false);
11
11
  const refresh = vi.fn(async () => {
12
12
  data.value = await handler();
@@ -0,0 +1,225 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+ import { mockNuxtImport } from "@nuxt/test-utils/runtime";
3
+ import { ref } from "vue";
4
+ import {
5
+ useCartMutations,
6
+ isCartLockedError,
7
+ } from "~/composables/useCartMutations";
8
+ import type { Schemas } from "#shopware";
9
+
10
+ const {
11
+ mockRefreshCart,
12
+ mockAddProducts,
13
+ mockChangeProductQuantity,
14
+ mockRemoveItem,
15
+ mockToastAdd,
16
+ } = vi.hoisted(() => ({
17
+ mockRefreshCart: vi.fn(),
18
+ mockAddProducts: vi.fn(),
19
+ mockChangeProductQuantity: vi.fn(),
20
+ mockRemoveItem: vi.fn(),
21
+ mockToastAdd: vi.fn(),
22
+ }));
23
+
24
+ // Read lazily inside the factory, i.e. after module initialisation.
25
+ const cart = ref<Schemas["Cart"] | undefined>();
26
+
27
+ mockNuxtImport("useCart", () => () => ({
28
+ cart,
29
+ refreshCart: mockRefreshCart,
30
+ addProducts: mockAddProducts,
31
+ changeProductQuantity: mockChangeProductQuantity,
32
+ removeItem: mockRemoveItem,
33
+ }));
34
+
35
+ mockNuxtImport("useToast", () => () => ({
36
+ add: mockToastAdd,
37
+ }));
38
+
39
+ function cartWith(items: { id: string; quantity: number }[]) {
40
+ return { lineItems: items } as unknown as Schemas["Cart"];
41
+ }
42
+
43
+ /** A promise the test resolves by hand. */
44
+ function deferred<T = void>() {
45
+ let resolve!: (value: T) => void;
46
+ let reject!: (reason?: unknown) => void;
47
+ const promise = new Promise<T>((res, rej) => {
48
+ resolve = res;
49
+ reject = rej;
50
+ });
51
+ return { promise, resolve, reject };
52
+ }
53
+
54
+ const lockedError = Object.assign(new Error("locked"), {
55
+ status: 409,
56
+ details: { errors: [{ code: "CHECKOUT__CART_LOCKED" }] },
57
+ });
58
+
59
+ describe("isCartLockedError", () => {
60
+ it("detects the lock conflict by status or error code", () => {
61
+ expect(isCartLockedError(lockedError)).toBe(true);
62
+ expect(isCartLockedError({ status: 409 })).toBe(true);
63
+ expect(
64
+ isCartLockedError({
65
+ details: { errors: [{ code: "CHECKOUT__CART_LOCKED" }] },
66
+ }),
67
+ ).toBe(true);
68
+ expect(isCartLockedError({ status: 500 })).toBe(false);
69
+ expect(isCartLockedError(new Error("x"))).toBe(false);
70
+ expect(isCartLockedError(undefined)).toBe(false);
71
+ });
72
+ });
73
+
74
+ describe("useCartMutations", () => {
75
+ beforeEach(() => {
76
+ vi.clearAllMocks();
77
+ cart.value = cartWith([{ id: "li-1", quantity: 1 }]);
78
+ mockRefreshCart.mockResolvedValue(undefined);
79
+ mockAddProducts.mockResolvedValue({ id: "cart" });
80
+ mockRemoveItem.mockResolvedValue({ id: "cart" });
81
+ mockChangeProductQuantity.mockImplementation(
82
+ async ({ id, quantity }: { id: string; quantity: number }) => {
83
+ cart.value = cartWith([{ id, quantity }]);
84
+ return cart.value;
85
+ },
86
+ );
87
+ });
88
+
89
+ it("runs writes one after another in call order", async () => {
90
+ const first = deferred<Schemas["Cart"]>();
91
+ mockAddProducts.mockReturnValueOnce(first.promise);
92
+
93
+ const { addLineItems, removeLineItem, isMutating } = useCartMutations();
94
+
95
+ const add = addLineItems([{ id: "p1", quantity: 1, type: "product" }]);
96
+ const remove = removeLineItem({ id: "li-1" } as Schemas["LineItem"]);
97
+ await Promise.resolve();
98
+
99
+ expect(isMutating.value).toBe(true);
100
+ expect(mockAddProducts).toHaveBeenCalledTimes(1);
101
+ expect(mockRemoveItem).not.toHaveBeenCalled();
102
+
103
+ first.resolve({ id: "cart" } as unknown as Schemas["Cart"]);
104
+ await add;
105
+ await remove;
106
+
107
+ expect(mockRemoveItem).toHaveBeenCalledTimes(1);
108
+ expect(mockAddProducts.mock.invocationCallOrder[0]).toBeLessThan(
109
+ mockRemoveItem.mock.invocationCallOrder[0] as number,
110
+ );
111
+ expect(isMutating.value).toBe(false);
112
+ });
113
+
114
+ it("coalesces rapid quantity changes into one request with the latest value", async () => {
115
+ const inFlight = deferred<Schemas["Cart"]>();
116
+ mockChangeProductQuantity.mockReturnValueOnce(inFlight.promise);
117
+
118
+ const { setQuantity } = useCartMutations();
119
+
120
+ const p1 = setQuantity("li-1", 2);
121
+ await Promise.resolve();
122
+ expect(mockChangeProductQuantity).toHaveBeenCalledWith({
123
+ id: "li-1",
124
+ quantity: 2,
125
+ });
126
+
127
+ // while the first request is in flight: three more clicks
128
+ const p2 = setQuantity("li-1", 3);
129
+ const p3 = setQuantity("li-1", 4);
130
+ const p4 = setQuantity("li-1", 5);
131
+ expect(p3).toBe(p2);
132
+ expect(p4).toBe(p2);
133
+
134
+ cart.value = cartWith([{ id: "li-1", quantity: 2 }]);
135
+ inFlight.resolve(cart.value);
136
+ await Promise.all([p1, p2, p3, p4]);
137
+
138
+ expect(mockChangeProductQuantity).toHaveBeenCalledTimes(2);
139
+ expect(mockChangeProductQuantity).toHaveBeenLastCalledWith({
140
+ id: "li-1",
141
+ quantity: 5,
142
+ });
143
+ });
144
+
145
+ it("skips the request when the latest value equals the cart quantity", async () => {
146
+ const { setQuantity } = useCartMutations();
147
+
148
+ await setQuantity("li-1", 1);
149
+
150
+ expect(mockChangeProductQuantity).not.toHaveBeenCalled();
151
+ });
152
+
153
+ it("retries once when the cart is locked", async () => {
154
+ vi.useFakeTimers();
155
+ try {
156
+ mockChangeProductQuantity
157
+ .mockRejectedValueOnce(lockedError)
158
+ .mockResolvedValueOnce(cartWith([{ id: "li-1", quantity: 3 }]));
159
+
160
+ const { setQuantity } = useCartMutations();
161
+ const pending = setQuantity("li-1", 3);
162
+ await vi.advanceTimersByTimeAsync(400);
163
+ await pending;
164
+
165
+ expect(mockChangeProductQuantity).toHaveBeenCalledTimes(2);
166
+ expect(mockToastAdd).not.toHaveBeenCalled();
167
+ } finally {
168
+ vi.useRealTimers();
169
+ }
170
+ });
171
+
172
+ it("syncs the shared cart from the write response", async () => {
173
+ const returned = cartWith([
174
+ { id: "li-1", quantity: 1 },
175
+ { id: "li-2", quantity: 1 },
176
+ ]);
177
+ mockAddProducts.mockResolvedValueOnce(returned);
178
+
179
+ const { addLineItems } = useCartMutations();
180
+ await addLineItems([{ id: "p2", quantity: 1, type: "product" }]);
181
+
182
+ expect(mockRefreshCart).toHaveBeenCalledWith(returned);
183
+ });
184
+
185
+ it("does not retry other errors", async () => {
186
+ mockChangeProductQuantity.mockRejectedValueOnce(new Error("network"));
187
+
188
+ const { setQuantity } = useCartMutations();
189
+ await setQuantity("li-1", 3);
190
+
191
+ expect(mockChangeProductQuantity).toHaveBeenCalledTimes(1);
192
+ expect(mockToastAdd).toHaveBeenCalledWith(
193
+ expect.objectContaining({ title: "Menge konnte nicht geändert werden" }),
194
+ );
195
+ });
196
+
197
+ it("reports a final failure, re-syncs the cart and resolves undefined", async () => {
198
+ mockRemoveItem.mockRejectedValue(new Error("network"));
199
+
200
+ const { removeLineItem, isMutating } = useCartMutations();
201
+ const result = await removeLineItem({ id: "li-1" } as Schemas["LineItem"]);
202
+
203
+ expect(result).toBeUndefined();
204
+ expect(mockToastAdd).toHaveBeenCalledWith(
205
+ expect.objectContaining({
206
+ title: "Artikel konnte nicht entfernt werden",
207
+ color: "error",
208
+ }),
209
+ );
210
+ expect(mockRefreshCart).toHaveBeenCalledWith();
211
+ expect(isMutating.value).toBe(false);
212
+ });
213
+
214
+ it("keeps processing the queue after a failure", async () => {
215
+ mockAddProducts.mockRejectedValueOnce(new Error("boom"));
216
+
217
+ const { addLineItems } = useCartMutations();
218
+ const failed = addLineItems([{ id: "p1", quantity: 1, type: "product" }]);
219
+ const next = addLineItems([{ id: "p2", quantity: 1, type: "product" }]);
220
+
221
+ expect(await failed).toBeUndefined();
222
+ expect(await next).toEqual({ id: "cart" });
223
+ expect(mockAddProducts).toHaveBeenCalledTimes(2);
224
+ });
225
+ });
@@ -0,0 +1,126 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+ import { mockNuxtImport } from "@nuxt/test-utils/runtime";
3
+ import { useCartUpsell } from "~/composables/useCartUpsell";
4
+ import type { Schemas } from "#shopware";
5
+
6
+ const {
7
+ mockInvoke,
8
+ mockAddLineItems,
9
+ mockTriggerCartItemAdded,
10
+ mockTrackUpsellAdd,
11
+ } = vi.hoisted(() => ({
12
+ mockInvoke: vi.fn(),
13
+ mockAddLineItems: vi.fn(),
14
+ mockTriggerCartItemAdded: vi.fn(),
15
+ mockTrackUpsellAdd: vi.fn(),
16
+ }));
17
+
18
+ mockNuxtImport("useShopwareContext", () => () => ({
19
+ apiClient: { invoke: mockInvoke },
20
+ }));
21
+ mockNuxtImport("useCartMutations", () => () => ({
22
+ addLineItems: mockAddLineItems,
23
+ }));
24
+ mockNuxtImport("useProductEvents", () => () => ({
25
+ triggerCartItemAdded: mockTriggerCartItemAdded,
26
+ }));
27
+ mockNuxtImport("useTrackEvent", () => () => ({
28
+ trackUpsellAdd: mockTrackUpsellAdd,
29
+ }));
30
+
31
+ const tiramisu = {
32
+ id: "tiramisu",
33
+ productNumber: "D1",
34
+ translated: { name: "Tiramisu" },
35
+ } as unknown as Schemas["Product"];
36
+
37
+ describe("useCartUpsell", () => {
38
+ beforeEach(() => {
39
+ vi.clearAllMocks();
40
+ });
41
+
42
+ it("loads the flagged products without variant parents", async () => {
43
+ mockInvoke.mockResolvedValue({ data: { elements: [tiramisu] } });
44
+
45
+ const result = await useCartUpsell().loadUpsellProducts();
46
+
47
+ expect(result).toEqual([tiramisu]);
48
+ expect(mockInvoke).toHaveBeenCalledWith(
49
+ "readProduct post /product",
50
+ expect.objectContaining({
51
+ headers: { "sw-inheritance": "true" },
52
+ body: expect.objectContaining({
53
+ limit: 6,
54
+ filter: [
55
+ {
56
+ type: "equals",
57
+ field: "customFields.shopbite_cart_upsell",
58
+ value: true,
59
+ },
60
+ // Variants have no child count (null), main products 0.
61
+ {
62
+ type: "multi",
63
+ operator: "or",
64
+ queries: [
65
+ { type: "equals", field: "childCount", value: 0 },
66
+ { type: "equals", field: "childCount", value: null },
67
+ ],
68
+ },
69
+ ],
70
+ }),
71
+ }),
72
+ );
73
+ });
74
+
75
+ it("returns an empty list when the request fails", async () => {
76
+ mockInvoke.mockRejectedValue(new Error("Network error"));
77
+ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
78
+
79
+ expect(await useCartUpsell().loadUpsellProducts()).toEqual([]);
80
+ expect(consoleSpy).toHaveBeenCalled();
81
+ consoleSpy.mockRestore();
82
+ });
83
+
84
+ it("adds the product with quantity 1 and reports the add", async () => {
85
+ mockAddLineItems.mockResolvedValue({ id: "cart" });
86
+
87
+ await useCartUpsell().addUpsellProduct(tiramisu);
88
+
89
+ expect(mockAddLineItems).toHaveBeenCalledWith([
90
+ { id: "tiramisu", quantity: 1, type: "product" },
91
+ ]);
92
+ expect(mockTriggerCartItemAdded).toHaveBeenCalledWith(tiramisu, 1);
93
+ expect(mockTrackUpsellAdd).toHaveBeenCalledWith(tiramisu, 1);
94
+ });
95
+
96
+ it("reports nothing when the add failed", async () => {
97
+ // useCartMutations shows the error toast itself and resolves undefined.
98
+ mockAddLineItems.mockResolvedValue(undefined);
99
+
100
+ await useCartUpsell().addUpsellProduct(tiramisu);
101
+
102
+ expect(mockTriggerCartItemAdded).not.toHaveBeenCalled();
103
+ expect(mockTrackUpsellAdd).not.toHaveBeenCalled();
104
+ });
105
+
106
+ it("marks the product as pending while the add is queued", async () => {
107
+ let resolveAdd: (cart: unknown) => void = () => {};
108
+ mockAddLineItems.mockReturnValue(
109
+ new Promise((resolve) => {
110
+ resolveAdd = resolve;
111
+ }),
112
+ );
113
+ const { addUpsellProduct, isAdding } = useCartUpsell();
114
+
115
+ const adding = addUpsellProduct(tiramisu);
116
+ expect(isAdding("tiramisu")).toBe(true);
117
+
118
+ // A second click while queued does not add the product twice.
119
+ await addUpsellProduct(tiramisu);
120
+ expect(mockAddLineItems).toHaveBeenCalledTimes(1);
121
+
122
+ resolveAdd({ id: "cart" });
123
+ await adding;
124
+ expect(isAdding("tiramisu")).toBe(false);
125
+ });
126
+ });