Skip to content
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
29 changes: 15 additions & 14 deletions packages/preact-query/src/__tests__/usePrefetchQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import {
import { ErrorBoundary } from './ErrorBoundary'
import { renderWithClient } from './utils'

const generateQueryFn = (data: string) =>
vi
.fn<(...args: Array<any>) => Promise<string>>()
.mockImplementation(() => sleep(10).then(() => data))

describe('usePrefetchQuery', () => {
let queryCache: QueryCache
let queryClient: QueryClient
Expand All @@ -37,12 +32,12 @@ describe('usePrefetchQuery', () => {
it('should prefetch query if query state does not exist', async () => {
const queryOpts = {
queryKey: queryKey(),
queryFn: generateQueryFn('prefetchQuery'),
queryFn: vi.fn(() => sleep(10).then(() => 'prefetchQuery')),
}

const componentQueryOpts = {
...queryOpts,
queryFn: generateQueryFn('useSuspenseQuery'),
queryFn: () => sleep(10).then(() => 'useSuspenseQuery'),
}

function Page() {
Expand Down Expand Up @@ -72,7 +67,9 @@ describe('usePrefetchQuery', () => {
it('should not prefetch query if query state exists', async () => {
const queryOpts = {
queryKey: queryKey(),
queryFn: generateQueryFn('The usePrefetchQuery hook is smart!'),
queryFn: vi.fn(() =>
sleep(10).then(() => 'The usePrefetchQuery hook is smart!'),
),
}

function Page() {
Expand Down Expand Up @@ -105,7 +102,7 @@ describe('usePrefetchQuery', () => {
it('should let errors fall through and not refetch failed queries', async () => {
const consoleMock = vi.spyOn(console, 'error')
consoleMock.mockImplementation(() => undefined)
const queryFn = generateQueryFn('Not an error')
const queryFn = vi.fn(() => sleep(10).then(() => 'Not an error'))

const queryOpts = {
queryKey: queryKey(),
Expand Down Expand Up @@ -148,7 +145,7 @@ describe('usePrefetchQuery', () => {
})

it('should not create an endless loop when using inside a suspense boundary', async () => {
const queryFn = generateQueryFn('prefetchedQuery')
const queryFn = vi.fn(() => sleep(10).then(() => 'prefetchedQuery'))

const queryOpts = {
queryKey: queryKey(),
Expand Down Expand Up @@ -184,7 +181,9 @@ describe('usePrefetchQuery', () => {
it('should be able to recover from errors and try fetching again', async () => {
const consoleMock = vi.spyOn(console, 'error')
consoleMock.mockImplementation(() => undefined)
const queryFn = generateQueryFn('This is fine :dog: :fire:')
const queryFn = vi.fn(() =>
sleep(10).then(() => 'This is fine :dog: :fire:'),
)

const queryOpts = {
queryKey: queryKey(),
Expand Down Expand Up @@ -242,17 +241,19 @@ describe('usePrefetchQuery', () => {
it('should not create a suspense waterfall if prefetch is fired', async () => {
const firstQueryOpts = {
queryKey: queryKey(),
queryFn: generateQueryFn('Prefetch is nice!'),
queryFn: vi.fn(() => sleep(10).then(() => 'Prefetch is nice!')),
}

const secondQueryOpts = {
queryKey: queryKey(),
queryFn: generateQueryFn('Prefetch is really nice!!'),
queryFn: vi.fn(() => sleep(10).then(() => 'Prefetch is really nice!!')),
}

const thirdQueryOpts = {
queryKey: queryKey(),
queryFn: generateQueryFn('Prefetch does not create waterfalls!!'),
queryFn: vi.fn(() =>
sleep(10).then(() => 'Prefetch does not create waterfalls!!'),
),
}

const Fallback = vi.fn().mockImplementation(() => <div>Loading...</div>)
Expand Down
29 changes: 15 additions & 14 deletions packages/react-query/src/__tests__/usePrefetchQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ import {
} from '..'
import { renderWithClient } from './utils'

const generateQueryFn = (data: string) =>
vi
.fn<(...args: Array<any>) => Promise<string>>()
.mockImplementation(() => sleep(10).then(() => data))

describe('usePrefetchQuery', () => {
let queryCache: QueryCache
let queryClient: QueryClient
Expand All @@ -35,12 +30,12 @@ describe('usePrefetchQuery', () => {
it('should prefetch query if query state does not exist', async () => {
const queryOpts = {
queryKey: queryKey(),
queryFn: generateQueryFn('prefetchQuery'),
queryFn: vi.fn(() => sleep(10).then(() => 'prefetchQuery')),
}

const componentQueryOpts = {
...queryOpts,
queryFn: generateQueryFn('useSuspenseQuery'),
queryFn: () => sleep(10).then(() => 'useSuspenseQuery'),
}

function Page() {
Expand Down Expand Up @@ -70,7 +65,9 @@ describe('usePrefetchQuery', () => {
it('should not prefetch query if query state exists', async () => {
const queryOpts = {
queryKey: queryKey(),
queryFn: generateQueryFn('The usePrefetchQuery hook is smart!'),
queryFn: vi.fn(() =>
sleep(10).then(() => 'The usePrefetchQuery hook is smart!'),
),
}

function Page() {
Expand Down Expand Up @@ -103,7 +100,7 @@ describe('usePrefetchQuery', () => {
it('should let errors fall through and not refetch failed queries', async () => {
const consoleMock = vi.spyOn(console, 'error')
consoleMock.mockImplementation(() => undefined)
const queryFn = generateQueryFn('Not an error')
const queryFn = vi.fn(() => sleep(10).then(() => 'Not an error'))

const queryOpts = {
queryKey: queryKey(),
Expand Down Expand Up @@ -146,7 +143,7 @@ describe('usePrefetchQuery', () => {
})

it('should not create an endless loop when using inside a suspense boundary', async () => {
const queryFn = generateQueryFn('prefetchedQuery')
const queryFn = vi.fn(() => sleep(10).then(() => 'prefetchedQuery'))

const queryOpts = {
queryKey: queryKey(),
Expand Down Expand Up @@ -182,7 +179,9 @@ describe('usePrefetchQuery', () => {
it('should be able to recover from errors and try fetching again', async () => {
const consoleMock = vi.spyOn(console, 'error')
consoleMock.mockImplementation(() => undefined)
const queryFn = generateQueryFn('This is fine :dog: :fire:')
const queryFn = vi.fn(() =>
sleep(10).then(() => 'This is fine :dog: :fire:'),
)

const queryOpts = {
queryKey: queryKey(),
Expand Down Expand Up @@ -240,17 +239,19 @@ describe('usePrefetchQuery', () => {
it('should not create a suspense waterfall if prefetch is fired', async () => {
const firstQueryOpts = {
queryKey: queryKey(),
queryFn: generateQueryFn('Prefetch is nice!'),
queryFn: vi.fn(() => sleep(10).then(() => 'Prefetch is nice!')),
}

const secondQueryOpts = {
queryKey: queryKey(),
queryFn: generateQueryFn('Prefetch is really nice!!'),
queryFn: vi.fn(() => sleep(10).then(() => 'Prefetch is really nice!!')),
}

const thirdQueryOpts = {
queryKey: queryKey(),
queryFn: generateQueryFn('Prefetch does not create waterfalls!!'),
queryFn: vi.fn(() =>
sleep(10).then(() => 'Prefetch does not create waterfalls!!'),
),
}

const Fallback = vi.fn().mockImplementation(() => <div>Loading...</div>)
Expand Down
Loading