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

devtools.test.tsx

99 lines3.2 KB

1import '@testing-library/jest-dom/vitest'
2import React from 'react'
3import { act, render } from '@testing-library/react'
4import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
5import { TanStackDevtoolsCore } from '@tanstack/devtools'
6import { TanStackDevtools } from '../src/devtools'
7import type {
8 TanStackDevtoolsPlugin,
9 TanStackDevtoolsPluginProps,
10} from '@tanstack/devtools'
11​
12// The real core does a full Solid DOM mount which is heavy in jsdom. Mock it
13// down to the methods the React adapter actually calls (constructor + mount +
14// unmount + setConfig), so we can drive the portal wiring in isolation.
15vi.mock('@tanstack/devtools', () => ({
16 TanStackDevtoolsCore: vi.fn().mockImplementation(() => ({
17 mount: vi.fn(),
18 unmount: vi.fn(),
19 setConfig: vi.fn(),
20 })),
21}))
22​
23const CoreMock = vi.mocked(TanStackDevtoolsCore)
24​
25describe('TanStackDevtools (React adapter)', () => {
26 beforeEach(() => {
27 CoreMock.mockClear()
28 })
29​
30 afterEach(() => {
31 document.querySelectorAll('#p').forEach((el) => el.remove())
32 })
33​
34 it('constructs the core, mounts into the ref div, portals the plugin element, and unmounts', () => {
35 let unmount: () => void = () => {}
36​
37 act(() => {
38 const result = render(
39 <TanStackDevtools
40 plugins={[{ id: 'p', name: 'P', render: <div data-testid="x" /> }]}
41 />,
42 )
43 unmount = result.unmount
44 })
45​
46 // Constructed exactly once.
47 expect(CoreMock).toHaveBeenCalledTimes(1)
48​
49 const coreInstance = CoreMock.mock.results[0]!.value as {
50 mount: ReturnType<typeof vi.fn>
51 unmount: ReturnType<typeof vi.fn>
52 setConfig: ReturnType<typeof vi.fn>
53 }
54​
55 // mount() was called with the ref div the adapter renders.
56 expect(coreInstance.mount).toHaveBeenCalledTimes(1)
57 const mountTarget = coreInstance.mount.mock.calls[0]![0] as HTMLElement
58 expect(mountTarget).toBeInstanceOf(HTMLDivElement)
59​
60 // Grab the plugins the adapter passed to the core constructor.
61 const ctorArg = CoreMock.mock.calls[0]![0] as {
62 plugins: Array<TanStackDevtoolsPlugin>
63 }
64 const corePlugin = ctorArg.plugins[0]!
65 expect(corePlugin.id).toBe('p')
66​
67 // The container is only registered if the element is findable by id in the
68 // owner document (adapter does `e.ownerDocument.getElementById(id)`), so the
69 // fake element must carry id='p' and live in the document.
70 const fakeEl = document.createElement('div')
71 fakeEl.setAttribute('id', 'p')
72 document.body.appendChild(fakeEl)
73​
74 const props: TanStackDevtoolsPluginProps = {
75 theme: 'dark',
76 devtoolsOpen: true,
77 }
78​
79 // Drive the adapter's render callback: convertRender -> setPluginContainers
80 // + setPluginComponents, which then portals the React element into fakeEl.
81 act(() => {
82 ;(corePlugin.render as (el: HTMLElement, props: any) => void)(
83 fakeEl,
84 props,
85 )
86 })
87​
88 // The plugin React element is portaled into the core-provided container.
89 expect(fakeEl.querySelector('[data-testid="x"]')).toBeInTheDocument()
90​
91 // Unmounting tears down the core and throws nothing.
92 expect(() => {
93 act(() => {
94 unmount()
95 })
96 }).not.toThrow()
97 expect(coreInstance.unmount).toHaveBeenCalledTimes(1)
98 })
99})