| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179 |
- import {
- type HMRRuntime,
- computed,
- createApp,
- h,
- nextTick,
- onActivated,
- onDeactivated,
- onMounted,
- onUnmounted,
- provide,
- ref,
- toDisplayString,
- } from '@vue/runtime-dom'
- import { compileToVaporRender as compileToFunction, makeRender } from './_utils'
- import {
- createComponent,
- createSlot,
- createTemplateRefSetter,
- defineVaporAsyncComponent,
- defineVaporComponent,
- delegateEvents,
- renderEffect,
- setText,
- template,
- vaporInteropPlugin,
- withVaporCtx,
- } from '@vue/runtime-vapor'
- import { BindingTypes } from '@vue/compiler-core'
- import type { VaporComponent } from '../src/component'
- declare var __VUE_HMR_RUNTIME__: HMRRuntime
- const { createRecord, rerender, reload } = __VUE_HMR_RUNTIME__
- const define = makeRender()
- const timeout = (n: number = 0) => new Promise(r => setTimeout(r, n))
- const triggerEvent = (type: string, el: Element) => {
- const event = new Event(type, { bubbles: true })
- el.dispatchEvent(event)
- }
- delegateEvents('click')
- beforeEach(() => {
- document.body.innerHTML = ''
- })
- describe('hot module replacement', () => {
- test('inject global runtime', () => {
- expect(createRecord).toBeDefined()
- expect(rerender).toBeDefined()
- expect(reload).toBeDefined()
- })
- test('createRecord', () => {
- expect(createRecord('test1', {})).toBe(true)
- // if id has already been created, should return false
- expect(createRecord('test1', {})).toBe(false)
- })
- test('rerender', async () => {
- const root = document.createElement('div')
- const parentId = 'test2-parent'
- const childId = 'test2-child'
- document.body.appendChild(root)
- const Child = defineVaporComponent({
- __hmrId: childId,
- render: compileToFunction('<div><slot/></div>'),
- })
- createRecord(childId, Child as any)
- const Parent = defineVaporComponent({
- __hmrId: parentId,
- components: { Child },
- setup() {
- const count = ref(0)
- return { count }
- },
- render: compileToFunction(
- `<div @click="count++">{{ count }}<Child>{{ count }}</Child></div>`,
- ),
- })
- createRecord(parentId, Parent as any)
- const { mount } = define(Parent).create()
- mount(root)
- expect(root.innerHTML).toBe(`<div>0<div>0<!--slot--></div></div>`)
- // Perform some state change. This change should be preserved after the
- // re-render!
- // triggerEvent(root.children[0] as TestElement, 'click')
- triggerEvent('click', root.children[0])
- await nextTick()
- expect(root.innerHTML).toBe(`<div>1<div>1<!--slot--></div></div>`)
- // Update text while preserving state
- rerender(
- parentId,
- compileToFunction(
- `<div @click="count++">{{ count }}!<Child>{{ count }}</Child></div>`,
- ),
- )
- expect(root.innerHTML).toBe(`<div>1!<div>1<!--slot--></div></div>`)
- // Should force child update on slot content change
- rerender(
- parentId,
- compileToFunction(
- `<div @click="count++">{{ count }}!<Child>{{ count }}!</Child></div>`,
- ),
- )
- expect(root.innerHTML).toBe(`<div>1!<div>1!<!--slot--></div></div>`)
- // Should force update element children despite block optimization
- rerender(
- parentId,
- compileToFunction(
- `<div @click="count++">{{ count }}<span>{{ count }}</span>
- <Child>{{ count }}!</Child>
- </div>`,
- ),
- )
- expect(root.innerHTML).toBe(
- `<div>1<span>1</span><div>1!<!--slot--></div></div>`,
- )
- // Should force update child slot elements
- rerender(
- parentId,
- compileToFunction(
- `<div @click="count++">
- <Child><span>{{ count }}</span></Child>
- </div>`,
- ),
- )
- expect(root.innerHTML).toBe(
- `<div><div><span>1</span><!--slot--></div></div>`,
- )
- })
- test('reload', async () => {
- const root = document.createElement('div')
- const childId = 'test3-child'
- const unmountSpy = vi.fn()
- const mountSpy = vi.fn()
- const Child = defineVaporComponent({
- __hmrId: childId,
- setup() {
- onUnmounted(unmountSpy)
- const count = ref(0)
- return { count }
- },
- render: compileToFunction(`<div @click="count++">{{ count }}</div>`),
- })
- createRecord(childId, Child as any)
- const Parent = defineVaporComponent({
- __hmrId: 'parentId',
- render: () => createComponent(Child),
- })
- define(Parent).create().mount(root)
- expect(root.innerHTML).toBe(`<div>0</div>`)
- reload(childId, {
- __hmrId: childId,
- setup() {
- onMounted(mountSpy)
- const count = ref(1)
- return { count }
- },
- render: compileToFunction(`<div @click="count++">{{ count }}</div>`),
- })
- await nextTick()
- expect(root.innerHTML).toBe(`<div>1</div>`)
- expect(unmountSpy).toHaveBeenCalledTimes(1)
- expect(mountSpy).toHaveBeenCalledTimes(1)
- })
- test('reload KeepAlive slot', async () => {
- const root = document.createElement('div')
- document.body.appendChild(root)
- const childId = 'test-child-keep-alive'
- const unmountSpy = vi.fn()
- const mountSpy = vi.fn()
- const activeSpy = vi.fn()
- const deactivatedSpy = vi.fn()
- const Child = defineVaporComponent({
- __hmrId: childId,
- setup() {
- onUnmounted(unmountSpy)
- const count = ref(0)
- return { count }
- },
- render: compileToFunction(`<div>{{ count }}</div>`),
- })
- createRecord(childId, Child as any)
- const Parent = defineVaporComponent({
- __hmrId: 'parentId',
- components: { Child },
- setup() {
- const toggle = ref(true)
- return { toggle }
- },
- render: compileToFunction(
- `<button @click="toggle = !toggle" />
- <KeepAlive><Child v-if="toggle" /></KeepAlive>`,
- ),
- })
- define(Parent).create().mount(root)
- expect(root.innerHTML).toBe(`<button></button><div>0</div><!--if-->`)
- reload(childId, {
- __hmrId: childId,
- __vapor: true,
- setup() {
- onMounted(mountSpy)
- onUnmounted(unmountSpy)
- onActivated(activeSpy)
- onDeactivated(deactivatedSpy)
- const count = ref(1)
- return { count }
- },
- render: compileToFunction(`<div>{{ count }}</div>`),
- })
- await nextTick()
- expect(root.innerHTML).toBe(`<button></button><div>1</div><!--if-->`)
- expect(unmountSpy).toHaveBeenCalledTimes(1)
- expect(mountSpy).toHaveBeenCalledTimes(1)
- expect(activeSpy).toHaveBeenCalledTimes(1)
- expect(deactivatedSpy).toHaveBeenCalledTimes(0)
- // should not unmount when toggling
- triggerEvent('click', root.children[0] as Element)
- await nextTick()
- expect(unmountSpy).toHaveBeenCalledTimes(1)
- expect(mountSpy).toHaveBeenCalledTimes(1)
- expect(activeSpy).toHaveBeenCalledTimes(1)
- expect(deactivatedSpy).toHaveBeenCalledTimes(1)
- // should not mount when toggling
- triggerEvent('click', root.children[0] as Element)
- await nextTick()
- expect(unmountSpy).toHaveBeenCalledTimes(1)
- expect(mountSpy).toHaveBeenCalledTimes(1)
- expect(activeSpy).toHaveBeenCalledTimes(2)
- expect(deactivatedSpy).toHaveBeenCalledTimes(1)
- })
- test('reload KeepAlive slot in Transition', async () => {
- const root = document.createElement('div')
- document.body.appendChild(root)
- const childId = 'test-transition-keep-alive-reload'
- const unmountSpy = vi.fn()
- const mountSpy = vi.fn()
- const activeSpy = vi.fn()
- const deactivatedSpy = vi.fn()
- const Child = defineVaporComponent({
- __hmrId: childId,
- setup() {
- onUnmounted(unmountSpy)
- const count = ref(0)
- return { count }
- },
- render: compileToFunction(`<div>{{ count }}</div>`),
- })
- createRecord(childId, Child as any)
- const Parent = defineVaporComponent({
- __hmrId: 'parentId',
- components: { Child },
- setup() {
- const toggle = ref(true)
- function onLeave(_: any, done: Function) {
- setTimeout(done, 0)
- }
- return { toggle, onLeave }
- },
- render: compileToFunction(
- `<button @click="toggle = !toggle" />
- <Transition @leave="onLeave">
- <KeepAlive><Child v-if="toggle" /></KeepAlive>
- </Transition>`,
- ),
- })
- define(Parent).create().mount(root)
- expect(root.innerHTML).toBe(`<button></button><div>0</div><!--if-->`)
- reload(childId, {
- __hmrId: childId,
- __vapor: true,
- setup() {
- onMounted(mountSpy)
- onUnmounted(unmountSpy)
- onActivated(activeSpy)
- onDeactivated(deactivatedSpy)
- const count = ref(1)
- return { count }
- },
- render: compileToFunction(`<div>{{ count }}</div>`),
- })
- await nextTick()
- await new Promise(r => setTimeout(r, 0))
- expect(root.innerHTML).toBe(`<button></button><div>1</div><!--if-->`)
- expect(unmountSpy).toHaveBeenCalledTimes(1)
- expect(mountSpy).toHaveBeenCalledTimes(1)
- expect(activeSpy).toHaveBeenCalledTimes(1)
- expect(deactivatedSpy).toHaveBeenCalledTimes(0)
- // should not unmount when toggling
- triggerEvent('click', root.children[0] as Element)
- await nextTick()
- expect(root.innerHTML).toBe(`<button></button><!--if-->`)
- expect(unmountSpy).toHaveBeenCalledTimes(1)
- expect(mountSpy).toHaveBeenCalledTimes(1)
- expect(activeSpy).toHaveBeenCalledTimes(1)
- expect(deactivatedSpy).toHaveBeenCalledTimes(1)
- // should not mount when toggling
- triggerEvent('click', root.children[0] as Element)
- await nextTick()
- expect(root.innerHTML).toBe(`<button></button><div>1</div><!--if-->`)
- expect(unmountSpy).toHaveBeenCalledTimes(1)
- expect(mountSpy).toHaveBeenCalledTimes(1)
- expect(activeSpy).toHaveBeenCalledTimes(2)
- expect(deactivatedSpy).toHaveBeenCalledTimes(1)
- })
- test('reload KeepAlive slot in Transition with out-in', async () => {
- const root = document.createElement('div')
- document.body.appendChild(root)
- const childId = 'test-transition-keep-alive-reload-with-out-in'
- const unmountSpy = vi.fn()
- const mountSpy = vi.fn()
- const activeSpy = vi.fn()
- const deactivatedSpy = vi.fn()
- const Child = defineVaporComponent({
- name: 'original',
- __hmrId: childId,
- setup() {
- onUnmounted(unmountSpy)
- const count = ref(0)
- return { count }
- },
- render: compileToFunction(`<div>{{ count }}</div>`),
- })
- createRecord(childId, Child as any)
- const Parent = defineVaporComponent({
- components: { Child },
- setup() {
- function onLeave(_: any, done: Function) {
- setTimeout(done, 0)
- }
- const toggle = ref(true)
- return { toggle, onLeave }
- },
- render: compileToFunction(
- `<button @click="toggle = !toggle" />
- <Transition mode="out-in" @leave="onLeave">
- <KeepAlive><Child v-if="toggle" /></KeepAlive>
- </Transition>`,
- ),
- })
- define(Parent).create().mount(root)
- expect(root.innerHTML).toBe(`<button></button><div>0</div><!--if-->`)
- reload(childId, {
- name: 'updated',
- __hmrId: childId,
- __vapor: true,
- setup() {
- onMounted(mountSpy)
- onUnmounted(unmountSpy)
- onActivated(activeSpy)
- onDeactivated(deactivatedSpy)
- const count = ref(1)
- return { count }
- },
- render: compileToFunction(`<div>{{ count }}</div>`),
- })
- await nextTick()
- await new Promise(r => setTimeout(r, 0))
- expect(root.innerHTML).toBe(`<button></button><div>1</div><!--if-->`)
- expect(unmountSpy).toHaveBeenCalledTimes(1)
- expect(mountSpy).toHaveBeenCalledTimes(1)
- expect(activeSpy).toHaveBeenCalledTimes(1)
- expect(deactivatedSpy).toHaveBeenCalledTimes(0)
- // should not unmount when toggling
- triggerEvent('click', root.children[0] as Element)
- await nextTick()
- await new Promise(r => setTimeout(r, 0))
- expect(root.innerHTML).toBe(`<button></button><!--if-->`)
- expect(unmountSpy).toHaveBeenCalledTimes(1)
- expect(mountSpy).toHaveBeenCalledTimes(1)
- expect(activeSpy).toHaveBeenCalledTimes(1)
- expect(deactivatedSpy).toHaveBeenCalledTimes(1)
- // should not mount when toggling
- triggerEvent('click', root.children[0] as Element)
- await nextTick()
- expect(root.innerHTML).toBe(`<button></button><div>1</div><!--if-->`)
- expect(unmountSpy).toHaveBeenCalledTimes(1)
- expect(mountSpy).toHaveBeenCalledTimes(1)
- expect(activeSpy).toHaveBeenCalledTimes(2)
- expect(deactivatedSpy).toHaveBeenCalledTimes(1)
- })
- // TODO: renderEffect not re-run after child reload
- // it requires parent rerender to align with vdom
- test.todo('reload: avoid infinite recursion', async () => {
- const root = document.createElement('div')
- document.body.appendChild(root)
- const childId = 'test-child-6930'
- const unmountSpy = vi.fn()
- const mountSpy = vi.fn()
- const Child = defineVaporComponent({
- __hmrId: childId,
- setup(_, { expose }) {
- const count = ref(0)
- expose({
- count,
- })
- onUnmounted(unmountSpy)
- return { count }
- },
- render: compileToFunction(`<div @click="count++">{{ count }}</div>`),
- })
- createRecord(childId, Child as any)
- const Parent = defineVaporComponent({
- setup() {
- const com1 = ref()
- const changeRef1 = (value: any) => (com1.value = value)
- const com2 = ref()
- const changeRef2 = (value: any) => (com2.value = value)
- const setRef = createTemplateRefSetter()
- const n0 = createComponent(Child)
- setRef(n0, changeRef1)
- const n1 = createComponent(Child)
- setRef(n1, changeRef2)
- const n2 = template(' ')() as any
- renderEffect(() => {
- setText(n2, toDisplayString(com1.value.count))
- })
- return [n0, n1, n2]
- },
- })
- define(Parent).create().mount(root)
- await nextTick()
- expect(root.innerHTML).toBe(`<div>0</div><div>0</div>0`)
- reload(childId, {
- __hmrId: childId,
- __vapor: true,
- setup() {
- onMounted(mountSpy)
- const count = ref(1)
- return { count }
- },
- render: compileToFunction(`<div @click="count++">{{ count }}</div>`),
- })
- await nextTick()
- expect(root.innerHTML).toBe(`<div>1</div><div>1</div>1`)
- expect(unmountSpy).toHaveBeenCalledTimes(2)
- expect(mountSpy).toHaveBeenCalledTimes(2)
- })
- test('static el reference', async () => {
- const root = document.createElement('div')
- document.body.appendChild(root)
- const id = 'test-static-el'
- const template = `<div>
- <div>{{ count }}</div>
- <button @click="count++">++</button>
- </div>`
- const Comp = defineVaporComponent({
- __hmrId: id,
- setup() {
- const count = ref(0)
- return { count }
- },
- render: compileToFunction(template),
- })
- createRecord(id, Comp as any)
- define(Comp).create().mount(root)
- expect(root.innerHTML).toBe(`<div><div>0</div><button>++</button></div>`)
- // 1. click to trigger update
- triggerEvent('click', root.children[0].children[1] as Element)
- await nextTick()
- expect(root.innerHTML).toBe(`<div><div>1</div><button>++</button></div>`)
- // 2. trigger HMR
- rerender(
- id,
- compileToFunction(template.replace(`<button`, `<button class="foo"`)),
- )
- expect(root.innerHTML).toBe(
- `<div><div>1</div><button class="foo">++</button></div>`,
- )
- })
- test('force update child component w/ static props', () => {
- const root = document.createElement('div')
- const parentId = 'test-force-props-parent'
- const childId = 'test-force-props-child'
- const Child = defineVaporComponent({
- __hmrId: childId,
- props: {
- msg: String,
- },
- render: compileToFunction(`<div>{{ msg }}</div>`, {
- bindingMetadata: {
- msg: BindingTypes.PROPS,
- },
- }),
- })
- createRecord(childId, Child as any)
- const Parent = defineVaporComponent({
- __hmrId: parentId,
- components: { Child },
- render: compileToFunction(`<Child msg="foo" />`),
- })
- createRecord(parentId, Parent as any)
- define(Parent).create().mount(root)
- expect(root.innerHTML).toBe(`<div>foo</div>`)
- rerender(parentId, compileToFunction(`<Child msg="bar" />`))
- expect(root.innerHTML).toBe(`<div>bar</div>`)
- })
- test('remove static class from parent', () => {
- const root = document.createElement('div')
- const parentId = 'test-force-class-parent'
- const childId = 'test-force-class-child'
- const Child = defineVaporComponent({
- __hmrId: childId,
- render: compileToFunction(`<div>child</div>`),
- })
- createRecord(childId, Child as any)
- const Parent = defineVaporComponent({
- __hmrId: parentId,
- components: { Child },
- render: compileToFunction(`<Child class="test" />`),
- })
- createRecord(parentId, Parent as any)
- define(Parent).create().mount(root)
- expect(root.innerHTML).toBe(`<div class="test">child</div>`)
- rerender(parentId, compileToFunction(`<Child/>`))
- expect(root.innerHTML).toBe(`<div>child</div>`)
- })
- test('rerender if any parent in the parent chain', () => {
- const root = document.createElement('div')
- const parent = 'test-force-props-parent-'
- const childId = 'test-force-props-child'
- const numberOfParents = 5
- const Child = defineVaporComponent({
- __hmrId: childId,
- render: compileToFunction(`<div>child</div>`),
- })
- createRecord(childId, Child as any)
- const components: VaporComponent[] = []
- for (let i = 0; i < numberOfParents; i++) {
- const parentId = `${parent}${i}`
- const parentComp: VaporComponent = {
- __vapor: true,
- __hmrId: parentId,
- }
- components.push(parentComp)
- if (i === 0) {
- parentComp.render = compileToFunction(`<Child />`)
- parentComp.components = {
- Child,
- }
- } else {
- parentComp.render = compileToFunction(`<Parent />`)
- parentComp.components = {
- Parent: components[i - 1],
- }
- }
- createRecord(parentId, parentComp as any)
- }
- const last = components[components.length - 1]
- define(last).create().mount(root)
- expect(root.innerHTML).toBe(`<div>child</div>`)
- rerender(last.__hmrId!, compileToFunction(`<Parent class="test"/>`))
- expect(root.innerHTML).toBe(`<div class="test">child</div>`)
- })
- test('rerender with Teleport', () => {
- const root = document.createElement('div')
- const target = document.createElement('div')
- document.body.appendChild(root)
- document.body.appendChild(target)
- const parentId = 'parent-teleport'
- const Child = defineVaporComponent({
- setup() {
- return { target }
- },
- render: compileToFunction(`
- <teleport :to="target">
- <div>
- <slot/>
- </div>
- </teleport>
- `),
- })
- const Parent = {
- __vapor: true,
- __hmrId: parentId,
- components: { Child },
- render: compileToFunction(`
- <Child>
- <template #default>
- <div>1</div>
- </template>
- </Child>
- `),
- }
- createRecord(parentId, Parent as any)
- define(Parent).create().mount(root)
- expect(root.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
- expect(target.innerHTML).toBe(`<div><div>1</div><!--slot--></div>`)
- rerender(
- parentId,
- compileToFunction(`
- <Child>
- <template #default>
- <div>1</div>
- <div>2</div>
- </template>
- </Child>
- `),
- )
- expect(root.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
- expect(target.innerHTML).toBe(
- `<div><div>1</div><div>2</div><!--slot--></div>`,
- )
- })
- test('rerender for component that has no active instance yet', () => {
- const id = 'no-active-instance-rerender'
- const Foo = {
- __vapor: true,
- __hmrId: id,
- render: () => template('foo')(),
- }
- createRecord(id, Foo)
- rerender(id, () => template('bar')())
- const root = document.createElement('div')
- define(Foo).create().mount(root)
- expect(root.innerHTML).toBe('bar')
- })
- test('reload for component that has no active instance yet', () => {
- const id = 'no-active-instance-reload'
- const Foo = {
- __vapor: true,
- __hmrId: id,
- render: () => template('foo')(),
- }
- createRecord(id, Foo)
- reload(id, {
- __hmrId: id,
- render: () => template('bar')(),
- })
- const root = document.createElement('div')
- define(Foo).render({}, root)
- expect(root.innerHTML).toBe('bar')
- })
- test('force update slot content change', () => {
- const root = document.createElement('div')
- const parentId = 'test-force-computed-parent'
- const childId = 'test-force-computed-child'
- const Child = {
- __vapor: true,
- __hmrId: childId,
- setup(_: any, { slots }: any) {
- const slotContent = computed(() => {
- return slots.default?.()
- })
- return { slotContent }
- },
- render: compileToFunction(`<component :is="() => slotContent" />`),
- }
- createRecord(childId, Child)
- const Parent = {
- __vapor: true,
- __hmrId: parentId,
- components: { Child },
- render: compileToFunction(`<Child>1</Child>`),
- }
- createRecord(parentId, Parent)
- // render(h(Parent), root)
- define(Parent).render({}, root)
- expect(root.innerHTML).toBe(`1<!--dynamic-component-->`)
- rerender(parentId, compileToFunction(`<Child>2</Child>`))
- expect(root.innerHTML).toBe(`2<!--dynamic-component-->`)
- })
- // #11248
- test('reload async component with multiple instances', async () => {
- const root = document.createElement('div')
- const childId = 'test-child-id'
- const Child = {
- __vapor: true,
- __hmrId: childId,
- setup() {
- const count = ref(0)
- return { count }
- },
- render: compileToFunction(`<div>{{ count }}</div>`),
- }
- const Comp = defineVaporAsyncComponent(() => Promise.resolve(Child))
- const appId = 'test-app-id'
- const App = {
- __hmrId: appId,
- render() {
- return [createComponent(Comp), createComponent(Comp)]
- },
- }
- createRecord(appId, App)
- define(App).render({}, root)
- await timeout()
- expect(root.innerHTML).toBe(
- `<div>0</div><!--async component--><div>0</div><!--async component-->`,
- )
- // change count to 1
- reload(childId, {
- __vapor: true,
- __hmrId: childId,
- setup() {
- const count = ref(1)
- return { count }
- },
- render: compileToFunction(`<div>{{ count }}</div>`),
- })
- await timeout()
- expect(root.innerHTML).toBe(
- `<div>1</div><!--async component--><div>1</div><!--async component-->`,
- )
- })
- test.todo('reload async child wrapped in Suspense + KeepAlive', async () => {
- // const id = 'async-child-reload'
- // const AsyncChild: ComponentOptions = {
- // __hmrId: id,
- // async setup() {
- // await nextTick()
- // return () => 'foo'
- // },
- // }
- // createRecord(id, AsyncChild)
- // const appId = 'test-app-id'
- // const App: ComponentOptions = {
- // __hmrId: appId,
- // components: { AsyncChild },
- // render: compileToFunction(`
- // <div>
- // <Suspense>
- // <KeepAlive>
- // <AsyncChild />
- // </KeepAlive>
- // </Suspense>
- // </div>
- // `),
- // }
- // const root = nodeOps.createElement('div')
- // render(h(App), root)
- // expect(serializeInner(root)).toBe('<div><!----></div>')
- // await timeout()
- // expect(serializeInner(root)).toBe('<div>foo</div>')
- // reload(id, {
- // __hmrId: id,
- // async setup() {
- // await nextTick()
- // return () => 'bar'
- // },
- // })
- // await timeout()
- // expect(serializeInner(root)).toBe('<div>bar</div>')
- })
- test.todo('multi reload child wrapped in Suspense + KeepAlive', async () => {
- // const id = 'test-child-reload-3'
- // const Child: ComponentOptions = {
- // __hmrId: id,
- // setup() {
- // const count = ref(0)
- // return { count }
- // },
- // render: compileToFunction(`<div>{{ count }}</div>`),
- // }
- // createRecord(id, Child)
- // const appId = 'test-app-id'
- // const App: ComponentOptions = {
- // __hmrId: appId,
- // components: { Child },
- // render: compileToFunction(`
- // <KeepAlive>
- // <Suspense>
- // <Child />
- // </Suspense>
- // </KeepAlive>
- // `),
- // }
- // const root = nodeOps.createElement('div')
- // render(h(App), root)
- // expect(serializeInner(root)).toBe('<div>0</div>')
- // await timeout()
- // reload(id, {
- // __hmrId: id,
- // setup() {
- // const count = ref(1)
- // return { count }
- // },
- // render: compileToFunction(`<div>{{ count }}</div>`),
- // })
- // await timeout()
- // expect(serializeInner(root)).toBe('<div>1</div>')
- // reload(id, {
- // __hmrId: id,
- // setup() {
- // const count = ref(2)
- // return { count }
- // },
- // render: compileToFunction(`<div>{{ count }}</div>`),
- // })
- // await timeout()
- // expect(serializeInner(root)).toBe('<div>2</div>')
- })
- test('rerender for nested component', () => {
- const id = 'child-nested-rerender'
- const Foo = {
- __vapor: true,
- __hmrId: id,
- setup(_ctx: any, { slots }: any) {
- return slots.default()
- },
- }
- createRecord(id, Foo)
- const parentId = 'parent-nested-rerender'
- const Parent = {
- __vapor: true,
- __hmrId: parentId,
- render() {
- return createComponent(
- Foo,
- {},
- {
- default: withVaporCtx(() => {
- return createSlot('default')
- }),
- },
- )
- },
- }
- const appId = 'app-nested-rerender'
- const App = {
- __vapor: true,
- __hmrId: appId,
- render: () =>
- createComponent(
- Parent,
- {},
- {
- default: withVaporCtx(() => {
- return createComponent(
- Foo,
- {},
- {
- default: () => template('foo')(),
- },
- )
- }),
- },
- ),
- }
- createRecord(parentId, App)
- const root = document.createElement('div')
- define(App).render({}, root)
- expect(root.innerHTML).toBe('foo<!--slot-->')
- rerender(id, () => template('bar')())
- expect(root.innerHTML).toBe('bar')
- })
- test('reload nested components from single update', async () => {
- const innerId = 'nested-reload-inner'
- const outerId = 'nested-reload-outer'
- let Inner = {
- __vapor: true,
- __hmrId: innerId,
- render() {
- return template('<div>foo</div>')()
- },
- }
- let Outer = {
- __vapor: true,
- __hmrId: outerId,
- render() {
- return createComponent(Inner as any)
- },
- }
- createRecord(innerId, Inner)
- createRecord(outerId, Outer)
- const App = {
- __vapor: true,
- render: () => createComponent(Outer),
- }
- const root = document.createElement('div')
- define(App).render({}, root)
- expect(root.innerHTML).toBe('<div>foo</div>')
- Inner = {
- __vapor: true,
- __hmrId: innerId,
- render() {
- return template('<div>bar</div>')()
- },
- }
- Outer = {
- __vapor: true,
- __hmrId: outerId,
- render() {
- return createComponent(Inner as any)
- },
- }
- // trigger reload for both Outer and Inner
- reload(outerId, Outer)
- reload(innerId, Inner)
- await nextTick()
- expect(root.innerHTML).toBe('<div>bar</div>')
- })
- test('child reload + parent reload', async () => {
- const root = document.createElement('div')
- const childId = 'test1-child-reload'
- const parentId = 'test1-parent-reload'
- const { component: Child } = define({
- __hmrId: childId,
- setup() {
- const msg = ref('child')
- return { msg }
- },
- render: compileToFunction(`<div>{{ msg }}</div>`),
- })
- createRecord(childId, Child as any)
- const { mount, component: Parent } = define({
- __hmrId: parentId,
- components: { Child },
- setup() {
- const msg = ref('root')
- return { msg }
- },
- render: compileToFunction(`<Child/><div>{{ msg }}</div>`),
- }).create()
- createRecord(parentId, Parent as any)
- mount(root)
- expect(root.innerHTML).toMatchInlineSnapshot(
- `"<div>child</div><div>root</div>"`,
- )
- // reload child
- reload(childId, {
- __hmrId: childId,
- __vapor: true,
- setup() {
- const msg = ref('child changed')
- return { msg }
- },
- render: compileToFunction(`<div>{{ msg }}</div>`),
- })
- expect(root.innerHTML).toMatchInlineSnapshot(
- `"<div>child changed</div><div>root</div>"`,
- )
- // reload child again
- reload(childId, {
- __hmrId: childId,
- __vapor: true,
- setup() {
- const msg = ref('child changed2')
- return { msg }
- },
- render: compileToFunction(`<div>{{ msg }}</div>`),
- })
- expect(root.innerHTML).toMatchInlineSnapshot(
- `"<div>child changed2</div><div>root</div>"`,
- )
- // reload parent
- reload(parentId, {
- __hmrId: parentId,
- __vapor: true,
- // @ts-expect-error
- components: { Child },
- setup() {
- const msg = ref('root changed')
- return { msg }
- },
- render: compileToFunction(`<Child/><div>{{ msg }}</div>`),
- })
- expect(root.innerHTML).toMatchInlineSnapshot(
- `"<div>child changed2</div><div>root changed</div>"`,
- )
- })
- // Vapor router-view has no render function (setup-only).
- // When HMR rerender is triggered, the setup function is re-executed.
- // Ensure provide() warning is suppressed.
- test('rerender setup-only component', async () => {
- const childId = 'test-child-reload-01'
- const Child = defineVaporComponent({
- __hmrId: childId,
- render: compileToFunction(`<div>foo</div>`),
- })
- createRecord(childId, Child as any)
- // without a render function
- const Parent = defineVaporComponent({
- setup() {
- provide('foo', 'bar')
- return createComponent(Child)
- },
- })
- const { html } = define({
- setup() {
- return createComponent(Parent)
- },
- }).render()
- expect(html()).toBe('<div>foo</div>')
- // will trigger parent rerender
- reload(childId, {
- __hmrId: childId,
- render: compileToFunction(`<div>bar</div>`),
- })
- await nextTick()
- expect(html()).toBe('<div>bar</div>')
- expect('provide() can only be used inside setup()').not.toHaveBeenWarned()
- })
- describe('switch vapor/vdom modes', () => {
- test('vapor -> vdom', async () => {
- const id = 'vapor-to-vdom'
- const Comp = {
- __vapor: true,
- __hmrId: id,
- render() {
- return template('<div>foo</div>')()
- },
- }
- createRecord(id, Comp)
- const App = {
- render() {
- return h(Comp as any)
- },
- }
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- expect(root.innerHTML).toBe('<div>foo</div>')
- // switch to vdom
- reload(id, {
- __hmrId: id,
- render() {
- return h('div', 'bar')
- },
- })
- await nextTick()
- expect(root.innerHTML).toBe('<div>bar</div>')
- })
- test('vdom -> vapor', async () => {
- const id = 'vdom-to-vapor'
- const Comp = {
- __hmrId: id,
- render() {
- return h('div', 'foo')
- },
- }
- createRecord(id, Comp)
- const App = {
- render() {
- return h(Comp)
- },
- }
- const root = document.createElement('div')
- const app = createApp(App)
- app.use(vaporInteropPlugin)
- app.mount(root)
- expect(root.innerHTML).toBe('<div>foo</div>')
- // switch to vapor
- reload(id, {
- __vapor: true,
- __hmrId: id,
- render() {
- return template('<div>bar</div>')()
- },
- })
- await nextTick()
- expect(root.innerHTML).toBe('<div>bar</div>')
- })
- })
- })
|