From 40c98362f36f396faad7d0a9ddc1624b5e2f74d6 Mon Sep 17 00:00:00 2001 From: mkucmus Date: Sun, 29 Mar 2020 19:09:28 +0200 Subject: [PATCH] feat(api-client): set current context's address - billing & shipping (#536) --- api/shopware-6-client.api.md | 6 ++ .../setCurrentBillingAddress.spec.ts | 55 +++++++++++++++++++ .../setCurrentShippingAddress.spec.ts | 55 +++++++++++++++++++ .../src/services/contextService.ts | 22 ++++++++ 4 files changed, 138 insertions(+) create mode 100644 packages/shopware-6-client/__tests__/services/ContextService/setCurrentBillingAddress.spec.ts create mode 100644 packages/shopware-6-client/__tests__/services/ContextService/setCurrentShippingAddress.spec.ts diff --git a/api/shopware-6-client.api.md b/api/shopware-6-client.api.md index 519478e33..8e785bbc1 100644 --- a/api/shopware-6-client.api.md +++ b/api/shopware-6-client.api.md @@ -211,6 +211,9 @@ export function register(params: CustomerRegistrationParams): Promise; +// @alpha +export function setCurrentBillingAddress(billingAddressId: string): Promise; + // @alpha (undocumented) export function setCurrentCurrency(newCurrencyID: string): Promise; @@ -220,6 +223,9 @@ export function setCurrentLanguage(newLanguageId: string): Promise; +// @alpha +export function setCurrentShippingAddress(shippingAddressId: string): Promise; + // @alpha (undocumented) export function setCurrentShippingMethod(newShippingMethodId: string): Promise; diff --git a/packages/shopware-6-client/__tests__/services/ContextService/setCurrentBillingAddress.spec.ts b/packages/shopware-6-client/__tests__/services/ContextService/setCurrentBillingAddress.spec.ts new file mode 100644 index 000000000..ce1c0e7af --- /dev/null +++ b/packages/shopware-6-client/__tests__/services/ContextService/setCurrentBillingAddress.spec.ts @@ -0,0 +1,55 @@ +import { apiService } from "../../../src/apiService"; +import { + setCurrentBillingAddress, + update +} from "@shopware-pwa/shopware-6-client"; + +jest.mock("../../../src/apiService"); +const mockedAxios = apiService as jest.Mocked; + +describe("ContextService - setCurrentBillingAddress with contextToken given", () => { + beforeEach(() => { + update({ contextToken: "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6" }); + jest.resetAllMocks(); + }); + + it("should return context token", async () => { + mockedAxios.patch.mockResolvedValueOnce({ + data: { "sw-context-token": "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6" } + }); + + let newBillingAddressId = "45f96f681f9d4834b29e9e15df3a7149"; + + const result = await setCurrentBillingAddress(newBillingAddressId); + + expect(mockedAxios.patch).toBeCalledTimes(1); + expect(mockedAxios.patch).toBeCalledWith("/context", { + billingAddressId: "45f96f681f9d4834b29e9e15df3a7149" + }); + + expect(result.contextToken).toEqual("NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6"); + }); +}); + +describe("ContextService - setCurrentBillingAddress without contextToken given", () => { + beforeEach(() => { + update({ contextToken: undefined }); + jest.resetAllMocks(); + }); + + it("should return context token", async () => { + mockedAxios.patch.mockResolvedValueOnce({ + data: { "sw-context-token": "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6" } + }); + + let newBillingAddressId = "45f96f681f9d4834b29e9e15df3a7149"; + + const result = await setCurrentBillingAddress(newBillingAddressId); + expect(mockedAxios.patch).toBeCalledTimes(1); + expect(mockedAxios.patch).toBeCalledWith("/context", { + billingAddressId: "45f96f681f9d4834b29e9e15df3a7149" + }); + + expect(result.contextToken).toEqual("NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6"); + }); +}); diff --git a/packages/shopware-6-client/__tests__/services/ContextService/setCurrentShippingAddress.spec.ts b/packages/shopware-6-client/__tests__/services/ContextService/setCurrentShippingAddress.spec.ts new file mode 100644 index 000000000..61afefa9c --- /dev/null +++ b/packages/shopware-6-client/__tests__/services/ContextService/setCurrentShippingAddress.spec.ts @@ -0,0 +1,55 @@ +import { apiService } from "../../../src/apiService"; +import { + setCurrentShippingAddress, + update +} from "@shopware-pwa/shopware-6-client"; + +jest.mock("../../../src/apiService"); +const mockedAxios = apiService as jest.Mocked; + +describe("ContextService - setCurrentShippingAddress with contextToken given", () => { + beforeEach(() => { + update({ contextToken: "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6" }); + jest.resetAllMocks(); + }); + + it("should return context token", async () => { + mockedAxios.patch.mockResolvedValueOnce({ + data: { "sw-context-token": "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6" } + }); + + let newShippingAddressId = "45f96f681f9d4834b29e9e15df3a7149"; + + const result = await setCurrentShippingAddress(newShippingAddressId); + + expect(mockedAxios.patch).toBeCalledTimes(1); + expect(mockedAxios.patch).toBeCalledWith("/context", { + shippingAddressId: "45f96f681f9d4834b29e9e15df3a7149" + }); + + expect(result.contextToken).toEqual("NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6"); + }); +}); + +describe("ContextService - setCurrentShippingAddress without contextToken given", () => { + beforeEach(() => { + update({ contextToken: undefined }); + jest.resetAllMocks(); + }); + + it("should return context token", async () => { + mockedAxios.patch.mockResolvedValueOnce({ + data: { "sw-context-token": "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6" } + }); + + let newShippingAddressId = "45f96f681f9d4834b29e9e15df3a7149"; + + const result = await setCurrentShippingAddress(newShippingAddressId); + expect(mockedAxios.patch).toBeCalledTimes(1); + expect(mockedAxios.patch).toBeCalledWith("/context", { + shippingAddressId: "45f96f681f9d4834b29e9e15df3a7149" + }); + + expect(result.contextToken).toEqual("NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6"); + }); +}); diff --git a/packages/shopware-6-client/src/services/contextService.ts b/packages/shopware-6-client/src/services/contextService.ts index f4bc3d2b0..dbf45963a 100644 --- a/packages/shopware-6-client/src/services/contextService.ts +++ b/packages/shopware-6-client/src/services/contextService.ts @@ -32,6 +32,28 @@ async function updateContext( return { contextToken }; } +/** + * Set the current session's shipping address to correspoding to id + * @throws ClientApiError + * @alpha + */ +export function setCurrentShippingAddress( + shippingAddressId: string +): Promise { + return updateContext({ shippingAddressId }); +} + +/** + * Set the current session's billing address to correspoding to id + * @throws ClientApiError + * @alpha + */ +export function setCurrentBillingAddress( + billingAddressId: string +): Promise { + return updateContext({ billingAddressId }); +} + /** * @throws ClientApiError * @alpha