@shopware/composables 0.0.0-canary-20250116155626
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/LICENSE +21 -0
- package/README.md +172 -0
- package/composables/index.ts +1 -0
- package/dist/index.d.mts +2239 -0
- package/dist/index.d.ts +2239 -0
- package/dist/index.mjs +2686 -0
- package/nuxt.config.ts +14 -0
- package/package.json +83 -0
- package/src/_test/index.ts +1 -0
- package/src/_test/useSetup.ts +43 -0
- package/src/cms/README.md +9 -0
- package/src/cms/cmsAssociations.ts +28 -0
- package/src/cms/index.ts +2 -0
- package/src/cms/useCmsElementConfig.test.ts +145 -0
- package/src/cms/useCmsElementConfig.ts +29 -0
- package/src/cms/useCmsElementImage.test.ts +179 -0
- package/src/cms/useCmsElementImage.ts +103 -0
- package/src/cms/useCmsElementProductBox.test.ts +15 -0
- package/src/cms/useCmsElementProductBox.ts +25 -0
- package/src/createShopwareContext.test.ts +23 -0
- package/src/createShopwareContext.ts +43 -0
- package/src/devtools/plugin.ts +201 -0
- package/src/helpers/ContextError.spec.ts +20 -0
- package/src/helpers/ContextError.ts +6 -0
- package/src/index.test.ts +54 -0
- package/src/index.ts +111 -0
- package/src/mocks/Cart.ts +774 -0
- package/src/mocks/CmsPage.ts +544 -0
- package/src/mocks/CmsProductBox.ts +377 -0
- package/src/mocks/Configurator.ts +123 -0
- package/src/mocks/Country.ts +184 -0
- package/src/mocks/CrossSellingResponse.ts +552 -0
- package/src/mocks/LandingPage.ts +62 -0
- package/src/mocks/Menu.ts +598 -0
- package/src/mocks/Order.ts +1151 -0
- package/src/mocks/Product.ts +473 -0
- package/src/mocks/ProductTierPrices.ts +771 -0
- package/src/mocks/Salutations.ts +51 -0
- package/src/mocks/Search.ts +518 -0
- package/src/types/cmsBlockTypes.ts +80 -0
- package/src/types/cmsElementTypes.ts +381 -0
- package/src/types/cmsSectionTypes.ts +15 -0
- package/src/types/index.ts +52 -0
- package/src/useAddToCart/useAddToCart.test.ts +39 -0
- package/src/useAddToCart/useAddToCart.ts +90 -0
- package/src/useAddress/useAddress.test.ts +134 -0
- package/src/useAddress/useAddress.ts +202 -0
- package/src/useB2bQuoteManagement/useB2bQuoteManagement.test.ts +137 -0
- package/src/useB2bQuoteManagement/useB2bQuoteManagement.ts +225 -0
- package/src/useBreadcrumbs/useBreadcrumbs.test.ts +71 -0
- package/src/useBreadcrumbs/useBreadcrumbs.ts +89 -0
- package/src/useCart/useCart.test.ts +251 -0
- package/src/useCart/useCart.ts +314 -0
- package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.test.ts +122 -0
- package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.ts +80 -0
- package/src/useCartItem/useCartItem.test.ts +168 -0
- package/src/useCartItem/useCartItem.ts +157 -0
- package/src/useCartNotification/useCartNotification.test.ts +129 -0
- package/src/useCartNotification/useCartNotification.ts +63 -0
- package/src/useCategory/useCategory.test.ts +14 -0
- package/src/useCategory/useCategory.ts +32 -0
- package/src/useCategorySearch/useCategorySearch.test.ts +98 -0
- package/src/useCategorySearch/useCategorySearch.ts +81 -0
- package/src/useCheckout/useCheckout.test.ts +104 -0
- package/src/useCheckout/useCheckout.ts +163 -0
- package/src/useCmsBlock/useCmsBlock.test.ts +25 -0
- package/src/useCmsBlock/useCmsBlock.ts +36 -0
- package/src/useCmsMeta/useCmsMeta.test.ts +17 -0
- package/src/useCmsMeta/useCmsMeta.ts +46 -0
- package/src/useCmsSection/useCmsSection.test.ts +18 -0
- package/src/useCmsSection/useCmsSection.ts +39 -0
- package/src/useCmsTranslations/useCmsTranslations.test.ts +10 -0
- package/src/useCmsTranslations/useCmsTranslations.ts +9 -0
- package/src/useContext/useContext.ts +36 -0
- package/src/useCountries/useCountries.test.ts +64 -0
- package/src/useCountries/useCountries.ts +64 -0
- package/src/useCustomerOrders/useCustomerOrders.test.ts +47 -0
- package/src/useCustomerOrders/useCustomerOrders.ts +89 -0
- package/src/useCustomerPassword/useCustomerPassword.test.ts +164 -0
- package/src/useCustomerPassword/useCustomerPassword.ts +57 -0
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +41 -0
- package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.ts +45 -0
- package/src/useInternationalization/useInternationalization.test.ts +155 -0
- package/src/useInternationalization/useInternationalization.ts +159 -0
- package/src/useLandingSearch/useLandingSearch.test.ts +30 -0
- package/src/useLandingSearch/useLandingSearch.ts +58 -0
- package/src/useListing/useListing.test.ts +249 -0
- package/src/useListing/useListing.ts +592 -0
- package/src/useLocalWishlist/useLocalWishlist.test.ts +54 -0
- package/src/useLocalWishlist/useLocalWishlist.ts +95 -0
- package/src/useNavigation/useNavigation.test.ts +68 -0
- package/src/useNavigation/useNavigation.ts +83 -0
- package/src/useNavigationContext/useNavigationContext.test.ts +48 -0
- package/src/useNavigationContext/useNavigationContext.ts +39 -0
- package/src/useNavigationSearch/useNavigationSearch.test.ts +110 -0
- package/src/useNavigationSearch/useNavigationSearch.ts +60 -0
- package/src/useNewsletter/useNewsletter.test.ts +55 -0
- package/src/useNewsletter/useNewsletter.ts +109 -0
- package/src/useNotifications/useNotifications.test.ts +56 -0
- package/src/useNotifications/useNotifications.ts +119 -0
- package/src/useOrderDetails/useOrderDetails.test.ts +224 -0
- package/src/useOrderDetails/useOrderDetails.ts +329 -0
- package/src/useOrderPayment/useOrderPayment.test.ts +80 -0
- package/src/useOrderPayment/useOrderPayment.ts +119 -0
- package/src/usePrice/usePrice.test.ts +97 -0
- package/src/usePrice/usePrice.ts +106 -0
- package/src/useProduct/useProduct.test.ts +36 -0
- package/src/useProduct/useProduct.ts +56 -0
- package/src/useProductAssociations/useProductAssociations.spec.ts +105 -0
- package/src/useProductAssociations/useProductAssociations.ts +107 -0
- package/src/useProductConfigurator/useProductConfigurator.spec.ts +77 -0
- package/src/useProductConfigurator/useProductConfigurator.ts +134 -0
- package/src/useProductPrice/useProductPrice.test.ts +126 -0
- package/src/useProductPrice/useProductPrice.ts +131 -0
- package/src/useProductReviews/useProductReviews.test.ts +45 -0
- package/src/useProductReviews/useProductReviews.ts +80 -0
- package/src/useProductSearch/useProductSearch.test.ts +37 -0
- package/src/useProductSearch/useProductSearch.ts +58 -0
- package/src/useProductSearchSuggest/useProductSearchSuggest.test.ts +19 -0
- package/src/useProductSearchSuggest/useProductSearchSuggest.ts +67 -0
- package/src/useProductWishlist/useProductWishlist.test.ts +63 -0
- package/src/useProductWishlist/useProductWishlist.ts +74 -0
- package/src/useSalutations/useSalutations.spec.ts +37 -0
- package/src/useSalutations/useSalutations.ts +55 -0
- package/src/useSessionContext/useSessionContext.test.ts +221 -0
- package/src/useSessionContext/useSessionContext.ts +281 -0
- package/src/useShopwareContext/useShopwareContext.test.ts +8 -0
- package/src/useShopwareContext/useShopwareContext.ts +31 -0
- package/src/useSyncWishlist/useSyncWishlist.test.ts +117 -0
- package/src/useSyncWishlist/useSyncWishlist.ts +117 -0
- package/src/useUrlResolver/useUrlResolver.test.ts +25 -0
- package/src/useUrlResolver/useUrlResolver.ts +31 -0
- package/src/useUser/useUser.test.ts +290 -0
- package/src/useUser/useUser.ts +321 -0
- package/src/useWishlist/useWishlist.test.ts +178 -0
- package/src/useWishlist/useWishlist.ts +128 -0
- package/types/api-types.ts +11 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { ref } from "vue";
|
|
3
|
+
import { useCart, useCartErrorParamsResolver } from "#imports";
|
|
4
|
+
|
|
5
|
+
vi.mock("../useCart/useCart.ts");
|
|
6
|
+
|
|
7
|
+
describe("useCartErrorParamsResolver", () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
vi.resetAllMocks();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("should return resolved product-stock-reached error", () => {
|
|
13
|
+
vi.mocked(useCart).mockReturnValue({
|
|
14
|
+
cartItems: ref([
|
|
15
|
+
{
|
|
16
|
+
id: "c29ad46d64474ef0b72845832f0d879b",
|
|
17
|
+
label: "Greta Glass drink dispenser 5 l with tap and wooden stand",
|
|
18
|
+
quantityInformation: {
|
|
19
|
+
maxPurchase: 5,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
]),
|
|
23
|
+
} as unknown as ReturnType<typeof useCart>);
|
|
24
|
+
const { resolveCartError } = useCartErrorParamsResolver();
|
|
25
|
+
const error = resolveCartError({
|
|
26
|
+
message:
|
|
27
|
+
"The product Greta Glass drink dispenser 5 l with tap and wooden stand is only available 5 times",
|
|
28
|
+
code: 0,
|
|
29
|
+
key: "product-stock-reachedc29ad46d64474ef0b72845832f0d879b",
|
|
30
|
+
level: 10,
|
|
31
|
+
messageKey: "product-stock-reached",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(error).toEqual({
|
|
35
|
+
params: {
|
|
36
|
+
name: "Greta Glass drink dispenser 5 l with tap and wooden stand",
|
|
37
|
+
quantity: 5,
|
|
38
|
+
},
|
|
39
|
+
messageKey: "product-stock-reached",
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should return resolved product-stock-reached-empty error if there is no product data", () => {
|
|
44
|
+
vi.mocked(useCart).mockReturnValue({
|
|
45
|
+
cartItems: ref([]),
|
|
46
|
+
} as unknown as ReturnType<typeof useCart>);
|
|
47
|
+
const { resolveCartError } = useCartErrorParamsResolver();
|
|
48
|
+
const error = resolveCartError({
|
|
49
|
+
message:
|
|
50
|
+
"The product Greta Glass drink dispenser 5 l with tap and wooden stand is only available 5 times",
|
|
51
|
+
code: 0,
|
|
52
|
+
key: "product-stock-reachedc29ad46d64474ef0b72845832f0d879b",
|
|
53
|
+
level: 10,
|
|
54
|
+
messageKey: "product-stock-reached",
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
expect(error).toEqual({
|
|
58
|
+
params: null,
|
|
59
|
+
messageKey: "product-stock-reached-empty",
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should return passed params if error is not supported", () => {
|
|
64
|
+
vi.mocked(useCart).mockReturnValue({
|
|
65
|
+
cartItems: ref([]),
|
|
66
|
+
} as unknown as ReturnType<typeof useCart>);
|
|
67
|
+
const { resolveCartError } = useCartErrorParamsResolver();
|
|
68
|
+
const error = resolveCartError({
|
|
69
|
+
key: "custom-errorc29ad46d64474ef0b72845832f0d879b",
|
|
70
|
+
messageKey: "custom-error",
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
expect(error).toEqual({
|
|
74
|
+
params: {
|
|
75
|
+
key: "custom-errorc29ad46d64474ef0b72845832f0d879b",
|
|
76
|
+
messageKey: "custom-error",
|
|
77
|
+
},
|
|
78
|
+
messageKey: "custom-error",
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("should return resolved shipping-method-blocked error", () => {
|
|
83
|
+
vi.mocked(useCart).mockReturnValue({
|
|
84
|
+
cartItems: ref([]),
|
|
85
|
+
} as unknown as ReturnType<typeof useCart>);
|
|
86
|
+
const { resolveCartError } = useCartErrorParamsResolver();
|
|
87
|
+
const error = resolveCartError({
|
|
88
|
+
message: "shipping-method-blocked-UPS",
|
|
89
|
+
code: 0,
|
|
90
|
+
key: "shipping-method-blocked-UPS",
|
|
91
|
+
level: 10,
|
|
92
|
+
messageKey: "shipping-method-blocked",
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
expect(error).toEqual({
|
|
96
|
+
params: {
|
|
97
|
+
name: "UPS",
|
|
98
|
+
},
|
|
99
|
+
messageKey: "shipping-method-blocked",
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should return resolved shipping-method-blocked error - no message", () => {
|
|
104
|
+
vi.mocked(useCart).mockReturnValue({
|
|
105
|
+
cartItems: ref([]),
|
|
106
|
+
} as unknown as ReturnType<typeof useCart>);
|
|
107
|
+
const { resolveCartError } = useCartErrorParamsResolver();
|
|
108
|
+
const error = resolveCartError({
|
|
109
|
+
code: 0,
|
|
110
|
+
key: "shipping-method-blocked-UPS",
|
|
111
|
+
level: 10,
|
|
112
|
+
messageKey: "shipping-method-blocked",
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
expect(error).toEqual({
|
|
116
|
+
params: {
|
|
117
|
+
name: "",
|
|
118
|
+
},
|
|
119
|
+
messageKey: "shipping-method-blocked",
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { useCart } from "#imports";
|
|
2
|
+
|
|
3
|
+
export type UseCartErrorParamsResolver = {
|
|
4
|
+
resolveCartError: (errorObject: ErrorObjectParam) => {
|
|
5
|
+
params: { [key: string]: string | number | null | undefined } | null;
|
|
6
|
+
messageKey: string;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type ErrorObjectParam = {
|
|
11
|
+
messageKey: string;
|
|
12
|
+
key: string;
|
|
13
|
+
message?: string;
|
|
14
|
+
code?: number;
|
|
15
|
+
level?: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @category Cart & Checkout
|
|
20
|
+
*
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
export function useCartErrorParamsResolver(): UseCartErrorParamsResolver {
|
|
24
|
+
const resolveCartError = (errorObject: ErrorObjectParam) => {
|
|
25
|
+
let params = null;
|
|
26
|
+
let messageKey = errorObject.messageKey;
|
|
27
|
+
|
|
28
|
+
const { cartItems } = useCart();
|
|
29
|
+
|
|
30
|
+
const extractProductId = (message: string, error: string) => {
|
|
31
|
+
return message.replace(error, "");
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const buildProductStockReached = () => {
|
|
35
|
+
const productId = extractProductId(
|
|
36
|
+
errorObject.key,
|
|
37
|
+
"product-stock-reached",
|
|
38
|
+
);
|
|
39
|
+
const product = getItem(productId);
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
name: product?.label || "",
|
|
43
|
+
quantity: product?.quantityInformation?.maxPurchase || null,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const getItem = (id: string) => {
|
|
48
|
+
return cartItems.value.find((item) => item.id === id);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const buildShippingMethodBlocked = (): string => {
|
|
52
|
+
return errorObject.message?.replace("shipping-method-blocked-", "") || "";
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
switch (errorObject.messageKey) {
|
|
56
|
+
case "product-stock-reached":
|
|
57
|
+
params = buildProductStockReached();
|
|
58
|
+
if (!params.name || !params.quantity) {
|
|
59
|
+
messageKey = "product-stock-reached-empty";
|
|
60
|
+
params = null;
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
case "shipping-method-blocked":
|
|
64
|
+
params = {
|
|
65
|
+
name: buildShippingMethodBlocked(),
|
|
66
|
+
};
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
params = { ...errorObject };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
params,
|
|
74
|
+
messageKey,
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
resolveCartError,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { ref } from "vue";
|
|
3
|
+
import type { Ref } from "vue";
|
|
4
|
+
import type { Schemas } from "#shopware";
|
|
5
|
+
import { useSetup } from "../_test";
|
|
6
|
+
import Cart from "../mocks/Cart";
|
|
7
|
+
import { useCartItem } from "./useCartItem";
|
|
8
|
+
|
|
9
|
+
const lineItem = Cart.lineItems[0];
|
|
10
|
+
|
|
11
|
+
const removeItemSpy = vi.fn();
|
|
12
|
+
const changeProductQuantitySpy = vi.fn();
|
|
13
|
+
vi.mock("../useCart/useCart.ts", async () => {
|
|
14
|
+
return {
|
|
15
|
+
useCart: () => {
|
|
16
|
+
return {
|
|
17
|
+
removeItem: removeItemSpy,
|
|
18
|
+
changeProductQuantity: changeProductQuantitySpy,
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe("useCartItem", () => {
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
vi.clearAllMocks();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("init cart item", () => {
|
|
30
|
+
const { vm } = useSetup(() =>
|
|
31
|
+
useCartItem(ref(lineItem) as unknown as Ref<Schemas["LineItem"]>),
|
|
32
|
+
);
|
|
33
|
+
expect(vm.isProduct).toBe(true);
|
|
34
|
+
expect(vm.isPromotion).toBe(false);
|
|
35
|
+
expect(vm.isRemovable).toBe(true);
|
|
36
|
+
expect(vm.isStackable).toBe(true);
|
|
37
|
+
expect(vm.isDigital).toBe(false);
|
|
38
|
+
expect(vm.itemImageThumbnailUrl).toBe(lineItem.cover?.url);
|
|
39
|
+
expect(vm.itemType).toBe("product");
|
|
40
|
+
expect(vm.itemQuantity).toBe(1);
|
|
41
|
+
expect(vm.itemStock).toBe(49485);
|
|
42
|
+
expect(vm.itemTotalPrice).toBe(17.9);
|
|
43
|
+
expect(vm.itemSpecialPrice).toBe(undefined);
|
|
44
|
+
expect(vm.itemRegularPrice).toBe(17.9);
|
|
45
|
+
expect(vm.itemOptions).toStrictEqual([]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should show item options", async () => {
|
|
49
|
+
const payload = {
|
|
50
|
+
options: [
|
|
51
|
+
{
|
|
52
|
+
group: "test",
|
|
53
|
+
option: "test",
|
|
54
|
+
translated: {
|
|
55
|
+
group: "test",
|
|
56
|
+
option: "test",
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
} as Schemas["ProductJsonApi"];
|
|
61
|
+
|
|
62
|
+
const { vm } = useSetup(() =>
|
|
63
|
+
useCartItem(
|
|
64
|
+
ref({
|
|
65
|
+
type: "product",
|
|
66
|
+
payload,
|
|
67
|
+
} as Schemas["LineItem"]),
|
|
68
|
+
),
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
expect(vm.itemOptions).toStrictEqual(payload.options);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should show default empty options", async () => {
|
|
75
|
+
const payload = {} as Schemas["ProductJsonApi"];
|
|
76
|
+
|
|
77
|
+
const { vm } = useSetup(() =>
|
|
78
|
+
useCartItem(
|
|
79
|
+
ref({
|
|
80
|
+
type: "product",
|
|
81
|
+
payload,
|
|
82
|
+
} as Schemas["LineItem"]),
|
|
83
|
+
),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
expect(vm.itemOptions).toStrictEqual([]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should remove item from the cart", async () => {
|
|
90
|
+
const { vm } = useSetup(() =>
|
|
91
|
+
useCartItem(ref(lineItem) as unknown as Ref<Schemas["LineItem"]>),
|
|
92
|
+
);
|
|
93
|
+
const mockedResponse = {
|
|
94
|
+
test: 123,
|
|
95
|
+
};
|
|
96
|
+
removeItemSpy.mockResolvedValueOnce(mockedResponse);
|
|
97
|
+
const result = await vm.removeItem();
|
|
98
|
+
|
|
99
|
+
expect(removeItemSpy).toHaveBeenCalledWith(lineItem);
|
|
100
|
+
expect(result).toEqual(mockedResponse);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should change item quantity", async () => {
|
|
104
|
+
const { vm } = useSetup(() =>
|
|
105
|
+
useCartItem(ref(lineItem) as unknown as Ref<Schemas["LineItem"]>),
|
|
106
|
+
);
|
|
107
|
+
const mockedResponse = {
|
|
108
|
+
test: 123,
|
|
109
|
+
};
|
|
110
|
+
changeProductQuantitySpy.mockResolvedValueOnce(mockedResponse);
|
|
111
|
+
const result = await vm.changeItemQuantity(5);
|
|
112
|
+
|
|
113
|
+
expect(changeProductQuantitySpy).toHaveBeenCalledWith({
|
|
114
|
+
quantity: 5,
|
|
115
|
+
id: lineItem.id,
|
|
116
|
+
});
|
|
117
|
+
expect(result).toEqual(mockedResponse);
|
|
118
|
+
|
|
119
|
+
// @ts-expect-error it should not be allowed by typescript, but still should work properly
|
|
120
|
+
vm.changeItemQuantity("6");
|
|
121
|
+
expect(changeProductQuantitySpy).toHaveBeenCalledWith({
|
|
122
|
+
quantity: 6,
|
|
123
|
+
id: lineItem.id,
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("itemSpecialPrice", () => {
|
|
128
|
+
const { vm } = useSetup(() =>
|
|
129
|
+
useCartItem(
|
|
130
|
+
ref(
|
|
131
|
+
Object.assign(
|
|
132
|
+
{ ...lineItem },
|
|
133
|
+
{
|
|
134
|
+
price: {
|
|
135
|
+
listPrice: {
|
|
136
|
+
price: 10,
|
|
137
|
+
},
|
|
138
|
+
unitPrice: 5,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
),
|
|
142
|
+
) as unknown as Ref<Schemas["LineItem"]>,
|
|
143
|
+
),
|
|
144
|
+
);
|
|
145
|
+
expect(vm.itemSpecialPrice).toBe(5);
|
|
146
|
+
});
|
|
147
|
+
it("itemRegularPrice from listPrice", () => {
|
|
148
|
+
const { vm } = useSetup(() =>
|
|
149
|
+
useCartItem(
|
|
150
|
+
ref({
|
|
151
|
+
price: {
|
|
152
|
+
listPrice: {
|
|
153
|
+
price: 10,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
}) as unknown as Ref<Schemas["LineItem"]>,
|
|
157
|
+
),
|
|
158
|
+
);
|
|
159
|
+
expect(vm.itemRegularPrice).toBe(10);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("should throw an error if cartItem is not provided", () => {
|
|
163
|
+
expect(() =>
|
|
164
|
+
// @ts-expect-error we deliberately pass null to invoke error
|
|
165
|
+
useCartItem(null),
|
|
166
|
+
).toThrow("[useCartItem] mandatory cartItem argument is missing.");
|
|
167
|
+
});
|
|
168
|
+
});
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { getMainImageUrl } from "@shopware-pwa/helpers-next";
|
|
2
|
+
import { computed } from "vue";
|
|
3
|
+
import type { ComputedRef, Ref } from "vue";
|
|
4
|
+
import { useCart } from "#imports";
|
|
5
|
+
import type { Schemas } from "#shopware";
|
|
6
|
+
|
|
7
|
+
export type UseCartItemReturn = {
|
|
8
|
+
/**
|
|
9
|
+
* Calculated price {number} for the current item
|
|
10
|
+
*/
|
|
11
|
+
itemRegularPrice: ComputedRef<number | undefined>;
|
|
12
|
+
/**
|
|
13
|
+
* Calculated price {number} for the current item if list price is set
|
|
14
|
+
*/
|
|
15
|
+
itemSpecialPrice: ComputedRef<number | undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* Total price for the current item of given quantity in the cart
|
|
18
|
+
*/
|
|
19
|
+
itemTotalPrice: ComputedRef<number | undefined>;
|
|
20
|
+
/**
|
|
21
|
+
* Thumbnail url for the current item's entity
|
|
22
|
+
*/
|
|
23
|
+
itemImageThumbnailUrl: ComputedRef<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Options (of variation) for the current item
|
|
26
|
+
*/
|
|
27
|
+
itemOptions: ComputedRef<Schemas["LineItem"]["payload"]["options"]>;
|
|
28
|
+
/**
|
|
29
|
+
* Type of the current item: "product" or "promotion"
|
|
30
|
+
*/
|
|
31
|
+
itemType: ComputedRef<Schemas["LineItem"]["type"] | undefined>;
|
|
32
|
+
/**
|
|
33
|
+
* Determines if the current item is a product
|
|
34
|
+
*/
|
|
35
|
+
isProduct: ComputedRef<boolean>;
|
|
36
|
+
/**
|
|
37
|
+
* Determines if the current item is a promotion
|
|
38
|
+
*/
|
|
39
|
+
isPromotion: ComputedRef<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* Determines if the current item can be removed from cart
|
|
42
|
+
*/
|
|
43
|
+
isRemovable: ComputedRef<boolean>;
|
|
44
|
+
/**
|
|
45
|
+
* Determines if the current item's quantity can be changed
|
|
46
|
+
*/
|
|
47
|
+
isStackable: ComputedRef<boolean>;
|
|
48
|
+
/**
|
|
49
|
+
* Determines if the current item is a digital product (to download)
|
|
50
|
+
*/
|
|
51
|
+
isDigital: ComputedRef<boolean>;
|
|
52
|
+
/**
|
|
53
|
+
* Stock information for the current item
|
|
54
|
+
*/
|
|
55
|
+
itemStock: ComputedRef<number | undefined>;
|
|
56
|
+
/**
|
|
57
|
+
* Quantity of the current item in the cart
|
|
58
|
+
*/
|
|
59
|
+
itemQuantity: ComputedRef<number | undefined>;
|
|
60
|
+
/**
|
|
61
|
+
* Changes the current item quantity in the cart
|
|
62
|
+
*/
|
|
63
|
+
changeItemQuantity(quantity: number): Promise<Schemas["Cart"]>;
|
|
64
|
+
/**
|
|
65
|
+
* Removes the current item from the cart
|
|
66
|
+
*/
|
|
67
|
+
removeItem(): Promise<Schemas["Cart"]>;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Composable to manage specific cart item
|
|
72
|
+
* @public
|
|
73
|
+
* @category Cart & Checkout
|
|
74
|
+
*/
|
|
75
|
+
export function useCartItem(
|
|
76
|
+
cartItem: Ref<Schemas["LineItem"]>,
|
|
77
|
+
): UseCartItemReturn {
|
|
78
|
+
if (!cartItem) {
|
|
79
|
+
throw new Error("[useCartItem] mandatory cartItem argument is missing.");
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const { changeProductQuantity, removeItem: cartRemoveItem } = useCart();
|
|
83
|
+
|
|
84
|
+
const itemQuantity = computed(() => cartItem.value.quantity);
|
|
85
|
+
const itemImageThumbnailUrl = computed(() => getMainImageUrl(cartItem.value));
|
|
86
|
+
|
|
87
|
+
const itemRegularPrice = computed<number | undefined>(
|
|
88
|
+
() =>
|
|
89
|
+
cartItem.value?.price?.listPrice?.price ||
|
|
90
|
+
cartItem.value?.price?.unitPrice,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const itemSpecialPrice = computed(
|
|
94
|
+
() =>
|
|
95
|
+
cartItem.value?.price?.listPrice?.price &&
|
|
96
|
+
cartItem.value?.price?.unitPrice,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const itemTotalPrice = computed(() => cartItem.value.price?.totalPrice);
|
|
100
|
+
|
|
101
|
+
const itemOptions = computed(
|
|
102
|
+
() =>
|
|
103
|
+
(cartItem.value.type === "product" && cartItem.value.payload?.options) ||
|
|
104
|
+
[],
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const itemStock = computed<number | undefined>(
|
|
108
|
+
() => cartItem.value.deliveryInformation.stock,
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const isDigital = computed(
|
|
112
|
+
() => !!cartItem.value.states?.includes("is-download"),
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const isStackable = computed(() => !!cartItem.value.stackable);
|
|
116
|
+
|
|
117
|
+
const itemType = computed(() => cartItem.value.type);
|
|
118
|
+
|
|
119
|
+
const isProduct = computed(() => cartItem.value.type === "product");
|
|
120
|
+
|
|
121
|
+
const isPromotion = computed(() => cartItem.value.type === "promotion");
|
|
122
|
+
|
|
123
|
+
const isRemovable = computed(() => !!cartItem.value.removable);
|
|
124
|
+
|
|
125
|
+
async function removeItem() {
|
|
126
|
+
return await cartRemoveItem(cartItem.value);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function changeItemQuantity(
|
|
130
|
+
quantity: number,
|
|
131
|
+
): Promise<Schemas["Cart"]> {
|
|
132
|
+
const result = changeProductQuantity({
|
|
133
|
+
id: cartItem.value.id,
|
|
134
|
+
quantity: +quantity,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
changeItemQuantity,
|
|
142
|
+
removeItem,
|
|
143
|
+
itemRegularPrice,
|
|
144
|
+
itemSpecialPrice,
|
|
145
|
+
itemTotalPrice,
|
|
146
|
+
itemOptions,
|
|
147
|
+
itemStock,
|
|
148
|
+
itemQuantity,
|
|
149
|
+
itemType,
|
|
150
|
+
itemImageThumbnailUrl,
|
|
151
|
+
isProduct,
|
|
152
|
+
isPromotion,
|
|
153
|
+
isRemovable,
|
|
154
|
+
isStackable,
|
|
155
|
+
isDigital,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { useCart, useCartNotification } from "#imports";
|
|
3
|
+
import { useSetup } from "../_test";
|
|
4
|
+
|
|
5
|
+
const pushErrorSpy = vi.fn();
|
|
6
|
+
const pushSuccessSpy = vi.fn();
|
|
7
|
+
|
|
8
|
+
vi.mock("../useNotifications/useNotifications.ts", async () => {
|
|
9
|
+
return {
|
|
10
|
+
useNotifications: () => {
|
|
11
|
+
return {
|
|
12
|
+
pushError: pushErrorSpy,
|
|
13
|
+
pushSuccess: pushSuccessSpy,
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
vi.mock("../useCart/useCart.ts");
|
|
20
|
+
|
|
21
|
+
describe("useCartNotification", () => {
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
vi.resetAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should display success notification", () => {
|
|
27
|
+
vi.mocked(useCart).mockReturnValue({
|
|
28
|
+
consumeCartErrors: vi.fn(() => {
|
|
29
|
+
return JSON.parse(
|
|
30
|
+
JSON.stringify({
|
|
31
|
+
"promotion-discount-added-1326f60448d84aed964d77ca1c2dde90": {
|
|
32
|
+
message: "Discount Your lucky day! has been added",
|
|
33
|
+
code: 0,
|
|
34
|
+
key: "promotion-discount-added-1326f60448d84aed964d77ca1c2dde90",
|
|
35
|
+
level: 0,
|
|
36
|
+
messageKey: "promotion-discount-added",
|
|
37
|
+
},
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
}),
|
|
41
|
+
} as unknown as ReturnType<typeof useCart>);
|
|
42
|
+
|
|
43
|
+
const { vm } = useSetup(() => useCartNotification());
|
|
44
|
+
vm.codeErrorsNotification();
|
|
45
|
+
expect(pushSuccessSpy).toBeCalledWith(
|
|
46
|
+
"Discount Your lucky day! has been added",
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should return empty array if errors are not present", () => {
|
|
51
|
+
vi.mocked(useCart).mockReturnValue({
|
|
52
|
+
consumeCartErrors: vi.fn(() => {
|
|
53
|
+
return null;
|
|
54
|
+
}),
|
|
55
|
+
} as unknown as ReturnType<typeof useCart>);
|
|
56
|
+
|
|
57
|
+
const { vm } = useSetup(() => useCartNotification());
|
|
58
|
+
vm.codeErrorsNotification();
|
|
59
|
+
expect(pushSuccessSpy).not.toBeCalledWith();
|
|
60
|
+
expect(pushErrorSpy).not.toBeCalledWith();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should display error notification", () => {
|
|
64
|
+
vi.mocked(useCart).mockReturnValue({
|
|
65
|
+
consumeCartErrors: vi.fn(() => {
|
|
66
|
+
return JSON.parse(
|
|
67
|
+
JSON.stringify({
|
|
68
|
+
"product-stock-reachedc29ad46d64474ef0b72845832f0d879b": {
|
|
69
|
+
message:
|
|
70
|
+
"The product Greta Glass drink dispenser 5 l with tap and wooden stand is only available 5 times",
|
|
71
|
+
code: 0,
|
|
72
|
+
key: "product-stock-reachedc29ad46d64474ef0b72845832f0d879b",
|
|
73
|
+
level: 10,
|
|
74
|
+
messageKey: "product-stock-reached",
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
}),
|
|
79
|
+
} as unknown as ReturnType<typeof useCart>);
|
|
80
|
+
|
|
81
|
+
const { vm } = useSetup(() => useCartNotification());
|
|
82
|
+
vm.codeErrorsNotification();
|
|
83
|
+
expect(pushErrorSpy).toBeCalledWith(
|
|
84
|
+
"The product Greta Glass drink dispenser 5 l with tap and wooden stand is only available 5 times",
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should return errors codes", () => {
|
|
89
|
+
vi.mocked(useCart).mockReturnValue({
|
|
90
|
+
consumeCartErrors: vi.fn(() => {
|
|
91
|
+
return JSON.parse(
|
|
92
|
+
JSON.stringify({
|
|
93
|
+
"product-stock-reachedc29ad46d64474ef0b72845832f0d879b": {
|
|
94
|
+
message:
|
|
95
|
+
"The product Greta Glass drink dispenser 5 l with tap and wooden stand is only available 5 times",
|
|
96
|
+
code: 0,
|
|
97
|
+
key: "product-stock-reachedc29ad46d64474ef0b72845832f0d879b",
|
|
98
|
+
level: 10,
|
|
99
|
+
messageKey: "product-stock-reached",
|
|
100
|
+
},
|
|
101
|
+
}),
|
|
102
|
+
);
|
|
103
|
+
}),
|
|
104
|
+
} as unknown as ReturnType<typeof useCart>);
|
|
105
|
+
const { vm } = useSetup(() => useCartNotification());
|
|
106
|
+
|
|
107
|
+
expect(vm.getErrorsCodes()).toEqual([
|
|
108
|
+
{
|
|
109
|
+
code: 0,
|
|
110
|
+
key: "product-stock-reachedc29ad46d64474ef0b72845832f0d879b",
|
|
111
|
+
level: 10,
|
|
112
|
+
message:
|
|
113
|
+
"The product Greta Glass drink dispenser 5 l with tap and wooden stand is only available 5 times",
|
|
114
|
+
messageKey: "product-stock-reached",
|
|
115
|
+
},
|
|
116
|
+
]);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("should return empty array if errors are not present", () => {
|
|
120
|
+
vi.mocked(useCart).mockReturnValue({
|
|
121
|
+
consumeCartErrors: vi.fn(() => {
|
|
122
|
+
return null;
|
|
123
|
+
}),
|
|
124
|
+
} as unknown as ReturnType<typeof useCart>);
|
|
125
|
+
|
|
126
|
+
const { vm } = useSetup(() => useCartNotification());
|
|
127
|
+
expect(vm.getErrorsCodes()).toEqual([]);
|
|
128
|
+
});
|
|
129
|
+
});
|