Skip to content

Commit

Permalink
feat: async refetch
Browse files Browse the repository at this point in the history
Fixes #51
  • Loading branch information
simoneb committed Oct 23, 2019
1 parent 3c564f4 commit f018d0a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function useAxios<T = any>(
options?: Options
): [
ResponseValues<T>,
(config?: AxiosRequestConfig, options?: RefetchOptions) => void
(config?: AxiosRequestConfig, options?: RefetchOptions) => AxiosPromise
]

export function loadCache(data: any[]): void
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@ async function request(config, dispatch) {
dispatch({ type: actions.REQUEST_START })
const response = await axiosInstance(config)
dispatch({ type: actions.REQUEST_END, payload: response })
return response
} catch (err) {
dispatch({ type: actions.REQUEST_END, payload: err, error: true })
throw err
}
}

function executeRequestWithCache(config, dispatch) {
request({ ...config, adapter: cacheAdapter }, dispatch)
return request({ ...config, adapter: cacheAdapter }, dispatch)
}

function executeRequestWithoutCache(config, dispatch) {
Expand Down Expand Up @@ -138,7 +140,7 @@ export default function useAxios(config, options) {

React.useEffect(() => {
if (!options.manual) {
executeRequest(config, options, dispatch)
executeRequest(config, options, dispatch).catch(() => {})
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [stringifiedConfig])
Expand Down
87 changes: 79 additions & 8 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,25 @@ it('should set the response', async () => {
})

it('should reset error when request completes and returns data', async () => {
axios.mockResolvedValueOnce({ data: 'whatever' })
const error = new Error('boom')

const { result, waitForNextUpdate } = renderHook(() => useAxios(''))
axios.mockRejectedValue(error)

result.current[0].error = {
isAxiosError: true,
config: {},
name: '',
message: ''
}
const { result, waitForNextUpdate } = renderHook(() => useAxios(''))

await waitForNextUpdate()

expect(result.current[0].error).toBe(error)

axios.mockResolvedValue({ data: 'whatever' })

// Refetch
act(() => {
result.current[1]()
})

await waitForNextUpdate()

expect(result.current[0].error).toBe(null)
})

Expand Down Expand Up @@ -85,6 +86,76 @@ it('should refetch', async () => {
expect(axios).toHaveBeenCalledTimes(2)
})

describe('refetch', () => {
describe('when axios resolves', () => {
it('should resolve to the response by default', () => {
const response = { data: 'whatever' }

axios.mockResolvedValue(response)

const {
result: {
current: [, refetch]
}
} = renderHook(() => useAxios(''))

act(() => {
expect(refetch()).resolves.toEqual(response)
})
})

it('should resolve to the response when using cache', () => {
const response = { data: 'whatever' }

axios.mockResolvedValue(response)

const {
result: {
current: [, refetch]
}
} = renderHook(() => useAxios(''))

act(() => {
expect(refetch({}, { useCache: true })).resolves.toEqual(response)
})
})
})

describe('when axios rejects', () => {
it('should reject with the error by default', () => {
const error = new Error('boom')

axios.mockRejectedValue(error)

const {
result: {
current: [, refetch]
}
} = renderHook(() => useAxios(''))

act(() => {
expect(refetch()).rejects.toEqual(error)
})
})

it('should reject with the error by when using cache', () => {
const error = new Error('boom')

axios.mockRejectedValue(error)

const {
result: {
current: [, refetch]
}
} = renderHook(() => useAxios(''))

act(() => {
expect(refetch({}, { useCache: true })).rejects.toEqual(error)
})
})
})
})

it('should return the same reference to the fetch function', async () => {
axios.mockResolvedValue({ data: 'whatever' })

Expand Down

0 comments on commit f018d0a

Please sign in to comment.