Browse Source

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

三咲智子 Kevin Deng 1 year ago
parent
commit
598b55f1e8

+ 31 - 17
packages/runtime-vapor/__tests__/apiExpose.spec.ts

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

+ 5 - 9
packages/runtime-vapor/__tests__/apiInject.spec.ts

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

+ 0 - 1
packages/runtime-vapor/src/component.ts

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