// @vitest-environment happy-dom import type { App, Component } from 'vue' import type { InstalledDevtoolsHook } from '../../../packages/kit/src/hook/install' import type { DevtoolsRuntime } from '../../../packages/kit/src/runtime/runtime' import { createApp, defineComponent, h, nextTick, ref, setDevtoolsHook } from 'vue' import { afterEach, describe, expect, it, vi } from 'vitest' import { installDevtoolsHook } from '../../../packages/kit/src/hook/install' import { createDevtoolsRuntime } from '../../../packages/kit/src/runtime/runtime' const apps: App[] = [] const containers: HTMLElement[] = [] let runtime: DevtoolsRuntime | undefined let installed: InstalledDevtoolsHook | undefined afterEach(() => { for (const app of apps.splice(0)) app.unmount() for (const container of containers.splice(0)) container.remove() installed?.dispose() runtime?.dispose() setDevtoolsHook(undefined!, {}) vi.unstubAllGlobals() runtime = undefined installed = undefined }) function installRuntime(collectionActive: boolean) { runtime = createDevtoolsRuntime({ collectionInitiallyActive: collectionActive, budget: { components: { updateDebounceMs: 0 } }, }) installed = installDevtoolsHook({ runtime, target: {} }) vi.stubGlobal('__VUE_DEVTOOLS_GLOBAL_HOOK__', installed.hook) setDevtoolsHook(installed.hook as unknown as Parameters[0], globalThis) return runtime } function mount(root: Component, host?: HTMLElement) { const app = createApp(root) const container = document.createElement('div') ;(host ?? document.body).append(container) containers.push(host ?? container) app.mount(container) apps.push(app) return app } const Item = defineComponent({ name: 'ListItem', props: ['label'], render() { return h('li', String(this.label)) }, }) function createList(labels: ReturnType>) { return defineComponent({ name: 'ListRoot', setup: () => () => h( 'ul', labels.value!.map((label) => h(Item, { key: label, label })), ), }) } async function treeNames(runtime: DevtoolsRuntime, appId: string) { const tree = await runtime.query({ appId, type: 'components:treeSnapshot' }) return tree.nodes.map((node) => node.name) } describe('hook events while collection is inactive', () => { it('skips component events and reads the current tree once a client connects', async () => { const runtime = installRuntime(false) const labels = ref(['a', 'b']) mount(createList(labels)) await nextTick() const dropped = runtime.performance.snapshot().droppedEvents labels.value = ['b', 'c', 'd'] await nextTick() // Adds, updates and removes are dropped at the hook without reaching the runtime. const after = runtime.performance.snapshot() expect(after.droppedEvents).toBeGreaterThan(dropped) expect(after.receivedEvents).toBeGreaterThanOrEqual(after.droppedEvents) const appId = runtime.registry.listApps()[0]!.id runtime.setCollectionActive(true, 1) expect((await treeNames(runtime, appId)).filter((name) => name === 'ListItem')).toHaveLength(3) // Live updates resume after the client connects. labels.value = ['d'] await nextTick() await new Promise((resolve) => setTimeout(resolve, 0)) expect((await treeNames(runtime, appId)).filter((name) => name === 'ListItem')).toHaveLength(1) }) it('still registers and unregisters apps while inactive', async () => { const runtime = installRuntime(false) const app = mount(createList(ref(['a']))) await nextTick() expect(runtime.registry.listApps()).toHaveLength(1) app.unmount() apps.splice(apps.indexOf(app), 1) expect(runtime.registry.listApps()).toHaveLength(0) }) }) describe('hidden DevTools apps', () => { it('ignores a hidden app across mount and later renders', async () => { const runtime = installRuntime(true) const hiddenLabels = ref(['x']) const hidden = createList(hiddenLabels) // `devtools.hide` is read from the root component options at runtime. mount({ ...hidden, name: 'HiddenRoot', devtools: { hide: true } } as Component) mount(createList(ref(['a']))) await nextTick() hiddenLabels.value = ['x', 'y'] await nextTick() const listed = runtime.registry.listApps() expect(listed).toHaveLength(1) expect(await treeNames(runtime, listed[0]!.id)).not.toContain('HiddenRoot') }) it('ignores an app mounted in a Vite DevTools shadow root across renders', async () => { const runtime = installRuntime(true) const host = document.createElement('vite-devtools-panel') document.body.append(host) containers.push(host) const labels = ref(['x']) // Vue sets `_container` only after the first render, so the mount-time component // events must not cache the app as visible. const app = createApp(createList(labels)) app.mount(host.attachShadow({ mode: 'open' }) as unknown as Element) apps.push(app) await nextTick() labels.value = ['x', 'y'] await nextTick() expect(runtime.registry.listApps()).toHaveLength(0) }) })