-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add packaging options client
This adds the client for packaging options for an order. It also updates missing types for the checkout order such as metadata.
- Loading branch information
Ana Pimentel
committed
Feb 9, 2024
1 parent
24ca467
commit 6aaaa3a
Showing
24 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/client/src/checkout/__fixtures__/getPackagingOptions.fixtures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { rest, type RestHandler } from 'msw'; | ||
import type { PackagingOption } from '../types/index.js'; | ||
|
||
const path = '/api/checkout/v1/packagingOptions'; | ||
|
||
const fixtures = { | ||
success: (response: PackagingOption[]): RestHandler => | ||
rest.get(path, (_req, res, ctx) => | ||
res(ctx.status(200), ctx.json(response)), | ||
), | ||
failure: (): RestHandler => | ||
rest.get(path, (_req, res, ctx) => | ||
res(ctx.status(404), ctx.json({ message: 'stub error' })), | ||
), | ||
}; | ||
|
||
export default fixtures; |
11 changes: 11 additions & 0 deletions
11
packages/client/src/checkout/__tests__/__snapshots__/getPackagingOptions.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`checkout client getPackagingOptions should receive a client request error 1`] = ` | ||
Object { | ||
"code": "-1", | ||
"message": "stub error", | ||
"name": "AxiosError", | ||
"status": 404, | ||
"transportLayerErrorCode": "ERR_BAD_REQUEST", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/client/src/checkout/__tests__/getPackagingOptions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as checkoutClient from '../index.js'; | ||
import { mockPackagingOptionsResponse } from 'tests/__fixtures__/checkout/packagingOptions.fixtures.mjs'; | ||
import { type PackagingOption } from '../types/index.js'; | ||
import client from '../../helpers/client/index.js'; | ||
import fixtures from '../__fixtures__/getPackagingOptions.fixtures.js'; | ||
import mswServer from '../../../tests/mswServer.js'; | ||
|
||
describe('checkout client', () => { | ||
const expectedConfig = undefined; | ||
|
||
beforeEach(() => jest.clearAllMocks()); | ||
|
||
describe('getPackagingOptions', () => { | ||
const spy = jest.spyOn(client, 'get'); | ||
const query = { channelCode: 'code' }; | ||
const urlToBeCalled = `/checkout/v1/packagingOptions?channelCode=${query.channelCode}`; | ||
|
||
it('should handle a client request successfully', async () => { | ||
const response: PackagingOption[] = mockPackagingOptionsResponse; | ||
|
||
mswServer.use(fixtures.success(response)); | ||
|
||
await expect( | ||
checkoutClient.getPackagingOptions(query), | ||
).resolves.toStrictEqual(response); | ||
expect(spy).toHaveBeenCalledWith(urlToBeCalled, expectedConfig); | ||
}); | ||
|
||
it('should receive a client request error', async () => { | ||
mswServer.use(fixtures.failure()); | ||
|
||
await expect( | ||
checkoutClient.getPackagingOptions(query), | ||
).rejects.toMatchSnapshot(); | ||
expect(spy).toHaveBeenCalledWith(urlToBeCalled, expectedConfig); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { adaptError } from '../helpers/client/formatError.js'; | ||
import client from '../helpers/client/index.js'; | ||
import join from 'proper-url-join'; | ||
import type { GetPackagingOptions } from './types/index.js'; | ||
|
||
/** | ||
* Method responsible for loading the packaging options. | ||
* | ||
* @param query - Query params. | ||
* @param config - Custom configurations to send to the client instance (axios). | ||
* | ||
* @returns Promise that will resolve when the call to the endpoint finishes. | ||
*/ | ||
const getPackagingOptions: GetPackagingOptions = (query, config) => | ||
client | ||
.get(join('/checkout/v1/packagingOptions', { query }), config) | ||
.then(response => response.data) | ||
.catch(error => { | ||
throw adaptError(error); | ||
}); | ||
|
||
export default getPackagingOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/client/src/checkout/types/getPackagingOptions.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Config } from '../../types/index.js'; | ||
import type { PackagingOption } from './index.js'; | ||
|
||
export type GetPackagingOptionsQuery = { | ||
channelCode: string; | ||
}; | ||
|
||
export type GetPackagingOptions = ( | ||
query?: GetPackagingOptionsQuery, | ||
config?: Config, | ||
) => Promise<PackagingOption[]>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type PackagingOption = string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './checkout.fixtures.mjs'; | ||
export * from './checkoutSessions.fixtures.mjs'; | ||
export * from './draftOrders.fixtures.mjs'; | ||
export * from './packagingOptions.fixtures.mjs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const mockPackagingOption = 'Basic'; | ||
|
||
export const mockPackagingOptionsResponse = [mockPackagingOption, 'Signature']; |