Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat(api-client): set current context's address - billing & shipping (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucmus authored Mar 29, 2020
1 parent 235da4a commit 40c9836
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/shopware-6-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ export function register(params: CustomerRegistrationParams): Promise<CustomerRe
// @alpha
export function removeCartItem(itemId: string): Promise<Cart>;

// @alpha
export function setCurrentBillingAddress(billingAddressId: string): Promise<ContextTokenResponse>;

// @alpha (undocumented)
export function setCurrentCurrency(newCurrencyID: string): Promise<ContextTokenResponse>;

Expand All @@ -220,6 +223,9 @@ export function setCurrentLanguage(newLanguageId: string): Promise<ContextTokenR
// @alpha (undocumented)
export function setCurrentPaymentMethod(newPaymentMethodId: string): Promise<ContextTokenResponse>;

// @alpha
export function setCurrentShippingAddress(shippingAddressId: string): Promise<ContextTokenResponse>;

// @alpha (undocumented)
export function setCurrentShippingMethod(newShippingMethodId: string): Promise<ContextTokenResponse>;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<typeof apiService>;

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");
});
});
Original file line number Diff line number Diff line change
@@ -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<typeof apiService>;

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");
});
});
22 changes: 22 additions & 0 deletions packages/shopware-6-client/src/services/contextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContextTokenResponse> {
return updateContext({ shippingAddressId });
}

/**
* Set the current session's billing address to correspoding to id
* @throws ClientApiError
* @alpha
*/
export function setCurrentBillingAddress(
billingAddressId: string
): Promise<ContextTokenResponse> {
return updateContext({ billingAddressId });
}

/**
* @throws ClientApiError
* @alpha
Expand Down

0 comments on commit 40c9836

Please sign in to comment.