Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

createInfiniteQuery.test.ts

127 lines3.4 KB

1import { describe, expect, test } from 'vitest'
2import { render, waitFor } from '@testing-library/svelte'
3import { get, writable } from 'svelte/store'
4import BaseExample from './BaseExample.svelte'
5import SelectExample from './SelectExample.svelte'
6import type { Writable } from 'svelte/store'
7import type { QueryObserverResult } from '@tanstack/query-core'
8​
9describe('createInfiniteQuery', () => {
10 test('Return the correct states for a successful query', async () => {
11 const statesStore: Writable<Array<QueryObserverResult>> = writable([])
12​
13 const rendered = render(BaseExample, {
14 props: {
15 states: statesStore,
16 },
17 })
18​
19 await waitFor(() => {
20 expect(rendered.queryByText('Status: success')).toBeInTheDocument()
21 })
22​
23 const states = get(statesStore)
24​
25 expect(states).toHaveLength(2)
26​
27 expect(states[0]).toEqual({
28 data: undefined,
29 dataUpdatedAt: 0,
30 error: null,
31 errorUpdatedAt: 0,
32 failureCount: 0,
33 failureReason: null,
34 errorUpdateCount: 0,
35 fetchNextPage: expect.any(Function),
36 fetchPreviousPage: expect.any(Function),
37 hasNextPage: false,
38 hasPreviousPage: false,
39 isError: false,
40 isFetched: false,
41 isFetchedAfterMount: false,
42 isFetching: true,
43 isPaused: false,
44 isFetchNextPageError: false,
45 isFetchingNextPage: false,
46 isFetchPreviousPageError: false,
47 isFetchingPreviousPage: false,
48 isLoading: true,
49 isPending: true,
50 isInitialLoading: true,
51 isLoadingError: false,
52 isPlaceholderData: false,
53 isRefetchError: false,
54 isRefetching: false,
55 isStale: true,
56 isSuccess: false,
57 refetch: expect.any(Function),
58 status: 'pending',
59 fetchStatus: 'fetching',
60 promise: expect.any(Promise),
61 })
62​
63 expect(states[1]).toEqual({
64 data: { pages: [0], pageParams: [0] },
65 dataUpdatedAt: expect.any(Number),
66 error: null,
67 errorUpdatedAt: 0,
68 failureCount: 0,
69 failureReason: null,
70 errorUpdateCount: 0,
71 fetchNextPage: expect.any(Function),
72 fetchPreviousPage: expect.any(Function),
73 hasNextPage: true,
74 hasPreviousPage: false,
75 isError: false,
76 isFetched: true,
77 isFetchedAfterMount: true,
78 isFetching: false,
79 isPaused: false,
80 isFetchNextPageError: false,
81 isFetchingNextPage: false,
82 isFetchPreviousPageError: false,
83 isFetchingPreviousPage: false,
84 isLoading: false,
85 isPending: false,
86 isInitialLoading: false,
87 isLoadingError: false,
88 isPlaceholderData: false,
89 isRefetchError: false,
90 isRefetching: false,
91 isStale: true,
92 isSuccess: true,
93 refetch: expect.any(Function),
94 status: 'success',
95 fetchStatus: 'idle',
96 promise: expect.any(Promise),
97 })
98 })
99​
100 test('Select a part of the data', async () => {
101 const statesStore: Writable<Array<QueryObserverResult>> = writable([])
102​
103 const rendered = render(SelectExample, {
104 props: {
105 states: statesStore,
106 },
107 })
108​
109 await waitFor(() => {
110 expect(rendered.queryByText('count: 1')).toBeInTheDocument()
111 })
112​
113 const states = get(statesStore)
114​
115 expect(states).toHaveLength(2)
116​
117 expect(states[0]).toMatchObject({
118 data: undefined,
119 isSuccess: false,
120 })
121​
122 expect(states[1]).toMatchObject({
123 data: { pages: ['count: 1'] },
124 isSuccess: true,
125 })
126 })
127})