Skip to content

Commit fe47bdb

Browse files
committed
fix(svelte-query-devtools): forward option changes to the devtools instance after mount
1 parent d6798b5 commit fe47bdb

3 files changed

Lines changed: 122 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/svelte-query-devtools': patch
3+
---
4+
5+
fix(svelte-query-devtools): forward option changes to the devtools instance after mount

packages/svelte-query-devtools/src/Devtools.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
}: DevtoolsOptions = $props()
6262
6363
let ref: HTMLDivElement
64-
let devtools: TanstackQueryDevtools | undefined
64+
let devtools = $state<TanstackQueryDevtools | undefined>(undefined)
6565
6666
if (DEV && BROWSER) {
6767
onMount(() => {

packages/svelte-query-devtools/tests/Devtools.svelte.test.ts

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1-
import { describe, expect, it } from 'vitest'
1+
import { afterEach, describe, expect, it, vi } from 'vitest'
22
import { render } from '@testing-library/svelte'
33
import { QueryClient } from '@tanstack/svelte-query'
44
import SvelteQueryDevtools from '../src/Devtools.svelte'
55
import Wrapper from './Wrapper.svelte'
6+
import type { TanstackQueryDevtools } from '@tanstack/query-devtools'
7+
8+
const setButtonPositionMock = vi.fn()
9+
const setPositionMock = vi.fn()
10+
const setInitialIsOpenMock = vi.fn()
11+
const setErrorTypesMock = vi.fn()
12+
13+
vi.mock('@tanstack/query-devtools', () => ({
14+
TanstackQueryDevtools: vi.fn(function (this: TanstackQueryDevtools) {
15+
this.mount = vi.fn()
16+
this.unmount = vi.fn()
17+
this.setButtonPosition = setButtonPositionMock
18+
this.setPosition = setPositionMock
19+
this.setInitialIsOpen = setInitialIsOpenMock
20+
this.setErrorTypes = setErrorTypesMock
21+
}),
22+
}))
623

724
describe('SvelteQueryDevtools', () => {
25+
afterEach(() => {
26+
vi.clearAllMocks()
27+
})
28+
829
it('should render the parent container without throwing in non-development environments', () => {
930
const queryClient = new QueryClient()
1031

@@ -36,4 +57,98 @@ describe('SvelteQueryDevtools', () => {
3657
render(SvelteQueryDevtools, { props: { client: queryClient } }),
3758
).not.toThrow()
3859
})
60+
61+
it('should forward the initial "position" to the devtools instance', async () => {
62+
const queryClient = new QueryClient()
63+
render(SvelteQueryDevtools, {
64+
props: { client: queryClient, position: 'left' },
65+
})
66+
await vi.dynamicImportSettled()
67+
68+
expect(setPositionMock).toHaveBeenCalledWith('left')
69+
})
70+
71+
it('should forward the initial "buttonPosition" to the devtools instance', async () => {
72+
const queryClient = new QueryClient()
73+
render(SvelteQueryDevtools, {
74+
props: { client: queryClient, buttonPosition: 'top-left' },
75+
})
76+
await vi.dynamicImportSettled()
77+
78+
expect(setButtonPositionMock).toHaveBeenCalledWith('top-left')
79+
})
80+
81+
it('should forward the initial "initialIsOpen" to the devtools instance', async () => {
82+
const queryClient = new QueryClient()
83+
render(SvelteQueryDevtools, {
84+
props: { client: queryClient, initialIsOpen: true },
85+
})
86+
await vi.dynamicImportSettled()
87+
88+
expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
89+
})
90+
91+
it('should forward the initial "errorTypes" to the devtools instance', async () => {
92+
const queryClient = new QueryClient()
93+
const errorTypes = [{ name: 'Error', initializer: () => new Error() }]
94+
render(SvelteQueryDevtools, {
95+
props: { client: queryClient, errorTypes },
96+
})
97+
await vi.dynamicImportSettled()
98+
99+
expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
100+
})
101+
102+
it('should forward a "position" change to the devtools instance after mount', async () => {
103+
const queryClient = new QueryClient()
104+
const { rerender } = render(SvelteQueryDevtools, {
105+
props: { client: queryClient, position: 'bottom' },
106+
})
107+
await vi.dynamicImportSettled()
108+
setPositionMock.mockClear()
109+
110+
await rerender({ client: queryClient, position: 'top' })
111+
112+
expect(setPositionMock).toHaveBeenCalledWith('top')
113+
})
114+
115+
it('should forward a "buttonPosition" change to the devtools instance after mount', async () => {
116+
const queryClient = new QueryClient()
117+
const { rerender } = render(SvelteQueryDevtools, {
118+
props: { client: queryClient, buttonPosition: 'bottom-right' },
119+
})
120+
await vi.dynamicImportSettled()
121+
setButtonPositionMock.mockClear()
122+
123+
await rerender({ client: queryClient, buttonPosition: 'top-left' })
124+
125+
expect(setButtonPositionMock).toHaveBeenCalledWith('top-left')
126+
})
127+
128+
it('should forward an "initialIsOpen" change to the devtools instance after mount', async () => {
129+
const queryClient = new QueryClient()
130+
const { rerender } = render(SvelteQueryDevtools, {
131+
props: { client: queryClient, initialIsOpen: false },
132+
})
133+
await vi.dynamicImportSettled()
134+
setInitialIsOpenMock.mockClear()
135+
136+
await rerender({ client: queryClient, initialIsOpen: true })
137+
138+
expect(setInitialIsOpenMock).toHaveBeenCalledWith(true)
139+
})
140+
141+
it('should forward an "errorTypes" change to the devtools instance after mount', async () => {
142+
const queryClient = new QueryClient()
143+
const { rerender } = render(SvelteQueryDevtools, {
144+
props: { client: queryClient, errorTypes: [] },
145+
})
146+
await vi.dynamicImportSettled()
147+
setErrorTypesMock.mockClear()
148+
149+
const errorTypes = [{ name: 'Error', initializer: () => new Error() }]
150+
await rerender({ client: queryClient, errorTypes })
151+
152+
expect(setErrorTypesMock).toHaveBeenCalledWith(errorTypes)
153+
})
39154
})

0 commit comments

Comments
 (0)