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

class.test.tsx

139 lines4.8 KB

1/** @jsxImportSource solid-js - we use Solid.js as JSX here */
2import { beforeEach, describe, expect, it, vi } from 'vitest'
3import { constructCoreClass } from './class'
4​
5const disposeMock = vi.fn()
6const mountComponentMock = vi.fn(() => disposeMock)
7​
8vi.mock('./class-mount-impl', () => ({
9 __mountComponent: mountComponentMock,
10}))
11​
12const importFn = () =>
13 Promise.resolve({ default: () => <div>Test Component</div> })
14​
15describe('constructCoreClass', () => {
16 beforeEach(() => {
17 vi.clearAllMocks()
18 })
19​
20 it('should export DevtoolsCore and NoOpDevtoolsCore classes and make no calls to Solid.js primitives', () => {
21 const [DevtoolsCore, NoOpDevtoolsCore] = constructCoreClass(importFn)
22 expect(DevtoolsCore).toBeDefined()
23 expect(NoOpDevtoolsCore).toBeDefined()
24 expect(mountComponentMock).not.toHaveBeenCalled()
25 })
26​
27 it('DevtoolsCore should call __mountComponent when mount is called', async () => {
28 const [DevtoolsCore] = constructCoreClass(importFn)
29 const instance = new DevtoolsCore()
30 const el = document.createElement('div')
31 const props = { theme: 'dark' as const, devtoolsOpen: true }
32 await instance.mount(el, props)
33 expect(mountComponentMock).toHaveBeenCalledWith(el, props, importFn)
34 })
35​
36 it('DevtoolsCore should throw if mount is called twice without unmounting', async () => {
37 const [DevtoolsCore] = constructCoreClass(importFn)
38 const instance = new DevtoolsCore()
39 await instance.mount(document.createElement('div'), {
40 theme: 'dark',
41 devtoolsOpen: true,
42 })
43 await expect(
44 instance.mount(document.createElement('div'), {
45 theme: 'dark',
46 devtoolsOpen: true,
47 }),
48 ).rejects.toThrow('Devtools is already mounted')
49 })
50​
51 it('DevtoolsCore should throw if unmount is called before mount', () => {
52 const [DevtoolsCore] = constructCoreClass(importFn)
53 const instance = new DevtoolsCore()
54 expect(() => instance.unmount()).toThrow('Devtools is not mounted')
55 })
56​
57 it('DevtoolsCore should allow mount after unmount', async () => {
58 const [DevtoolsCore] = constructCoreClass(importFn)
59 const instance = new DevtoolsCore()
60 await instance.mount(document.createElement('div'), {
61 theme: 'dark',
62 devtoolsOpen: true,
63 })
64 instance.unmount()
65 await expect(
66 instance.mount(document.createElement('div'), {
67 theme: 'dark',
68 devtoolsOpen: true,
69 }),
70 ).resolves.not.toThrow()
71 })
72​
73 it('DevtoolsCore should call dispose on unmount', async () => {
74 const [DevtoolsCore] = constructCoreClass(importFn)
75 const instance = new DevtoolsCore()
76 await instance.mount(document.createElement('div'), {
77 theme: 'dark',
78 devtoolsOpen: true,
79 })
80 instance.unmount()
81 expect(disposeMock).toHaveBeenCalled()
82 })
83​
84 it('DevtoolsCore should abort mount if unmount is called during mounting', async () => {
85 const [DevtoolsCore] = constructCoreClass(importFn)
86 const instance = new DevtoolsCore()
87 const mountPromise = instance.mount(document.createElement('div'), {
88 theme: 'dark',
89 devtoolsOpen: true,
90 })
91 // Unmount while mount is in progress β€” triggers abort path
92 // Note: since the mock resolves immediately, this tests the #abortMount flag
93 await mountPromise
94 // Mount completed, so unmount should work normally
95 instance.unmount()
96 expect(disposeMock).toHaveBeenCalled()
97 })
98​
99 it('NoOpDevtoolsCore should not call __mountComponent when mount is called', async () => {
100 const [, NoOpDevtoolsCore] = constructCoreClass(importFn)
101 const noOpInstance = new NoOpDevtoolsCore()
102 await noOpInstance.mount(document.createElement('div'), {
103 theme: 'dark',
104 devtoolsOpen: true,
105 })
106 expect(mountComponentMock).not.toHaveBeenCalled()
107 })
108​
109 it('NoOpDevtoolsCore should not throw if mount is called multiple times', async () => {
110 const [, NoOpDevtoolsCore] = constructCoreClass(importFn)
111 const noOpInstance = new NoOpDevtoolsCore()
112 await noOpInstance.mount(document.createElement('div'), {
113 theme: 'dark',
114 devtoolsOpen: true,
115 })
116 await expect(
117 noOpInstance.mount(document.createElement('div'), {
118 theme: 'dark',
119 devtoolsOpen: true,
120 }),
121 ).resolves.not.toThrow()
122 })
123​
124 it('NoOpDevtoolsCore should not throw if unmount is called before mount', () => {
125 const [, NoOpDevtoolsCore] = constructCoreClass(importFn)
126 const noOpInstance = new NoOpDevtoolsCore()
127 expect(() => noOpInstance.unmount()).not.toThrow()
128 })
129​
130 it('NoOpDevtoolsCore should not throw if unmount is called after mount', async () => {
131 const [, NoOpDevtoolsCore] = constructCoreClass(importFn)
132 const noOpInstance = new NoOpDevtoolsCore()
133 await noOpInstance.mount(document.createElement('div'), {
134 theme: 'dark',
135 devtoolsOpen: true,
136 })
137 expect(() => noOpInstance.unmount()).not.toThrow()
138 })
139})