@shopbite-de/storefront 2.0.2 → 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.
- package/CLAUDE.md +4 -3
- package/api-types/storeApiSchema.json +227 -0
- package/api-types/storeApiTypes.d.ts +56 -2
- package/app/app.vue +10 -12
- package/app/components/Checkout/LoginOrRegister.vue +2 -8
- package/app/components/Checkout/PaymentAndDelivery.vue +3 -3
- package/app/components/Checkout/Summary.vue +53 -5
- package/app/components/Header.vue +1 -1
- package/app/components/Product/Detail.vue +22 -0
- package/app/components/Product/QuickView.vue +6 -0
- package/app/components/Wishlist.vue +264 -58
- package/app/components/WishlistSaveButton.vue +57 -0
- package/app/composables/useCheckoutMethodGuard.ts +19 -30
- package/app/composables/useOrderMode.ts +6 -8
- package/app/composables/useShippingMethodChoice.ts +37 -0
- package/app/composables/useTrackEvent.ts +3 -1
- package/app/composables/useWishlistEntries.ts +291 -0
- package/app/pages/anmelden.vue +1 -4
- package/app/pages/registrierung/bestaetigen.vue +0 -2
- package/package.json +1 -1
- package/test/nuxt/CartCheckoutPreset.test.ts +8 -5
- package/test/nuxt/CheckoutSummary.test.ts +39 -3
- package/test/nuxt/PaymentAndDelivery.test.ts +8 -5
- package/test/nuxt/RegisterConfirmPage.test.ts +1 -6
- package/test/nuxt/WishlistPage.test.ts +107 -0
- package/test/nuxt/WishlistSaveButton.test.ts +67 -0
- package/test/nuxt/useCheckoutMethodGuard.test.ts +12 -80
- package/test/nuxt/useShippingMethodChoice.test.ts +46 -0
- package/test/nuxt/useWishlistEntries.test.ts +207 -0
- package/app/components/AddToWishlist.vue +0 -40
- package/app/composables/useWishlistActions.ts +0 -96
- package/test/nuxt/useWishlistActions.test.ts +0 -117
|
@@ -154,66 +154,21 @@ describe("useCheckoutMethodGuard", () => {
|
|
|
154
154
|
expect(isShippingMethodBlocked.value).toBe(false);
|
|
155
155
|
});
|
|
156
156
|
|
|
157
|
-
it("switches
|
|
157
|
+
it("never switches a blocked shipping method, it only reports it", async () => {
|
|
158
|
+
// La Fattoria: "Lieferung" is blocked until the guest has entered an
|
|
159
|
+
// address in the delivery area; switching made every order a pickup
|
|
158
160
|
cart.value = cartWith({ [shippingBlocked.key]: shippingBlocked });
|
|
159
|
-
mockRefreshCart.mockImplementationOnce(async () => {});
|
|
160
|
-
nextRefreshSets({});
|
|
161
|
-
const extra = {
|
|
162
|
-
id: "sm-extra",
|
|
163
|
-
name: "Express",
|
|
164
|
-
position: 0,
|
|
165
|
-
} as Schemas["ShippingMethod"];
|
|
166
|
-
mockGetShippingMethods.mockResolvedValue(ref([extra, delivery, pickup]));
|
|
167
|
-
|
|
168
|
-
const { ensureAvailableShippingMethod } = useCheckoutMethodGuard();
|
|
169
|
-
|
|
170
|
-
await expect(ensureAvailableShippingMethod()).resolves.toBe(true);
|
|
171
|
-
|
|
172
|
-
expect(mockGetShippingMethods).toHaveBeenCalledWith({
|
|
173
|
-
forceReload: true,
|
|
174
|
-
});
|
|
175
|
-
expect(mockSetShippingMethod).toHaveBeenCalledWith({ id: pickup.id });
|
|
176
|
-
expect(mockRefreshCart).toHaveBeenCalledTimes(2);
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
it("falls back to the first available method when the default is not available", async () => {
|
|
180
|
-
cart.value = cartWith({ [shippingBlocked.key]: shippingBlocked });
|
|
181
|
-
sessionContext.value = {
|
|
182
|
-
salesChannel: { shippingMethodId: delivery.id },
|
|
183
|
-
};
|
|
184
|
-
mockRefreshCart.mockImplementationOnce(async () => {});
|
|
185
|
-
nextRefreshSets({});
|
|
186
|
-
// The blocked method is still returned by the API in some setups
|
|
187
|
-
mockGetShippingMethods.mockResolvedValue(ref([delivery, pickup]));
|
|
188
|
-
|
|
189
|
-
const { ensureAvailableShippingMethod } = useCheckoutMethodGuard();
|
|
190
|
-
|
|
191
|
-
await expect(ensureAvailableShippingMethod()).resolves.toBe(true);
|
|
192
|
-
expect(mockSetShippingMethod).toHaveBeenCalledWith({ id: pickup.id });
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it("returns false when no alternative shipping method exists", async () => {
|
|
196
|
-
cart.value = cartWith({ [shippingBlocked.key]: shippingBlocked });
|
|
197
|
-
mockGetShippingMethods.mockResolvedValue(ref([delivery]));
|
|
198
161
|
|
|
199
162
|
const { ensureAvailableShippingMethod, isShippingMethodBlocked } =
|
|
200
163
|
useCheckoutMethodGuard();
|
|
201
164
|
|
|
202
165
|
await expect(ensureAvailableShippingMethod()).resolves.toBe(false);
|
|
203
166
|
|
|
167
|
+
expect(mockRefreshCart).toHaveBeenCalledTimes(1);
|
|
168
|
+
expect(mockGetShippingMethods).not.toHaveBeenCalled();
|
|
204
169
|
expect(mockSetShippingMethod).not.toHaveBeenCalled();
|
|
205
170
|
expect(isShippingMethodBlocked.value).toBe(true);
|
|
206
171
|
});
|
|
207
|
-
|
|
208
|
-
it("returns false when the cart is still blocked after switching", async () => {
|
|
209
|
-
cart.value = cartWith({ [shippingBlocked.key]: shippingBlocked });
|
|
210
|
-
|
|
211
|
-
const { ensureAvailableShippingMethod } = useCheckoutMethodGuard();
|
|
212
|
-
|
|
213
|
-
await expect(ensureAvailableShippingMethod()).resolves.toBe(false);
|
|
214
|
-
|
|
215
|
-
expect(mockSetShippingMethod).toHaveBeenCalledWith({ id: pickup.id });
|
|
216
|
-
});
|
|
217
172
|
});
|
|
218
173
|
|
|
219
174
|
describe("ensureAvailablePaymentMethod", () => {
|
|
@@ -260,40 +215,17 @@ describe("useCheckoutMethodGuard", () => {
|
|
|
260
215
|
expect(mockSetPaymentMethod).not.toHaveBeenCalled();
|
|
261
216
|
});
|
|
262
217
|
|
|
263
|
-
it("
|
|
264
|
-
cart.value = cartWith({
|
|
265
|
-
[shippingBlocked.key]: shippingBlocked,
|
|
266
|
-
[paymentBlocked.key]: paymentBlocked,
|
|
267
|
-
});
|
|
268
|
-
mockRefreshCart.mockImplementationOnce(async () => {});
|
|
269
|
-
// after shipping switch: only payment still blocked
|
|
270
|
-
nextRefreshSets({ [paymentBlocked.key]: paymentBlocked });
|
|
271
|
-
// after payment switch: clean
|
|
272
|
-
nextRefreshSets({});
|
|
273
|
-
|
|
274
|
-
const { ensureAvailableCheckoutMethods } = useCheckoutMethodGuard();
|
|
275
|
-
|
|
276
|
-
await expect(ensureAvailableCheckoutMethods()).resolves.toBe(true);
|
|
277
|
-
|
|
278
|
-
expect(mockSetShippingMethod).toHaveBeenCalledWith({ id: pickup.id });
|
|
279
|
-
expect(mockSetPaymentMethod).toHaveBeenCalledWith({ id: paypal.id });
|
|
280
|
-
expect(mockSetShippingMethod.mock.invocationCallOrder[0]).toBeLessThan(
|
|
281
|
-
mockSetPaymentMethod.mock.invocationCallOrder[0] as number,
|
|
282
|
-
);
|
|
283
|
-
expect(mockRefreshCart).toHaveBeenCalledTimes(3);
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
it("leaves the payment method untouched when shipping cannot be resolved", async () => {
|
|
218
|
+
it("keeps a blocked shipping method and leaves payment untouched", async () => {
|
|
287
219
|
cart.value = cartWith({
|
|
288
220
|
[shippingBlocked.key]: shippingBlocked,
|
|
289
221
|
[paymentBlocked.key]: paymentBlocked,
|
|
290
222
|
});
|
|
291
|
-
mockGetShippingMethods.mockResolvedValue(ref([delivery]));
|
|
292
223
|
|
|
293
224
|
const { ensureAvailableCheckoutMethods } = useCheckoutMethodGuard();
|
|
294
225
|
|
|
295
226
|
await expect(ensureAvailableCheckoutMethods()).resolves.toBe(false);
|
|
296
227
|
|
|
228
|
+
expect(mockSetShippingMethod).not.toHaveBeenCalled();
|
|
297
229
|
expect(mockGetPaymentMethods).not.toHaveBeenCalled();
|
|
298
230
|
expect(mockSetPaymentMethod).not.toHaveBeenCalled();
|
|
299
231
|
});
|
|
@@ -310,18 +242,18 @@ describe("useCheckoutMethodGuard", () => {
|
|
|
310
242
|
});
|
|
311
243
|
|
|
312
244
|
it("serializes concurrent calls so the switch happens only once", async () => {
|
|
313
|
-
cart.value = cartWith({ [
|
|
245
|
+
cart.value = cartWith({ [paymentBlocked.key]: paymentBlocked });
|
|
314
246
|
let releaseFirstRefresh: () => void = () => {};
|
|
315
247
|
mockRefreshCart.mockImplementationOnce(
|
|
316
248
|
() => new Promise<void>((r) => (releaseFirstRefresh = r)),
|
|
317
249
|
);
|
|
318
250
|
nextRefreshSets({});
|
|
319
251
|
|
|
320
|
-
const { ensureAvailableCheckoutMethods,
|
|
252
|
+
const { ensureAvailableCheckoutMethods, ensureAvailablePaymentMethod } =
|
|
321
253
|
useCheckoutMethodGuard();
|
|
322
254
|
|
|
323
255
|
const first = ensureAvailableCheckoutMethods();
|
|
324
|
-
const second =
|
|
256
|
+
const second = ensureAvailablePaymentMethod();
|
|
325
257
|
await Promise.resolve();
|
|
326
258
|
expect(mockRefreshCart).toHaveBeenCalledTimes(1);
|
|
327
259
|
|
|
@@ -329,11 +261,11 @@ describe("useCheckoutMethodGuard", () => {
|
|
|
329
261
|
await expect(first).resolves.toBe(true);
|
|
330
262
|
await expect(second).resolves.toBe(true);
|
|
331
263
|
|
|
332
|
-
expect(
|
|
264
|
+
expect(mockSetPaymentMethod).toHaveBeenCalledTimes(1);
|
|
333
265
|
// second call re-checked after the first one finished
|
|
334
266
|
expect(mockRefreshCart).toHaveBeenCalledTimes(3);
|
|
335
267
|
expect(mockRefreshCart.mock.invocationCallOrder[2]).toBeGreaterThan(
|
|
336
|
-
|
|
268
|
+
mockSetPaymentMethod.mock.invocationCallOrder[0] as number,
|
|
337
269
|
);
|
|
338
270
|
});
|
|
339
271
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
import { mockNuxtImport } from "@nuxt/test-utils/runtime";
|
|
3
|
+
|
|
4
|
+
const { mockInvoke } = vi.hoisted(() => ({ mockInvoke: vi.fn() }));
|
|
5
|
+
|
|
6
|
+
mockNuxtImport("useShopwareContext", () => () => ({
|
|
7
|
+
apiClient: { invoke: mockInvoke },
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
describe("useShippingMethodChoice", () => {
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
vi.clearAllMocks();
|
|
13
|
+
mockInvoke.mockResolvedValue({
|
|
14
|
+
data: {
|
|
15
|
+
elements: [
|
|
16
|
+
{ id: "pickup", name: "Abholung", position: 2 },
|
|
17
|
+
{ id: "delivery", name: "Lieferung", position: 1 },
|
|
18
|
+
],
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("loads all active methods, also those whose rule needs an address", async () => {
|
|
24
|
+
// "Lieferung" at La Fattoria only matches with a shipping city in the
|
|
25
|
+
// delivery area; onlyAvailable hid it from guests without an address
|
|
26
|
+
const { methods, load } = useShippingMethodChoice();
|
|
27
|
+
await load({ forceReload: true });
|
|
28
|
+
|
|
29
|
+
expect(mockInvoke).toHaveBeenCalledWith(
|
|
30
|
+
"readShippingMethod post /shipping-method",
|
|
31
|
+
expect.objectContaining({ query: { onlyAvailable: false } }),
|
|
32
|
+
);
|
|
33
|
+
expect(methods.value.map((method) => method.name)).toEqual([
|
|
34
|
+
"Lieferung",
|
|
35
|
+
"Abholung",
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("does not reload once loaded", async () => {
|
|
40
|
+
const { load } = useShippingMethodChoice();
|
|
41
|
+
await load({ forceReload: true });
|
|
42
|
+
await load();
|
|
43
|
+
|
|
44
|
+
expect(mockInvoke).toHaveBeenCalledTimes(1);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -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
|
-
});
|