@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
package/nuxt.config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineNuxtConfig } from "nuxt/config";
|
|
2
|
+
import type { NuxtConfig } from "nuxt/config";
|
|
3
|
+
|
|
4
|
+
export default defineNuxtConfig({
|
|
5
|
+
imports: {
|
|
6
|
+
dirs: ["src"],
|
|
7
|
+
},
|
|
8
|
+
build: {
|
|
9
|
+
transpile: ["@shopware/composables"],
|
|
10
|
+
},
|
|
11
|
+
telemetry: {
|
|
12
|
+
enabled: false,
|
|
13
|
+
},
|
|
14
|
+
}) as NuxtConfig;
|
package/package.json
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shopware/composables",
|
|
3
|
+
"version": "0.0.0-canary-20250116155626",
|
|
4
|
+
"description": "Shopware Frontends composables for Vue",
|
|
5
|
+
"author": "Shopware",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/shopware/frontends.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://frontends.shopware.com/packages/composables.html",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"shopware",
|
|
13
|
+
"vue",
|
|
14
|
+
"composables"
|
|
15
|
+
],
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/shopware/frontends/issues"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"types": "./src/index.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./src/index.ts",
|
|
25
|
+
"import": "./src/index.ts"
|
|
26
|
+
},
|
|
27
|
+
"./nuxt-layer": {
|
|
28
|
+
"import": "./nuxt.config.ts"
|
|
29
|
+
},
|
|
30
|
+
"./composables": {
|
|
31
|
+
"import": "./composables/index.ts"
|
|
32
|
+
},
|
|
33
|
+
"./lib": {
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
|
+
"import": "./dist/index.mjs"
|
|
36
|
+
},
|
|
37
|
+
"./dist": {
|
|
38
|
+
"types": "./dist/index.d.mts",
|
|
39
|
+
"import": "./dist/index.mjs"
|
|
40
|
+
},
|
|
41
|
+
"./types/api-types": {
|
|
42
|
+
"import": "./types/api-types.ts"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"dist",
|
|
47
|
+
"metadata",
|
|
48
|
+
"src",
|
|
49
|
+
"composables",
|
|
50
|
+
"nuxt.config.ts",
|
|
51
|
+
"types"
|
|
52
|
+
],
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@vueuse/core": "11.2.0",
|
|
55
|
+
"defu": "6.1.4",
|
|
56
|
+
"scule": "1.3.0",
|
|
57
|
+
"@shopware-pwa/helpers-next": "1.2.0",
|
|
58
|
+
"@shopware/api-client": "1.2.0"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@biomejs/biome": "1.8.3",
|
|
62
|
+
"@nuxt/kit": "3.14.1592",
|
|
63
|
+
"@vitest/coverage-v8": "2.1.8",
|
|
64
|
+
"@vue/test-utils": "2.4.6",
|
|
65
|
+
"happy-dom": "16.6.0",
|
|
66
|
+
"typescript": "5.6.3",
|
|
67
|
+
"unbuild": "2.0.0",
|
|
68
|
+
"vitest": "2.1.8",
|
|
69
|
+
"vue": "3.5.13",
|
|
70
|
+
"tsconfig": "0.0.0"
|
|
71
|
+
},
|
|
72
|
+
"scripts": {
|
|
73
|
+
"build": "unbuild && pnpm build:types",
|
|
74
|
+
"build:types": "npx -p typescript tsc",
|
|
75
|
+
"dev": "unbuild --stub",
|
|
76
|
+
"lint": "biome check .",
|
|
77
|
+
"lint:fix": "biome check . --write && pnpm run typecheck",
|
|
78
|
+
"typecheck": "tsc --noEmit",
|
|
79
|
+
"test": "vitest run",
|
|
80
|
+
"coverage": "vitest run --coverage",
|
|
81
|
+
"test:watch": "vitest"
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useSetup } from "./useSetup";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { shallowMount } from "@vue/test-utils";
|
|
2
|
+
import { defu } from "defu";
|
|
3
|
+
import { vi } from "vitest";
|
|
4
|
+
import { defineComponent, h } from "vue";
|
|
5
|
+
|
|
6
|
+
type Injections = {
|
|
7
|
+
shopware: unknown;
|
|
8
|
+
apiClient: {
|
|
9
|
+
invoke: ReturnType<typeof vi.fn>;
|
|
10
|
+
};
|
|
11
|
+
swNotifications?: unknown;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function useSetup<V>(setup: () => V, customMocks?: Partial<Injections>) {
|
|
15
|
+
const defaultInjections: Injections = {
|
|
16
|
+
shopware: {},
|
|
17
|
+
apiClient: { invoke: customMocks?.apiClient?.invoke ?? vi.fn() },
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const component = defineComponent({
|
|
21
|
+
setup,
|
|
22
|
+
render() {
|
|
23
|
+
return h("div", []);
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const injections: Injections = defu(
|
|
28
|
+
defaultInjections,
|
|
29
|
+
customMocks || {},
|
|
30
|
+
) as Injections;
|
|
31
|
+
|
|
32
|
+
const wrapper = shallowMount(component, {
|
|
33
|
+
global: {
|
|
34
|
+
provide: injections,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
injections,
|
|
40
|
+
wrapper,
|
|
41
|
+
vm: wrapper.vm,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export const cmsAssociations = {
|
|
2
|
+
associations: {
|
|
3
|
+
media: {},
|
|
4
|
+
cmsPage: {
|
|
5
|
+
associations: {
|
|
6
|
+
sections: {
|
|
7
|
+
associations: {
|
|
8
|
+
blocks: {
|
|
9
|
+
associations: {
|
|
10
|
+
slots: {
|
|
11
|
+
associations: {
|
|
12
|
+
block: {
|
|
13
|
+
associations: {
|
|
14
|
+
slots: {
|
|
15
|
+
associations: {},
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
};
|
package/src/cms/index.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import type {
|
|
3
|
+
CmsElementBuyBox,
|
|
4
|
+
CmsElementForm,
|
|
5
|
+
CmsElementImage,
|
|
6
|
+
} from "../types";
|
|
7
|
+
import { useCmsElementConfig } from "./useCmsElementConfig";
|
|
8
|
+
|
|
9
|
+
describe("useCmsElementConfig", () => {
|
|
10
|
+
describe("methods", () => {
|
|
11
|
+
describe("getConfigValue", () => {
|
|
12
|
+
it("should return config value", () => {
|
|
13
|
+
const cmsElement: CmsElementBuyBox = {
|
|
14
|
+
type: "buy-box",
|
|
15
|
+
slot: "buyBox",
|
|
16
|
+
config: {
|
|
17
|
+
alignment: {
|
|
18
|
+
source: "static",
|
|
19
|
+
value: "flex-start",
|
|
20
|
+
},
|
|
21
|
+
product: {
|
|
22
|
+
source: "static",
|
|
23
|
+
value: "123",
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
customFields: [],
|
|
27
|
+
data: {
|
|
28
|
+
apiAlias: "cms_product_description_reviews",
|
|
29
|
+
productId: "123",
|
|
30
|
+
configuratorSettings: null,
|
|
31
|
+
ratingSuccess: false,
|
|
32
|
+
reviews: {
|
|
33
|
+
entity: "product_review",
|
|
34
|
+
total: 0,
|
|
35
|
+
aggregations: [],
|
|
36
|
+
page: 1,
|
|
37
|
+
limit: 10,
|
|
38
|
+
elements: [],
|
|
39
|
+
apiAlias: "product_review",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
_uniqueIdentifier: "cms_product_description_reviews",
|
|
43
|
+
locked: false,
|
|
44
|
+
versionId: "1",
|
|
45
|
+
createdAt: new Date(),
|
|
46
|
+
updatedAt: new Date(),
|
|
47
|
+
extensions: [{}],
|
|
48
|
+
id: "123",
|
|
49
|
+
translated: {},
|
|
50
|
+
translations: {},
|
|
51
|
+
block: null,
|
|
52
|
+
blockId: "123",
|
|
53
|
+
fieldConfig: [],
|
|
54
|
+
apiAlias: "cms_slot",
|
|
55
|
+
} as unknown as CmsElementBuyBox;
|
|
56
|
+
|
|
57
|
+
const { getConfigValue } = useCmsElementConfig(cmsElement);
|
|
58
|
+
|
|
59
|
+
expect(getConfigValue("alignment")).toEqual("flex-start");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should return undefined if config value is undefined", () => {
|
|
63
|
+
const { getConfigValue } = useCmsElementConfig({
|
|
64
|
+
config: {
|
|
65
|
+
alignment: {
|
|
66
|
+
value: undefined,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
} as unknown as CmsElementBuyBox);
|
|
70
|
+
|
|
71
|
+
expect(getConfigValue("alignment")).toBeUndefined();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should return undefined if config value is null", () => {
|
|
75
|
+
const { getConfigValue } = useCmsElementConfig({
|
|
76
|
+
config: {
|
|
77
|
+
boxLayout: {
|
|
78
|
+
value: null,
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
} as unknown as CmsElementImage);
|
|
82
|
+
|
|
83
|
+
expect(getConfigValue("boxLayout")).toEqual(null);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should return undefined if config value is null", () => {
|
|
87
|
+
const cmsElement = {
|
|
88
|
+
type: "form",
|
|
89
|
+
slot: "qwe",
|
|
90
|
+
config: {
|
|
91
|
+
confirmationText: {
|
|
92
|
+
source: "static",
|
|
93
|
+
value: "qwe",
|
|
94
|
+
},
|
|
95
|
+
defaultMailReceiver: {
|
|
96
|
+
source: "static",
|
|
97
|
+
value: false,
|
|
98
|
+
},
|
|
99
|
+
mailReceiver: {
|
|
100
|
+
source: "static",
|
|
101
|
+
value: ["first", "second"],
|
|
102
|
+
},
|
|
103
|
+
title: {
|
|
104
|
+
source: "static",
|
|
105
|
+
value: "Cool mail",
|
|
106
|
+
},
|
|
107
|
+
type: {
|
|
108
|
+
source: "static",
|
|
109
|
+
value: "contact",
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
customFields: [],
|
|
113
|
+
data: [],
|
|
114
|
+
_uniqueIdentifier: "qwe",
|
|
115
|
+
locked: false,
|
|
116
|
+
versionId: "1",
|
|
117
|
+
createdAt: new Date(),
|
|
118
|
+
updatedAt: new Date(),
|
|
119
|
+
extensions: [{}],
|
|
120
|
+
id: "123",
|
|
121
|
+
translated: {
|
|
122
|
+
config: {
|
|
123
|
+
content: {
|
|
124
|
+
source: "static",
|
|
125
|
+
value: "123",
|
|
126
|
+
},
|
|
127
|
+
verticalAlign: {
|
|
128
|
+
source: "static",
|
|
129
|
+
value: "flex-start",
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
translations: {},
|
|
134
|
+
block: null,
|
|
135
|
+
blockId: "123",
|
|
136
|
+
fieldConfig: [],
|
|
137
|
+
apiAlias: "cms_slot",
|
|
138
|
+
} as unknown as CmsElementForm;
|
|
139
|
+
const { getConfigValue } = useCmsElementConfig(cmsElement);
|
|
140
|
+
expect(getConfigValue("defaultMailReceiver")).toEqual(false);
|
|
141
|
+
expect(getConfigValue("mailReceiver")).toEqual(["first", "second"]);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Schemas } from "#shopware";
|
|
2
|
+
import type { ElementConfig } from "../types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Composable to get cms element config
|
|
6
|
+
*
|
|
7
|
+
* @category CMS (Shopping Experiences)
|
|
8
|
+
*/
|
|
9
|
+
export function useCmsElementConfig<
|
|
10
|
+
T extends Schemas["CmsSlot"] & {
|
|
11
|
+
config: T["config"] extends {
|
|
12
|
+
[key in infer X extends keyof T["config"]]: ElementConfig<unknown>;
|
|
13
|
+
}
|
|
14
|
+
? { [key in X]: ElementConfig<T["config"][key]["value"]> }
|
|
15
|
+
: never;
|
|
16
|
+
},
|
|
17
|
+
>(element: T) {
|
|
18
|
+
const getConfigValue = <ELEMENT_CONFIG extends keyof T["config"]>(
|
|
19
|
+
key: ELEMENT_CONFIG,
|
|
20
|
+
): (typeof element.config)[ELEMENT_CONFIG]["value"] => {
|
|
21
|
+
return (
|
|
22
|
+
element.config[key]?.source !== "mapped" && element.config[key]?.value
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
getConfigValue,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import type { CmsElementImage, CmsElementManufacturerLogo } from "../types";
|
|
3
|
+
import { useCmsElementImage } from "./useCmsElementImage";
|
|
4
|
+
|
|
5
|
+
describe("useCmsElementImage", () => {
|
|
6
|
+
describe("computed", () => {
|
|
7
|
+
describe("containerStyle", () => {
|
|
8
|
+
it("should return minHeight css property", () => {
|
|
9
|
+
const { containerStyle } = useCmsElementImage({
|
|
10
|
+
config: {
|
|
11
|
+
minHeight: {
|
|
12
|
+
value: "100px",
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
} as CmsElementImage);
|
|
16
|
+
|
|
17
|
+
expect(containerStyle.value).toEqual({ minHeight: "100px" });
|
|
18
|
+
});
|
|
19
|
+
it("should return anchorAttrs", () => {
|
|
20
|
+
const { anchorAttrs } = useCmsElementImage({
|
|
21
|
+
config: {
|
|
22
|
+
url: {
|
|
23
|
+
value: "https://shopware.com",
|
|
24
|
+
},
|
|
25
|
+
newTab: {
|
|
26
|
+
value: true,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
} as CmsElementImage);
|
|
30
|
+
|
|
31
|
+
expect(anchorAttrs.value).toEqual({
|
|
32
|
+
href: "https://shopware.com",
|
|
33
|
+
target: "_blank",
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should return default anchorAttrs", () => {
|
|
38
|
+
const { anchorAttrs } = useCmsElementImage({
|
|
39
|
+
config: {
|
|
40
|
+
url: {
|
|
41
|
+
value: "https://shopware.com",
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
} as CmsElementImage);
|
|
45
|
+
|
|
46
|
+
expect(anchorAttrs.value).toEqual({
|
|
47
|
+
href: "https://shopware.com",
|
|
48
|
+
target: "_self",
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("should return imageContainerAttrs - absolute link", () => {
|
|
53
|
+
const { imageContainerAttrs } = useCmsElementImage({
|
|
54
|
+
data: {
|
|
55
|
+
newTab: true,
|
|
56
|
+
url: "https://shopware.com/logo.png",
|
|
57
|
+
},
|
|
58
|
+
config: {
|
|
59
|
+
url: {
|
|
60
|
+
value: "https://shopware.com",
|
|
61
|
+
},
|
|
62
|
+
newTab: {
|
|
63
|
+
value: true,
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
} as CmsElementImage);
|
|
67
|
+
|
|
68
|
+
expect(imageContainerAttrs.value).toEqual({
|
|
69
|
+
href: "https://shopware.com/logo.png",
|
|
70
|
+
target: "blank",
|
|
71
|
+
rel: "noopener noreferrer",
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("should return imageContainerAttrs", () => {
|
|
76
|
+
const { imageContainerAttrs } = useCmsElementImage({
|
|
77
|
+
data: {
|
|
78
|
+
newTab: true,
|
|
79
|
+
url: "/logo.png",
|
|
80
|
+
},
|
|
81
|
+
config: {
|
|
82
|
+
url: {
|
|
83
|
+
value: "https://shopware.com",
|
|
84
|
+
},
|
|
85
|
+
newTab: {
|
|
86
|
+
value: true,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
} as CmsElementImage);
|
|
90
|
+
|
|
91
|
+
expect(imageContainerAttrs.value).toEqual({
|
|
92
|
+
href: "/logo.png",
|
|
93
|
+
target: "blank",
|
|
94
|
+
rel: "noopener noreferrer",
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("should return imageLink", () => {
|
|
99
|
+
const { imageLink } = useCmsElementImage({
|
|
100
|
+
data: {
|
|
101
|
+
newTab: true,
|
|
102
|
+
url: "https://shopware.com/logo.png",
|
|
103
|
+
},
|
|
104
|
+
} as CmsElementImage);
|
|
105
|
+
|
|
106
|
+
expect(imageLink.value).toEqual({
|
|
107
|
+
newTab: true,
|
|
108
|
+
url: "https://shopware.com/logo.png",
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
it("should return imageAttrs", () => {
|
|
112
|
+
const { imageAttrs } = useCmsElementImage({
|
|
113
|
+
data: {
|
|
114
|
+
media: {
|
|
115
|
+
url: "https://shopware.com/logo.png",
|
|
116
|
+
fileName: "logo.png",
|
|
117
|
+
thumbnails: [
|
|
118
|
+
{
|
|
119
|
+
url: "https://shopware.com/logo-128px.png",
|
|
120
|
+
width: "128",
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
} as unknown as CmsElementManufacturerLogo);
|
|
126
|
+
|
|
127
|
+
expect(imageAttrs.value).toEqual({
|
|
128
|
+
alt: "",
|
|
129
|
+
src: "https://shopware.com/logo.png",
|
|
130
|
+
srcset: "https://shopware.com/logo-128px.png 128w",
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("should return displayMode", () => {
|
|
135
|
+
const { displayMode } = useCmsElementImage({
|
|
136
|
+
config: {
|
|
137
|
+
displayMode: {
|
|
138
|
+
value: "contain",
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
} as CmsElementImage);
|
|
142
|
+
|
|
143
|
+
expect(displayMode.value).toEqual("contain");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("should return default displayMode", () => {
|
|
147
|
+
const { displayMode } = useCmsElementImage({
|
|
148
|
+
config: {},
|
|
149
|
+
} as CmsElementImage);
|
|
150
|
+
|
|
151
|
+
expect(displayMode.value).toEqual("initial");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("should return isVideoElement", () => {
|
|
155
|
+
const { isVideoElement } = useCmsElementImage({
|
|
156
|
+
data: {
|
|
157
|
+
media: {
|
|
158
|
+
mimeType: "video",
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
} as CmsElementImage);
|
|
162
|
+
|
|
163
|
+
expect(isVideoElement.value).toBeTruthy();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("should return mimeType", () => {
|
|
167
|
+
const { mimeType } = useCmsElementImage({
|
|
168
|
+
data: {
|
|
169
|
+
media: {
|
|
170
|
+
mimeType: "image/png",
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
} as CmsElementImage);
|
|
174
|
+
|
|
175
|
+
expect(mimeType.value).toEqual("image/png");
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSrcSetForMedia,
|
|
3
|
+
relativeUrlSlash,
|
|
4
|
+
urlIsAbsolute,
|
|
5
|
+
} from "@shopware-pwa/helpers-next";
|
|
6
|
+
import { computed } from "vue";
|
|
7
|
+
import type {
|
|
8
|
+
AnchorHTMLAttributes,
|
|
9
|
+
CSSProperties,
|
|
10
|
+
ComputedRef,
|
|
11
|
+
ImgHTMLAttributes,
|
|
12
|
+
} from "vue";
|
|
13
|
+
import { useCmsElementConfig } from "#imports";
|
|
14
|
+
import type {
|
|
15
|
+
CmsElementImage,
|
|
16
|
+
CmsElementManufacturerLogo,
|
|
17
|
+
DisplayMode,
|
|
18
|
+
} from "../types";
|
|
19
|
+
|
|
20
|
+
export type ImageContainerAttrs = {
|
|
21
|
+
href?: string;
|
|
22
|
+
target?: string;
|
|
23
|
+
rel?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type UseCmsElementImage = {
|
|
27
|
+
containerStyle: ComputedRef<CSSProperties>;
|
|
28
|
+
anchorAttrs: ComputedRef<AnchorHTMLAttributes>;
|
|
29
|
+
imageAttrs: ComputedRef<ImgHTMLAttributes>;
|
|
30
|
+
imageContainerAttrs: ComputedRef<ImageContainerAttrs>;
|
|
31
|
+
imageLink: ComputedRef<{ newTab: boolean; url: string }>;
|
|
32
|
+
displayMode: ComputedRef<DisplayMode>;
|
|
33
|
+
isVideoElement: ComputedRef<boolean>;
|
|
34
|
+
mimeType: ComputedRef<string | undefined>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Composable to get cms element image
|
|
39
|
+
*
|
|
40
|
+
* @category CMS (Shopping Experiences)
|
|
41
|
+
*/
|
|
42
|
+
export function useCmsElementImage(
|
|
43
|
+
element: CmsElementImage | CmsElementManufacturerLogo,
|
|
44
|
+
): UseCmsElementImage {
|
|
45
|
+
const { getConfigValue } = useCmsElementConfig(element);
|
|
46
|
+
|
|
47
|
+
const containerStyle: ComputedRef<CSSProperties> = computed(() => ({
|
|
48
|
+
minHeight: getConfigValue("minHeight"),
|
|
49
|
+
}));
|
|
50
|
+
|
|
51
|
+
const anchorAttrs = computed(() => ({
|
|
52
|
+
href: getConfigValue("url"),
|
|
53
|
+
target: getConfigValue("newTab") ? "_blank" : "_self",
|
|
54
|
+
}));
|
|
55
|
+
|
|
56
|
+
const imageLink = computed(() => ({
|
|
57
|
+
newTab: element.data?.newTab,
|
|
58
|
+
url: element.data?.url,
|
|
59
|
+
}));
|
|
60
|
+
|
|
61
|
+
const imageContainerAttrs = computed(() => {
|
|
62
|
+
const attr: ImageContainerAttrs = {};
|
|
63
|
+
if (imageLink.value.url) {
|
|
64
|
+
attr.href = urlIsAbsolute(imageLink.value.url)
|
|
65
|
+
? imageLink.value.url
|
|
66
|
+
: relativeUrlSlash(imageLink.value.url);
|
|
67
|
+
}
|
|
68
|
+
if (imageLink.value.newTab) {
|
|
69
|
+
attr.target = "blank";
|
|
70
|
+
attr.rel = "noopener noreferrer";
|
|
71
|
+
}
|
|
72
|
+
return attr;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const imageAttrs: ComputedRef<ImgHTMLAttributes> = computed(() => ({
|
|
76
|
+
src: element.data?.media?.url,
|
|
77
|
+
alt: element.data?.media?.alt || "",
|
|
78
|
+
srcset: getSrcSetForMedia(element.data?.media),
|
|
79
|
+
}));
|
|
80
|
+
|
|
81
|
+
const displayMode = computed(
|
|
82
|
+
() => getConfigValue("displayMode") || "initial",
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const isVideoElement = computed(() => {
|
|
86
|
+
return !!element.data?.media?.mimeType?.includes("video");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const mimeType = computed(() => {
|
|
90
|
+
return element.data?.media?.mimeType;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
containerStyle,
|
|
95
|
+
anchorAttrs,
|
|
96
|
+
imageAttrs,
|
|
97
|
+
imageContainerAttrs,
|
|
98
|
+
imageLink,
|
|
99
|
+
displayMode,
|
|
100
|
+
isVideoElement,
|
|
101
|
+
mimeType,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { useSetup } from "../_test";
|
|
3
|
+
import CmsProductBox from "../mocks/CmsProductBox";
|
|
4
|
+
import type { CmsElementProductBox } from "../types/cmsElementTypes";
|
|
5
|
+
import { useCmsElementProductBox } from "./useCmsElementProductBox";
|
|
6
|
+
|
|
7
|
+
describe("useCmsElementProductBox", () => {
|
|
8
|
+
it("should init product box", () => {
|
|
9
|
+
const { vm } = useSetup(() =>
|
|
10
|
+
useCmsElementProductBox(CmsProductBox as unknown as CmsElementProductBox),
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
expect(vm.product).toEqual(CmsProductBox.data.product);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { inject } from "vue";
|
|
2
|
+
import { computed } from "vue";
|
|
3
|
+
import type { ComputedRef } from "vue";
|
|
4
|
+
import type { Schemas } from "#shopware";
|
|
5
|
+
import { useCmsElementConfig } from "../index";
|
|
6
|
+
import type { BoxLayout, CmsElementProductBox } from "../types";
|
|
7
|
+
|
|
8
|
+
export type UseCmsElementProductBox = {
|
|
9
|
+
product: Schemas["Product"];
|
|
10
|
+
boxLayout: ComputedRef<BoxLayout>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function useCmsElementProductBox(
|
|
14
|
+
element: CmsElementProductBox,
|
|
15
|
+
): UseCmsElementProductBox {
|
|
16
|
+
const product = inject("product", element.data.product);
|
|
17
|
+
|
|
18
|
+
const { getConfigValue } = useCmsElementConfig(element);
|
|
19
|
+
const boxLayout = computed(() => getConfigValue("boxLayout"));
|
|
20
|
+
|
|
21
|
+
return {
|
|
22
|
+
product,
|
|
23
|
+
boxLayout,
|
|
24
|
+
};
|
|
25
|
+
}
|