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