Skip to content

Commit

Permalink
CHK-3367: fix shipping line fee
Browse files Browse the repository at this point in the history
  • Loading branch information
bold-nima committed Aug 9, 2024
1 parent 5c9d97e commit d37f669
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/shipping/changeShippingLineWithCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {apiTypeKeys, apiTypes, fetchAPI, getApiOptions, getApiUrl, IApiReturnObject, checkApiResponse} from 'src';

/**
* # changeShippingLine
*
* calls post shipping lines endpoint and sets the values for a shipping line
*
* @param index id of the appropriate available shipping line
* @param code
* @param numOfRetries
*/
export async function changeShippingLineWithCode(index: string, code = '', numOfRetries = 0): Promise<IApiReturnObject> {
const {changeShippingLines} = apiTypeKeys;
const url = getApiUrl(changeShippingLines);
const options = getApiOptions(changeShippingLines, { index, code });
const fetchRes = await fetchAPI(url, options, numOfRetries);
const {keysToTest} = apiTypes.changeShippingLines;

return checkApiResponse(fetchRes, keysToTest);
}
1 change: 1 addition & 0 deletions src/shipping/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './getShippingLines';
export * from './estimateShippingLines';
export * from './changeShippingLine';
export * from './changeShippingLineWithCode';
1 change: 1 addition & 0 deletions src/types/apiInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ export interface IShippingLine {
id: string;
description: string;
amount: number;
code: string;
}

export interface IPigiActionType {
Expand Down
10 changes: 7 additions & 3 deletions src/variables/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ export const patchOrderMetaDataMock: IPatchOrderMetaDataRequest = {
export const selectShippingLineMock: IShippingLine = {
id: 'test_select_shipping_line_id',
description: 'Test Description',
amount: 100
amount: 100,
code: '',

};

export const availableShippingLineMock: IAvailableShippingLine = {
Expand Down Expand Up @@ -284,11 +286,13 @@ export const selectShippingLineArrayMock: Array<IShippingLine> = [
{
id: '1',
description: 'First shipping line Option',
amount: 999
amount: 999,
code: '111',
}, {
id: '2',
description: 'Second shipping line Option',
amount: 1500
amount: 1500,
code: '222'
}
];

Expand Down
3 changes: 2 additions & 1 deletion src/variables/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export const shipping: IShipping = {
selected_shipping: {
id: '',
description: '',
amount: 0
amount: 0,
code: '',
},
available_shipping_lines: [],
taxes: [],
Expand Down
71 changes: 71 additions & 0 deletions tests/shipping/changeShippingLineWithCode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {apiTypeKeys, baseReturnObject, methods, apiTypes, changeShippingLineWithCode} from 'src';
import {applicationStateMock, selectShippingLineMock} from 'src/variables/mocks';
import * as fetchAPI from 'src/utils/fetchAPI';
import * as getApiOptions from 'src/utils/getApiOptions';
import * as apiUrl from 'src/utils/apiUrl';
import * as apiResponse from 'src/utils/apiResponse';

describe('testing set shipping address api', () => {
const returnObject = {...baseReturnObject};
const timesWhenCalled = 1;
const index = '1';
const code = 'code';
const apiUrlMock = 'https://api.com/checkout/storefront/123/123/addresses/shipping';
const {keysToTest} = apiTypes.changeShippingLines;
let optionsMock: RequestInit;
let getApiOptionsSpy: jest.SpyInstance;
let getApiUrlSpy: jest.SpyInstance;
let fetchApiSpy: jest.SpyInstance;
let checkApiResponseSpy: jest.SpyInstance;

beforeEach(() => {
global.Headers = jest.fn().mockReturnValue({
append: jest.fn(() => null)
});
optionsMock = {method: methods.POST, headers: new Headers(), body: JSON.stringify({})};
getApiOptionsSpy = jest.spyOn(getApiOptions, 'getApiOptions').mockReturnValue(optionsMock);
getApiUrlSpy = jest.spyOn(apiUrl, 'getApiUrl').mockReturnValue(apiUrlMock);
fetchApiSpy = jest.spyOn(fetchAPI, 'fetchAPI').mockReturnValue(Promise.resolve(returnObject));
checkApiResponseSpy = jest.spyOn(apiResponse, 'checkApiResponse').mockReturnValue(returnObject);
returnObject.response = { data: { selected_shipping: selectShippingLineMock, application_state: applicationStateMock }};
returnObject.success = true;
});

afterEach(() => {
jest.restoreAllMocks();
});

test('calling shippingLines', async () => {
const res = await changeShippingLineWithCode(index, code);

expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines, {index, code});
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines);
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 0);
expect(checkApiResponseSpy).toHaveBeenCalledWith(returnObject, keysToTest);
expect(res).toStrictEqual(returnObject);
});

test('calling shippingLines w/ success = false', async () => {
const tempReturnObject = {...baseReturnObject};

fetchApiSpy.mockReturnValueOnce(Promise.resolve(tempReturnObject));
checkApiResponseSpy.mockReturnValueOnce(tempReturnObject);

const res = await changeShippingLineWithCode(index, code, 1);

expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines, {index, code});
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines);
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 1);
expect(checkApiResponseSpy).toHaveBeenCalledWith(tempReturnObject, keysToTest);
expect(res).toStrictEqual(tempReturnObject);
});

});

0 comments on commit d37f669

Please sign in to comment.