Skip to content

Commit

Permalink
refactor: tests
Browse files Browse the repository at this point in the history
every stubbed queryFn needs a mock implementation to avoid console errors:

Error: Query data cannot be undefined
  • Loading branch information
TkDodo committed Feb 10, 2022
1 parent 76cc389 commit f6a6ae3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/core/tests/queriesObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ describe('queriesObserver', () => {
test('should trigger all fetches when subscribed', async () => {
const key1 = queryKey()
const key2 = queryKey()
const queryFn1 = jest.fn()
const queryFn2 = jest.fn()
const queryFn1 = jest.fn().mockReturnValue(1)
const queryFn2 = jest.fn().mockReturnValue(2)
const observer = new QueriesObserver(queryClient, [
{ queryKey: key1, queryFn: queryFn1 },
{ queryKey: key2, queryFn: queryFn2 },
Expand Down
4 changes: 2 additions & 2 deletions src/core/tests/queryClient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ describe('queryClient', () => {
describe('refetchQueries', () => {
test('should not refetch if all observers are disabled', async () => {
const key = queryKey()
const queryFn = jest.fn()
const queryFn = jest.fn().mockReturnValue('data')
await queryClient.fetchQuery(key, queryFn)
const observer1 = new QueryObserver(queryClient, {
queryKey: key,
Expand All @@ -877,7 +877,7 @@ describe('queryClient', () => {
})
test('should refetch if at least one observer is enabled', async () => {
const key = queryKey()
const queryFn = jest.fn()
const queryFn = jest.fn().mockReturnValue('data')
await queryClient.fetchQuery(key, queryFn)
const observer1 = new QueryObserver(queryClient, {
queryKey: key,
Expand Down
8 changes: 4 additions & 4 deletions src/core/tests/queryObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('queryObserver', () => {

test('should trigger a fetch when subscribed', async () => {
const key = queryKey()
const queryFn = jest.fn()
const queryFn = jest.fn().mockReturnValue('data')
const observer = new QueryObserver(queryClient, { queryKey: key, queryFn })
const unsubscribe = observer.subscribe(() => undefined)
await sleep(1)
Expand Down Expand Up @@ -309,7 +309,7 @@ describe('queryObserver', () => {

test('should not trigger a fetch when subscribed and disabled', async () => {
const key = queryKey()
const queryFn = jest.fn()
const queryFn = jest.fn().mockReturnValue('data')
const observer = new QueryObserver(queryClient, {
queryKey: key,
queryFn,
Expand All @@ -323,7 +323,7 @@ describe('queryObserver', () => {

test('should not trigger a fetch when not subscribed', async () => {
const key = queryKey()
const queryFn = jest.fn()
const queryFn = jest.fn().mockReturnValue('data')
new QueryObserver(queryClient, { queryKey: key, queryFn })
await sleep(1)
expect(queryFn).toHaveBeenCalledTimes(0)
Expand Down Expand Up @@ -544,7 +544,7 @@ describe('queryObserver', () => {

test('should not refetch in background if refetchIntervalInBackground is false', async () => {
const key = queryKey()
const queryFn = jest.fn()
const queryFn = jest.fn().mockReturnValue('data')

focusManager.setFocused(false)
const observer = new QueryObserver(queryClient, {
Expand Down
2 changes: 1 addition & 1 deletion src/reactjs/tests/ssr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Server Side Rendering', () => {
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
const key = queryKey()
const queryFn = jest.fn()
const queryFn = jest.fn().mockReturnValue('data')

function Page() {
const query = useQuery(key, queryFn)
Expand Down
4 changes: 2 additions & 2 deletions src/reactjs/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2416,7 +2416,7 @@ describe('useQuery', () => {

it('should not refetch query on focus when `enabled` is set to `false`', async () => {
const key = queryKey()
const queryFn = jest.fn()
const queryFn = jest.fn().mockReturnValue('data')

function Page() {
const { data = 'default' } = useQuery(key, queryFn, {
Expand Down Expand Up @@ -3648,7 +3648,7 @@ describe('useQuery', () => {

it('should not cause memo churn when data does not change', async () => {
const key = queryKey()
const queryFn = jest.fn()
const queryFn = jest.fn().mockReturnValue('data')
const memoFn = jest.fn()

function Page() {
Expand Down

0 comments on commit f6a6ae3

Please sign in to comment.