fix: lingering error (#908)

This commit is contained in:
Simone Busoli 2022-05-31 12:23:29 +02:00 committed by GitHub
parent b48caf1270
commit 695cc84d1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 5 deletions

3
.eslintignore Normal file
View File

@ -0,0 +1,3 @@
cjs
coverage
es

View File

@ -1,6 +1,5 @@
{
"root": true,
"parser": "@babel/eslint-parser",
"extends": [
"eslint:recommended",
"plugin:import/recommended",
@ -8,7 +7,7 @@
"plugin:react-hooks/recommended"
],
"parserOptions": {
"ecmaVersion": 2018
"ecmaVersion": "latest"
},
"env": {
"browser": true,

View File

@ -26,7 +26,7 @@
"build": "run-p build:*",
"clean": "rimraf cjs es",
"format": "prettier --write \"{src,test}/**/*.{js?(x),md,ts?(x)}\"",
"lint": "eslint src test",
"lint": "eslint . --ext .js,.jsx",
"prepare": "npm run clean && npm run build && husky install",
"release": "standard-version",
"pretest": "cp ./test/index.test.jsx ./test/index.test.tsx && cp ./test/index.test.ssr.jsx ./test/index.test.ssr.tsx",
@ -44,7 +44,6 @@
"devDependencies": {
"@babel/cli": "7.17.10",
"@babel/core": "7.18.2",
"@babel/eslint-parser": "7.18.2",
"@babel/plugin-transform-runtime": "7.18.2",
"@babel/preset-env": "7.18.2",
"@babel/preset-react": "7.17.12",

View File

@ -151,7 +151,9 @@ export function makeUseAxios(configureOptions) {
return {
...state,
loading: false,
...(action.error ? {} : { data: action.payload.data }),
// set data and error
...(action.error ? {} : { data: action.payload.data, error: null }),
// set raw response or error
[action.error ? 'error' : 'response']: action.payload
}
}

View File

@ -1023,6 +1023,32 @@ function standardTests(
expect(axios).toHaveBeenCalledTimes(1)
})
it('should reset error when one successful request follows a failing request', async () => {
axios.mockResolvedValueOnce({ data: 'working 1' })
const { result, waitForNextUpdate, rerender } = setup('working')
await waitForNextUpdate()
const error = new Error('boom')
axios.mockRejectedValueOnce(error)
rerender({ config: 'boom', options: {} })
await waitForNextUpdate()
expect(result.current[0].error).toBe(error)
axios.mockResolvedValueOnce({ data: 'working 2' })
rerender({ config: 'working', options: {} })
expect(result.current[0].error).toBe(null)
// because it's coming from cache
expect(result.current[0].data).toBe('working 1')
})
})
describe('configure', () => {