Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { render } from '@testing-library/svelte'
3import { QueryClient } from '@tanstack/svelte-query'
4import SvelteQueryDevtools from '../src/SvelteQueryDevtools.svelte'
5import Wrapper from './Wrapper.svelte'
6import type { TanstackQueryDevtools } from '@tanstack/query-devtools'
7β
8const mountMock = vi.fn<TanstackQueryDevtools['mount']>()
9const unmountMock = vi.fn<TanstackQueryDevtools['unmount']>()
10const setButtonPositionMock =
11 vi.fn<TanstackQueryDevtools['setButtonPosition']>()
12const setPositionMock = vi.fn<TanstackQueryDevtools['setPosition']>()
13const setInitialIsOpenMock = vi.fn<TanstackQueryDevtools['setInitialIsOpen']>()
14const setErrorTypesMock = vi.fn<TanstackQueryDevtools['setErrorTypes']>()
15const setThemeMock = vi.fn<TanstackQueryDevtools['setTheme']>()
16β
17// oxlint-disable-next-line vitest/prefer-import-in-mock -- the mocked devtools classes have '#private' fields, and this environment cannot load the real Solid module
18vi.mock('@tanstack/query-devtools', () => ({
19 TanstackQueryDevtools: vi.fn<(this: TanstackQueryDevtools) => void>(function (
20 this: TanstackQueryDevtools,
21 ) {
22 this.mount = mountMock
23 this.unmount = unmountMock
24 this.setButtonPosition = setButtonPositionMock
25 this.setPosition = setPositionMock
26 this.setInitialIsOpen = setInitialIsOpenMock
27 this.setErrorTypes = setErrorTypesMock
28 this.setTheme = setThemeMock
29 }),
30}))
31β
32describe('SvelteQueryDevtools', () => {
33 let queryClient: QueryClient
34β
35 beforeEach(() => {
36 queryClient = new QueryClient()
37 })
38β
39 afterEach(() => {
40 vi.clearAllMocks()
41 })
42β
43 it('should render the parent container without throwing in non-development environments', () => {
44 const { container } = render(SvelteQueryDevtools, {
45 props: { client: queryClient },
46 })
47β
48 expect(
49 // oxlint-disable-next-line testing-library/no-node-access -- the devtools mount container has no role or text to query by
50 container.querySelector('.tsqd-parent-container'),
51 ).toBeInTheDocument()
52 })
53β
54 it('should throw an error if no query client has been set', () => {
55 expect(() => render(SvelteQueryDevtools)).toThrow(
56 'No QueryClient was found in Svelte context. Did you forget to wrap your component with QueryClientProvider?',
57 )
58 })
59β
60 it('should not throw an error if query client is provided via context', () => {
61 expect(() => render(Wrapper, { props: { queryClient } })).not.toThrow()
62 })
63β
64 it('should not throw an error if query client is provided via props', () => {
65 expect(() =>
66 render(SvelteQueryDevtools, { props: { client: queryClient } }),
67 ).not.toThrow()
68 })
69β
70 it('should forward the initial "position" to the devtools instance', async () => {
71 render(SvelteQueryDevtools, {
72 props: { client: queryClient, position: 'left' },
73 })
74β
75 await vi.dynamicImportSettled()
76 expect(setPositionMock).toHaveBeenCalledWith('left')
77 })
78β
79 it('should forward the initial "buttonPosition" to the devtools instance', async () => {
80 render(SvelteQueryDevtools, {
81 props: { client: queryClient, buttonPosition: 'top-left' },
82 })
83β
84 await vi.dynamicImportSettled()
85 expect(setButtonPositionMock).toHaveBeenCalledWith('top-left')
86 })
87β
88 it('should forward the initial "initialIsOpen" to the devtools instance', async () => {
89 render(SvelteQueryDevtools, {
90 props: { client: queryClient, initialIsOpen: true },
91 })
92β
93 await vi.dynamicImportSettled()
94 expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
95 })
96β
97 it('should forward the initial "errorTypes" to the devtools instance', async () => {
98 const errorTypes = [{ name: 'Error', initializer: () => new Error() }]
99 render(SvelteQueryDevtools, {
100 props: { client: queryClient, errorTypes },
101 })
102β
103 await vi.dynamicImportSettled()
104 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
105 })
106β
107 it('should forward the initial "theme" to the devtools instance', async () => {
108 render(SvelteQueryDevtools, {
109 props: { client: queryClient, theme: 'dark' },
110 })
111β
112 await vi.dynamicImportSettled()
113 expect(setThemeMock).toHaveBeenCalledWith('dark')
114 })
115β
116 it('should forward an undefined "theme" to the devtools instance when the prop is omitted', async () => {
117 render(SvelteQueryDevtools, {
118 props: { client: queryClient },
119 })
120β
121 await vi.dynamicImportSettled()
122 expect(setThemeMock).toHaveBeenCalledWith(undefined)
123 })
124β
125 it('should forward a "position" change to the devtools instance after mount', async () => {
126 const { rerender } = render(SvelteQueryDevtools, {
127 props: { client: queryClient, position: 'bottom' },
128 })
129β
130 await vi.dynamicImportSettled()
131 setPositionMock.mockClear()
132β
133 await rerender({ client: queryClient, position: 'top' })
134β
135 expect(setPositionMock).toHaveBeenCalledWith('top')
136 })
137β
138 it('should forward a "buttonPosition" change to the devtools instance after mount', async () => {
139 const { rerender } = render(SvelteQueryDevtools, {
140 props: { client: queryClient, buttonPosition: 'bottom-right' },
141 })
142β
143 await vi.dynamicImportSettled()
144 setButtonPositionMock.mockClear()
145β
146 await rerender({ client: queryClient, buttonPosition: 'top-left' })
147β
148 expect(setButtonPositionMock).toHaveBeenCalledWith('top-left')
149 })
150β
151 it('should forward an "initialIsOpen" change to the devtools instance after mount', async () => {
152 const { rerender } = render(SvelteQueryDevtools, {
153 props: { client: queryClient, initialIsOpen: false },
154 })
155β
156 await vi.dynamicImportSettled()
157 setInitialIsOpenMock.mockClear()
158β
159 await rerender({ client: queryClient, initialIsOpen: true })
160β
161 expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
162 })
163β
164 it('should forward an "errorTypes" change to the devtools instance after mount', async () => {
165 const { rerender } = render(SvelteQueryDevtools, {
166 props: { client: queryClient, errorTypes: [] },
167 })
168β
169 await vi.dynamicImportSettled()
170 setErrorTypesMock.mockClear()
171β
172 const errorTypes = [{ name: 'Error', initializer: () => new Error() }]
173 await rerender({ client: queryClient, errorTypes })
174β
175 expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
176 })
177β
178 it('should forward a "theme" change to the devtools instance after mount', async () => {
179 const { rerender } = render(SvelteQueryDevtools, {
180 props: { client: queryClient, theme: 'light' },
181 })
182β
183 await vi.dynamicImportSettled()
184 setThemeMock.mockClear()
185β
186 await rerender({ client: queryClient, theme: 'dark' })
187β
188 expect(setThemeMock).toHaveBeenCalledWith('dark')
189 })
190β
191 it('should not export the devtools component in non-development environments', async () => {
192 vi.doMock(import('esm-env'), async (importOriginal) => ({
193 ...(await importOriginal()),
194 DEV: false,
195 }))
196 vi.resetModules()
197β
198 try {
199 const { SvelteQueryDevtools: ProductionDevtools } =
200 await import('../src/index')
201 const { default: Devtools } =
202 await import('../src/SvelteQueryDevtools.svelte')
203β
204 expect(ProductionDevtools).not.toBe(Devtools)
205 } finally {
206 vi.doUnmock('esm-env')
207 vi.resetModules()
208 }
209 })
210})