@shopware/composables 1.11.1 → 1.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/README.md +18 -13
  2. package/dist/index.d.mts +35 -5
  3. package/dist/index.d.ts +35 -5
  4. package/dist/index.mjs +167 -100
  5. package/nuxt.config.ts +10 -0
  6. package/package.json +14 -15
  7. package/src/cms/useCmsElementConfig.test.ts +1 -0
  8. package/src/cms/useCmsElementConfig.ts +1 -0
  9. package/src/cms/useCmsElementImage.test.ts +1 -0
  10. package/src/cms/useCmsElementImage.ts +2 -0
  11. package/src/cms/useCmsElementProductBox.test.ts +1 -0
  12. package/src/cms/useCmsElementProductBox.ts +2 -0
  13. package/src/createShopwareContext.test.ts +1 -0
  14. package/src/createShopwareContext.ts +8 -0
  15. package/src/devtools/plugin.ts +13 -13
  16. package/src/helpers/ContextError.spec.ts +1 -0
  17. package/src/index.test.ts +2 -0
  18. package/src/index.ts +1 -0
  19. package/src/types/cmsElementTypes.ts +1 -0
  20. package/src/useAddToCart/useAddToCart.test.ts +1 -0
  21. package/src/useAddToCart/useAddToCart.ts +1 -0
  22. package/src/useAddress/useAddress.test.ts +2 -0
  23. package/src/useAddress/useAddress.ts +1 -0
  24. package/src/useB2bQuoteManagement/useB2bQuoteManagement.test.ts +1 -0
  25. package/src/useBreadcrumbs/useBreadcrumbs.test.ts +2 -0
  26. package/src/useBreadcrumbs/useBreadcrumbs.ts +2 -1
  27. package/src/useCart/useCart.test.ts +19 -0
  28. package/src/useCart/useCart.ts +6 -3
  29. package/src/useCartErrorParamsResolver/useCartErrorParamsResolver.test.ts +1 -0
  30. package/src/useCartItem/useCartItem.test.ts +15 -0
  31. package/src/useCartItem/useCartItem.ts +15 -7
  32. package/src/useCartNotification/useCartNotification.test.ts +2 -0
  33. package/src/useCategory/useCategory.test.ts +2 -0
  34. package/src/useCategory/useCategory.ts +2 -0
  35. package/src/useCategorySearch/useCategorySearch.test.ts +104 -2
  36. package/src/useCategorySearch/useCategorySearch.ts +45 -23
  37. package/src/useCheckout/useCheckout.test.ts +1 -0
  38. package/src/useCheckout/useCheckout.ts +1 -0
  39. package/src/useCmsBlock/useCmsBlock.test.ts +2 -0
  40. package/src/useCmsMeta/useCmsMeta.test.ts +2 -0
  41. package/src/useCmsMeta/useCmsMeta.ts +1 -0
  42. package/src/useCmsSection/useCmsSection.test.ts +2 -0
  43. package/src/useCmsTranslations/useCmsTranslations.test.ts +2 -0
  44. package/src/useCountries/useCountries.test.ts +96 -6
  45. package/src/useCountries/useCountries.ts +19 -8
  46. package/src/useCustomerOrders/useCustomerOrders.test.ts +1 -0
  47. package/src/useCustomerOrders/useCustomerOrders.ts +1 -0
  48. package/src/useCustomerPassword/useCustomerPassword.test.ts +1 -0
  49. package/src/useDefaultOrderAssociations/useDefaultOrderAssociations.test.ts +1 -0
  50. package/src/useInternationalization/useInternationalization.test.ts +14 -1
  51. package/src/useInternationalization/useInternationalization.ts +5 -3
  52. package/src/useLandingSearch/useLandingSearch.test.ts +1 -0
  53. package/src/useLandingSearch/useLandingSearch.ts +1 -0
  54. package/src/useListing/useListing.test.ts +2 -0
  55. package/src/useListing/useListing.ts +3 -2
  56. package/src/useLocalWishlist/useLocalWishlist.test.ts +1 -0
  57. package/src/useNavigation/useNavigation.test.ts +24 -1
  58. package/src/useNavigation/useNavigation.ts +36 -14
  59. package/src/useNavigationContext/useNavigationContext.test.ts +1 -0
  60. package/src/useNavigationContext/useNavigationContext.ts +1 -0
  61. package/src/useNavigationSearch/useNavigationSearch.test.ts +30 -0
  62. package/src/useNavigationSearch/useNavigationSearch.ts +19 -12
  63. package/src/useNewsletter/useNewsletter.test.ts +1 -0
  64. package/src/useNewsletter/useNewsletter.ts +1 -0
  65. package/src/useNotifications/useNotifications.test.ts +1 -0
  66. package/src/useOrderDetails/useOrderDetails.test.ts +1 -0
  67. package/src/useOrderDetails/useOrderDetails.ts +1 -0
  68. package/src/useOrderPayment/useOrderPayment.test.ts +2 -0
  69. package/src/useOrderPayment/useOrderPayment.ts +1 -0
  70. package/src/usePrice/usePrice.test.ts +2 -0
  71. package/src/usePrice/usePrice.ts +1 -0
  72. package/src/useProduct/useProduct.test.ts +2 -0
  73. package/src/useProduct/useProduct.ts +2 -0
  74. package/src/useProductAssociations/useProductAssociations.spec.ts +48 -0
  75. package/src/useProductAssociations/useProductAssociations.ts +7 -3
  76. package/src/useProductConfigurator/useProductConfigurator.spec.ts +24 -0
  77. package/src/useProductConfigurator/useProductConfigurator.ts +20 -13
  78. package/src/useProductPrice/useProductPrice.test.ts +2 -0
  79. package/src/useProductPrice/useProductPrice.ts +1 -0
  80. package/src/useProductReviews/useProductReviews.test.ts +42 -2
  81. package/src/useProductReviews/useProductReviews.ts +18 -8
  82. package/src/useProductSearch/useProductSearch.test.ts +24 -1
  83. package/src/useProductSearch/useProductSearch.ts +24 -11
  84. package/src/useProductSearchSuggest/useProductSearchSuggest.test.ts +1 -0
  85. package/src/useProductSearchSuggest/useProductSearchSuggest.ts +1 -0
  86. package/src/useProductWishlist/useProductWishlist.test.ts +2 -0
  87. package/src/useProductWishlist/useProductWishlist.ts +1 -0
  88. package/src/useSalutations/useSalutations.spec.ts +15 -0
  89. package/src/useSalutations/useSalutations.ts +5 -2
  90. package/src/useSessionContext/useSessionContext.test.ts +84 -6
  91. package/src/useSessionContext/useSessionContext.ts +17 -2
  92. package/src/useShopwareContext/useShopwareContext.test.ts +1 -0
  93. package/src/useShopwareContext/useShopwareContext.ts +13 -0
  94. package/src/useSyncWishlist/useSyncWishlist.test.ts +1 -0
  95. package/src/useSyncWishlist/useSyncWishlist.ts +1 -0
  96. package/src/useUrlResolver/useUrlResolver.test.ts +1 -0
  97. package/src/useUser/useUser.test.ts +55 -2
  98. package/src/useUser/useUser.ts +34 -25
  99. package/src/useWishlist/useWishlist.test.ts +2 -0
  100. package/src/useWishlist/useWishlist.ts +1 -0
