Skip to content

Commit 1d32940

Browse files
authored
CHK-3367: fix shipping line fee (#28)
1 parent 5c9d97e commit 1d32940

File tree

6 files changed

+117
-4
lines changed

6 files changed

+117
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {apiTypeKeys, apiTypes, fetchAPI, getApiOptions, getApiUrl, IApiReturnObject, checkApiResponse} from 'src';
2+
3+
/**
4+
* # changeShippingLine
5+
*
6+
* calls post shipping lines endpoint and sets the values for a shipping line
7+
*
8+
* @param index id of the appropriate available shipping line
9+
* @param code
10+
* @param numOfRetries
11+
*/
12+
export async function changeShippingLineWithCode(index: string, code = '', numOfRetries = 0): Promise<IApiReturnObject> {
13+
const {changeShippingLines} = apiTypeKeys;
14+
const url = getApiUrl(changeShippingLines);
15+
const options = getApiOptions(changeShippingLines, { index, code });
16+
const fetchRes = await fetchAPI(url, options, numOfRetries);
17+
const {keysToTest} = apiTypes.changeShippingLines;
18+
19+
return checkApiResponse(fetchRes, keysToTest);
20+
}

src/shipping/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './getShippingLines';
22
export * from './estimateShippingLines';
33
export * from './changeShippingLine';
4+
export * from './changeShippingLineWithCode';

src/types/apiInterfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ export interface IShippingLine {
874874
id: string;
875875
description: string;
876876
amount: number;
877+
code: string;
877878
}
878879

879880
export interface IPigiActionType {

src/variables/mocks.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ export const patchOrderMetaDataMock: IPatchOrderMetaDataRequest = {
213213
export const selectShippingLineMock: IShippingLine = {
214214
id: 'test_select_shipping_line_id',
215215
description: 'Test Description',
216-
amount: 100
216+
amount: 100,
217+
code: '',
217218
};
218219

219220
export const availableShippingLineMock: IAvailableShippingLine = {
@@ -284,11 +285,13 @@ export const selectShippingLineArrayMock: Array<IShippingLine> = [
284285
{
285286
id: '1',
286287
description: 'First shipping line Option',
287-
amount: 999
288+
amount: 999,
289+
code: '111',
288290
}, {
289291
id: '2',
290292
description: 'Second shipping line Option',
291-
amount: 1500
293+
amount: 1500,
294+
code: '222'
292295
}
293296
];
294297

src/variables/variables.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ export const shipping: IShipping = {
111111
selected_shipping: {
112112
id: '',
113113
description: '',
114-
amount: 0
114+
amount: 0,
115+
code: '',
115116
},
116117
available_shipping_lines: [],
117118
taxes: [],
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import {apiTypeKeys, baseReturnObject, methods, apiTypes, changeShippingLineWithCode, changeShippingLine} from 'src';
2+
import {applicationStateMock, selectShippingLineMock} from 'src/variables/mocks';
3+
import * as fetchAPI from 'src/utils/fetchAPI';
4+
import * as getApiOptions from 'src/utils/getApiOptions';
5+
import * as apiUrl from 'src/utils/apiUrl';
6+
import * as apiResponse from 'src/utils/apiResponse';
7+
8+
describe('testing set shipping address api', () => {
9+
const returnObject = {...baseReturnObject};
10+
const timesWhenCalled = 1;
11+
const index = '1';
12+
const code = 'code';
13+
const code1 = '';
14+
const apiUrlMock = 'https://api.com/checkout/storefront/123/123/addresses/shipping';
15+
const {keysToTest} = apiTypes.changeShippingLines;
16+
let optionsMock: RequestInit;
17+
let getApiOptionsSpy: jest.SpyInstance;
18+
let getApiUrlSpy: jest.SpyInstance;
19+
let fetchApiSpy: jest.SpyInstance;
20+
let checkApiResponseSpy: jest.SpyInstance;
21+
22+
beforeEach(() => {
23+
global.Headers = jest.fn().mockReturnValue({
24+
append: jest.fn(() => null)
25+
});
26+
optionsMock = {method: methods.POST, headers: new Headers(), body: JSON.stringify({})};
27+
getApiOptionsSpy = jest.spyOn(getApiOptions, 'getApiOptions').mockReturnValue(optionsMock);
28+
getApiUrlSpy = jest.spyOn(apiUrl, 'getApiUrl').mockReturnValue(apiUrlMock);
29+
fetchApiSpy = jest.spyOn(fetchAPI, 'fetchAPI').mockReturnValue(Promise.resolve(returnObject));
30+
checkApiResponseSpy = jest.spyOn(apiResponse, 'checkApiResponse').mockReturnValue(returnObject);
31+
returnObject.response = { data: { selected_shipping: selectShippingLineMock, application_state: applicationStateMock }};
32+
returnObject.success = true;
33+
});
34+
35+
afterEach(() => {
36+
jest.restoreAllMocks();
37+
});
38+
39+
test('calling shippingLines', async () => {
40+
const res = await changeShippingLineWithCode(index, code);
41+
42+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
43+
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
44+
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
45+
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
46+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines, {index, code});
47+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines);
48+
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 0);
49+
expect(checkApiResponseSpy).toHaveBeenCalledWith(returnObject, keysToTest);
50+
expect(res).toStrictEqual(returnObject);
51+
});
52+
53+
test('calling shippingLines w/ success = false', async () => {
54+
const tempReturnObject = {...baseReturnObject};
55+
56+
fetchApiSpy.mockReturnValueOnce(Promise.resolve(tempReturnObject));
57+
checkApiResponseSpy.mockReturnValueOnce(tempReturnObject);
58+
59+
const res = await changeShippingLineWithCode(index, code, 1);
60+
61+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
62+
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
63+
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
64+
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
65+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines, {index, code});
66+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines);
67+
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 1);
68+
expect(checkApiResponseSpy).toHaveBeenCalledWith(tempReturnObject, keysToTest);
69+
expect(res).toStrictEqual(tempReturnObject);
70+
});
71+
72+
test('calling shippingLines default code', async () => {
73+
const code = '';
74+
const res = await changeShippingLineWithCode(index);
75+
76+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
77+
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
78+
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
79+
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
80+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines, {index, code});
81+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines);
82+
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 0);
83+
expect(checkApiResponseSpy).toHaveBeenCalledWith(returnObject, keysToTest);
84+
expect(res).toStrictEqual(returnObject);
85+
});
86+
87+
});

0 commit comments

Comments
 (0)