test(runtime-vapor): finish expose and inject tests

This commit is contained in:
三咲智子 Kevin Deng 2024-06-05 04:35:13 +08:00
parent bbd1944ce5
commit 598b55f1e8
No known key found for this signature in database
3 changed files with 36 additions and 27 deletions

View File

@ -6,11 +6,12 @@ import {
type ComponentInternalInstance, type ComponentInternalInstance,
getCurrentInstance, getCurrentInstance,
} from '../src/component' } from '../src/component'
import { defineComponent } from '../src/apiDefineComponent'
const define = makeRender() const define = makeRender()
describe('api: expose', () => { describe('api: expose', () => {
test('via setup context', () => { test('via setup context', () => {
const { component: Child } = define({ const Child = defineComponent({
setup(_, { expose }) { setup(_, { expose }) {
expose({ expose({
foo: 1, foo: 1,
@ -23,15 +24,14 @@ describe('api: expose', () => {
}, },
}) })
const childRef = ref() const childRef = ref()
const { render } = define({ define({
render: () => { render: () => {
const n0 = createComponent(Child) const n0 = createComponent(Child)
setRef(n0, childRef) setRef(n0, childRef)
return n0 return n0
}, },
}) }).render()
render()
expect(childRef.value).toBeTruthy() expect(childRef.value).toBeTruthy()
expect(childRef.value.foo).toBe(1) expect(childRef.value.foo).toBe(1)
expect(childRef.value.bar).toBe(2) expect(childRef.value.bar).toBe(2)
@ -40,56 +40,70 @@ describe('api: expose', () => {
test('via setup context (expose empty)', () => { test('via setup context (expose empty)', () => {
let childInstance: ComponentInternalInstance | null = null let childInstance: ComponentInternalInstance | null = null
const { component: Child } = define({ const Child = defineComponent({
setup(_) { setup(_) {
childInstance = getCurrentInstance() childInstance = getCurrentInstance()
}, },
}) })
const childRef = shallowRef() const childRef = shallowRef()
const { render } = define({ define({
render: () => { render: () => {
const n0 = createComponent(Child) const n0 = createComponent(Child)
setRef(n0, childRef) setRef(n0, childRef)
return n0 return n0
}, },
}) }).render()
render()
expect(childInstance!.exposed).toBeUndefined() expect(childInstance!.exposed).toBeUndefined()
expect(childRef.value).toBe(childInstance!) expect(childRef.value).toBe(childInstance!)
}) })
test('with mount', () => {
const { instance } = define({
setup(_, { expose }) {
expose({
foo: 1,
})
return {
bar: 2,
}
},
}).render()
expect(instance!.exposed!.foo).toBe(1)
expect(instance!.exposed!.bar).toBe(undefined)
})
test('warning for ref', () => { test('warning for ref', () => {
const { render } = define({ define({
setup(_, { expose }) { setup(_, { expose }) {
expose(ref(1)) expose(ref(1))
}, },
}) }).render()
render()
expect( expect(
'expose() should be passed a plain object, received ref', 'expose() should be passed a plain object, received ref',
).toHaveBeenWarned() ).toHaveBeenWarned()
}) })
test('warning for array', () => { test('warning for array', () => {
const { render } = define({ define({
setup(_, { expose }) { setup(_, { expose }) {
expose(['focus']) expose(['focus'])
}, },
}) }).render()
render()
expect( expect(
'expose() should be passed a plain object, received array', 'expose() should be passed a plain object, received array',
).toHaveBeenWarned() ).toHaveBeenWarned()
}) })
test('warning for function', () => { test('warning for function', () => {
const { render } = define({ define({
setup(_, { expose }) { setup(_, { expose }) {
expose(() => null) expose(() => null)
}, },
}) }).render()
render()
expect( expect(
'expose() should be passed a plain object, received function', 'expose() should be passed a plain object, received function',
).toHaveBeenWarned() ).toHaveBeenWarned()

View File

@ -1,5 +1,3 @@
// NOTE: This test is implemented based on the case of `runtime-core/__test__/apiInject.spec.ts`.
import { import {
type InjectionKey, type InjectionKey,
type Ref, type Ref,
@ -356,17 +354,15 @@ describe('api: provide/inject', () => {
}) })
// #2400 // #2400
it.todo('should not self-inject', () => { it('should not self-inject', () => {
const Comp = define({ const { host } = define({
setup() { setup() {
provide('foo', 'foo') provide('foo', 'foo')
const injection = inject('foo', null) const injection = inject('foo', null)
return () => injection return createTextNode(() => [injection])
}, },
}) }).render()
expect(host.innerHTML).toBe('')
Comp.render()
expect(Comp.host.innerHTML).toBe('')
}) })
describe('hasInjectionContext', () => { describe('hasInjectionContext', () => {

View File

@ -186,7 +186,6 @@ export interface ComponentInternalInstance {
attrsProxy?: Data attrsProxy?: Data
slotsProxy?: Slots slotsProxy?: Slots
exposeProxy?: Record<string, any>
// lifecycle // lifecycle
isMounted: boolean isMounted: boolean