@@ -1,5 +1,6 @@
1
1
  import { describe, expect, it, vi } from "vitest";
2
2
  import { computed, ref } from "vue";
3
+
3
4
  import { useSetup } from "../_test";
4
5
  import mockedCrossSelling from "../mocks/CrossSellingResponse";
5
6
  import mockedProduct from "../mocks/Product";
@@ -90,6 +91,53 @@ describe("useProductAssociations", () => {
90
91
  expect(vm.productAssociations).toStrictEqual(mockedCrossSelling);
91
92
  });
92
93
 
94
+ it("sends sw-include-seo-urls header when includeSeoUrls is true", async () => {
95
+ const { vm, injections } = useSetup(() =>
96
+ useProductAssociations(
97
+ computed(() => mockedProduct),
98
+ {
99
+ associationContext: "cross-selling",
100
+ includeSeoUrls: true,
101
+ },
102
+ ),
103
+ );
104
+ injections.apiClient.invoke.mockResolvedValue({ data: mockedCrossSelling });
105
+
106
+ await vm.loadAssociations({
107
+ method: "post",
108
+ searchParams: {},
109
+ });
110
+
111
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
112
+ expect.stringContaining("readProductCrossSellings"),
113
+ expect.objectContaining({
114
+ headers: {
115
+ "sw-include-seo-urls": true,
116
+ },
117
+ }),
118
+ );
119
+ });
120
+
121
+ it("omits sw-include-seo-urls header when includeSeoUrls is not set", async () => {
122
+ const { vm, injections } = useSetup(() =>
123
+ useProductAssociations(
124
+ computed(() => mockedProduct),
125
+ {
126
+ associationContext: "cross-selling",
127
+ },
128
+ ),
129
+ );
130
+ injections.apiClient.invoke.mockResolvedValue({ data: mockedCrossSelling });
131
+
132
+ await vm.loadAssociations({
133
+ method: "post",
134
+ searchParams: {},
135
+ });
136
+
137
+ const invokeOptions = injections.apiClient.invoke.mock.calls[0]?.[1];
138
+ expect(invokeOptions).not.toHaveProperty("headers");
139
+ });
140
+
93
141
  it("init without product should throw an error", () => {
94
142
  expect(() =>
95
143
  useSetup(() =>
@@ -1,5 +1,6 @@
1
1
  import { computed, ref } from "vue";
2
2
  import type { ComputedRef } from "vue";
3
+
3
4
  import { useShopwareContext } from "#imports";
4
5
  import type { Schemas } from "#shopware";
5
6
 
@@ -31,14 +32,12 @@ export function useProductAssociations(
31
32
  product: ComputedRef<Schemas["Product"]>,
32
33
  options: {
33
34
  associationContext: "cross-selling" | "reviews";
35
+ includeSeoUrls?: boolean;
34
36
  },
35
37
  ): UseProductAssociationsReturn {
36
38
  if (!product.value)
37
39
  throw new Error("[useProductAssociations]: Product is not provided.");
38
40
 
39
- // @ts-ignore: temporary until fixed or removed
40
- const association = options.associationContext;
41
-
42
41
  const { apiClient } = useShopwareContext();
43
42
  const isLoading = ref(false);
44
43
  const associations = ref<Schemas["CrossSellingElementCollection"]>([]);
@@ -84,6 +83,11 @@ export function useProductAssociations(
84
83
  const response = await apiClient.invoke(
85
84
  "readProductCrossSellings post /product/{productId}/cross-selling",
86
85
  {
86
+ ...(options.includeSeoUrls && {
87
+ headers: {
88
+ "sw-include-seo-urls": true,
89
+ },
90
+ }),
87
91
  pathParams: { productId: product.value.id },
88
92
  },
89
93
  );
@@ -1,6 +1,8 @@
1
1
  import { describe, expect, it, vi } from "vitest";
2
2
  import { ref } from "vue";
3
+
3
4
  import { useProduct, useProductConfigurator } from "#imports";
5
+
4
6
  import { useSetup } from "../_test";
5
7
  import mockedConfigurator from "../mocks/Configurator";
6
8
  import mockedProduct from "../mocks/Product";
@@ -40,6 +42,28 @@ describe("useProductConfigurator", () => {
40
42
  );
41
43
  });
42
44
 
45
+ it("findVariantForSelectedOptions uses the cacheable GET variant when cacheableReads is enabled", async () => {
46
+ vi.mocked(useProduct).mockReturnValue({
47
+ configurator: ref(mockedConfigurator),
48
+ product: ref(mockedProduct),
49
+ } as unknown as ReturnType<typeof useProduct>);
50
+ const { vm, injections } = useSetup(useProductConfigurator, {
51
+ shopware: { cacheableReads: true },
52
+ } as Parameters<typeof useSetup>[1]);
53
+ injections.apiClient.invoke.mockResolvedValue({
54
+ data: { elements: [mockedProduct] },
55
+ });
56
+
57
+ await vm.findVariantForSelectedOptions({ test: "test" });
58
+
59
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
60
+ "readProductGet get /product",
61
+ expect.objectContaining({
62
+ query: expect.objectContaining({ _criteria: expect.any(String) }),
63
+ }),
64
+ );
65
+ });
66
+
43
67
  it("findVariantForSelectedOptions - error", async () => {
44
68
  console.error = vi.fn();
45
69
  const { vm, injections } = useSetup(useProductConfigurator);
@@ -1,6 +1,8 @@
1
+ import { encodeForQuery } from "@shopware/api-client/helpers";
1
2
  import { getTranslatedProperty } from "@shopware/helpers";
2
3
  import { computed, ref } from "vue";
3
4
  import type { ComputedRef, Ref } from "vue";
5
+
4
6
  import { useProduct, useShopwareContext } from "#imports";
5
7
  import type { Schemas } from "#shopware";
6
8
 
@@ -40,7 +42,7 @@ export type UseProductConfiguratorReturn = {
40
42
  * @category Product
41
43
  */
42
44
  export function useProductConfigurator(): UseProductConfiguratorReturn {
43
- const { apiClient } = useShopwareContext();
45
+ const { apiClient, cacheableReads } = useShopwareContext();
44
46
 
45
47
  const { configurator, product } = useProduct();
46
48
 
@@ -91,19 +93,24 @@ export function useProductConfigurator(): UseProductConfiguratorReturn {
91
93
  ),
92
94
  ];
93
95
  try {
94
- const response = await apiClient.invoke("readProduct post /product", {
95
- body: {
96
- filter,
97
- limit: 1,
98
- includes: {
99
- product: ["id", "translated", "productNumber", "seoUrls"],
100
- seo_url: ["seoPathInfo"],
101
- },
102
- associations: {
103
- seoUrls: {},
104
- },
96
+ const criteria = {
97
+ filter,
98
+ limit: 1,
99
+ includes: {
100
+ product: ["id", "translated", "productNumber", "seoUrls"],
101
+ seo_url: ["seoPathInfo"],
105
102
  },
106
- });
103
+ associations: {
104
+ seoUrls: {},
105
+ },
106
+ };
107
+ const response = cacheableReads
108
+ ? await apiClient.invoke("readProductGet get /product", {
109
+ query: { _criteria: encodeForQuery(criteria) },
110
+ })
111
+ : await apiClient.invoke("readProduct post /product", {
112
+ body: criteria,
113
+ });
107
114
  return response.data.elements?.[0]; // return first matching product
108
115
  } catch (e) {
109
116
  console.error("SwProductDetails:findVariantForSelectedOptions", e);
@@ -1,6 +1,8 @@
1
1
  import { beforeEach, describe, expect, it, vi } from "vitest";
2
2
  import { ref } from "vue";
3
+
3
4
  import type { Schemas } from "#shopware";
5
+
4
6
  import { useSetup } from "../_test";
5
7
  import mockedProduct from "../mocks/Product";
6
8
  import productTierPrices from "../mocks/ProductTierPrices";
@@ -2,6 +2,7 @@ import { getProductTierPrices } from "@shopware/helpers";
2
2
  import type { TierPrice } from "@shopware/helpers";
3
3
  import { computed } from "vue";
4
4
  import type { ComputedRef, Ref } from "vue";
5
+
5
6
  import type { Schemas } from "#shopware";
6
7
 
7
8
  export type UseProductPriceReturn = {
@@ -1,5 +1,7 @@
1
+ import { encodeForQuery } from "@shopware/api-client/helpers";
1
2
  import { describe, expect, it } from "vitest";
2
3
  import { ref } from "vue";
4
+
3
5
  import { useSetup } from "../_test";
4
6
  import ProductMock from "../mocks/Product";
5
7
  import { useProductReviews } from "./useProductReviews";
@@ -14,8 +16,46 @@ describe("useProductReviews", () => {
14
16
  });
15
17
  await vm.loadProductReviews();
16
18
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
17
- expect.stringContaining("readProductReviews"),
18
- expect.objectContaining({}),
19
+ expect.stringContaining("readProductReviews post"),
20
+ expect.objectContaining({
21
+ body: {},
22
+ pathParams: {
23
+ productId: ProductMock.id,
24
+ },
25
+ }),
26
+ );
27
+ expect(vm.productReviews).toEqual([{ id: "1", content: "Great!" }]);
28
+ });
29
+
30
+ it("load product reviews uses GET when cacheableReads is enabled", async () => {
31
+ const { vm, injections } = useSetup(
32
+ () => useProductReviews(ref(ProductMock)),
33
+ {
34
+ shopware: { cacheableReads: true },
35
+ },
36
+ );
37
+ injections.apiClient.invoke.mockResolvedValue({
38
+ data: { elements: [{ id: "1", content: "Great!" }] },
39
+ });
40
+
41
+ await vm.loadProductReviews({
42
+ limit: 5,
43
+ sort: [{ field: "createdAt", order: "DESC" }],
44
+ });
45
+
46
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
47
+ expect.stringContaining("readProductReviewsGet get"),
48
+ expect.objectContaining({
49
+ pathParams: {
50
+ productId: ProductMock.id,
51
+ },
52
+ query: {
53
+ _criteria: encodeForQuery({
54
+ limit: 5,
55
+ sort: [{ field: "createdAt", order: "DESC" }],
56
+ }),
57
+ },
58
+ }),
19
59
  );
20
60
  expect(vm.productReviews).toEqual([{ id: "1", content: "Great!" }]);
21
61
  });
@@ -1,5 +1,7 @@
1
+ import { encodeForQuery } from "@shopware/api-client/helpers";
1
2
  import { computed, ref } from "vue";
2
3
  import type { ComputedRef, Ref } from "vue";
4
+
3
5
  import { useShopwareContext } from "#imports";
4
6
  import type { Schemas, operations } from "#shopware";
5
7
 
@@ -38,7 +40,7 @@ export type UseProductReviewsReturn = {
38
40
  export function useProductReviews(
39
41
  product: Ref<Schemas["Product"]>,
40
42
  ): UseProductReviewsReturn {
41
- const { apiClient } = useShopwareContext();
43
+ const { apiClient, cacheableReads } = useShopwareContext();
42
44
 
43
45
  const productReviews: Ref<Schemas["ProductReview"][]> = ref([]);
44
46
 
@@ -47,13 +49,21 @@ export function useProductReviews(
47
49
  ): Promise<
48
50
  operations["readProductReviews post /product/{productId}/reviews"]["response"]
49
51
  > => {
50
- const fetchedReviews = await apiClient.invoke(
51
- "readProductReviews post /product/{productId}/reviews",
52
- {
53
- pathParams: { productId: product.value.id },
54
- body: parameters,
55
- },
56
- );
52
+ const fetchedReviews = cacheableReads
53
+ ? await apiClient.invoke(
54
+ "readProductReviewsGet get /product/{productId}/reviews",
55
+ {
56
+ pathParams: { productId: product.value.id },
57
+ query: { _criteria: encodeForQuery(parameters) },
58
+ },
59
+ )
60
+ : await apiClient.invoke(
61
+ "readProductReviews post /product/{productId}/reviews",
62
+ {
63
+ pathParams: { productId: product.value.id },
64
+ body: parameters,
65
+ },
66
+ );
57
67
  productReviews.value = fetchedReviews.data.elements ?? [];
58
68
  return fetchedReviews.data;
59
69
  };
@@ -1,4 +1,6 @@
1
+ import { encodeForQuery } from "@shopware/api-client/helpers";
1
2
  import { describe, expect, it } from "vitest";
3
+
2
4
  import { useSetup } from "../_test";
3
5
  import { cmsAssociations } from "../cms/cmsAssociations";
4
6
  import { useProductSearch } from "./useProductSearch";
@@ -25,7 +27,7 @@ describe("useProductSearch", () => {
25
27
  withCmsAssociations: true,
26
28
  });
27
29
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
28
- expect.stringContaining("readProductDetail"),
30
+ expect.stringContaining("readProductDetail post"),
29
31
  expect.objectContaining({
30
32
  body: cmsAssociations,
31
33
  pathParams: {
@@ -34,4 +36,25 @@ describe("useProductSearch", () => {
34
36
  }),
35
37
  );
36
38
  });
39
+
40
+ it("uses the cacheable GET variant when cacheableReads is enabled", async () => {
41
+ const { vm, injections } = useSetup(() => useProductSearch(), {
42
+ shopware: { cacheableReads: true },
43
+ });
44
+ injections.apiClient.invoke.mockResolvedValue({ data: {} });
45
+ await vm.search("test", {
46
+ withCmsAssociations: true,
47
+ });
48
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
49
+ expect.stringContaining("readProductDetailGet get"),
50
+ expect.objectContaining({
51
+ query: {
52
+ _criteria: encodeForQuery(cmsAssociations),
53
+ },
54
+ pathParams: {
55
+ productId: "test",
56
+ },
57
+ }),
58
+ );
59
+ });
37
60
  });
@@ -1,6 +1,9 @@
1
+ import { encodeForQuery } from "@shopware/api-client/helpers";
1
2
  import { defu } from "defu";
3
+
2
4
  import { useShopwareContext } from "#imports";
3
5
  import type { Schemas } from "#shopware";
6
+
4
7
  import { cmsAssociations } from "../cms/cmsAssociations";
5
8
 
6
9
  type UseProductSearchReturnOptions = {
@@ -28,7 +31,7 @@ export type UseProductSearchReturn = {
28
31
  * @category Navigation & Routing
29
32
  */
30
33
  export function useProductSearch(): UseProductSearchReturn {
31
- const { apiClient } = useShopwareContext();
34
+ const { apiClient, cacheableReads } = useShopwareContext();
32
35
 
33
36
  const search = async (
34
37
  productId: string,
@@ -39,16 +42,26 @@ export function useProductSearch(): UseProductSearchReturn {
39
42
  options?.criteria,
40
43
  { associations: options?.associations ?? {} },
41
44
  );
42
- const result = await apiClient.invoke(
43
- "readProductDetail post /product/{productId}",
44
- {
45
- headers: {
46
- "sw-include-seo-urls": true,
47
- },
48
- pathParams: { productId },
49
- body: associations,
50
- },
51
- );
45
+ const result = cacheableReads
46
+ ? await apiClient.invoke(
47
+ "readProductDetailGet get /product/{productId}",
48
+ {
49
+ headers: {
50
+ "sw-include-seo-urls": true,
51
+ },
52
+ pathParams: { productId },
53
+ query: {
54
+ _criteria: encodeForQuery(associations),
55
+ },
56
+ },
57
+ )
58
+ : await apiClient.invoke("readProductDetail post /product/{productId}", {
59
+ headers: {
60
+ "sw-include-seo-urls": true,
61
+ },
62
+ pathParams: { productId },
63
+ body: associations,
64
+ });
52
65
  return result.data;
53
66
  };
54
67
 
@@ -1,4 +1,5 @@
1
1
  import { describe, expect, it } from "vitest";
2
+
2
3
  import { useSetup } from "../_test";
3
4
  import { useProductSearchSuggest } from "./useProductSearchSuggest";
4
5
 
@@ -1,5 +1,6 @@
1
1
  import { ref } from "vue";
2
2
  import type { ComputedRef, Ref } from "vue";
3
+
3
4
  import { useProductSearchListing } from "#imports";
4
5
  import type { Schemas, operations } from "#shopware";
5
6
 
@@ -1,6 +1,8 @@
1
1
  import { describe, expect, it, vi } from "vitest";
2
2
  import { ref } from "vue";
3
+
3
4
  import { useProductWishlist, useUser } from "#imports";
5
+
4
6
  import { useSetup } from "../_test";
5
7
 
6
8
  vi.mock("../useUser/useUser.ts");
@@ -1,5 +1,6 @@
1
1
  import { computed } from "vue";
2
2
  import type { ComputedRef } from "vue";
3
+
3
4
  import { useLocalWishlist, useSyncWishlist, useUser } from "#imports";
4
5
 
5
6
  export type UseProductWishlistReturn = {
@@ -1,6 +1,7 @@
1
1
  import { flushPromises } from "@vue/test-utils";
2
2
  import { describe, expect, it, vi } from "vitest";
3
3
  import { ref } from "vue";
4
+
4
5
  import { useSetup } from "../_test";
5
6
  import Salutations from "../mocks/Salutations";
6
7
  import { useSalutations } from "./useSalutations";
@@ -36,6 +37,20 @@ describe("useSalutations", () => {
36
37
  expect(vm.getSalutations).toStrictEqual([]);
37
38
  });
38
39
 
40
+ it("uses the cacheable GET variant when cacheableReads is enabled", async () => {
41
+ const { vm, injections } = useSetup(useSalutations, {
42
+ shopware: { cacheableReads: true },
43
+ apiClient: {
44
+ invoke: vi.fn().mockResolvedValue({ data: { elements: Salutations } }),
45
+ },
46
+ } as Parameters<typeof useSetup>[1]);
47
+ await vm.fetchSalutations();
48
+
49
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
50
+ "readSalutationGet get /salutation",
51
+ );
52
+ });
53
+
39
54
  it("should not fetch when swSalutations already populated", async () => {
40
55
  const preloadedSalutations = ref(Salutations);
41
56
  const { vm, injections } = useSetup(useSalutations, {
@@ -1,5 +1,6 @@
1
1
  import { computed, inject, onMounted, provide, ref } from "vue";
2
2
  import type { ComputedRef } from "vue";
3
+
3
4
  import { useShopwareContext } from "#imports";
4
5
  import type { Schemas, operations } from "#shopware";
5
6
 
@@ -22,7 +23,7 @@ export type UseSalutationsReturn = {
22
23
  * @category Context & Language
23
24
  */
24
25
  export function useSalutations(): UseSalutationsReturn {
25
- const { apiClient } = useShopwareContext();
26
+ const { apiClient, cacheableReads } = useShopwareContext();
26
27
 
27
28
  const _salutations = inject("swSalutations", ref());
28
29
  provide("swSalutations", _salutations);
@@ -30,7 +31,9 @@ export function useSalutations(): UseSalutationsReturn {
30
31
  const fetchSalutations = async (): Promise<
31
32
  operations["readSalutation post /salutation"]["response"]
32
33
  > => {
33
- const result = await apiClient.invoke("readSalutation post /salutation");
34
+ const result = cacheableReads
35
+ ? await apiClient.invoke("readSalutationGet get /salutation")
36
+ : await apiClient.invoke("readSalutation post /salutation");
34
37
  _salutations.value = result.data.elements;
35
38
  return result.data;
36
39
  };
@@ -1,13 +1,18 @@
1
- import { describe, expect, it, vi } from "vitest";
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+
2
3
  import type { Schemas } from "#shopware";
4
+
3
5
  import { useSetup } from "../_test";
4
6
  import { useSessionContext } from "./useSessionContext";
5
7
 
6
- const consoleErrorSpy = vi.spyOn(console, "error");
7
- consoleErrorSpy.mockImplementation(() => {});
8
-
9
8
  describe("useSessionContext", () => {
10
9
  const consoleErrorSpy = vi.spyOn(console, "error");
10
+ consoleErrorSpy.mockImplementation(() => {});
11
+
12
+ afterEach(() => {
13
+ vi.clearAllMocks();
14
+ });
15
+
11
16
  it("setLanguage", () => {
12
17
  const { vm, injections } = useSetup(() => useSessionContext());
13
18
  injections.apiClient.invoke.mockResolvedValue({ data: {} });
@@ -55,18 +60,35 @@ describe("useSessionContext", () => {
55
60
  });
56
61
 
57
62
  it("refreshSessionContext - error", async () => {
63
+ const error = new Error("error test");
58
64
  const { vm } = useSetup(() => useSessionContext(), {
59
65
  apiClient: {
60
66
  invoke: vi.fn().mockImplementation(() => {
61
- throw new Error("error test");
67
+ throw error;
62
68
  }),
63
69
  },
64
70
  });
65
71
  consoleErrorSpy.mockImplementation(() => {});
66
- await vm.refreshSessionContext();
72
+ await expect(vm.refreshSessionContext()).rejects.toThrow(error);
67
73
  expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
68
74
  });
69
75
 
76
+ it("currentLocaleCode reads languageInfo.localeCode from the context", async () => {
77
+ const { vm, injections } = useSetup(() => useSessionContext());
78
+ injections.apiClient.invoke.mockResolvedValue({
79
+ data: {
80
+ languageInfo: { name: "English", localeCode: "en-GB" },
81
+ },
82
+ });
83
+ await vm.refreshSessionContext();
84
+ expect(vm.currentLocaleCode).toBe("en-GB");
85
+ });
86
+
87
+ it("currentLocaleCode is undefined when the context has no languageInfo", () => {
88
+ const { vm } = useSetup(() => useSessionContext());
89
+ expect(vm.currentLocaleCode).toBeUndefined();
90
+ });
91
+
70
92
  it("setShippingMethod", async () => {
71
93
  const { vm, injections } = useSetup(() => useSessionContext());
72
94
  injections.apiClient.invoke.mockResolvedValue({ data: {} });
@@ -99,6 +121,62 @@ describe("useSessionContext", () => {
99
121
  );
100
122
  });
101
123
 
124
+ it("setActiveShippingAddress waits for refreshed context", async () => {
125
+ const { vm, injections } = useSetup(() => useSessionContext());
126
+ const calls: string[] = [];
127
+ injections.apiClient.invoke.mockImplementation(async (route) => {
128
+ calls.push(route);
129
+ return { data: {} };
130
+ });
131
+
132
+ await vm.setActiveShippingAddress({ id: "shipping-address-id" });
133
+
134
+ expect(calls).toEqual([
135
+ expect.stringContaining("updateContext"),
136
+ expect.stringContaining("readContext"),
137
+ ]);
138
+ });
139
+
140
+ it("setActiveShippingAddress rejects when refreshed context fails", async () => {
141
+ const error = new Error("refresh failed");
142
+ const { vm, injections } = useSetup(() => useSessionContext());
143
+ injections.apiClient.invoke
144
+ .mockResolvedValueOnce({ data: {} })
145
+ .mockRejectedValueOnce(error);
146
+
147
+ await expect(
148
+ vm.setActiveShippingAddress({ id: "shipping-address-id" }),
149
+ ).rejects.toThrow(error);
150
+ });
151
+
152
+ it("setActiveBillingAddress waits for refreshed context", async () => {
153
+ const { vm, injections } = useSetup(() => useSessionContext());
154
+ const calls: string[] = [];
155
+ injections.apiClient.invoke.mockImplementation(async (route) => {
156
+ calls.push(route);
157
+ return { data: {} };
158
+ });
159
+
160
+ await vm.setActiveBillingAddress({ id: "billing-address-id" });
161
+
162
+ expect(calls).toEqual([
163
+ expect.stringContaining("updateContext"),
164
+ expect.stringContaining("readContext"),
165
+ ]);
166
+ });
167
+
168
+ it("setActiveBillingAddress rejects when refreshed context fails", async () => {
169
+ const error = new Error("refresh failed");
170
+ const { vm, injections } = useSetup(() => useSessionContext());
171
+ injections.apiClient.invoke
172
+ .mockResolvedValueOnce({ data: {} })
173
+ .mockRejectedValueOnce(error);
174
+
175
+ await expect(
176
+ vm.setActiveBillingAddress({ id: "billing-address-id" }),
177
+ ).rejects.toThrow(error);
178
+ });
179
+
102
180
  it("setPaymentMethod", async () => {
103
181
  const { vm, injections } = useSetup(() => useSessionContext());
104
182
  injections.apiClient.invoke.mockResolvedValue({ data: {} });