Skip to content

Commit af14adf

Browse files
Add estimated shipping and taxes endpoint (#16)
* adding estimated shipping and taxes endpoint * Adding rsa enabled info to initial data --------- Co-authored-by: shagufa-ali <80478231+shagufa-ali@users.noreply.github.com>
1 parent 76ebcc6 commit af14adf

File tree

10 files changed

+207
-2
lines changed

10 files changed

+207
-2
lines changed

src/shipping/estimateShippingLines.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {
2+
apiTypeKeys,
3+
apiTypes,
4+
fetchAPI,
5+
getApiOptions,
6+
getApiUrl,
7+
IApiReturnObject,
8+
checkApiResponse,
9+
IEstimateShippingLinesRequest
10+
} from 'src';
11+
12+
/**
13+
*
14+
* Retrieve the estimated shipping lines
15+
*
16+
*/
17+
export async function estimateShippingLines(requestBody: IEstimateShippingLinesRequest, numOfRetries = 0): Promise<IApiReturnObject> {
18+
const {estimateShippingLines} = apiTypeKeys;
19+
const url = getApiUrl(estimateShippingLines);
20+
const options = getApiOptions(estimateShippingLines, requestBody);
21+
const fetchRes = await fetchAPI(url, options, numOfRetries);
22+
const {keysToTest} = apiTypes.estimateShippingLines;
23+
return checkApiResponse(fetchRes, keysToTest);
24+
}

src/shipping/index.ts

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

src/taxes/estimateTaxes.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
apiTypeKeys,
3+
fetchAPI,
4+
getApiUrl,
5+
getApiOptions,
6+
IApiReturnObject,
7+
checkApiResponse,
8+
apiTypes,
9+
IEstimateTaxRequest,
10+
} from 'src';
11+
12+
/** estimateTaxes
13+
*
14+
* Get the estimated taxes on the order
15+
*/
16+
export async function estimateTaxes(requestBody: IEstimateTaxRequest, numOfRetries = 0): Promise<IApiReturnObject> {
17+
const {estimateTaxes} = apiTypeKeys;
18+
const options = getApiOptions(estimateTaxes, requestBody);
19+
const url = getApiUrl(estimateTaxes);
20+
const fetchRes = await fetchAPI(url, options, numOfRetries);
21+
const {keysToTest} = apiTypes.estimateTaxes;
22+
return checkApiResponse(fetchRes, keysToTest);
23+
}

src/taxes/index.ts

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

src/types/apiInterfaces.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ export interface IApiTypes {
295295
walletPayCreateOrder: IApiTypesDetail,
296296
walletPayOnShipping: IApiTypesDetail,
297297
walletPayOnApprove: IApiTypesDetail,
298+
estimateShippingLines: IApiTypesDetail,
299+
estimateTaxes: IApiTypesDetail,
298300
}
299301

