Skip to content

Commit

Permalink
test: Add more query key tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Jun 22, 2020
1 parent 9c618f3 commit 5ce7b1e
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/tests/useQuery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,4 +768,97 @@ describe('useQuery', () => {
await waitFor(() => rendered.getByText('count: 5'))
await waitFor(() => rendered.getByText('count: 5'))
})

it('should error when using functions as query keys', () => {
jest.spyOn(console, 'error')
console.error.mockImplementation(() => {})

function Page() {
const { data } = useQuery(
() => {},
() => 'data'
)
return data
}

try {
render(<Page />)
} catch {}

expect(console.error).toHaveBeenCalledTimes(2)

console.error.mockRestore()
})

it('should error when using an empty query key', () => {
jest.spyOn(console, 'error')
console.error.mockImplementation(() => {})

function Page() {
const { data } = useQuery('', () => 'data')
return data
}

try {
render(<Page />)
} catch {}

expect(console.error).toHaveBeenCalledTimes(2)

console.error.mockRestore()
})

it('should error when using an undefined query key', () => {
jest.spyOn(console, 'error')
console.error.mockImplementation(() => {})

function Page() {
const { data } = useQuery(undefined, () => 'data')
return data
}

try {
render(<Page />)
} catch {}

expect(console.error).toHaveBeenCalledTimes(2)

console.error.mockRestore()
})

it('should error when using a falsy query key', () => {
jest.spyOn(console, 'error')
console.error.mockImplementation(() => {})

function Page() {
const { data } = useQuery(false, () => 'data')
return data
}

try {
render(<Page />)
} catch {}

expect(console.error).toHaveBeenCalledTimes(2)

console.error.mockRestore()
})

it('should error when using a null query key', () => {
jest.spyOn(console, 'error')
console.error.mockImplementation(() => {})

function Page() {
const { data } = useQuery(false, () => 'data')
return data
}

try {
render(<Page />)
} catch {}

expect(console.error).toHaveBeenCalledTimes(2)

console.error.mockRestore()
})
})

0 comments on commit 5ce7b1e

Please sign in to comment.