Skip to content

Commit 268c9d0

Browse files
authored
CHK-8290: Implement endpoint hooks in lib for Checkout Payment Method Delete (#39)
1 parent 1a0cc10 commit 268c9d0

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export * from './shipping';
2828
export * from './taxes';
2929
export * from './discounts';
3030
export * from './payment';
31+
export * from './paymentMethod';
3132
export * from './orderMetaData';
3233
export * from './order';
3334
export * from './externalPaymentGateway';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {
2+
apiTypeKeys,
3+
apiTypes,
4+
checkApiResponse,
5+
fetchAPI,
6+
getApiOptions,
7+
getApiUrl,
8+
IApiReturnObject
9+
} from 'src';
10+
11+
/**
12+
* # deletePaymentMethod
13+
*
14+
* Remove saved payment method
15+
*
16+
* @param {@link string} paymentMethodID - Payment Method pyblic ID to delete.
17+
* @param {number} [numOfRetries=0] - Number of retries for some HTTP errors - Number from 0 to 5
18+
*
19+
* @returns {Promise<IApiReturnObject>} response
20+
*/
21+
export async function deletePaymentMethod(paymentMethodID: string, numOfRetries = 0): Promise<IApiReturnObject> {
22+
const {deletePaymentMethod} = apiTypeKeys;
23+
const url = `${getApiUrl(deletePaymentMethod)}/${paymentMethodID}`;
24+
const options = getApiOptions(deletePaymentMethod);
25+
const fetchRes = await fetchAPI(url, options, numOfRetries);
26+
const {keysToTest} = apiTypes.deletePaymentMethod;
27+
return checkApiResponse(fetchRes, keysToTest);
28+
}

src/paymentMethod/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './deletePaymentMethod';

src/types/apiInterfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ export interface IApiTypes {
272272
updatePayment: IApiTypesDetail;
273273
deletePayment: IApiTypesDetail;
274274
deleteGiftCardPayment: IApiTypesDetail;
275+
deletePaymentMethod: IApiTypesDetail;
275276
patchOrderMetaData: IApiTypesDetail;
276277
batchRequest: IApiTypesDetail;
277278
addLog: IApiTypesDetail;
@@ -313,6 +314,7 @@ export interface IApiTypeKeys {
313314
updatePayment: keyof IApiTypes;
314315
deletePayment: keyof IApiTypes;
315316
deleteGiftCardPayment: keyof IApiTypes;
317+
deletePaymentMethod: keyof IApiTypes;
316318
patchOrderMetaData: keyof IApiTypes;
317319
batchRequest: keyof IApiTypes;
318320
addLog: keyof IApiTypes;

src/variables/constants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ export const apiTypes: IApiTypes = {
252252
useJwt: true,
253253
keysToTest: [...appStateKeysToTest]
254254
},
255+
deletePaymentMethod: {
256+
path: '/payments/saved',
257+
method: methods.DELETE,
258+
useJwt: true,
259+
keysToTest: [...appStateKeysToTest]
260+
},
255261
patchOrderMetaData: {
256262
path: '/meta_data',
257263
method: methods.PATCH,
@@ -333,6 +339,7 @@ export const apiTypeKeys: IApiTypeKeys = {
333339
updatePayment: 'updatePayment',
334340
deletePayment: 'deletePayment',
335341
deleteGiftCardPayment: 'deleteGiftCardPayment',
342+
deletePaymentMethod: 'deletePaymentMethod',
336343
patchOrderMetaData: 'patchOrderMetaData',
337344
batchRequest: 'batchRequest',
338345
addLog: 'addLog',
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {apiTypeKeys, baseReturnObject, keysToTestFromResponse, methods, deletePaymentMethod, FetchError} from 'src';
2+
import * as fetchAPI from 'src/utils/fetchAPI';
3+
import * as getApiOptions from 'src/utils/getApiOptions';
4+
import * as apiUrl from 'src/utils/apiUrl';
5+
import * as apiResponse from 'src/utils/apiResponse';
6+
import {applicationStateMock} from 'src/variables/mocks';
7+
8+
describe('testing deletePaymentMethod', () => {
9+
const returnObject = {...baseReturnObject};
10+
const apiUrlMock = 'https://api.com/checkout/storefront/123/123/payments/saved';
11+
const keysToTest = [keysToTestFromResponse.data, keysToTestFromResponse.applicationState];
12+
let optionsMock: RequestInit;
13+
let getApiOptionsSpy: jest.SpyInstance;
14+
let getApiUrlSpy: jest.SpyInstance;
15+
let fetchApiSpy: jest.SpyInstance;
16+
let checkApiResponseSpy: jest.SpyInstance;
17+
18+
beforeEach(() => {
19+
global.Headers = jest.fn().mockReturnValue({append: jest.fn()});
20+
returnObject.response = { data: { application_state: applicationStateMock }};
21+
returnObject.success = true;
22+
optionsMock = {method: methods.DELETE, headers: new Headers(), body: JSON.stringify({})};
23+
getApiOptionsSpy = jest.spyOn(getApiOptions, 'getApiOptions').mockReturnValue(optionsMock);
24+
getApiUrlSpy = jest.spyOn(apiUrl, 'getApiUrl').mockReturnValue(apiUrlMock);
25+
fetchApiSpy = jest.spyOn(fetchAPI, 'fetchAPI').mockReturnValue(Promise.resolve(returnObject));
26+
checkApiResponseSpy = jest.spyOn(apiResponse, 'checkApiResponse').mockReturnValue(returnObject);
27+
});
28+
29+
afterEach(() => {
30+
jest.restoreAllMocks();
31+
});
32+
33+
test('successful call (200)', async () => {
34+
const res = await deletePaymentMethod('test');
35+
36+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(1);
37+
expect(getApiUrlSpy).toHaveBeenCalledTimes(1);
38+
expect(fetchApiSpy).toHaveBeenCalledTimes(1);
39+
expect(checkApiResponseSpy).toHaveBeenCalledTimes(1);
40+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.deletePaymentMethod);
41+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.deletePaymentMethod);
42+
expect(fetchApiSpy).toHaveBeenCalledWith(`${apiUrlMock}/test`, optionsMock, 0);
43+
expect(checkApiResponseSpy).toHaveBeenCalledWith(returnObject, keysToTest);
44+
expect(res).toStrictEqual(returnObject);
45+
});
46+
47+
test('failed call (422)', async () => {
48+
const tempReturnObject = {...baseReturnObject};
49+
tempReturnObject.error = new FetchError(422, 'Unprocessable Entity');
50+
checkApiResponseSpy = jest.spyOn(apiResponse, 'checkApiResponse').mockReturnValueOnce(tempReturnObject);
51+
52+
fetchApiSpy.mockReturnValueOnce(Promise.resolve(tempReturnObject));
53+
54+
const res = await deletePaymentMethod('test', 1);
55+
56+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(1);
57+
expect(getApiUrlSpy).toHaveBeenCalledTimes(1);
58+
expect(fetchApiSpy).toHaveBeenCalledTimes(1);
59+
expect(checkApiResponseSpy).toHaveBeenCalledTimes(1);
60+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.deletePaymentMethod);
61+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.deletePaymentMethod);
62+
expect(fetchApiSpy).toHaveBeenCalledWith(`${apiUrlMock}/test`, optionsMock, 1);
63+
expect(checkApiResponseSpy).toHaveBeenCalledWith(tempReturnObject, keysToTest);
64+
expect(res).toStrictEqual(tempReturnObject);
65+
});
66+
});

0 commit comments

Comments
 (0)