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
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
{
queries: ids.map((id) => ({
queryKey: [id],
queryFn: async () => {
await sleep(5)
return id
},
queryFn: () => sleep(10).then(() => id),
})),
combine: (results) => {
return {
Expand Down
44 changes: 32 additions & 12 deletions packages/svelte-query/tests/createQueries/createQueries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,64 @@ describe('createQueries', () => {
queries: [
{
queryKey: ['key-1'],
queryFn: async () => {
await sleep(5)
return 'Success 1'
},
queryFn: () => sleep(10).then(() => 'Success 1'),
},
{
queryKey: ['key-2'],
queryFn: async () => {
await sleep(5)
return 'Success 2'
},
queryFn: () => sleep(10).then(() => 'Success 2'),
},
],
},
queryClient: new QueryClient(),
},
})

await vi.advanceTimersByTimeAsync(0)
expect(rendered.getByText('Status 1: pending')).toBeInTheDocument()
expect(rendered.getByText('Status 2: pending')).toBeInTheDocument()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If queryFn's sleeping time is different, it would be better to add test code for the case after just 10ms, not only this.

await vi.advanceTimersByTimeAsync(6)
await vi.advanceTimersByTimeAsync(11)
expect(rendered.getByText('Status 1: success')).toBeInTheDocument()
expect(rendered.getByText('Status 2: success')).toBeInTheDocument()
})

test('Render and wait for success when queries resolve at different times', async () => {
const rendered = render(BaseExample, {
props: {
options: {
queries: [
{
queryKey: ['key-1'],
queryFn: () => sleep(10).then(() => 'Success 1'),
},
{
queryKey: ['key-2'],
queryFn: () => sleep(20).then(() => 'Success 2'),
},
],
},
queryClient: new QueryClient(),
},
})

expect(rendered.getByText('Status 1: pending')).toBeInTheDocument()
expect(rendered.getByText('Status 2: pending')).toBeInTheDocument()

await vi.advanceTimersByTimeAsync(11)
expect(rendered.getByText('Status 1: success')).toBeInTheDocument()
await vi.advanceTimersByTimeAsync(10)
expect(rendered.getByText('Status 2: success')).toBeInTheDocument()
})

Comment on lines +44 to +71
Copy link
Contributor Author

@sukvvon sukvvon Jul 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If queryFn's sleeping time is different 10ms, 20ms, it would be better to add test code for the case after just 10ms, not only for case after 20ms.

@manudeli

I added the test as you suggested.

test('Combine queries', async () => {
const rendered = render(CombineExample, {
props: {
queryClient: new QueryClient(),
},
})

await vi.advanceTimersByTimeAsync(0)
expect(rendered.getByText('isPending: true')).toBeInTheDocument()

await vi.advanceTimersByTimeAsync(6)
await vi.advanceTimersByTimeAsync(11)
expect(rendered.getByText('Data: 1,2,3')).toBeInTheDocument()
})
})
Loading