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