Skip to content

Commit 55bdced

Browse files
authored
fix(runtime-core): support uid key for useInstanceOption (#14272)
resolve vuejs/rfcs#814
1 parent 30dbdc1 commit 55bdced

2 files changed

Lines changed: 85 additions & 1 deletion

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { h, nodeOps, render, useInstanceOption } from '@vue/runtime-test'
2+
3+
describe('useInstanceOption', () => {
4+
test(`'ce' key`, () => {
5+
let hasInstance: boolean | undefined
6+
let ce: any
7+
const Comp = {
8+
setup() {
9+
const option = useInstanceOption('ce', true)
10+
hasInstance = option.hasInstance
11+
ce = option.value
12+
return () => null
13+
},
14+
}
15+
render(h(Comp), nodeOps.createElement('div'))
16+
expect(hasInstance).toBe(true)
17+
// custom element definition is not supported in test renderer, so check to access it is enough
18+
expect(ce).toBeUndefined()
19+
})
20+
21+
test(`'type' key`, () => {
22+
let hasInstance: boolean | undefined
23+
let type: any
24+
const Comp = {
25+
__i18n: { locale: 'en' }, // inject by custom blocks
26+
setup() {
27+
const option = useInstanceOption('type', true)
28+
hasInstance = option.hasInstance
29+
type = option.value
30+
return () => null
31+
},
32+
mounted() {},
33+
}
34+
render(h(Comp), nodeOps.createElement('div'))
35+
expect(hasInstance).toBe(true)
36+
expect('setup' in type).toBe(true)
37+
expect('mounted' in type).toBe(true)
38+
expect(type.__i18n).toEqual({ locale: 'en' })
39+
})
40+
41+
test(`'uid' key`, () => {
42+
let hasInstance: boolean | undefined
43+
let uid: any
44+
const Comp = {
45+
setup() {
46+
const option = useInstanceOption('uid', true)
47+
hasInstance = option.hasInstance
48+
uid = option.value
49+
return () => null
50+
},
51+
}
52+
render(h(Comp), nodeOps.createElement('div'))
53+
expect(hasInstance).toBe(true)
54+
expect(typeof uid).toBe('number')
55+
})
56+
57+
test('not allowed key', () => {
58+
let hasInstance: boolean | undefined
59+
let value: any
60+
const Comp = {
61+
setup() {
62+
const option = useInstanceOption('foo' as any, true)
63+
hasInstance = option.hasInstance
64+
value = option.value
65+
return () => null
66+
},
67+
}
68+
render(h(Comp), nodeOps.createElement('div'))
69+
expect(hasInstance).toBe(true)
70+
expect(value).toBeUndefined()
71+
expect(
72+
`useInstanceOption only accepts 'ce', 'type', 'uid' as key, got 'foo'.`,
73+
).toHaveBeenWarned()
74+
})
75+
76+
test('not active instance', () => {
77+
const { hasInstance, value } = useInstanceOption('type')
78+
expect(hasInstance).toBe(false)
79+
expect(value).toBeUndefined()
80+
expect(
81+
'useInstanceOption called without an active component instance.',
82+
).toHaveBeenWarned()
83+
})
84+
})

packages/runtime-core/src/componentCurrentInstance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const setCurrentInstance = (
9292
}
9393
}
9494

95-
const internalOptions = ['ce', 'type'] as const
95+
const internalOptions = ['ce', 'type', 'uid'] as const
9696

9797
/**
9898
* @internal

0 commit comments

Comments
 (0)