Skip to content

Commit

Permalink
Fix errors reported by unit tests (#1)
Browse files Browse the repository at this point in the history
Updated tests to remove React warnings about dismounted component updating themselves
  • Loading branch information
chris-peerman authored Nov 5, 2019
1 parent 2b8118c commit 4722d7a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions axios.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AxiosStatic } from "axios";

declare module "axios" {
interface AxiosStatic {
mockImplementation: Function;
mockResolvedValue: Function;
mockResolvedValueOnce: Function;
mockRejectedValue: Function;
Expand Down
29 changes: 17 additions & 12 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import useAxios, { configure, resetConfigure } from './'
jest.mock('axios')

it('should set loading to true', async () => {
axios.mockImplementation(() => new Promise(() => null))

const { result } = renderHook(() => useAxios(''))

expect(result.current[0].loading).toBe(true)
Expand Down Expand Up @@ -84,11 +86,13 @@ it('should refetch', async () => {

expect(result.current[0].loading).toBe(true)
expect(axios).toHaveBeenCalledTimes(2)

await waitForNextUpdate()
})

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

axios.mockResolvedValue(response)
Expand All @@ -99,12 +103,12 @@ describe('refetch', () => {
}
} = renderHook(() => useAxios(''))

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

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

axios.mockResolvedValue(response)
Expand All @@ -115,14 +119,14 @@ describe('refetch', () => {
}
} = renderHook(() => useAxios(''))

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

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

axios.mockRejectedValue(error)
Expand All @@ -133,12 +137,12 @@ describe('refetch', () => {
}
} = renderHook(() => useAxios(''))

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

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

axios.mockRejectedValue(error)
Expand All @@ -149,7 +153,7 @@ describe('refetch', () => {
}
} = renderHook(() => useAxios(''))

act(() => {
await act(async () => {
expect(refetch({}, { useCache: true })).rejects.toEqual(error)
})
})
Expand All @@ -159,17 +163,18 @@ describe('refetch', () => {
it('should return the same reference to the fetch function', async () => {
axios.mockResolvedValue({ data: 'whatever' })

const { result, rerender } = renderHook(() => useAxios(''))
const { result, rerender, waitForNextUpdate } = renderHook(() => useAxios(''))

const firstRefetch = result.current[1]

rerender()
await waitForNextUpdate()

expect(result.current[1]).toBe(firstRefetch)
})

describe('manual option', () => {
it('should set loading to false', async () => {
it('should set loading to false', () => {
const { result } = renderHook(() => useAxios('', { manual: true }))

expect(result.current[0].loading).toBe(false)
Expand Down Expand Up @@ -258,8 +263,8 @@ describe('useCache option', () => {
describe('configure', () => {
afterEach(() => resetConfigure())

it('should provide a custom implementation of axios', () => {
const mockAxios = jest.fn()
it('should provide a custom implementation of axios', async () => {
const mockAxios = jest.fn(() => new Promise(() => null))

configure({ axios: mockAxios })

Expand Down

0 comments on commit 4722d7a

Please sign in to comment.