300302
export interface IApiTypeKeys {
@@ -334,6 +336,8 @@ export interface IApiTypeKeys {
334336
walletPayCreateOrder: keyof IApiTypes;
335337
walletPayOnShipping: keyof IApiTypes;
336338
walletPayOnApprove: keyof IApiTypes;
339+
estimateShippingLines: keyof IApiTypes;
340+
estimateTaxes: keyof IApiTypes;
337341
}
338342

339343
export interface IValidateAddress {
@@ -427,6 +431,7 @@ export interface ICheckoutProcess{
427431
tax_exempt_checkbox_enabled?: boolean,
428432
tax_shipping?: boolean,
429433
batch_requests?: boolean,
434+
rsa_enabled?: boolean,
430435
}
431436

432437
export interface IAddressAutoComplete{
@@ -812,6 +817,9 @@ export type IUpdatePaymentRequest = IAddPaymentRequest;
812817

813818
export type IDeletePaymentRequest = IAddPaymentRequest;
814819

820+
export type IEstimateTaxRequest = IAddress;
821+
export type IEstimateShippingLinesRequest = IAddress;
822+
815823
export interface IPatchOrderMetaDataRequest {
816824
cart_parameters: ICartParameters | null;
817825
note_attributes: ICartParameters | null;
@@ -839,6 +847,8 @@ export type IGetApiOptionsBody =
839847
IWalletPayOnApproveRequest |
840848
IWalletPayCreateOrderRequest |
841849
IWalletPayOnShippingRequest |
850+
IEstimateTaxRequest |
851+
IEstimateShippingLinesRequest |
842852
Record<string, unknown>;
843853

844854
export interface IShippingLine {

src/variables/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ export const apiTypes: IApiTypes = {
316316
useJwt: true,
317317
keysToTest: [...appStateKeysToTest]
318318
},
319+
estimateTaxes: {
320+
path: '/taxes/estimate',
321+
method: methods.POST,
322+
useJwt: true,
323+
keysToTest: [...appStateKeysToTest]
324+
},
325+
estimateShippingLines: {
326+
path: '/shipping_lines/estimate',
327+
method: methods.POST,
328+
useJwt: true,
329+
keysToTest: [...appStateKeysToTest]
330+
},
319331
};
320332

321333
export const apiTypeKeys: IApiTypeKeys = {
@@ -355,6 +367,8 @@ export const apiTypeKeys: IApiTypeKeys = {
355367
walletPayCreateOrder: 'walletPayCreateOrder',
356368
walletPayOnShipping: 'walletPayOnShipping',
357369
walletPayOnApprove: 'walletPayOnApprove',
370+
estimateShippingLines: 'estimateShippingLines',
371+
estimateTaxes: 'estimateTaxes',
358372
};
359373

360374
export const baseReturnObject: IApiReturnObject = {

src/variables/mocks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ export const orderInitialDataMock: IOrderInitialData = {
334334
checkout_process: {
335335
company_name_option: 'required',
336336
phone_number_required: false,
337-
accepts_marketing_checkbox_option: 'checked'
337+
accepts_marketing_checkbox_option: 'checked',
338+
rsa_enabled: false,
338339
},
339340
address_autocomplete: {
340341
provider: null,

src/variables/variables.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ export const generalSettings: IGeneralSettings = {
147147
accepts_marketing_checkbox_option: 'checked',
148148
tax_exempt_checkbox_enabled: undefined,
149149
tax_shipping: true,
150-
batch_requests: false
150+
batch_requests: false,
151+
rsa_enabled: false
151152
},
152153
address_autocomplete: {
153154
provider: null,
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {apiTypeKeys, baseReturnObject, methods, apiTypes, estimateShippingLines} from 'src';
2+
import {applicationStateMock, selectShippingLineMock, shippingAddressMock} 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 estimate shipping lines api', () => {
9+
const returnObject = {...baseReturnObject};
10+
const timesWhenCalled = 1;
11+
const apiUrlMock = 'https://api.com/checkout/storefront/123/123/addresses/shipping/estimate';
12+
const {keysToTest} = apiTypes.estimateShippingLines;
13+
let optionsMock: RequestInit;
14+
let getApiOptionsSpy: jest.SpyInstance;
15+
let getApiUrlSpy: jest.SpyInstance;
16+
let fetchApiSpy: jest.SpyInstance;
17+
let checkApiResponseSpy: jest.SpyInstance;
18+
19+
beforeEach(() => {
20+
global.Headers = jest.fn().mockReturnValue({
21+
append: jest.fn(() => null)
22+
});
23+
optionsMock = {method: methods.POST, headers: new Headers(), body: JSON.stringify({})};
24+
getApiOptionsSpy = jest.spyOn(getApiOptions, 'getApiOptions').mockReturnValue(optionsMock);
25+
getApiUrlSpy = jest.spyOn(apiUrl, 'getApiUrl').mockReturnValue(apiUrlMock);
26+
fetchApiSpy = jest.spyOn(fetchAPI, 'fetchAPI').mockReturnValue(Promise.resolve(returnObject));
27+
checkApiResponseSpy = jest.spyOn(apiResponse, 'checkApiResponse').mockReturnValue(returnObject);
28+
returnObject.response = { data: { selected_shipping: selectShippingLineMock, application_state: applicationStateMock }};
29+
returnObject.success = true;
30+
});
31+
32+
afterEach(() => {
33+
jest.restoreAllMocks();
34+
});
35+
36+
test('calling shippingLines', async () => {
37+
const res = await estimateShippingLines(shippingAddressMock);
38+
39+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
40+
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
41+
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
42+
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
43+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.estimateShippingLines, shippingAddressMock);
44+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.estimateShippingLines);
45+
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 0);
46+
expect(checkApiResponseSpy).toHaveBeenCalledWith(returnObject, keysToTest);
47+
expect(res).toStrictEqual(returnObject);
48+
});
49+
50+
test('calling shippingLines w/ success = false', async () => {
51+
const tempReturnObject = {...baseReturnObject};
52+
53+
fetchApiSpy.mockReturnValueOnce(Promise.resolve(tempReturnObject));
54+
checkApiResponseSpy.mockReturnValueOnce(tempReturnObject);
55+
56+
const res = await estimateShippingLines(shippingAddressMock, 1);
57+
58+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
59+
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
60+
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
61+
expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled);
62+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.estimateShippingLines, shippingAddressMock);
63+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.estimateShippingLines);
64+
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 1);
65+
expect(checkApiResponseSpy).toHaveBeenCalledWith(tempReturnObject, keysToTest);
66+
expect(res).toStrictEqual(tempReturnObject);
67+
});
68+
69+
});

