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