Skip to content

Commit

Permalink
adding estimated shipping and taxes endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
shagufa-ali committed Dec 6, 2023
1 parent 76ebcc6 commit 6b76db4
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/shipping/estimateShippingLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
apiTypeKeys,
apiTypes,
fetchAPI,
getApiOptions,
getApiUrl,
IApiReturnObject,
checkApiResponse,
IEstimateShippingLinesRequest
} from 'src';

/**
*
* Retrieve the estimated shipping lines
*
*/
export async function estimateShippingLines(requestBody: IEstimateShippingLinesRequest, numOfRetries = 0): Promise<IApiReturnObject> {
const {estimateShippingLines} = apiTypeKeys;
const url = getApiUrl(estimateShippingLines);
const options = getApiOptions(estimateShippingLines, requestBody);
const fetchRes = await fetchAPI(url, options, numOfRetries);
const {keysToTest} = apiTypes.estimateShippingLines;
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,2 +1,3 @@
export * from './getShippingLines';
export * from './estimateShippingLines';
export * from './changeShippingLine';
23 changes: 23 additions & 0 deletions src/taxes/estimateTaxes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
apiTypeKeys,
fetchAPI,
getApiUrl,
getApiOptions,
IApiReturnObject,
checkApiResponse,
apiTypes,
IEstimateTaxRequest,
} from 'src';

/** estimateTaxes
*
* Get the estimated taxes on the order
*/
export async function estimateTaxes(requestBody: IEstimateTaxRequest, numOfRetries = 0): Promise<IApiReturnObject> {
const {estimateTaxes} = apiTypeKeys;
const options = getApiOptions(estimateTaxes, requestBody);
const url = getApiUrl(estimateTaxes);
const fetchRes = await fetchAPI(url, options, numOfRetries);
const {keysToTest} = apiTypes.estimateTaxes;
return checkApiResponse(fetchRes, keysToTest);
}
1 change: 1 addition & 0 deletions src/taxes/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './estimateTaxes';
export * from './setTaxes';
9 changes: 9 additions & 0 deletions src/types/apiInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ export interface IApiTypes {
walletPayCreateOrder: IApiTypesDetail,
walletPayOnShipping: IApiTypesDetail,
walletPayOnApprove: IApiTypesDetail,
estimateShippingLines: IApiTypesDetail,
estimateTaxes: IApiTypesDetail,
}

export interface IApiTypeKeys {
Expand Down Expand Up @@ -334,6 +336,8 @@ export interface IApiTypeKeys {
walletPayCreateOrder: keyof IApiTypes;
walletPayOnShipping: keyof IApiTypes;
walletPayOnApprove: keyof IApiTypes;
estimateShippingLines: keyof IApiTypes;
estimateTaxes: keyof IApiTypes;
}

export interface IValidateAddress {
Expand Down Expand Up @@ -812,6 +816,9 @@ export type IUpdatePaymentRequest = IAddPaymentRequest;

export type IDeletePaymentRequest = IAddPaymentRequest;

export type IEstimateTaxRequest = IAddress;
export type IEstimateShippingLinesRequest = IAddress;

export interface IPatchOrderMetaDataRequest {
cart_parameters: ICartParameters | null;
note_attributes: ICartParameters | null;
Expand Down Expand Up @@ -839,6 +846,8 @@ export type IGetApiOptionsBody =
IWalletPayOnApproveRequest |
IWalletPayCreateOrderRequest |
IWalletPayOnShippingRequest |
IEstimateTaxRequest |
IEstimateShippingLinesRequest |
Record<string, unknown>;

export interface IShippingLine {
Expand Down
14 changes: 14 additions & 0 deletions src/variables/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ export const apiTypes: IApiTypes = {
useJwt: true,
keysToTest: [...appStateKeysToTest]
},
estimateTaxes: {
path: '/taxes/estimate',
method: methods.POST,
useJwt: true,
keysToTest: [...appStateKeysToTest]
},
estimateShippingLines: {
path: '/shipping_lines/estimate',
method: methods.POST,
useJwt: true,
keysToTest: [...appStateKeysToTest]
},
};

export const apiTypeKeys: IApiTypeKeys = {
Expand Down Expand Up @@ -355,6 +367,8 @@ export const apiTypeKeys: IApiTypeKeys = {
walletPayCreateOrder: 'walletPayCreateOrder',
walletPayOnShipping: 'walletPayOnShipping',
walletPayOnApprove: 'walletPayOnApprove',
estimateShippingLines: 'estimateShippingLines',
estimateTaxes: 'estimateTaxes',
};

export const baseReturnObject: IApiReturnObject = {
Expand Down
69 changes: 69 additions & 0 deletions tests/shipping/estimateShippingLine.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {apiTypeKeys, baseReturnObject, methods, apiTypes, estimateShippingLines} from 'src';
import {applicationStateMock, selectShippingLineMock, shippingAddressMock} 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 estimate shipping lines api', () => {
const returnObject = {...baseReturnObject};
const timesWhenCalled = 1;
const apiUrlMock = 'https://api.com/checkout/storefront/123/123/addresses/shipping/estimate';
const {keysToTest} = apiTypes.estimateShippingLines;
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 estimateShippingLines(shippingAddressMock);

expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.estimateShippingLines, shippingAddressMock);
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.estimateShippingLines);
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 estimateShippingLines(shippingAddressMock, 1);

expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.estimateShippingLines, shippingAddressMock);
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.estimateShippingLines);
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 1);
expect(checkApiResponseSpy).toHaveBeenCalledWith(tempReturnObject, keysToTest);
expect(res).toStrictEqual(tempReturnObject);
});

});
61 changes: 61 additions & 0 deletions tests/taxes/estimateTaxes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {apiTypeKeys, baseReturnObject, estimateTaxes, methods} from 'src';
import * as fetchAPI from 'src/utils/fetchAPI';
import * as getApiOptions from 'src/utils/getApiOptions';
import * as apiUrl from 'src/utils/apiUrl';
import {applicationStateMock, shippingAddressMock, taxesArrayMock} from 'src/variables/mocks';
import * as apiResponse from 'src/utils/apiResponse';

describe('testing estimate taxes api', () => {
const returnObject = {...baseReturnObject};
const timesWhenCalled = 1;
const apiUrlMock = 'https://api.com/checkout/storefront/123/123/taxes/estimate';
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()});
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: { taxes: taxesArrayMock, application_state: applicationStateMock }};
returnObject.success = true;
});

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

test('calling setTaxes', async () => {
const res = await estimateTaxes(shippingAddressMock);

expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.estimateTaxes, shippingAddressMock);
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.estimateTaxes);
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 0);
expect(res).toStrictEqual(returnObject);
});

test('calling setTaxes w/ success = false', async () => {
const tempReturnObject = {...baseReturnObject};
checkApiResponseSpy.mockReturnValueOnce(tempReturnObject);
fetchApiSpy.mockReturnValueOnce(Promise.resolve(tempReturnObject));

const res = await estimateTaxes(shippingAddressMock, 1);

expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.estimateTaxes, shippingAddressMock);
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.estimateTaxes);
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 1);
expect(res).toStrictEqual(tempReturnObject);
});
});

0 comments on commit 6b76db4

Please sign in to comment.