Skip to content

Commit

Permalink
Merge branch 'feature/forward-axios-config-in-error-object'
Browse files Browse the repository at this point in the history
  • Loading branch information
caaatisgood committed Mar 22, 2021
2 parents ea6ac28 + 9e8c3ac commit ea3efbc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
40 changes: 23 additions & 17 deletions spec/createRequestPromise.test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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: {
Expand Down Expand Up @@ -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,
Expand All @@ -179,22 +183,24 @@ 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)
})
})
})
})

describe('revalidate behavior', () => {
const currentime = 1579508700000
let path, testSetCount = 0
let path; let testSetCount = 0

beforeEach(() => {
testSetCount++
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions spec/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}))
Expand Down Expand Up @@ -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)
})
Expand Down
7 changes: 4 additions & 3 deletions src/createRequestPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
})
}
1 change: 0 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,3 @@ export function paramsExtractor ({ baseUrl }) {
const _window = typeof window === 'undefined' ? null : window

export { _window as window }

0 comments on commit ea3efbc

Please sign in to comment.