]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test(runtime-vapor): add provide/inject tests for vdom interop
authordaiwei <daiwei521@126.com>
Mon, 17 Nov 2025 08:06:49 +0000 (16:06 +0800)
committeredison <daiwei521@126.com>
Mon, 17 Nov 2025 08:41:34 +0000 (16:41 +0800)
packages/runtime-vapor/__tests__/apiInject.spec.ts
packages/runtime-vapor/__tests__/vdomInterop.spec.ts

index 845ec9e015645fa0a02367ca3aacc4d09c73c6bc..50507715063ee0ecf9acff138f2da9572e8ff45d 100644 (file)
@@ -1,6 +1,7 @@
 import {
   type InjectionKey,
   type Ref,
+  h,
   hasInjectionContext,
   inject,
   nextTick,
@@ -17,6 +18,7 @@ import {
   createVaporApp,
   defineVaporComponent,
   renderEffect,
+  vaporInteropPlugin,
   withVaporCtx,
 } from '../src'
 import { makeRender } from './_utils'
@@ -422,3 +424,34 @@ describe('api: provide/inject', () => {
     })
   })
 })
+
+describe('vdom interop', () => {
+  test('should inject value from vapor parent', async () => {
+    const VdomChild = {
+      setup() {
+        const foo = inject('foo')
+        return () => h('div', null, [toDisplayString(foo)])
+      },
+    }
+
+    const value = ref('foo')
+    const App = defineVaporComponent({
+      setup() {
+        provide('foo', value)
+        return createComponent(VdomChild as any)
+      },
+    })
+
+    const root = document.createElement('div')
+    document.body.appendChild(root)
+    const app = createVaporApp(App)
+    app.use(vaporInteropPlugin)
+    app.mount(root)
+
+    expect(root.innerHTML).toBe('<div>foo</div>')
+
+    value.value = 'bar'
+    await nextTick()
+    expect(root.innerHTML).toBe('<div>bar</div>')
+  })
+})
index 546078c8bcbd91a1170c52ca48bc2accf8acad27..68861845c5d275be218f722899a1a30ccc857dd5 100644 (file)
@@ -3,12 +3,14 @@ import {
   createVNode,
   defineComponent,
   h,
+  inject,
   nextTick,
   onActivated,
   onBeforeMount,
   onDeactivated,
   onMounted,
   onUnmounted,
+  provide,
   ref,
   renderSlot,
   toDisplayString,
@@ -234,9 +236,32 @@ describe('vdomInterop', () => {
     })
   })
 
-  describe.todo('provide', () => {})
+  describe('provide / inject', () => {
+    it('should inject value from vdom parent', async () => {
+      const VaporChild = defineVaporComponent({
+        setup() {
+          const foo = inject('foo')
+          const n0 = template(' ')() as any
+          renderEffect(() => setText(n0, toDisplayString(foo)))
+          return n0
+        },
+      })
+
+      const value = ref('foo')
+      const { html } = define({
+        setup() {
+          provide('foo', value)
+          return () => h(VaporChild as any)
+        },
+      }).render()
+
+      expect(html()).toBe('foo')
 
-  describe.todo('inject', () => {})
+      value.value = 'bar'
+      await nextTick()
+      expect(html()).toBe('bar')
+    })
+  })
 
   describe.todo('template ref', () => {})