-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
113 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/runtime-vapor/__tests__/apiCreateDynamicComponent.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { shallowRef } from '@vue/reactivity' | ||
import { nextTick } from '@vue/runtime-dom' | ||
import { createDynamicComponent } from '../src' | ||
import { makeRender } from './_utils' | ||
|
||
const define = makeRender() | ||
|
||
describe('api: createDynamicComponent', () => { | ||
const A = () => document.createTextNode('AAA') | ||
const B = () => document.createTextNode('BBB') | ||
|
||
test('direct value', async () => { | ||
const val = shallowRef<any>(A) | ||
|
||
const { html } = define({ | ||
setup() { | ||
return createDynamicComponent(() => val.value) | ||
}, | ||
}).render() | ||
|
||
expect(html()).toBe('AAA<!--dynamic-component-->') | ||
|
||
val.value = B | ||
await nextTick() | ||
expect(html()).toBe('BBB<!--dynamic-component-->') | ||
|
||
// fallback | ||
val.value = 'foo' | ||
await nextTick() | ||
expect(html()).toBe('<foo></foo><!--dynamic-component-->') | ||
}) | ||
|
||
test('global registration', async () => { | ||
const val = shallowRef('foo') | ||
|
||
const { app, html, mount } = define({ | ||
setup() { | ||
return createDynamicComponent(() => val.value) | ||
}, | ||
}).create() | ||
|
||
app.component('foo', A) | ||
app.component('bar', B) | ||
|
||
mount() | ||
expect(html()).toBe('AAA<!--dynamic-component-->') | ||
|
||
val.value = 'bar' | ||
await nextTick() | ||
expect(html()).toBe('BBB<!--dynamic-component-->') | ||
|
||
// fallback | ||
val.value = 'baz' | ||
await nextTick() | ||
expect(html()).toBe('<baz></baz><!--dynamic-component-->') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { resolveDynamicComponent } from '@vue/runtime-dom' | ||
import { DynamicFragment, type Fragment } from './block' | ||
import { createComponentWithFallback } from './component' | ||
import { renderEffect } from './renderEffect' | ||
import type { RawProps } from './componentProps' | ||
import type { RawSlots } from './componentSlots' | ||
|
||
export function createDynamicComponent( | ||
getter: () => any, | ||
rawProps?: RawProps | null, | ||
rawSlots?: RawSlots | null, | ||
isSingleRoot?: boolean, | ||
): Fragment { | ||
const frag = __DEV__ | ||
? new DynamicFragment('dynamic-component') | ||
: new DynamicFragment() | ||
renderEffect(() => { | ||
const value = getter() | ||
frag.update( | ||
() => | ||
createComponentWithFallback( | ||
resolveDynamicComponent(value) as any, | ||
rawProps, | ||
rawSlots, | ||
isSingleRoot, | ||
), | ||
value, | ||
) | ||
}) | ||
return frag | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters