Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: async refetch #52

Merged
merged 1 commit into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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