1
1
import { devtools } from '@vue/devtools-kit'
2
2
import { mount } from '@vue/test-utils'
3
3
import type { Plugin } from 'vue'
4
- import { resetDevToolsContext , resetDevToolsState } from '../../src/state'
4
+ import { devtoolsAppRecords , resetDevToolsContext , resetDevToolsState } from '../../src/state'
5
5
6
6
import { DevToolsPluginApi } from '../../src/api'
7
7
import { onDevToolsConnected , setupDevToolsPlugin } from '../../src'
8
+ import { DevToolsHooks } from '../../src/types'
8
9
import App from '../fixtures/App.vue'
9
10
10
11
function createDevToolsPlugin ( fn : ( api : DevToolsPluginApi ) => void ) : Plugin {
@@ -35,10 +36,12 @@ describe('devtools api', () => {
35
36
color : 0x92A2BF ,
36
37
}
37
38
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
+ )
42
45
} )
43
46
mount ( App , {
44
47
attachTo : document . body ,
@@ -88,6 +91,13 @@ describe('devtools api', () => {
88
91
89
92
it ( 'should work w/ addInspector api' , async ( ) => {
90
93
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
+ }
91
101
const inspectorData = {
92
102
id : 'vueuse' ,
93
103
label : 'VueUse' ,
@@ -96,10 +106,13 @@ describe('devtools api', () => {
96
106
treeFilterPlaceholder : 'Search' ,
97
107
}
98
108
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
+ )
103
116
} )
104
117
mount ( App , {
105
118
attachTo : document . body ,
@@ -111,4 +124,27 @@ describe('devtools api', () => {
111
124
} )
112
125
} )
113
126
} )
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
+ } )
114
150
} )
0 commit comments