Open History to see the last commit
1import { beforeEach, describe, expect, it } from 'vitest'
2import { QueryClient, onlineManager } from '@tanstack/query-core'
3import { TanstackQueryDevtools } from '..'
4β
5describe('TanstackQueryDevtools', () => {
6 let devtools: TanstackQueryDevtools
7β
8 beforeEach(() => {
9 devtools = new TanstackQueryDevtools({
10 client: new QueryClient(),
11 queryFlavor: 'TanStack Query',
12 version: '5',
13 onlineManager,
14 })
15 })
16β
17 describe('mount', () => {
18 it('should mount devtools to the provided element', () => {
19 const el = document.createElement('div')
20β
21 expect(() => devtools.mount(el)).not.toThrow()
22β
23 devtools.unmount()
24 })
25β
26 it('should throw if mount is called twice without unmount', () => {
27 const el = document.createElement('div')
28 devtools.mount(el)
29β
30 expect(() => devtools.mount(el)).toThrow('Devtools is already mounted')
31β
32 devtools.unmount()
33 })
34 })
35β
36 describe('unmount', () => {
37 it('should unmount devtools and allow remounting', () => {
38 const el = document.createElement('div')
39 devtools.mount(el)
40β
41 expect(() => devtools.unmount()).not.toThrow()
42 expect(() => devtools.mount(el)).not.toThrow()
43β
44 devtools.unmount()
45 })
46β
47 it('should throw if unmount is called before mount', () => {
48 expect(() => devtools.unmount()).toThrow('Devtools is not mounted')
49 })
50β
51 it('should throw if unmount is called twice', () => {
52 const el = document.createElement('div')
53 devtools.mount(el)
54 devtools.unmount()
55β
56 expect(() => devtools.unmount()).toThrow('Devtools is not mounted')
57 })
58 })
59β
60 describe('setters', () => {
61 describe('before mount', () => {
62 it('should not throw when "setButtonPosition" is called', () => {
63 expect(() => devtools.setButtonPosition('top-left')).not.toThrow()
64 })
65β
66 it('should not throw when "setPosition" is called', () => {
67 expect(() => devtools.setPosition('left')).not.toThrow()
68 })
69β
70 it('should not throw when "setInitialIsOpen" is called', () => {
71 expect(() => devtools.setInitialIsOpen(true)).not.toThrow()
72 })
73β
74 it('should not throw when "setErrorTypes" is called', () => {
75 expect(() =>
76 devtools.setErrorTypes([
77 { name: 'NetworkError', initializer: () => new Error('Network') },
78 ]),
79 ).not.toThrow()
80 })
81β
82 it('should not throw when "setClient" is called', () => {
83 expect(() => devtools.setClient(new QueryClient())).not.toThrow()
84 })
85β
86 it('should not throw when "setTheme" is called', () => {
87 expect(() => devtools.setTheme('dark')).not.toThrow()
88 expect(() => devtools.setTheme('light')).not.toThrow()
89 expect(() => devtools.setTheme('system')).not.toThrow()
90 expect(() => devtools.setTheme(undefined)).not.toThrow()
91 })
92 })
93β
94 describe('after mount', () => {
95 it('should not throw when setters are called on a mounted instance', () => {
96 const el = document.createElement('div')
97 devtools.mount(el)
98β
99 try {
100 expect(() => devtools.setButtonPosition('top-left')).not.toThrow()
101 expect(() => devtools.setPosition('left')).not.toThrow()
102 expect(() => devtools.setInitialIsOpen(true)).not.toThrow()
103 expect(() =>
104 devtools.setErrorTypes([
105 { name: 'NetworkError', initializer: () => new Error('Network') },
106 ]),
107 ).not.toThrow()
108 expect(() => devtools.setClient(new QueryClient())).not.toThrow()
109 expect(() => devtools.setTheme('dark')).not.toThrow()
110 } finally {
111 devtools.unmount()
112 }
113 })
114 })
115 })
116})