|
@@ -1,10 +1,13 @@
|
|
|
import type { NodeRef } from '../../src/apiTemplateRef'
|
|
import type { NodeRef } from '../../src/apiTemplateRef'
|
|
|
import {
|
|
import {
|
|
|
|
|
+ child,
|
|
|
createComponent,
|
|
createComponent,
|
|
|
|
|
+ createDynamicComponent,
|
|
|
createFor,
|
|
createFor,
|
|
|
createIf,
|
|
createIf,
|
|
|
createSlot,
|
|
createSlot,
|
|
|
createTemplateRefSetter,
|
|
createTemplateRefSetter,
|
|
|
|
|
+ defineVaporComponent,
|
|
|
insert,
|
|
insert,
|
|
|
renderEffect,
|
|
renderEffect,
|
|
|
template,
|
|
template,
|
|
@@ -19,7 +22,8 @@ import {
|
|
|
useTemplateRef,
|
|
useTemplateRef,
|
|
|
watchEffect,
|
|
watchEffect,
|
|
|
} from '@vue/runtime-dom'
|
|
} from '@vue/runtime-dom'
|
|
|
-import { setElementText } from '../../src/dom/prop'
|
|
|
|
|
|
|
+import { setElementText, setText } from '../../src/dom/prop'
|
|
|
|
|
+import type { VaporComponent } from '../../src/component'
|
|
|
|
|
|
|
|
const define = makeRender()
|
|
const define = makeRender()
|
|
|
|
|
|
|
@@ -676,6 +680,39 @@ describe('api: template ref', () => {
|
|
|
expect(r!.value).toBe(n)
|
|
expect(r!.value).toBe(n)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+ test('work with dynamic component', async () => {
|
|
|
|
|
+ const Child = defineVaporComponent({
|
|
|
|
|
+ setup(_, { expose }) {
|
|
|
|
|
+ const msg = ref('one')
|
|
|
|
|
+ expose({ setMsg: (m: string) => (msg.value = m) })
|
|
|
|
|
+ const n0 = template(`<div> </div>`)() as any
|
|
|
|
|
+ const x0 = child(n0) as any
|
|
|
|
|
+ renderEffect(() => setText(x0, msg.value))
|
|
|
|
|
+ return n0
|
|
|
|
|
+ },
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ const views: Record<string, VaporComponent> = { child: Child }
|
|
|
|
|
+ const view = ref('child')
|
|
|
|
|
+ const refKey = ref<any>(null)
|
|
|
|
|
+
|
|
|
|
|
+ const { html } = define({
|
|
|
|
|
+ setup() {
|
|
|
|
|
+ const setRef = createTemplateRefSetter()
|
|
|
|
|
+ const n0 = createDynamicComponent(() => views[view.value]) as any
|
|
|
|
|
+ setRef(n0, refKey)
|
|
|
|
|
+ return n0
|
|
|
|
|
+ },
|
|
|
|
|
+ }).render()
|
|
|
|
|
+
|
|
|
|
|
+ expect(html()).toBe('<div>one</div><!--dynamic-component-->')
|
|
|
|
|
+ expect(refKey.value).toBeDefined()
|
|
|
|
|
+
|
|
|
|
|
+ refKey.value.setMsg('changed')
|
|
|
|
|
+ await nextTick()
|
|
|
|
|
+ expect(html()).toBe('<div>changed</div><!--dynamic-component-->')
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
// TODO: can not reproduce in Vapor
|
|
// TODO: can not reproduce in Vapor
|
|
|
// // #2078
|
|
// // #2078
|
|
|
// test('handling multiple merged refs', async () => {
|
|
// test('handling multiple merged refs', async () => {
|