diff --git a/package.json b/package.json index a5f7054..0992316 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "redux-api-middleman", - "version": "3.2.2", + "version": "3.2.2-canary.0", "description": "A Redux middleware making sending request a breeze", "main": "lib/index.js", "jest": { @@ -18,7 +18,7 @@ } ], "scripts": { - "test": "jest", + "test": "jest --watch --verbose false", "test:ci": "yarn lint && jest", "lint": "standard --fix --verbose | snazzy", "size": "size-limit", diff --git a/spec/createRequestPromise.test.js b/spec/createRequestPromise.test.js index 63b7395..42a5f45 100644 --- a/spec/createRequestPromise.test.js +++ b/spec/createRequestPromise.test.js @@ -1,5 +1,5 @@ import Promise from 'es6-promise' -import { CALL_API, CHAIN_API } from '../src' +import { CALL_API } from '../src' import createRequestPromise from '../src/createRequestPromise' import axios from 'axios' import MockDate from 'mockdate' @@ -8,16 +8,17 @@ import * as utils from '../src/utils' jest.mock('axios') jest.mock('../src/log') -const getMockAxiosPromise = ({ error } = {}) => { +const getMockAxiosPromise = ({ error, config } = {}) => { return new Promise((resolve, reject) => { if (error) { - process.nextTick(() => reject(new Error({ - response: { - data: { - key_1: 'val_1' - } + const _error = new Error('Mock error') + _error.config = config + _error.response = { + data: { + key_1: 'val_1' } - }))) + } + process.nextTick(() => reject(_error)) } else { process.nextTick(() => resolve({ data: { @@ -160,14 +161,17 @@ describe('createRequestPromise', () => { }) describe('when axios catches error', () => { + let config beforeEach(() => { - axios.mockReturnValue(getMockAxiosPromise({ error: true })) + config = { key: 'value' } + axios.mockReturnValue(getMockAxiosPromise({ error: true, config })) }) - it('should call errorInterceptor', () => { + + it('should call errorInterceptor', async () => { const errorInterceptor = jest.fn(({ proceedError }) => { proceedError() }) - createRequestPromise({ + await createRequestPromise({ timeout, generateDefaultParams, createCallApiAction, @@ -179,14 +183,16 @@ describe('createRequestPromise', () => { })(mockPrevBody) .catch(() => { expect(errorInterceptor).toHaveBeenCalledTimes(1) - expect(errorInterceptor.mock.calls[0][0]).toMatchObject({ + expect(errorInterceptor.mock.calls[0][0]).toEqual({ err: { + config, data: { key1: 'val_1' } }, - dispatch, - getState + getState, + proceedError: expect.any(Function), + replay: expect.any(Function) }) }) }) @@ -194,7 +200,7 @@ describe('createRequestPromise', () => { describe('revalidate behavior', () => { const currentime = 1579508700000 - let path, testSetCount = 0 + let path; let testSetCount = 0 beforeEach(() => { testSetCount++ @@ -210,8 +216,8 @@ describe('createRequestPromise', () => { jest.clearAllMocks() }) - function createRequest({ revalidate, revalidateDisabled } = {}){ - extractParams = jest.fn().mockReturnValue({ ...mockParams, revalidate}) + function createRequest ({ revalidate, revalidateDisabled } = {}) { + extractParams = jest.fn().mockReturnValue({ ...mockParams, revalidate }) createRequestPromise({ revalidateDisabled, timeout, diff --git a/spec/index.test.js b/spec/index.test.js index 65a30a1..a2d1a7c 100644 --- a/spec/index.test.js +++ b/spec/index.test.js @@ -7,10 +7,10 @@ import createApiMiddleware, { CHAIN_API } from '../src' -const createRequestPromise = require('../src/createRequestPromise') - import * as utils from '../src/utils' +const createRequestPromise = require('../src/createRequestPromise') + jest.mock('../src/log', () => ({ error: jest.fn() })) @@ -558,7 +558,7 @@ describe('Middleware::Api', () => { it('dispatches CHAIN_API with revalidateDisabled = true if action type is CALL_API', async () => { const action = { [CHAIN_API]: [ () => ({ [CALL_API]: {} }) - ]} + ] } await apiMiddleware({ dispatch, getState })(next)(action) expect(createRequestPromise.default.mock.calls[0][0].revalidateDisabled).toBe(true) }) diff --git a/src/createRequestPromise.js b/src/createRequestPromise.js index a1d8e46..ff6fbb4 100644 --- a/src/createRequestPromise.js +++ b/src/createRequestPromise.js @@ -117,6 +117,7 @@ export default function ({ function prepareErrorPayload ({ error, camelize }) { const res = error.response || {} + res.config = error.config if (camelize) { res.data = camelizeKeys(res.data) } @@ -164,19 +165,19 @@ export default function ({ } } -function _getRevalidationKey(actionObj) { +function _getRevalidationKey (actionObj) { const { method, path, url, params, - data, + data } = actionObj return JSON.stringify({ method, path, url, params, - data, + data }) } diff --git a/src/utils.js b/src/utils.js index 680aa64..9d0660a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -70,4 +70,3 @@ export function paramsExtractor ({ baseUrl }) { const _window = typeof window === 'undefined' ? null : window export { _window as window } -