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

CHK-3367: fix shipping line fee #28

Merged
merged 1 commit into from
Aug 12, 2024
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
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
9 changes: 6 additions & 3 deletions src/variables/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ 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 +285,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
87 changes: 87 additions & 0 deletions tests/shipping/changeShippingLineWithCode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {apiTypeKeys, baseReturnObject, methods, apiTypes, changeShippingLineWithCode, changeShippingLine} from 'src';

Check warning on line 1 in tests/shipping/changeShippingLineWithCode.test.ts

View workflow job for this annotation

GitHub Actions / Yarn Build

'changeShippingLine' is defined but never used
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 code1 = '';

Check warning on line 13 in tests/shipping/changeShippingLineWithCode.test.ts

View workflow job for this annotation

GitHub Actions / Yarn Build

'code1' is assigned a value but never used
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);
});

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

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);
});

});
Loading