apiCreateDynamicComponent.spec.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { shallowRef } from '@vue/reactivity'
  2. import { nextTick } from '@vue/runtime-dom'
  3. import { createDynamicComponent } from '../src'
  4. import { makeRender } from './_utils'
  5. const define = makeRender()
  6. describe('api: createDynamicComponent', () => {
  7. const A = () => document.createTextNode('AAA')
  8. const B = () => document.createTextNode('BBB')
  9. test('direct value', async () => {
  10. const val = shallowRef<any>(A)
  11. const { html } = define({
  12. setup() {
  13. return createDynamicComponent(() => val.value)
  14. },
  15. }).render()
  16. expect(html()).toBe('AAA<!--dynamic-component-->')
  17. val.value = B
  18. await nextTick()
  19. expect(html()).toBe('BBB<!--dynamic-component-->')
  20. // fallback
  21. val.value = 'foo'
  22. await nextTick()
  23. expect(html()).toBe('<foo></foo><!--dynamic-component-->')
  24. })
  25. test('global registration', async () => {
  26. const val = shallowRef('foo')
  27. const { app, html, mount } = define({
  28. setup() {
  29. return createDynamicComponent(() => val.value)
  30. },
  31. }).create()
  32. app.component('foo', A)
  33. app.component('bar', B)
  34. mount()
  35. expect(html()).toBe('AAA<!--dynamic-component-->')
  36. val.value = 'bar'
  37. await nextTick()
  38. expect(html()).toBe('BBB<!--dynamic-component-->')
  39. // fallback
  40. val.value = 'baz'
  41. await nextTick()
  42. expect(html()).toBe('<baz></baz><!--dynamic-component-->')
  43. })
  44. })