Skip to content

Commit 6b43083

Browse files
tests(devtools-kit): use vi.waitFor instead of setTimeout and add test for #247 (#259)
1 parent 7c18de5 commit 6b43083

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

packages/devtools-kit/__tests__/api/api.test.ts

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { devtools } from '@vue/devtools-kit'
22
import { mount } from '@vue/test-utils'
33
import type { Plugin } from 'vue'
4-
import { resetDevToolsContext, resetDevToolsState } from '../../src/state'
4+
import { devtoolsAppRecords, resetDevToolsContext, resetDevToolsState } from '../../src/state'
55

66
import { DevToolsPluginApi } from '../../src/api'
77
import { onDevToolsConnected, setupDevToolsPlugin } from '../../src'
8+
import { DevToolsHooks } from '../../src/types'
89
import App from '../fixtures/App.vue'
910

1011
function createDevToolsPlugin(fn: (api: DevToolsPluginApi) => void): Plugin {
@@ -35,10 +36,12 @@ describe('devtools api', () => {
3536
color: 0x92A2BF,
3637
}
3738
devtools.hook.on.vueAppInit(() => {
38-
setTimeout(() => {
39-
expect(devtools.context.timelineLayer).toEqual([timelineLayerData])
40-
resolve()
41-
}, 300)
39+
vi.waitFor(
40+
() => {
41+
expect(devtools.context.timelineLayer).toEqual([timelineLayerData])
42+
resolve()
43+
},
44+
)
4245
})
4346
mount(App, {
4447
attachTo: document.body,
@@ -88,6 +91,13 @@ describe('devtools api', () => {
8891

8992
it('should work w/ addInspector api', async () => {
9093
await new Promise<void>((resolve) => {
94+
// Originated from https://github.com/vuejs/devtools-next/blob/main/packages/devtools-kit/src/plugins/component.ts#L20-L24
95+
const componentInspector = {
96+
id: 'components',
97+
nodeId: '',
98+
filter: '',
99+
treeFilterPlaceholder: 'Search components',
100+
}
91101
const inspectorData = {
92102
id: 'vueuse',
93103
label: 'VueUse',
@@ -96,10 +106,13 @@ describe('devtools api', () => {
96106
treeFilterPlaceholder: 'Search',
97107
}
98108
devtools.hook.on.vueAppInit(() => {
99-
setTimeout(() => {
100-
expect(devtools.context.inspector).toEqual([inspectorData])
101-
}, 100)
102-
resolve()
109+
vi.waitFor(
110+
() => {
111+
const { label, ...inspectorDataWithoutLabel } = inspectorData
112+
expect(devtools.context.inspector).toEqual([inspectorDataWithoutLabel, componentInspector])
113+
resolve()
114+
},
115+
)
103116
})
104117
mount(App, {
105118
attachTo: document.body,
@@ -111,4 +124,27 @@ describe('devtools api', () => {
111124
})
112125
})
113126
})
127+
128+
it('legacy plugin can be registered after app is created', async () => {
129+
// Refs: #247
130+
await new Promise<void>((resolve) => {
131+
const setupFn = vitest.fn()
132+
const globalHook = __VUE_DEVTOOLS_GLOBAL_HOOK__
133+
134+
mount(App, {
135+
attachTo: document.body,
136+
})
137+
138+
onDevToolsConnected(() => {
139+
const { app, api } = devtoolsAppRecords.active
140+
expect(setupFn).not.toHaveBeenCalled()
141+
globalHook.emit(DevToolsHooks.SETUP_DEVTOOLS_PLUGIN, { app }, setupFn)
142+
143+
vi.waitFor(() => {
144+
expect(setupFn).toBeCalledWith(api)
145+
resolve()
146+
})
147+
})
148+
})
149+
})
114150
})

0 commit comments

Comments
 (0)