tests/taxes/estimateTaxes.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {apiTypeKeys, baseReturnObject, estimateTaxes, methods} 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 {applicationStateMock, shippingAddressMock, taxesArrayMock} from 'src/variables/mocks';
6+
import * as apiResponse from 'src/utils/apiResponse';
7+
8+
describe('testing estimate taxes api', () => {
9+
const returnObject = {...baseReturnObject};
10+
const timesWhenCalled = 1;
11+
const apiUrlMock = 'https://api.com/checkout/storefront/123/123/taxes/estimate';
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+
optionsMock = {method: methods.POST, headers: new Headers(), body: JSON.stringify({})};
21+
getApiOptionsSpy = jest.spyOn(getApiOptions, 'getApiOptions').mockReturnValue(optionsMock);
22+
getApiUrlSpy = jest.spyOn(apiUrl, 'getApiUrl').mockReturnValue(apiUrlMock);
23+
fetchApiSpy = jest.spyOn(fetchAPI, 'fetchAPI').mockReturnValue(Promise.resolve(returnObject));
24+
checkApiResponseSpy = jest.spyOn(apiResponse, 'checkApiResponse').mockReturnValue(returnObject);
25+
returnObject.response = { data: { taxes: taxesArrayMock, application_state: applicationStateMock }};
26+
returnObject.success = true;
27+
});
28+
29+
afterEach(() => {
30+
jest.restoreAllMocks();
31+
});
32+
33+
test('calling setTaxes', async () => {
34+
const res = await estimateTaxes(shippingAddressMock);
35+
36+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
37+
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
38+
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
39+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.estimateTaxes, shippingAddressMock);
40+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.estimateTaxes);
41+
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 0);
42+
expect(res).toStrictEqual(returnObject);
43+
});
44+
45+
test('calling setTaxes w/ success = false', async () => {
46+
const tempReturnObject = {...baseReturnObject};
47+
checkApiResponseSpy.mockReturnValueOnce(tempReturnObject);
48+
fetchApiSpy.mockReturnValueOnce(Promise.resolve(tempReturnObject));
49+
50+
const res = await estimateTaxes(shippingAddressMock, 1);
51+
52+
expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled);
53+
expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled);
54+
expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled);
55+
expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.estimateTaxes, shippingAddressMock);
56+
expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.estimateTaxes);
57+
expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 1);
58+
expect(res).toStrictEqual(tempReturnObject);
59+
});
60+
});
61+

0 commit comments

Comments
 (0)