@shopware/composables 1.12.0 → 1.13.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.
@@ -1,3 +1,4 @@
1
+ import { encodeForQuery } from "@shopware/api-client/helpers";
1
2
  import { describe, expect, it } from "vitest";
2
3
  import { ref } from "vue";
3
4
 
@@ -15,8 +16,46 @@ describe("useProductReviews", () => {
15
16
  });
16
17
  await vm.loadProductReviews();
17
18
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
18
- expect.stringContaining("readProductReviews"),
19
- 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
+ }),
20
59
  );
21
60
  expect(vm.productReviews).toEqual([{ id: "1", content: "Great!" }]);
22
61
  });
@@ -1,3 +1,4 @@
1
+ import { encodeForQuery } from "@shopware/api-client/helpers";
1
2
  import { computed, ref } from "vue";
2
3
  import type { ComputedRef, Ref } from "vue";
3
4
 
@@ -39,7 +40,7 @@ export type UseProductReviewsReturn = {
39
40
  export function useProductReviews(
40
41
  product: Ref<Schemas["Product"]>,
41
42
  ): UseProductReviewsReturn {
42
- const { apiClient } = useShopwareContext();
43
+ const { apiClient, cacheableReads } = useShopwareContext();
43
44
 
44
45
  const productReviews: Ref<Schemas["ProductReview"][]> = ref([]);
45
46
 
@@ -48,13 +49,21 @@ export function useProductReviews(
48
49
  ): Promise<
49
50
  operations["readProductReviews post /product/{productId}/reviews"]["response"]
50
51
  > => {
51
- const fetchedReviews = await apiClient.invoke(
52
- "readProductReviews post /product/{productId}/reviews",
53
- {
54
- pathParams: { productId: product.value.id },
55
- body: parameters,
56
- },
57
- );
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
+ );
58
67
  productReviews.value = fetchedReviews.data.elements ?? [];
59
68
  return fetchedReviews.data;
60
69
  };
@@ -9,7 +9,7 @@ import { cmsAssociations } from "../cms/cmsAssociations";
9
9
  type UseProductSearchReturnOptions = {
10
10
  withCmsAssociations?: boolean;
11
11
  criteria?: Partial<Schemas["Criteria"]>;
12
- associations?: Partial<Schemas["Association"]>;
12
+ associations?: Partial<Schemas["Associations"]>;
13
13
  };
14
14
 
15
15
  export type UseProductSearchReturn = {
@@ -1,15 +1,18 @@
1
- import { describe, expect, it, vi } from "vitest";
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
2
 
3
3
  import type { Schemas } from "#shopware";
4
4
 
5
5
  import { useSetup } from "../_test";
6
6
  import { useSessionContext } from "./useSessionContext";
7
7
 
8
- const consoleErrorSpy = vi.spyOn(console, "error");
9
- consoleErrorSpy.mockImplementation(() => {});
10
-
11
8
  describe("useSessionContext", () => {
12
9
  const consoleErrorSpy = vi.spyOn(console, "error");
10
+ consoleErrorSpy.mockImplementation(() => {});
11
+
12
+ afterEach(() => {
13
+ vi.clearAllMocks();
14
+ });
15
+
13
16
  it("setLanguage", () => {
14
17
  const { vm, injections } = useSetup(() => useSessionContext());
15
18
  injections.apiClient.invoke.mockResolvedValue({ data: {} });
@@ -57,15 +60,16 @@ describe("useSessionContext", () => {
57
60
  });
58
61
 
59
62
  it("refreshSessionContext - error", async () => {
63
+ const error = new Error("error test");
60
64
  const { vm } = useSetup(() => useSessionContext(), {
61
65
  apiClient: {
62
66
  invoke: vi.fn().mockImplementation(() => {
63
- throw new Error("error test");
67
+ throw error;
64
68
  }),
65
69
  },
66
70
  });
67
71
  consoleErrorSpy.mockImplementation(() => {});
68
- await vm.refreshSessionContext();
72
+ await expect(vm.refreshSessionContext()).rejects.toThrow(error);
69
73
  expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
70
74
  });
71
75
 
@@ -117,6 +121,62 @@ describe("useSessionContext", () => {
117
121
  );
118
122
  });
119
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
+
120
180
  it("setPaymentMethod", async () => {
121
181
  const { vm, injections } = useSetup(() => useSessionContext());
122
182
  injections.apiClient.invoke.mockResolvedValue({ data: {} });
@@ -21,6 +21,10 @@ export type UseSessionContextReturn = {
21
21
  sessionContext: ComputedRef<Schemas["SalesChannelContext"] | undefined>;
22
22
  /**
23
23
  * Fetches the session context and assigns the result to the `sessionContext` property
24
+ *
25
+ * Throws when the context cannot be loaded. Callers that depend on a verified
26
+ * context switch, such as logout or checkout flows, should block progression
27
+ * when this fails.
24
28
  */
25
29
  refreshSessionContext(): Promise<void>;
26
30
  /**
@@ -137,6 +141,7 @@ export function useSessionContext(
137
141
  _sessionContext.value = data;
138
142
  } catch (e) {
139
143
  console.error("[UseSessionContext][refreshSessionContext]", e);
144
+ throw e;
140
145
  }
141
146
  };
142
147
 
@@ -227,7 +232,7 @@ export function useSessionContext(
227
232
  shippingAddressId: address.id,
228
233
  },
229
234
  });
230
- refreshSessionContext();
235
+ await refreshSessionContext();
231
236
  };
232
237
 
233
238
  // TODO: replace the source from defaultBillingAddress by new value once NEXT-28627 is solved
@@ -247,7 +252,7 @@ export function useSessionContext(
247
252
  billingAddressId: address.id,
248
253
  },
249
254
  });
250
- refreshSessionContext();
255
+ await refreshSessionContext();
251
256
  };
252
257
 
253
258
  const setContext = (context: Schemas["SalesChannelContext"]) => {
@@ -56,6 +56,8 @@ const REGISTRATION_DATA: Omit<
56
56
 
57
57
  describe("useUser", () => {
58
58
  afterEach(() => {
59
+ vi.clearAllMocks();
60
+ refreshSessionContextSpy.mockReset();
59
61
  userFromContextRef.value = undefined;
60
62
  });
61
63
 
@@ -151,9 +153,27 @@ describe("useUser", () => {
151
153
  expect(injections.apiClient.invoke).toHaveBeenCalledWith(
152
154
  expect.stringContaining("logoutCustomer"),
153
155
  );
156
+ expect(refreshSessionContextSpy).toHaveBeenCalled();
157
+ expect(refreshCartSpy).toHaveBeenCalled();
154
158
  expect(result).toEqual(logoutResponse.data);
155
159
  });
156
160
 
161
+ it("logout rejects when the session context cannot be verified", async () => {
162
+ const { vm, injections } = useSetup(() => useUser());
163
+ const error = new Error("context refresh failed");
164
+ injections.apiClient.invoke.mockResolvedValue({
165
+ data: { redirectUrl: "/" },
166
+ });
167
+ refreshSessionContextSpy.mockRejectedValue(error);
168
+
169
+ await expect(vm.logout()).rejects.toThrow(error);
170
+
171
+ expect(injections.apiClient.invoke).toHaveBeenCalledWith(
172
+ expect.stringContaining("logoutCustomer"),
173
+ );
174
+ expect(refreshCartSpy).not.toHaveBeenCalled();
175
+ });
176
+
157
177
  it("refreshUser", async () => {
158
178
  const { vm, injections } = useSetup(() => useUser());
159
179
  const customerData = { id: "cust-1", email: "test@test.com" };