@shopbite-de/storefront 2.0.3 → 2.1.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.
@@ -0,0 +1,207 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+ import { mockNuxtImport } from "@nuxt/test-utils/runtime";
3
+ import { ref } from "vue";
4
+ import { ApiClientError } from "@shopware/api-client";
5
+ import {
6
+ isSameConfiguration,
7
+ normalizeWishlistList,
8
+ } from "~/composables/useWishlistEntries";
9
+
10
+ const { invoke } = vi.hoisted(() => ({ invoke: vi.fn() }));
11
+ const isLoggedIn = ref(false);
12
+
13
+ mockNuxtImport("useShopwareContext", () => () => ({
14
+ apiClient: { invoke },
15
+ }));
16
+ mockNuxtImport("useUser", () => () => ({ isLoggedIn }));
17
+
18
+ function missingRoute() {
19
+ const error = Object.create(ApiClientError.prototype) as ApiClientError<{
20
+ errors: [];
21
+ }>;
22
+ Object.defineProperty(error, "status", { value: 404 });
23
+ return error;
24
+ }
25
+
26
+ const pizza = {
27
+ productId: "p1",
28
+ productNumber: "21",
29
+ without: ["Pilze"],
30
+ extras: ["X1"],
31
+ };
32
+
33
+ function remoteEntry(id: string, configuration = pizza) {
34
+ return { id, createdAt: "2026-10-08T12:00:00Z", ...configuration };
35
+ }
36
+
37
+ describe("useWishlistEntries (#467)", () => {
38
+ beforeEach(() => {
39
+ vi.clearAllMocks();
40
+ localStorage.clear();
41
+ isLoggedIn.value = false;
42
+ const { entries, loaded } = useWishlistEntries();
43
+ entries.value = [];
44
+ loaded.value = false;
45
+ useState("shopbite-wishlist-routes-missing").value = false;
46
+ useState("shopbite-wishlist-core-migrated").value = false;
47
+ });
48
+
49
+ it("compares configurations independent of order and duplicates", () => {
50
+ expect(normalizeWishlistList([" b", "a", "b", ""])).toEqual(["a", "b"]);
51
+ expect(
52
+ isSameConfiguration(
53
+ { productId: "p1", without: ["Pilze", "Käse"], extras: ["X2", "X1"] },
54
+ { productId: "p1", without: ["Käse", "Pilze"], extras: ["X1", "X2"] },
55
+ ),
56
+ ).toBe(true);
57
+ expect(isSameConfiguration(pizza, { ...pizza, extras: ["X1", "X2"] })).toBe(
58
+ false,
59
+ );
60
+ });
61
+
62
+ it("keeps guest entries in local storage, the same dish twice when configured differently", async () => {
63
+ const { add, find, entries, remove, load } = useWishlistEntries();
64
+
65
+ await add(pizza);
66
+ await add({ ...pizza, without: [] });
67
+ await add({ ...pizza, without: ["Pilze"] }); // same as the first
68
+
69
+ expect(entries.value).toHaveLength(2);
70
+ expect(invoke).not.toHaveBeenCalled();
71
+ const stored = JSON.parse(localStorage.getItem("shopbite-wishlist")!);
72
+ expect(stored).toHaveLength(2);
73
+
74
+ await remove(find(pizza)!.id);
75
+ entries.value = [];
76
+ await load();
77
+ expect(entries.value).toHaveLength(1);
78
+ expect(entries.value[0]!.without).toEqual([]);
79
+ });
80
+
81
+ it("turns the product ids of the old guest wishlist into entries", async () => {
82
+ localStorage.setItem("sw-wishlist-items", JSON.stringify(["p9"]));
83
+ const { load, entries } = useWishlistEntries();
84
+
85
+ await load();
86
+
87
+ expect(entries.value).toMatchObject([
88
+ { productId: "p9", without: [], extras: [] },
89
+ ]);
90
+ });
91
+
92
+ it("loads and saves a customer's entries through the plugin", async () => {
93
+ isLoggedIn.value = true;
94
+ invoke.mockImplementation(async (operation: string) => {
95
+ if (operation.startsWith("shopbite.wishlist.list"))
96
+ return { data: { elements: [remoteEntry("e1")] } };
97
+ if (operation.startsWith("shopbite.wishlist.add"))
98
+ return { data: remoteEntry("e2", { ...pizza, without: [] }) };
99
+ if (operation.startsWith("readCustomerWishlist"))
100
+ return { data: { products: { elements: [] } } };
101
+ return { data: undefined };
102
+ });
103
+ const { load, add, entries, remove } = useWishlistEntries();
104
+
105
+ await load();
106
+ expect(entries.value.map((entry) => entry.id)).toEqual(["e1"]);
107
+
108
+ await add({ ...pizza, without: [] });
109
+ expect(invoke).toHaveBeenCalledWith(
110
+ "shopbite.wishlist.add post /shopbite/wishlist/add",
111
+ { body: { productId: "p1", without: [], extras: ["X1"] } },
112
+ );
113
+ expect(entries.value.map((entry) => entry.id)).toEqual(["e2", "e1"]);
114
+
115
+ await remove("e1");
116
+ expect(invoke).toHaveBeenCalledWith(
117
+ "shopbite.wishlist.delete delete /shopbite/wishlist/{id}",
118
+ { pathParams: { id: "e1" } },
119
+ );
120
+ expect(localStorage.getItem("shopbite-wishlist")).toBeNull();
121
+ });
122
+
123
+ it("moves the Shopware wishlist of a customer into the plugin once", async () => {
124
+ isLoggedIn.value = true;
125
+ let stored: ReturnType<typeof remoteEntry>[] = [];
126
+ let core = [{ id: "old" }];
127
+ invoke.mockImplementation(async (operation: string) => {
128
+ if (operation.startsWith("shopbite.wishlist.list"))
129
+ return { data: { elements: stored } };
130
+ if (operation.startsWith("readCustomerWishlist"))
131
+ return { data: { products: { elements: core } } };
132
+ if (operation.startsWith("shopbite.wishlist.merge")) {
133
+ stored = [
134
+ remoteEntry("e3", {
135
+ productId: "old",
136
+ productNumber: "5",
137
+ without: [],
138
+ extras: [],
139
+ }),
140
+ ];
141
+ return { data: { elements: stored } };
142
+ }
143
+ if (operation.startsWith("deleteProductOnWishlist")) core = [];
144
+ return { data: undefined };
145
+ });
146
+ const { load, entries } = useWishlistEntries();
147
+
148
+ await load();
149
+ await load();
150
+
151
+ expect(entries.value.map((entry) => entry.id)).toEqual(["e3"]);
152
+ expect(
153
+ invoke.mock.calls.filter(([operation]) =>
154
+ operation.startsWith("readCustomerWishlist"),
155
+ ),
156
+ ).toHaveLength(1);
157
+ expect(invoke).toHaveBeenCalledWith(
158
+ "deleteProductOnWishlist delete /customer/wishlist/delete/{productId}",
159
+ { pathParams: { productId: "old" } },
160
+ );
161
+ });
162
+
163
+ it("moves guest entries into the account after the login", async () => {
164
+ const { add, mergeGuestEntries, entries } = useWishlistEntries();
165
+ await add(pizza);
166
+ isLoggedIn.value = true;
167
+ invoke.mockImplementation(async (operation: string) => {
168
+ if (operation.startsWith("shopbite.wishlist.merge"))
169
+ return { data: { elements: [remoteEntry("e1")] } };
170
+ if (operation.startsWith("readCustomerWishlist"))
171
+ return { data: { products: { elements: [] } } };
172
+ return { data: undefined };
173
+ });
174
+
175
+ await mergeGuestEntries();
176
+
177
+ expect(invoke).toHaveBeenCalledWith(
178
+ "shopbite.wishlist.merge post /shopbite/wishlist/merge",
179
+ {
180
+ body: {
181
+ items: [{ productId: "p1", without: ["Pilze"], extras: ["X1"] }],
182
+ },
183
+ },
184
+ );
185
+ expect(entries.value.map((entry) => entry.id)).toEqual(["e1"]);
186
+ expect(localStorage.getItem("shopbite-wishlist")).toBeNull();
187
+ });
188
+
189
+ it("stays in the browser when the backend has no wishlist routes", async () => {
190
+ isLoggedIn.value = true;
191
+ invoke.mockRejectedValue(missingRoute());
192
+ const { load, add, entries } = useWishlistEntries();
193
+
194
+ await load();
195
+ await add(pizza);
196
+
197
+ expect(entries.value).toHaveLength(1);
198
+ expect(JSON.parse(localStorage.getItem("shopbite-wishlist")!)).toHaveLength(
199
+ 1,
200
+ );
201
+ expect(
202
+ invoke.mock.calls.filter(([operation]) =>
203
+ operation.startsWith("shopbite.wishlist.add"),
204
+ ),
205
+ ).toHaveLength(0);
206
+ });
207
+ });
@@ -1,40 +0,0 @@
1
- <script setup lang="ts">
2
- import type { Schemas } from "#shopware";
3
-
4
- /** Wishlist toggle for a dish (`aria-pressed`, #445). */
5
- const props = defineProps<{
6
- product: Schemas["Product"];
7
- }>();
8
-
9
- const { addToWishlist, isInWishlist, removeFromWishlist } = useProductWishlist(
10
- props.product.id,
11
- );
12
- const { trackAddToWishlist } = useTrackEvent();
13
- const productName = computed(
14
- () => props.product.translated?.name ?? props.product.name ?? "Gericht",
15
- );
16
-
17
- const toggleWishlistProduct = async () => {
18
- try {
19
- if (isInWishlist.value) {
20
- await removeFromWishlist();
21
- } else {
22
- await addToWishlist();
23
- trackAddToWishlist(props.product);
24
- }
25
- } catch (error) {
26
- console.error("[wishlist][handleWishlistError]", error);
27
- }
28
- };
29
- </script>
30
-
31
- <template>
32
- <SbIconButton
33
- :label="`${productName} merken`"
34
- :pressed="isInWishlist"
35
- variant="ghost"
36
- @click="toggleWishlistProduct"
37
- >
38
- <SbIcon name="heart" :filled="isInWishlist" />
39
- </SbIconButton>
40
- </template>
@@ -1,96 +0,0 @@
1
- import type { Schemas } from "#shopware";
2
-
3
- export function useWishlistActions() {
4
- const { addProducts, refreshCart } = useCart();
5
- const { triggerProductAdded } = useProductEvents();
6
- const { clearWishlist } = useWishlist();
7
- const { trackAddToCart: trackAddToCartEvent } = useTrackEvent();
8
-
9
- const isAddingToCart = ref(false);
10
- const addingItemId = ref<string | null>(null);
11
- const isLoading = ref(false);
12
-
13
- const clearWishlistHandler = async () => {
14
- try {
15
- isLoading.value = true;
16
- clearWishlist();
17
- } finally {
18
- isLoading.value = false;
19
- }
20
- };
21
-
22
- const addSingleItemToCart = async (product: Schemas["Product"]) => {
23
- try {
24
- addingItemId.value = product.id;
25
-
26
- // Check if this is a base product with variants
27
- const isBaseProduct = product.childCount && product.childCount > 0;
28
- if (isBaseProduct) {
29
- return;
30
- }
31
-
32
- const cartItems = [
33
- {
34
- id: product.id,
35
- quantity: 1,
36
- type: "product" as const,
37
- },
38
- ];
39
-
40
- const newCart = await addProducts(cartItems);
41
- await refreshCart(newCart);
42
-
43
- triggerProductAdded();
44
- trackAddToCartEvent(product, 1);
45
- } catch (error) {
46
- console.error("[wishlist][addSingleItemToCart] Error details:", error);
47
- } finally {
48
- addingItemId.value = null;
49
- }
50
- };
51
-
52
- const addAllItemsToCart = async (products: Schemas["Product"][]) => {
53
- if (products.length === 0) {
54
- return;
55
- }
56
-
57
- try {
58
- isAddingToCart.value = true;
59
-
60
- // Filter out base products (parent products with childCount > 0)
61
- // Only add actual variants or simple products
62
- const addableProducts = products.filter((product) => {
63
- const isBaseProduct = product.childCount && product.childCount > 0;
64
- return !isBaseProduct;
65
- });
66
-
67
- if (addableProducts.length === 0) {
68
- return;
69
- }
70
-
71
- const cartItems = addableProducts.map((product) => ({
72
- id: product.id,
73
- quantity: 1,
74
- type: "product" as const,
75
- }));
76
-
77
- const newCart = await addProducts(cartItems);
78
- await refreshCart(newCart);
79
-
80
- triggerProductAdded();
81
- } catch (error) {
82
- console.error("[wishlist][addAllItemsToCart] Error:", error);
83
- } finally {
84
- isAddingToCart.value = false;
85
- }
86
- };
87
-
88
- return {
89
- isAddingToCart,
90
- addingItemId,
91
- isLoading,
92
- clearWishlistHandler,
93
- addSingleItemToCart,
94
- addAllItemsToCart,
95
- };
96
- }
@@ -1,117 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from "vitest";
2
- import { mockNuxtImport } from "@nuxt/test-utils/runtime";
3
- import { useWishlistActions } from "../../app/composables/useWishlistActions";
4
- import type { Schemas } from "#shopware";
5
-
6
- const {
7
- mockAddProducts,
8
- mockRefreshCart,
9
- mockTriggerProductAdded,
10
- mockClearWishlist,
11
- mockTrackEvent,
12
- } = vi.hoisted(() => ({
13
- mockAddProducts: vi.fn(),
14
- mockRefreshCart: vi.fn(),
15
- mockTriggerProductAdded: vi.fn(),
16
- mockClearWishlist: vi.fn(),
17
- mockTrackEvent: vi.fn(),
18
- }));
19
-
20
- mockNuxtImport("useCart", () => () => ({
21
- addProducts: mockAddProducts,
22
- refreshCart: mockRefreshCart,
23
- }));
24
-
25
- mockNuxtImport("useProductEvents", () => () => ({
26
- triggerProductAdded: mockTriggerProductAdded,
27
- }));
28
-
29
- mockNuxtImport("useWishlist", () => () => ({
30
- clearWishlist: mockClearWishlist,
31
- }));
32
-
33
- mockNuxtImport("useTrackEvent", () => () => ({
34
- trackAddToCart: mockTrackEvent,
35
- }));
36
-
37
- describe("useWishlistActions", () => {
38
- const mockProduct = {
39
- id: "prod-1",
40
- translated: { name: "Test Product" },
41
- productNumber: "SW123",
42
- } as unknown as Schemas["Product"];
43
-
44
- beforeEach(() => {
45
- vi.clearAllMocks();
46
- });
47
-
48
- it("should clear wishlist", async () => {
49
- const { clearWishlistHandler, isLoading } = useWishlistActions();
50
- await clearWishlistHandler();
51
- expect(mockClearWishlist).toHaveBeenCalled();
52
- expect(isLoading.value).toBe(false);
53
- });
54
-
55
- it("should add single item to cart", async () => {
56
- const { addSingleItemToCart } = useWishlistActions();
57
- mockAddProducts.mockResolvedValue({ id: "cart-1" });
58
-
59
- await addSingleItemToCart(mockProduct);
60
-
61
- expect(mockAddProducts).toHaveBeenCalledWith([
62
- { id: "prod-1", quantity: 1, type: "product" },
63
- ]);
64
- expect(mockRefreshCart).toHaveBeenCalled();
65
- expect(mockTriggerProductAdded).toHaveBeenCalled();
66
- expect(mockTrackEvent).toHaveBeenCalledWith(mockProduct, 1);
67
- });
68
-
69
- it("should not add a base product with variants", async () => {
70
- const baseProduct = { ...mockProduct, childCount: 2 };
71
- const { addSingleItemToCart } = useWishlistActions();
72
-
73
- await addSingleItemToCart(baseProduct);
74
-
75
- expect(mockAddProducts).not.toHaveBeenCalled();
76
- });
77
-
78
- it("should add all items to cart", async () => {
79
- const products = [
80
- { id: "p1", translated: { name: "P1" } },
81
- { id: "p2", translated: { name: "P2" } },
82
- ] as unknown as Schemas["Product"][];
83
- const { addAllItemsToCart, isAddingToCart } = useWishlistActions();
84
- mockAddProducts.mockResolvedValue({ id: "cart-1" });
85
-
86
- await addAllItemsToCart(products);
87
-
88
- expect(mockAddProducts).toHaveBeenCalledWith([
89
- { id: "p1", quantity: 1, type: "product" },
90
- { id: "p2", quantity: 1, type: "product" },
91
- ]);
92
- expect(mockRefreshCart).toHaveBeenCalledWith({ id: "cart-1" });
93
- expect(isAddingToCart.value).toBe(false);
94
- });
95
-
96
- it("should handle empty products in addAllItemsToCart", async () => {
97
- const { addAllItemsToCart } = useWishlistActions();
98
- await addAllItemsToCart([]);
99
- expect(mockAddProducts).not.toHaveBeenCalled();
100
- });
101
-
102
- it("should skip base products in addAllItemsToCart", async () => {
103
- const products = [
104
- { id: "p1", translated: { name: "P1" } },
105
- { id: "p2", childCount: 1, translated: { name: "P2" } },
106
- ] as unknown as Schemas["Product"][];
107
- const { addAllItemsToCart } = useWishlistActions();
108
- mockAddProducts.mockResolvedValue({ id: "cart-1" });
109
-
110
- await addAllItemsToCart(products);
111
-
112
- expect(mockAddProducts).toHaveBeenCalledWith([
113
- { id: "p1", quantity: 1, type: "product" },
114
- ]);
115
- expect(mockTriggerProductAdded).toHaveBeenCalled();
116
- });
117
- });