Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

TanstackQueryDevtoolsPanel.test.tsx

121 lines3.8 KB

1import { beforeEach, describe, expect, it } from 'vitest'
2import { QueryClient, onlineManager } from '@tanstack/query-core'
3import { TanstackQueryDevtoolsPanel } from '..'
4​
5describe('TanstackQueryDevtoolsPanel', () => {
6 let devtools: TanstackQueryDevtoolsPanel
7​
8 beforeEach(() => {
9 devtools = new TanstackQueryDevtoolsPanel({
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 "setOnClose" is called', () => {
87 expect(() => devtools.setOnClose(() => {})).not.toThrow()
88 })
89​
90 it('should not throw when "setTheme" is called', () => {
91 expect(() => devtools.setTheme('dark')).not.toThrow()
92 expect(() => devtools.setTheme('light')).not.toThrow()
93 expect(() => devtools.setTheme('system')).not.toThrow()
94 expect(() => devtools.setTheme(undefined)).not.toThrow()
95 })
96 })
97​
98 describe('after mount', () => {
99 it('should not throw when setters are called on a mounted instance', () => {
100 const el = document.createElement('div')
101 devtools.mount(el)
102​
103 try {
104 expect(() => devtools.setButtonPosition('top-left')).not.toThrow()
105 expect(() => devtools.setPosition('left')).not.toThrow()
106 expect(() => devtools.setInitialIsOpen(true)).not.toThrow()
107 expect(() =>
108 devtools.setErrorTypes([
109 { name: 'NetworkError', initializer: () => new Error('Network') },
110 ]),
111 ).not.toThrow()
112 expect(() => devtools.setClient(new QueryClient())).not.toThrow()
113 expect(() => devtools.setOnClose(() => {})).not.toThrow()
114 expect(() => devtools.setTheme('dark')).not.toThrow()
115 } finally {
116 devtools.unmount()
117 }
118 })
119 })
120 })
121})