-
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.
- Loading branch information
Showing
6 changed files
with
102 additions
and
4 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,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); | ||
} |
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,3 +1,4 @@ | ||
export * from './getShippingLines'; | ||
export * from './estimateShippingLines'; | ||
export * from './changeShippingLine'; | ||
export * from './changeShippingLineWithCode'; |
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
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,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); | ||
}); | ||
|
||
}); |