Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): add checkout session patch #843

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,7 @@ Object {
"patchCheckoutOrderDeliveryBundleUpgrades": [Function],
"patchCheckoutOrderItem": [Function],
"patchCheckoutOrderItems": [Function],
"patchCheckoutSession": [Function],
"patchRaffleParticipation": [Function],
"patchReturn": [Function],
"patchUserAttribute": [Function],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { rest, type RestHandler } from 'msw';

import type { CheckoutSession } from '../types/index.js';

const path = '/api/checkout/v1/checkoutSessions/:id';

const fixtures = {
success: (response: CheckoutSession): RestHandler =>
rest.patch(path, (_req, res, ctx) =>
res(ctx.status(200), ctx.json(response)),
),
failure: (): RestHandler =>
rest.patch(path, (_req, res, ctx) =>
res(ctx.status(404), ctx.json({ message: 'stub error' })),
),
};

export default fixtures;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`checkout client patchCheckoutSession should receive a client request error 1`] = `
Object {
"code": "-1",
"message": "stub error",
"name": "AxiosError",
"status": 404,
"transportLayerErrorCode": "ERR_BAD_REQUEST",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import client from '../../helpers/client/index.js';

import patchCheckoutSession from '../patchCheckoutSession.js';

import mswServer from '../../../tests/mswServer.js';

import {
mockCheckoutSession,
mockCheckoutSessionId,
} from 'tests/__fixtures__/checkout/checkoutSessions.fixtures.mjs';
import fixtures from '../__fixtures__/patchCheckoutSession.fixtures.js';

import type { PatchCheckoutSessionData } from '../types/index.js';

describe('checkout client', () => {
const expectedConfig = undefined;

beforeEach(() => jest.clearAllMocks());

describe('patchCheckoutSession', () => {
const data: PatchCheckoutSessionData = {
email: '[email protected]',
};

const spy = jest.spyOn(client, 'patch');
const urlToBeCalled = `/checkout/v1/checkoutSessions/${mockCheckoutSessionId}`;

it('should handle a client request successfully', async () => {
const response = mockCheckoutSession;

mswServer.use(fixtures.success(response));

await expect(
patchCheckoutSession(mockCheckoutSessionId, data),
).resolves.toStrictEqual(response);

expect(spy).toHaveBeenCalledWith(urlToBeCalled, data, expectedConfig);
});

it('should receive a client request error', async () => {
mswServer.use(fixtures.failure());

await expect(
patchCheckoutSession(mockCheckoutSessionId, data),
).rejects.toMatchSnapshot();

expect(spy).toHaveBeenCalledWith(urlToBeCalled, data, expectedConfig);
});
});
});
1 change: 1 addition & 0 deletions packages/client/src/checkout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export { default as patchCheckoutOrder } from './patchCheckoutOrder.js';
export { default as patchCheckoutOrderDeliveryBundleUpgrades } from './patchCheckoutOrderDeliveryBundleUpgrades.js';
export { default as patchCheckoutOrderItem } from './patchCheckoutOrderItem.js';
export { default as patchCheckoutOrderItems } from './patchCheckoutOrderItems.js';
export { default as patchCheckoutSession } from './patchCheckoutSession.js';
export { default as postCheckoutOrder } from './postCheckoutOrder.js';
export { default as postCheckoutOrderCharge } from './postCheckoutOrderCharge.js';
export { default as postCheckoutOrderContext } from './postCheckoutOrderContext.js';
Expand Down
32 changes: 32 additions & 0 deletions packages/client/src/checkout/patchCheckoutSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { adaptError } from '../helpers/client/formatError.js';
import client from '../helpers/client/index.js';
import join from 'proper-url-join';
import type { PatchCheckoutSession } from './types/index.js';

/**
* Method responsible for changing the checkout session information. This is used for any
* type of changes to the checkout session object.
*
* @param checkoutSessionId - Universal identifier of the Checkout Session.
* @param data - Request data.
* @param config - Custom configurations to send to the client instance (axios).
*
* @returns Promise that will resolve when the call to the endpoint finishes.
*/
const patchCheckoutSession: PatchCheckoutSession = (
checkoutSessionId,
data,
config,
) =>
client
.patch(
join('/checkout/v1/checkoutSessions/', checkoutSessionId),
data,
config,
)
.then(response => response.data)
.catch(error => {
throw adaptError(error);
});

export default patchCheckoutSession;
1 change: 1 addition & 0 deletions packages/client/src/checkout/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export * from './patchCheckoutOrder.types.js';
export * from './patchCheckoutOrderDeliveryBundleUpgrades.types.js';
export * from './patchCheckoutOrderItem.types.js';
export * from './patchCheckoutOrderItems.types.js';
export * from './patchCheckoutSession.types.js';
export * from './postCheckoutOrder.types.js';
export * from './postCheckoutOrderCharge.types.js';
export * from './postCheckoutOrderContext.types.js';
Expand Down
16 changes: 16 additions & 0 deletions packages/client/src/checkout/types/patchCheckoutSession.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { CheckoutAddress, Config } from '../../types/index.js';
import type { CheckoutSession } from './checkoutSession.types.js';
import type { ClickAndCollect } from './checkoutOrder.types.js';

export type PatchCheckoutSessionData = {
shippingAddress?: CheckoutAddress;
billingAddress?: CheckoutAddress;
clickAndCollect?: ClickAndCollect;
email?: string;
};

export type PatchCheckoutSession = (
checkoutSessionId: CheckoutSession['id'],
data: PatchCheckoutSessionData,
config?: Config,
) => Promise<CheckoutSession>;
Loading