Open History to see the last commit
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 })
61β
62 expect(states[1]).toEqual({
63 data: { pages: [0], pageParams: [0] },
64 dataUpdatedAt: expect.any(Number),
65 error: null,
66 errorUpdatedAt: 0,
67 failureCount: 0,
68 failureReason: null,
69 errorUpdateCount: 0,
70 fetchNextPage: expect.any(Function),
71 fetchPreviousPage: expect.any(Function),
72 hasNextPage: true,
73 hasPreviousPage: false,
74 isError: false,
75 isFetched: true,
76 isFetchedAfterMount: true,
77 isFetching: false,
78 isPaused: false,
79 isFetchNextPageError: false,
80 isFetchingNextPage: false,
81 isFetchPreviousPageError: false,
82 isFetchingPreviousPage: false,
83 isLoading: false,
84 isPending: false,
85 isInitialLoading: false,
86 isLoadingError: false,
87 isPlaceholderData: false,
88 isRefetchError: false,
89 isRefetching: false,
90 isStale: true,
91 isSuccess: true,
92 refetch: expect.any(Function),
93 status: 'success',
94 fetchStatus: 'idle',
95 })
96 })
97β
98 test('Select a part of the data', async () => {
99 const statesStore: Writable<Array<QueryObserverResult>> = writable([])
100β
101 const rendered = render(SelectExample, {
102 props: {
103 states: statesStore,
104 },
105 })
106β
107 await waitFor(() => {
108 expect(rendered.queryByText('count: 1')).toBeInTheDocument()
109 })
110β
111 const states = get(statesStore)
112β
113 expect(states).toHaveLength(2)
114β
115 expect(states[0]).toMatchObject({
116 data: undefined,
117 isSuccess: false,
118 })
119β
120 expect(states[1]).toMatchObject({
121 data: { pages: ['count: 1'] },
122 isSuccess: true,
123 })
124 })
125})