Skip to content

Commit

Permalink
Disable revalidate on CHAIN_API actions
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlin committed Feb 17, 2021
1 parent 598d554 commit b9db070
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
16 changes: 15 additions & 1 deletion spec/createRequestPromise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ describe('createRequestPromise', () => {
jest.clearAllMocks()
})

function createRequest({ revalidate } = {}){
function createRequest({ revalidate, revalidateDisabled } = {}){
extractParams = jest.fn().mockReturnValue({ ...mockParams, revalidate})
createRequestPromise({
revalidateDisabled,
timeout,
generateDefaultParams,
createCallApiAction,
Expand Down Expand Up @@ -247,6 +248,19 @@ describe('createRequestPromise', () => {
expect(axios).not.toHaveBeenCalled()
})

it('always send request if revalidateDisabled = true', async () => {
const revalidateDisabled = true
const revalidate = 'never'
await createRequest({ revalidate, revalidateDisabled })
expect(axios).toHaveBeenCalled()

jest.clearAllMocks()
MockDate.set(currentime + (6 * 1000))

await createRequest({ revalidate })
expect(axios).toHaveBeenCalled()
})

it('always send request if window does not exist', async () => {
utils.window = null
const revalidate = 'never'
Expand Down
34 changes: 34 additions & 0 deletions spec/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import createApiMiddleware, {
CHAIN_API
} from '../src'

const createRequestPromise = require('../src/createRequestPromise')

import * as utils from '../src/utils'

jest.mock('../src/log', () => ({
Expand Down Expand Up @@ -527,4 +529,36 @@ describe('Middleware::Api', () => {
expect(dispatchedAction[CHAIN_API][0]()).toEqual(action)
})
})

describe('revalidateDisabled behavior', () => {
beforeEach(() => {
jest.spyOn(createRequestPromise, 'default').mockReturnValue(jest.fn())
jest.clearAllMocks()
})
it('calls createRequestPromise with revalidateDisabled = true if action.isNativeCallApi = false', async () => {
const action = {
isNativeCallApi: false,
[CHAIN_API]: [
() => ({ [CALL_API]: {} })
]
}
await apiMiddleware({ dispatch, getState })(next)(action)
expect(createRequestPromise.default.mock.calls[0][0].revalidateDisabled).toBe(true)
})
it('calls createRequestPromise with revalidateDisabled = false if action.isNativeCallApi = true', async () => {
const action = {
isNativeCallApi: true,
[CHAIN_API]: [
() => ({ [CALL_API]: {} })
]
}
await apiMiddleware({ dispatch, getState })(next)(action)
expect(createRequestPromise.default.mock.calls[0][0].revalidateDisabled).toBe(false)
})
it('dispatches CHAIN_API with isNativeCallApi = true if action type is CALL_API', async () => {
const action = { [CALL_API]: {} }
await apiMiddleware({ dispatch, getState })(next)(action)
expect(dispatch.mock.calls[0][0]).toMatchObject({ isNativeCallApi: true })
})
})
})
5 changes: 3 additions & 2 deletions src/createRequestPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ export default function ({
dispatch,
errorInterceptor,
extractParams,
maxReplayTimes
maxReplayTimes,
revalidateDisabled = false
}) {
return (prevBody) => {
const apiAction = createCallApiAction(prevBody)
const params = extractParams(apiAction[CALL_API])
let replayTimes = 0

const now = Math.floor(new Date().getTime() / 1000)
if (!!params.revalidate && !!window) {
if (!!params.revalidate && !!window && !revalidateDisabled) {
const revalidationKey = _getRevalidationKey(params)
const lastRevalidateTime = lastRevalidateTimeMap[revalidationKey] || 0
if (params.revalidate === 'never' && !!lastRevalidateTime) {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default ({
return ({ dispatch, getState }) => next => action => {
if (action[CALL_API]) {
return dispatch({
isNativeCallApi: true,
[CHAIN_API]: [
() => action
]
Expand All @@ -38,8 +39,8 @@ export default ({

return new Promise((resolve, reject) => {
const promiseCreators = action[CHAIN_API].map((createCallApiAction) => {

return createRequestPromise({
revalidateDisabled: !action.isNativeCallApi,
timeout,
generateDefaultParams,
createCallApiAction,
Expand Down

0 comments on commit b9db070

Please sign in to comment.