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

panel.test.ts

64 lines2.1 KB

1import { describe, expect, it, vi } from 'vitest'
2import { createSveltePanel } from './panel'
3​
4// Minimal stand-in for a class-based devtools core.
5function makeCoreClass() {
6 const mount = vi.fn()
7 const unmount = vi.fn()
8 let lastProps: unknown
9 class Core {
10 mount = mount
11 unmount = unmount
12 constructor(props: unknown) {
13 lastProps = props
14 }
15 }
16 return { Core, mount, unmount, getLastProps: () => lastProps }
18​
19describe('createSveltePanel', () => {
20 it('returns a [Panel, NoOpPanel] tuple of component functions', () => {
21 const { Core } = makeCoreClass()
22 const [Panel, NoOpPanel] = createSveltePanel(Core as any)
23 expect(typeof Panel).toBe('function')
24 expect(typeof NoOpPanel).toBe('function')
25 })
26​
27 it('Panel constructs the core with devtoolsProps, mounts it into a host element, and tears it down on destroy', () => {
28 const { Core, mount, unmount, getLastProps } = makeCoreClass()
29 const [Panel] = createSveltePanel(Core as any)
30​
31 const anchor = document.createElement('span')
32 document.body.appendChild(anchor)
33​
34 // Svelte 5 invokes a component with (anchor, props); the panel inserts its
35 // own host element before the anchor and mounts the core into it.
36 const instance = (Panel as any)(anchor, {
37 theme: 'dark',
38 devtoolsProps: { foo: 'bar' },
39 })
40​
41 expect(getLastProps()).toEqual({ foo: 'bar' })
42 expect(mount).toHaveBeenCalledTimes(1)
43 const call = mount.mock.calls[0]!
44 const mountedEl = call[0] as HTMLElement
45 const theme = call[1]
46 expect(mountedEl).toBeInstanceOf(HTMLElement)
47 expect(mountedEl.parentElement).toBe(document.body)
48 expect(theme).toBe('dark')
49​
50 instance.destroy()
51 expect(unmount).toHaveBeenCalledTimes(1)
52 expect(mountedEl.parentElement).toBeNull()
53​
54 anchor.remove()
55 })
56​
57 it('NoOpPanel never constructs or mounts the core class', () => {
58 const { Core, mount } = makeCoreClass()
59 const [, NoOpPanel] = createSveltePanel(Core as any)
60 const anchor = document.createElement('span')
61 ;(NoOpPanel as any)(anchor, { theme: 'dark' })
62 expect(mount).not.toHaveBeenCalled()
63 })
64})