|
| 1 | +import {apiTypeKeys, baseReturnObject, methods, apiTypes, changeShippingLineWithCode, changeShippingLine} from 'src'; |
| 2 | +import {applicationStateMock, selectShippingLineMock} 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 set shipping address api', () => { |
| 9 | + const returnObject = {...baseReturnObject}; |
| 10 | + const timesWhenCalled = 1; |
| 11 | + const index = '1'; |
| 12 | + const code = 'code'; |
| 13 | + const code1 = ''; |
| 14 | + const apiUrlMock = 'https://api.com/checkout/storefront/123/123/addresses/shipping'; |
| 15 | + const {keysToTest} = apiTypes.changeShippingLines; |
| 16 | + let optionsMock: RequestInit; |
| 17 | + let getApiOptionsSpy: jest.SpyInstance; |
| 18 | + let getApiUrlSpy: jest.SpyInstance; |
| 19 | + let fetchApiSpy: jest.SpyInstance; |
| 20 | + let checkApiResponseSpy: jest.SpyInstance; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + global.Headers = jest.fn().mockReturnValue({ |
| 24 | + append: jest.fn(() => null) |
| 25 | + }); |
| 26 | + optionsMock = {method: methods.POST, headers: new Headers(), body: JSON.stringify({})}; |
| 27 | + getApiOptionsSpy = jest.spyOn(getApiOptions, 'getApiOptions').mockReturnValue(optionsMock); |
| 28 | + getApiUrlSpy = jest.spyOn(apiUrl, 'getApiUrl').mockReturnValue(apiUrlMock); |
| 29 | + fetchApiSpy = jest.spyOn(fetchAPI, 'fetchAPI').mockReturnValue(Promise.resolve(returnObject)); |
| 30 | + checkApiResponseSpy = jest.spyOn(apiResponse, 'checkApiResponse').mockReturnValue(returnObject); |
| 31 | + returnObject.response = { data: { selected_shipping: selectShippingLineMock, application_state: applicationStateMock }}; |
| 32 | + returnObject.success = true; |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + jest.restoreAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + test('calling shippingLines', async () => { |
| 40 | + const res = await changeShippingLineWithCode(index, code); |
| 41 | + |
| 42 | + expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 43 | + expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 44 | + expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 45 | + expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 46 | + expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines, {index, code}); |
| 47 | + expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines); |
| 48 | + expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 0); |
| 49 | + expect(checkApiResponseSpy).toHaveBeenCalledWith(returnObject, keysToTest); |
| 50 | + expect(res).toStrictEqual(returnObject); |
| 51 | + }); |
| 52 | + |
| 53 | + test('calling shippingLines w/ success = false', async () => { |
| 54 | + const tempReturnObject = {...baseReturnObject}; |
| 55 | + |
| 56 | + fetchApiSpy.mockReturnValueOnce(Promise.resolve(tempReturnObject)); |
| 57 | + checkApiResponseSpy.mockReturnValueOnce(tempReturnObject); |
| 58 | + |
| 59 | + const res = await changeShippingLineWithCode(index, code, 1); |
| 60 | + |
| 61 | + expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 62 | + expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 63 | + expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 64 | + expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 65 | + expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines, {index, code}); |
| 66 | + expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines); |
| 67 | + expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 1); |
| 68 | + expect(checkApiResponseSpy).toHaveBeenCalledWith(tempReturnObject, keysToTest); |
| 69 | + expect(res).toStrictEqual(tempReturnObject); |
| 70 | + }); |
| 71 | + |
| 72 | + test('calling shippingLines default code', async () => { |
| 73 | + const code = ''; |
| 74 | + const res = await changeShippingLineWithCode(index); |
| 75 | + |
| 76 | + expect(getApiOptionsSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 77 | + expect(getApiUrlSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 78 | + expect(fetchApiSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 79 | + expect(checkApiResponseSpy).toHaveBeenCalledTimes(timesWhenCalled); |
| 80 | + expect(getApiOptionsSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines, {index, code}); |
| 81 | + expect(getApiUrlSpy).toHaveBeenCalledWith(apiTypeKeys.changeShippingLines); |
| 82 | + expect(fetchApiSpy).toHaveBeenCalledWith(apiUrlMock, optionsMock, 0); |
| 83 | + expect(checkApiResponseSpy).toHaveBeenCalledWith(returnObject, keysToTest); |
| 84 | + expect(res).toStrictEqual(returnObject); |
| 85 | + }); |
| 86 | + |
| 87 | +}); |
0 commit comments