Open History to see the last commit
1import { QueryClient, VueQueryPlugin } from '@tanstack/vue-query'
2import { afterEach, describe, expect, it, vi } from 'vitest'
3import { createApp, h, nextTick, shallowRef } from 'vue'
4import Devtools from '../devtools.vue'
5import DevtoolsPanel from '../devtoolsPanel.vue'
6import type { TanstackQueryDevtoolsPanel } from '@tanstack/query-devtools'
7import type { App } from 'vue'
8β
9const { setClient, mount, unmount } = vi.hoisted(() => ({
10 setClient: vi.fn<TanstackQueryDevtoolsPanel['setClient']>(),
11 mount: vi.fn<TanstackQueryDevtoolsPanel['mount']>(),
12 unmount: vi.fn<TanstackQueryDevtoolsPanel['unmount']>(),
13}))
14β
15// 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
16vi.mock('@tanstack/query-devtools', () => {
17 class MockDevtools {
18 setClient = setClient
19 mount = mount
20 unmount = unmount
21 setButtonPosition = vi.fn<TanstackQueryDevtoolsPanel['setButtonPosition']>()
22 setPosition = vi.fn<TanstackQueryDevtoolsPanel['setPosition']>()
23 setInitialIsOpen = vi.fn<TanstackQueryDevtoolsPanel['setInitialIsOpen']>()
24 setErrorTypes = vi.fn<TanstackQueryDevtoolsPanel['setErrorTypes']>()
25 setTheme = vi.fn<TanstackQueryDevtoolsPanel['setTheme']>()
26 setOnClose = vi.fn<TanstackQueryDevtoolsPanel['setOnClose']>()
27 }
28 return {
29 TanstackQueryDevtools: MockDevtools,
30 TanstackQueryDevtoolsPanel: MockDevtools,
31 }
32})
33β
34describe.each([
35 ['floating', Devtools],
36 ['embedded', DevtoolsPanel],
37] as const)('%s devtools client', (_, component) => {
38 let app: App | undefined
39 let container: HTMLDivElement | undefined
40β
41 afterEach(() => {
42 app?.unmount()
43 container?.remove()
44 vi.clearAllMocks()
45 })
46β
47 it('switches explicit clients without remounting or requiring a provider', async () => {
48 const first = new QueryClient()
49 const second = new QueryClient()
50 const client = shallowRef(first)
51 container = document.createElement('div')
52 app = createApp(() => h(component, { client: client.value }))
53 app.mount(container)
54β
55 expect(setClient.mock.lastCall?.[0]).toBe(first)
56 client.value = second
57 await nextTick()
58β
59 expect(setClient.mock.lastCall?.[0]).toBe(second)
60 expect(mount).toHaveBeenCalledTimes(1)
61 expect(unmount).not.toHaveBeenCalled()
62 })
63β
64 it('returns to the initial context client after an override is removed', async () => {
65 const contextClient = new QueryClient()
66 const override = new QueryClient()
67 const client = shallowRef<QueryClient>()
68 container = document.createElement('div')
69 app = createApp(() => h(component, { client: client.value }))
70 app.use(VueQueryPlugin, { queryClient: contextClient })
71 app.mount(container)
72β
73 expect(setClient.mock.lastCall?.[0]).toBe(contextClient)
74 client.value = override
75 await nextTick()
76 expect(setClient.mock.lastCall?.[0]).toBe(override)
77β
78 client.value = undefined
79 await nextTick()
80 expect(setClient.mock.lastCall?.[0]).toBe(contextClient)
81 expect(mount).toHaveBeenCalledTimes(1)
82 })
83})