-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding estimated shipping and taxes endpoint
- Loading branch information
1 parent
b0377a2
commit c436196
Showing
8 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './getShippingLines'; | ||
export * from './estimateShippingLines'; | ||
export * from './changeShippingLine'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './estimateTaxes'; | ||
export * from './setTaxes'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
|