hmr.spec.ts 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. import {
  2. type HMRRuntime,
  3. computed,
  4. createApp,
  5. h,
  6. inject,
  7. nextTick,
  8. onActivated,
  9. onDeactivated,
  10. onMounted,
  11. onUnmounted,
  12. provide,
  13. ref,
  14. toDisplayString,
  15. } from '@vue/runtime-dom'
  16. import { compileToVaporRender as compileToFunction, makeRender } from './_utils'
  17. import {
  18. createComponent,
  19. createSlot,
  20. createTemplateRefSetter,
  21. createVaporApp,
  22. defineVaporAsyncComponent,
  23. defineVaporComponent,
  24. delegateEvents,
  25. renderEffect,
  26. setText,
  27. template,
  28. vaporInteropPlugin,
  29. withVaporCtx,
  30. } from '@vue/runtime-vapor'
  31. import { BindingTypes } from '@vue/compiler-core'
  32. import type { VaporComponent } from '../src/component'
  33. declare var __VUE_HMR_RUNTIME__: HMRRuntime
  34. const { createRecord, rerender, reload } = __VUE_HMR_RUNTIME__
  35. const define = makeRender()
  36. const timeout = (n: number = 0) => new Promise(r => setTimeout(r, n))
  37. const triggerEvent = (type: string, el: Element) => {
  38. const event = new Event(type, { bubbles: true })
  39. el.dispatchEvent(event)
  40. }
  41. delegateEvents('click')
  42. beforeEach(() => {
  43. document.body.innerHTML = ''
  44. })
  45. describe('hot module replacement', () => {
  46. test('inject global runtime', () => {
  47. expect(createRecord).toBeDefined()
  48. expect(rerender).toBeDefined()
  49. expect(reload).toBeDefined()
  50. })
  51. test('createRecord', () => {
  52. expect(createRecord('test1', {})).toBe(true)
  53. // if id has already been created, should return false
  54. expect(createRecord('test1', {})).toBe(false)
  55. })
  56. test('rerender', async () => {
  57. const root = document.createElement('div')
  58. const parentId = 'test2-parent'
  59. const childId = 'test2-child'
  60. document.body.appendChild(root)
  61. const Child = defineVaporComponent({
  62. __hmrId: childId,
  63. render: compileToFunction('<div><slot/></div>'),
  64. })
  65. createRecord(childId, Child as any)
  66. const Parent = defineVaporComponent({
  67. __hmrId: parentId,
  68. components: { Child },
  69. setup() {
  70. const count = ref(0)
  71. return { count }
  72. },
  73. render: compileToFunction(
  74. `<div @click="count++">{{ count }}<Child>{{ count }}</Child></div>`,
  75. ),
  76. })
  77. createRecord(parentId, Parent as any)
  78. const { mount } = define(Parent).create()
  79. mount(root)
  80. expect(root.innerHTML).toBe(`<div>0<div>0<!--slot--></div></div>`)
  81. // Perform some state change. This change should be preserved after the
  82. // re-render!
  83. // triggerEvent(root.children[0] as TestElement, 'click')
  84. triggerEvent('click', root.children[0])
  85. await nextTick()
  86. expect(root.innerHTML).toBe(`<div>1<div>1<!--slot--></div></div>`)
  87. // Update text while preserving state
  88. rerender(
  89. parentId,
  90. compileToFunction(
  91. `<div @click="count++">{{ count }}!<Child>{{ count }}</Child></div>`,
  92. ),
  93. )
  94. expect(root.innerHTML).toBe(`<div>1!<div>1<!--slot--></div></div>`)
  95. // Should force child update on slot content change
  96. rerender(
  97. parentId,
  98. compileToFunction(
  99. `<div @click="count++">{{ count }}!<Child>{{ count }}!</Child></div>`,
  100. ),
  101. )
  102. expect(root.innerHTML).toBe(`<div>1!<div>1!<!--slot--></div></div>`)
  103. // Should force update element children despite block optimization
  104. rerender(
  105. parentId,
  106. compileToFunction(
  107. `<div @click="count++">{{ count }}<span>{{ count }}</span>
  108. <Child>{{ count }}!</Child>
  109. </div>`,
  110. ),
  111. )
  112. expect(root.innerHTML).toBe(
  113. `<div>1<span>1</span><div>1!<!--slot--></div></div>`,
  114. )
  115. // Should force update child slot elements
  116. rerender(
  117. parentId,
  118. compileToFunction(
  119. `<div @click="count++">
  120. <Child><span>{{ count }}</span></Child>
  121. </div>`,
  122. ),
  123. )
  124. expect(root.innerHTML).toBe(
  125. `<div><div><span>1</span><!--slot--></div></div>`,
  126. )
  127. })
  128. test('reload', async () => {
  129. const root = document.createElement('div')
  130. const childId = 'test3-child'
  131. const unmountSpy = vi.fn()
  132. const mountSpy = vi.fn()
  133. const Child = defineVaporComponent({
  134. __hmrId: childId,
  135. setup() {
  136. onUnmounted(unmountSpy)
  137. const count = ref(0)
  138. return { count }
  139. },
  140. render: compileToFunction(`<div @click="count++">{{ count }}</div>`),
  141. })
  142. createRecord(childId, Child as any)
  143. const Parent = defineVaporComponent({
  144. __hmrId: 'parentId',
  145. render: () => createComponent(Child),
  146. })
  147. define(Parent).create().mount(root)
  148. expect(root.innerHTML).toBe(`<div>0</div>`)
  149. reload(childId, {
  150. __hmrId: childId,
  151. setup() {
  152. onMounted(mountSpy)
  153. const count = ref(1)
  154. return { count }
  155. },
  156. render: compileToFunction(`<div @click="count++">{{ count }}</div>`),
  157. })
  158. await nextTick()
  159. expect(root.innerHTML).toBe(`<div>1</div>`)
  160. expect(unmountSpy).toHaveBeenCalledTimes(1)
  161. expect(mountSpy).toHaveBeenCalledTimes(1)
  162. })
  163. test('reload root vapor component should preserve appContext provide/inject', async () => {
  164. const root = document.createElement('div')
  165. const appId = 'test-root-reload-app-context'
  166. const Child = defineVaporComponent({
  167. setup() {
  168. const msg = inject('msg')
  169. return { msg }
  170. },
  171. render: compileToFunction(`<div>{{ msg }}</div>`),
  172. })
  173. const App = defineVaporComponent({
  174. __hmrId: appId,
  175. render: () => createComponent(Child),
  176. })
  177. createRecord(appId, App as any)
  178. const app = createVaporApp(App)
  179. app.provide('msg', 'app-injected')
  180. app.mount(root)
  181. expect(root.innerHTML).toBe(`<div>app-injected</div>`)
  182. reload(appId, {
  183. __vapor: true,
  184. __hmrId: appId,
  185. render: () => createComponent(Child),
  186. })
  187. await nextTick()
  188. expect(root.innerHTML).toBe(`<div>app-injected</div>`)
  189. })
  190. test('reload KeepAlive slot', async () => {
  191. const root = document.createElement('div')
  192. document.body.appendChild(root)
  193. const childId = 'test-child-keep-alive'
  194. const unmountSpy = vi.fn()
  195. const mountSpy = vi.fn()
  196. const activeSpy = vi.fn()
  197. const deactivatedSpy = vi.fn()
  198. const Child = defineVaporComponent({
  199. __hmrId: childId,
  200. setup() {
  201. onUnmounted(unmountSpy)
  202. const count = ref(0)
  203. return { count }
  204. },
  205. render: compileToFunction(`<div>{{ count }}</div>`),
  206. })
  207. createRecord(childId, Child as any)
  208. const Parent = defineVaporComponent({
  209. __hmrId: 'parentId',
  210. components: { Child },
  211. setup() {
  212. const toggle = ref(true)
  213. return { toggle }
  214. },
  215. render: compileToFunction(
  216. `<button @click="toggle = !toggle" />
  217. <KeepAlive><Child v-if="toggle" /></KeepAlive>`,
  218. ),
  219. })
  220. define(Parent).create().mount(root)
  221. expect(root.innerHTML).toBe(`<button></button><div>0</div><!--if-->`)
  222. reload(childId, {
  223. __hmrId: childId,
  224. __vapor: true,
  225. setup() {
  226. onMounted(mountSpy)
  227. onUnmounted(unmountSpy)
  228. onActivated(activeSpy)
  229. onDeactivated(deactivatedSpy)
  230. const count = ref(1)
  231. return { count }
  232. },
  233. render: compileToFunction(`<div>{{ count }}</div>`),
  234. })
  235. await nextTick()
  236. expect(root.innerHTML).toBe(`<button></button><div>1</div><!--if-->`)
  237. expect(unmountSpy).toHaveBeenCalledTimes(1)
  238. expect(mountSpy).toHaveBeenCalledTimes(1)
  239. expect(activeSpy).toHaveBeenCalledTimes(1)
  240. expect(deactivatedSpy).toHaveBeenCalledTimes(0)
  241. // should not unmount when toggling
  242. triggerEvent('click', root.children[0] as Element)
  243. await nextTick()
  244. expect(unmountSpy).toHaveBeenCalledTimes(1)
  245. expect(mountSpy).toHaveBeenCalledTimes(1)
  246. expect(activeSpy).toHaveBeenCalledTimes(1)
  247. expect(deactivatedSpy).toHaveBeenCalledTimes(1)
  248. // should not mount when toggling
  249. triggerEvent('click', root.children[0] as Element)
  250. await nextTick()
  251. expect(unmountSpy).toHaveBeenCalledTimes(1)
  252. expect(mountSpy).toHaveBeenCalledTimes(1)
  253. expect(activeSpy).toHaveBeenCalledTimes(2)
  254. expect(deactivatedSpy).toHaveBeenCalledTimes(1)
  255. })
  256. test('reload KeepAlive slot in Transition', async () => {
  257. const root = document.createElement('div')
  258. document.body.appendChild(root)
  259. const childId = 'test-transition-keep-alive-reload'
  260. const unmountSpy = vi.fn()
  261. const mountSpy = vi.fn()
  262. const activeSpy = vi.fn()
  263. const deactivatedSpy = vi.fn()
  264. const Child = defineVaporComponent({
  265. __hmrId: childId,
  266. setup() {
  267. onUnmounted(unmountSpy)
  268. const count = ref(0)
  269. return { count }
  270. },
  271. render: compileToFunction(`<div>{{ count }}</div>`),
  272. })
  273. createRecord(childId, Child as any)
  274. const Parent = defineVaporComponent({
  275. __hmrId: 'parentId',
  276. components: { Child },
  277. setup() {
  278. const toggle = ref(true)
  279. function onLeave(_: any, done: Function) {
  280. setTimeout(done, 0)
  281. }
  282. return { toggle, onLeave }
  283. },
  284. render: compileToFunction(
  285. `<button @click="toggle = !toggle" />
  286. <Transition @leave="onLeave">
  287. <KeepAlive><Child v-if="toggle" /></KeepAlive>
  288. </Transition>`,
  289. ),
  290. })
  291. define(Parent).create().mount(root)
  292. expect(root.innerHTML).toBe(`<button></button><div>0</div><!--if-->`)
  293. reload(childId, {
  294. __hmrId: childId,
  295. __vapor: true,
  296. setup() {
  297. onMounted(mountSpy)
  298. onUnmounted(unmountSpy)
  299. onActivated(activeSpy)
  300. onDeactivated(deactivatedSpy)
  301. const count = ref(1)
  302. return { count }
  303. },
  304. render: compileToFunction(`<div>{{ count }}</div>`),
  305. })
  306. await nextTick()
  307. await new Promise(r => setTimeout(r, 0))
  308. expect(root.innerHTML).toBe(`<button></button><div>1</div><!--if-->`)
  309. expect(unmountSpy).toHaveBeenCalledTimes(1)
  310. expect(mountSpy).toHaveBeenCalledTimes(1)
  311. expect(activeSpy).toHaveBeenCalledTimes(1)
  312. expect(deactivatedSpy).toHaveBeenCalledTimes(0)
  313. // should not unmount when toggling
  314. triggerEvent('click', root.children[0] as Element)
  315. await nextTick()
  316. expect(root.innerHTML).toBe(`<button></button><!--if-->`)
  317. expect(unmountSpy).toHaveBeenCalledTimes(1)
  318. expect(mountSpy).toHaveBeenCalledTimes(1)
  319. expect(activeSpy).toHaveBeenCalledTimes(1)
  320. expect(deactivatedSpy).toHaveBeenCalledTimes(1)
  321. // should not mount when toggling
  322. triggerEvent('click', root.children[0] as Element)
  323. await nextTick()
  324. expect(root.innerHTML).toBe(`<button></button><div>1</div><!--if-->`)
  325. expect(unmountSpy).toHaveBeenCalledTimes(1)
  326. expect(mountSpy).toHaveBeenCalledTimes(1)
  327. expect(activeSpy).toHaveBeenCalledTimes(2)
  328. expect(deactivatedSpy).toHaveBeenCalledTimes(1)
  329. })
  330. test('reload KeepAlive slot in Transition with out-in', async () => {
  331. const root = document.createElement('div')
  332. document.body.appendChild(root)
  333. const childId = 'test-transition-keep-alive-reload-with-out-in'
  334. const unmountSpy = vi.fn()
  335. const mountSpy = vi.fn()
  336. const activeSpy = vi.fn()
  337. const deactivatedSpy = vi.fn()
  338. const Child = defineVaporComponent({
  339. name: 'original',
  340. __hmrId: childId,
  341. setup() {
  342. onUnmounted(unmountSpy)
  343. const count = ref(0)
  344. return { count }
  345. },
  346. render: compileToFunction(`<div>{{ count }}</div>`),
  347. })
  348. createRecord(childId, Child as any)
  349. const Parent = defineVaporComponent({
  350. components: { Child },
  351. setup() {
  352. function onLeave(_: any, done: Function) {
  353. setTimeout(done, 0)
  354. }
  355. const toggle = ref(true)
  356. return { toggle, onLeave }
  357. },
  358. render: compileToFunction(
  359. `<button @click="toggle = !toggle" />
  360. <Transition mode="out-in" @leave="onLeave">
  361. <KeepAlive><Child v-if="toggle" /></KeepAlive>
  362. </Transition>`,
  363. ),
  364. })
  365. define(Parent).create().mount(root)
  366. expect(root.innerHTML).toBe(`<button></button><div>0</div><!--if-->`)
  367. reload(childId, {
  368. name: 'updated',
  369. __hmrId: childId,
  370. __vapor: true,
  371. setup() {
  372. onMounted(mountSpy)
  373. onUnmounted(unmountSpy)
  374. onActivated(activeSpy)
  375. onDeactivated(deactivatedSpy)
  376. const count = ref(1)
  377. return { count }
  378. },
  379. render: compileToFunction(`<div>{{ count }}</div>`),
  380. })
  381. await nextTick()
  382. await new Promise(r => setTimeout(r, 0))
  383. expect(root.innerHTML).toBe(`<button></button><div>1</div><!--if-->`)
  384. expect(unmountSpy).toHaveBeenCalledTimes(1)
  385. expect(mountSpy).toHaveBeenCalledTimes(1)
  386. expect(activeSpy).toHaveBeenCalledTimes(1)
  387. expect(deactivatedSpy).toHaveBeenCalledTimes(0)
  388. // should not unmount when toggling
  389. triggerEvent('click', root.children[0] as Element)
  390. await nextTick()
  391. await new Promise(r => setTimeout(r, 0))
  392. expect(root.innerHTML).toBe(`<button></button><!--if-->`)
  393. expect(unmountSpy).toHaveBeenCalledTimes(1)
  394. expect(mountSpy).toHaveBeenCalledTimes(1)
  395. expect(activeSpy).toHaveBeenCalledTimes(1)
  396. expect(deactivatedSpy).toHaveBeenCalledTimes(1)
  397. // should not mount when toggling
  398. triggerEvent('click', root.children[0] as Element)
  399. await nextTick()
  400. expect(root.innerHTML).toBe(`<button></button><div>1</div><!--if-->`)
  401. expect(unmountSpy).toHaveBeenCalledTimes(1)
  402. expect(mountSpy).toHaveBeenCalledTimes(1)
  403. expect(activeSpy).toHaveBeenCalledTimes(2)
  404. expect(deactivatedSpy).toHaveBeenCalledTimes(1)
  405. })
  406. // TODO: renderEffect not re-run after child reload
  407. // it requires parent rerender to align with vdom
  408. test.todo('reload: avoid infinite recursion', async () => {
  409. const root = document.createElement('div')
  410. document.body.appendChild(root)
  411. const childId = 'test-child-6930'
  412. const unmountSpy = vi.fn()
  413. const mountSpy = vi.fn()
  414. const Child = defineVaporComponent({
  415. __hmrId: childId,
  416. setup(_, { expose }) {
  417. const count = ref(0)
  418. expose({
  419. count,
  420. })
  421. onUnmounted(unmountSpy)
  422. return { count }
  423. },
  424. render: compileToFunction(`<div @click="count++">{{ count }}</div>`),
  425. })
  426. createRecord(childId, Child as any)
  427. const Parent = defineVaporComponent({
  428. setup() {
  429. const com1 = ref()
  430. const changeRef1 = (value: any) => (com1.value = value)
  431. const com2 = ref()
  432. const changeRef2 = (value: any) => (com2.value = value)
  433. const setRef = createTemplateRefSetter()
  434. const n0 = createComponent(Child)
  435. setRef(n0, changeRef1)
  436. const n1 = createComponent(Child)
  437. setRef(n1, changeRef2)
  438. const n2 = template(' ')() as any
  439. renderEffect(() => {
  440. setText(n2, toDisplayString(com1.value.count))
  441. })
  442. return [n0, n1, n2]
  443. },
  444. })
  445. define(Parent).create().mount(root)
  446. await nextTick()
  447. expect(root.innerHTML).toBe(`<div>0</div><div>0</div>0`)
  448. reload(childId, {
  449. __hmrId: childId,
  450. __vapor: true,
  451. setup() {
  452. onMounted(mountSpy)
  453. const count = ref(1)
  454. return { count }
  455. },
  456. render: compileToFunction(`<div @click="count++">{{ count }}</div>`),
  457. })
  458. await nextTick()
  459. expect(root.innerHTML).toBe(`<div>1</div><div>1</div>1`)
  460. expect(unmountSpy).toHaveBeenCalledTimes(2)
  461. expect(mountSpy).toHaveBeenCalledTimes(2)
  462. })
  463. test('static el reference', async () => {
  464. const root = document.createElement('div')
  465. document.body.appendChild(root)
  466. const id = 'test-static-el'
  467. const template = `<div>
  468. <div>{{ count }}</div>
  469. <button @click="count++">++</button>
  470. </div>`
  471. const Comp = defineVaporComponent({
  472. __hmrId: id,
  473. setup() {
  474. const count = ref(0)
  475. return { count }
  476. },
  477. render: compileToFunction(template),
  478. })
  479. createRecord(id, Comp as any)
  480. define(Comp).create().mount(root)
  481. expect(root.innerHTML).toBe(`<div><div>0</div><button>++</button></div>`)
  482. // 1. click to trigger update
  483. triggerEvent('click', root.children[0].children[1] as Element)
  484. await nextTick()
  485. expect(root.innerHTML).toBe(`<div><div>1</div><button>++</button></div>`)
  486. // 2. trigger HMR
  487. rerender(
  488. id,
  489. compileToFunction(template.replace(`<button`, `<button class="foo"`)),
  490. )
  491. expect(root.innerHTML).toBe(
  492. `<div><div>1</div><button class="foo">++</button></div>`,
  493. )
  494. })
  495. test('force update child component w/ static props', () => {
  496. const root = document.createElement('div')
  497. const parentId = 'test-force-props-parent'
  498. const childId = 'test-force-props-child'
  499. const Child = defineVaporComponent({
  500. __hmrId: childId,
  501. props: {
  502. msg: String,
  503. },
  504. render: compileToFunction(`<div>{{ msg }}</div>`, {
  505. bindingMetadata: {
  506. msg: BindingTypes.PROPS,
  507. },
  508. }),
  509. })
  510. createRecord(childId, Child as any)
  511. const Parent = defineVaporComponent({
  512. __hmrId: parentId,
  513. components: { Child },
  514. render: compileToFunction(`<Child msg="foo" />`),
  515. })
  516. createRecord(parentId, Parent as any)
  517. define(Parent).create().mount(root)
  518. expect(root.innerHTML).toBe(`<div>foo</div>`)
  519. rerender(parentId, compileToFunction(`<Child msg="bar" />`))
  520. expect(root.innerHTML).toBe(`<div>bar</div>`)
  521. })
  522. test('remove static class from parent', () => {
  523. const root = document.createElement('div')
  524. const parentId = 'test-force-class-parent'
  525. const childId = 'test-force-class-child'
  526. const Child = defineVaporComponent({
  527. __hmrId: childId,
  528. render: compileToFunction(`<div>child</div>`),
  529. })
  530. createRecord(childId, Child as any)
  531. const Parent = defineVaporComponent({
  532. __hmrId: parentId,
  533. components: { Child },
  534. render: compileToFunction(`<Child class="test" />`),
  535. })
  536. createRecord(parentId, Parent as any)
  537. define(Parent).create().mount(root)
  538. expect(root.innerHTML).toBe(`<div class="test">child</div>`)
  539. rerender(parentId, compileToFunction(`<Child/>`))
  540. expect(root.innerHTML).toBe(`<div>child</div>`)
  541. })
  542. test('rerender if any parent in the parent chain', () => {
  543. const root = document.createElement('div')
  544. const parent = 'test-force-props-parent-'
  545. const childId = 'test-force-props-child'
  546. const numberOfParents = 5
  547. const Child = defineVaporComponent({
  548. __hmrId: childId,
  549. render: compileToFunction(`<div>child</div>`),
  550. })
  551. createRecord(childId, Child as any)
  552. const components: VaporComponent[] = []
  553. for (let i = 0; i < numberOfParents; i++) {
  554. const parentId = `${parent}${i}`
  555. const parentComp: VaporComponent = {
  556. __vapor: true,
  557. __hmrId: parentId,
  558. }
  559. components.push(parentComp)
  560. if (i === 0) {
  561. parentComp.render = compileToFunction(`<Child />`)
  562. parentComp.components = {
  563. Child,
  564. }
  565. } else {
  566. parentComp.render = compileToFunction(`<Parent />`)
  567. parentComp.components = {
  568. Parent: components[i - 1],
  569. }
  570. }
  571. createRecord(parentId, parentComp as any)
  572. }
  573. const last = components[components.length - 1]
  574. define(last).create().mount(root)
  575. expect(root.innerHTML).toBe(`<div>child</div>`)
  576. rerender(last.__hmrId!, compileToFunction(`<Parent class="test"/>`))
  577. expect(root.innerHTML).toBe(`<div class="test">child</div>`)
  578. })
  579. test('rerender with Teleport', () => {
  580. const root = document.createElement('div')
  581. const target = document.createElement('div')
  582. document.body.appendChild(root)
  583. document.body.appendChild(target)
  584. const parentId = 'parent-teleport'
  585. const Child = defineVaporComponent({
  586. setup() {
  587. return { target }
  588. },
  589. render: compileToFunction(`
  590. <teleport :to="target">
  591. <div>
  592. <slot/>
  593. </div>
  594. </teleport>
  595. `),
  596. })
  597. const Parent = {
  598. __vapor: true,
  599. __hmrId: parentId,
  600. components: { Child },
  601. render: compileToFunction(`
  602. <Child>
  603. <template #default>
  604. <div>1</div>
  605. </template>
  606. </Child>
  607. `),
  608. }
  609. createRecord(parentId, Parent as any)
  610. define(Parent).create().mount(root)
  611. expect(root.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
  612. expect(target.innerHTML).toBe(`<div><div>1</div><!--slot--></div>`)
  613. rerender(
  614. parentId,
  615. compileToFunction(`
  616. <Child>
  617. <template #default>
  618. <div>1</div>
  619. <div>2</div>
  620. </template>
  621. </Child>
  622. `),
  623. )
  624. expect(root.innerHTML).toBe(`<!--teleport start--><!--teleport end-->`)
  625. expect(target.innerHTML).toBe(
  626. `<div><div>1</div><div>2</div><!--slot--></div>`,
  627. )
  628. })
  629. test('rerender for component that has no active instance yet', () => {
  630. const id = 'no-active-instance-rerender'
  631. const Foo = {
  632. __vapor: true,
  633. __hmrId: id,
  634. render: () => template('foo')(),
  635. }
  636. createRecord(id, Foo)
  637. rerender(id, () => template('bar')())
  638. const root = document.createElement('div')
  639. define(Foo).create().mount(root)
  640. expect(root.innerHTML).toBe('bar')
  641. })
  642. test('reload for component that has no active instance yet', () => {
  643. const id = 'no-active-instance-reload'
  644. const Foo = {
  645. __vapor: true,
  646. __hmrId: id,
  647. render: () => template('foo')(),
  648. }
  649. createRecord(id, Foo)
  650. reload(id, {
  651. __hmrId: id,
  652. render: () => template('bar')(),
  653. })
  654. const root = document.createElement('div')
  655. define(Foo).render({}, root)
  656. expect(root.innerHTML).toBe('bar')
  657. })
  658. test('force update slot content change', () => {
  659. const root = document.createElement('div')
  660. const parentId = 'test-force-computed-parent'
  661. const childId = 'test-force-computed-child'
  662. const Child = {
  663. __vapor: true,
  664. __hmrId: childId,
  665. setup(_: any, { slots }: any) {
  666. const slotContent = computed(() => {
  667. return slots.default?.()
  668. })
  669. return { slotContent }
  670. },
  671. render: compileToFunction(`<component :is="() => slotContent" />`),
  672. }
  673. createRecord(childId, Child)
  674. const Parent = {
  675. __vapor: true,
  676. __hmrId: parentId,
  677. components: { Child },
  678. render: compileToFunction(`<Child>1</Child>`),
  679. }
  680. createRecord(parentId, Parent)
  681. // render(h(Parent), root)
  682. define(Parent).render({}, root)
  683. expect(root.innerHTML).toBe(`1<!--dynamic-component-->`)
  684. rerender(parentId, compileToFunction(`<Child>2</Child>`))
  685. expect(root.innerHTML).toBe(`2<!--dynamic-component-->`)
  686. })
  687. // #11248
  688. test('reload async component with multiple instances', async () => {
  689. const root = document.createElement('div')
  690. const childId = 'test-child-id'
  691. const Child = {
  692. __vapor: true,
  693. __hmrId: childId,
  694. setup() {
  695. const count = ref(0)
  696. return { count }
  697. },
  698. render: compileToFunction(`<div>{{ count }}</div>`),
  699. }
  700. const Comp = defineVaporAsyncComponent(() => Promise.resolve(Child))
  701. const appId = 'test-app-id'
  702. const App = {
  703. __hmrId: appId,
  704. render() {
  705. return [createComponent(Comp), createComponent(Comp)]
  706. },
  707. }
  708. createRecord(appId, App)
  709. define(App).render({}, root)
  710. await timeout()
  711. expect(root.innerHTML).toBe(
  712. `<div>0</div><!--async component--><div>0</div><!--async component-->`,
  713. )
  714. // change count to 1
  715. reload(childId, {
  716. __vapor: true,
  717. __hmrId: childId,
  718. setup() {
  719. const count = ref(1)
  720. return { count }
  721. },
  722. render: compileToFunction(`<div>{{ count }}</div>`),
  723. })
  724. await timeout()
  725. expect(root.innerHTML).toBe(
  726. `<div>1</div><!--async component--><div>1</div><!--async component-->`,
  727. )
  728. })
  729. test.todo('reload async child wrapped in Suspense + KeepAlive', async () => {
  730. // const id = 'async-child-reload'
  731. // const AsyncChild: ComponentOptions = {
  732. // __hmrId: id,
  733. // async setup() {
  734. // await nextTick()
  735. // return () => 'foo'
  736. // },
  737. // }
  738. // createRecord(id, AsyncChild)
  739. // const appId = 'test-app-id'
  740. // const App: ComponentOptions = {
  741. // __hmrId: appId,
  742. // components: { AsyncChild },
  743. // render: compileToFunction(`
  744. // <div>
  745. // <Suspense>
  746. // <KeepAlive>
  747. // <AsyncChild />
  748. // </KeepAlive>
  749. // </Suspense>
  750. // </div>
  751. // `),
  752. // }
  753. // const root = nodeOps.createElement('div')
  754. // render(h(App), root)
  755. // expect(serializeInner(root)).toBe('<div><!----></div>')
  756. // await timeout()
  757. // expect(serializeInner(root)).toBe('<div>foo</div>')
  758. // reload(id, {
  759. // __hmrId: id,
  760. // async setup() {
  761. // await nextTick()
  762. // return () => 'bar'
  763. // },
  764. // })
  765. // await timeout()
  766. // expect(serializeInner(root)).toBe('<div>bar</div>')
  767. })
  768. test.todo('multi reload child wrapped in Suspense + KeepAlive', async () => {
  769. // const id = 'test-child-reload-3'
  770. // const Child: ComponentOptions = {
  771. // __hmrId: id,
  772. // setup() {
  773. // const count = ref(0)
  774. // return { count }
  775. // },
  776. // render: compileToFunction(`<div>{{ count }}</div>`),
  777. // }
  778. // createRecord(id, Child)
  779. // const appId = 'test-app-id'
  780. // const App: ComponentOptions = {
  781. // __hmrId: appId,
  782. // components: { Child },
  783. // render: compileToFunction(`
  784. // <KeepAlive>
  785. // <Suspense>
  786. // <Child />
  787. // </Suspense>
  788. // </KeepAlive>
  789. // `),
  790. // }
  791. // const root = nodeOps.createElement('div')
  792. // render(h(App), root)
  793. // expect(serializeInner(root)).toBe('<div>0</div>')
  794. // await timeout()
  795. // reload(id, {
  796. // __hmrId: id,
  797. // setup() {
  798. // const count = ref(1)
  799. // return { count }
  800. // },
  801. // render: compileToFunction(`<div>{{ count }}</div>`),
  802. // })
  803. // await timeout()
  804. // expect(serializeInner(root)).toBe('<div>1</div>')
  805. // reload(id, {
  806. // __hmrId: id,
  807. // setup() {
  808. // const count = ref(2)
  809. // return { count }
  810. // },
  811. // render: compileToFunction(`<div>{{ count }}</div>`),
  812. // })
  813. // await timeout()
  814. // expect(serializeInner(root)).toBe('<div>2</div>')
  815. })
  816. test('rerender for nested component', () => {
  817. const id = 'child-nested-rerender'
  818. const Foo = {
  819. __vapor: true,
  820. __hmrId: id,
  821. setup(_ctx: any, { slots }: any) {
  822. return slots.default()
  823. },
  824. }
  825. createRecord(id, Foo)
  826. const parentId = 'parent-nested-rerender'
  827. const Parent = {
  828. __vapor: true,
  829. __hmrId: parentId,
  830. render() {
  831. return createComponent(
  832. Foo,
  833. {},
  834. {
  835. default: withVaporCtx(() => {
  836. return createSlot('default')
  837. }),
  838. },
  839. )
  840. },
  841. }
  842. const appId = 'app-nested-rerender'
  843. const App = {
  844. __vapor: true,
  845. __hmrId: appId,
  846. render: () =>
  847. createComponent(
  848. Parent,
  849. {},
  850. {
  851. default: withVaporCtx(() => {
  852. return createComponent(
  853. Foo,
  854. {},
  855. {
  856. default: () => template('foo')(),
  857. },
  858. )
  859. }),
  860. },
  861. ),
  862. }
  863. createRecord(parentId, App)
  864. const root = document.createElement('div')
  865. define(App).render({}, root)
  866. expect(root.innerHTML).toBe('foo<!--slot-->')
  867. rerender(id, () => template('bar')())
  868. expect(root.innerHTML).toBe('bar')
  869. })
  870. test('reload nested components from single update', async () => {
  871. const innerId = 'nested-reload-inner'
  872. const outerId = 'nested-reload-outer'
  873. let Inner = {
  874. __vapor: true,
  875. __hmrId: innerId,
  876. render() {
  877. return template('<div>foo</div>')()
  878. },
  879. }
  880. let Outer = {
  881. __vapor: true,
  882. __hmrId: outerId,
  883. render() {
  884. return createComponent(Inner as any)
  885. },
  886. }
  887. createRecord(innerId, Inner)
  888. createRecord(outerId, Outer)
  889. const App = {
  890. __vapor: true,
  891. render: () => createComponent(Outer),
  892. }
  893. const root = document.createElement('div')
  894. define(App).render({}, root)
  895. expect(root.innerHTML).toBe('<div>foo</div>')
  896. Inner = {
  897. __vapor: true,
  898. __hmrId: innerId,
  899. render() {
  900. return template('<div>bar</div>')()
  901. },
  902. }
  903. Outer = {
  904. __vapor: true,
  905. __hmrId: outerId,
  906. render() {
  907. return createComponent(Inner as any)
  908. },
  909. }
  910. // trigger reload for both Outer and Inner
  911. reload(outerId, Outer)
  912. reload(innerId, Inner)
  913. await nextTick()
  914. expect(root.innerHTML).toBe('<div>bar</div>')
  915. })
  916. test('child reload + parent reload', async () => {
  917. const root = document.createElement('div')
  918. const childId = 'test1-child-reload'
  919. const parentId = 'test1-parent-reload'
  920. const { component: Child } = define({
  921. __hmrId: childId,
  922. setup() {
  923. const msg = ref('child')
  924. return { msg }
  925. },
  926. render: compileToFunction(`<div>{{ msg }}</div>`),
  927. })
  928. createRecord(childId, Child as any)
  929. const { mount, component: Parent } = define({
  930. __hmrId: parentId,
  931. components: { Child },
  932. setup() {
  933. const msg = ref('root')
  934. return { msg }
  935. },
  936. render: compileToFunction(`<Child/><div>{{ msg }}</div>`),
  937. }).create()
  938. createRecord(parentId, Parent as any)
  939. mount(root)
  940. expect(root.innerHTML).toMatchInlineSnapshot(
  941. `"<div>child</div><div>root</div>"`,
  942. )
  943. // reload child
  944. reload(childId, {
  945. __hmrId: childId,
  946. __vapor: true,
  947. setup() {
  948. const msg = ref('child changed')
  949. return { msg }
  950. },
  951. render: compileToFunction(`<div>{{ msg }}</div>`),
  952. })
  953. expect(root.innerHTML).toMatchInlineSnapshot(
  954. `"<div>child changed</div><div>root</div>"`,
  955. )
  956. // reload child again
  957. reload(childId, {
  958. __hmrId: childId,
  959. __vapor: true,
  960. setup() {
  961. const msg = ref('child changed2')
  962. return { msg }
  963. },
  964. render: compileToFunction(`<div>{{ msg }}</div>`),
  965. })
  966. expect(root.innerHTML).toMatchInlineSnapshot(
  967. `"<div>child changed2</div><div>root</div>"`,
  968. )
  969. // reload parent
  970. reload(parentId, {
  971. __hmrId: parentId,
  972. __vapor: true,
  973. // @ts-expect-error
  974. components: { Child },
  975. setup() {
  976. const msg = ref('root changed')
  977. return { msg }
  978. },
  979. render: compileToFunction(`<Child/><div>{{ msg }}</div>`),
  980. })
  981. expect(root.innerHTML).toMatchInlineSnapshot(
  982. `"<div>child changed2</div><div>root changed</div>"`,
  983. )
  984. })
  985. test('child reload in dynamic branch should not break subsequent parent reload', async () => {
  986. const root = document.createElement('div')
  987. const childId = 'test-dynamic-child-reload'
  988. const parentId = 'test-dynamic-parent-reload'
  989. const Child = defineVaporComponent({
  990. __hmrId: childId,
  991. setup() {
  992. const msg = ref('child')
  993. return { msg }
  994. },
  995. render: compileToFunction(`<div>{{ msg }}</div>`),
  996. })
  997. createRecord(childId, Child as any)
  998. const { mount, component: Parent } = define({
  999. __hmrId: parentId,
  1000. components: { Child },
  1001. setup() {
  1002. const ok = ref(true)
  1003. return { ok }
  1004. },
  1005. render: compileToFunction(`<Child v-if="ok" />`),
  1006. }).create()
  1007. createRecord(parentId, Parent as any)
  1008. mount(root)
  1009. expect(root.innerHTML).toBe(`<div>child</div><!--if-->`)
  1010. reload(childId, {
  1011. __vapor: true,
  1012. __hmrId: childId,
  1013. setup() {
  1014. const msg = ref('child changed')
  1015. return { msg }
  1016. },
  1017. render: compileToFunction(`<div>{{ msg }}</div>`),
  1018. })
  1019. expect(root.innerHTML).toBe(`<div>child changed</div><!--if-->`)
  1020. reload(parentId, {
  1021. __vapor: true,
  1022. __hmrId: parentId,
  1023. components: { Child },
  1024. setup() {
  1025. const ok = ref(true)
  1026. return { ok }
  1027. },
  1028. render: compileToFunction(`<Child v-if="ok" />`),
  1029. })
  1030. await nextTick()
  1031. expect(root.innerHTML).toBe(`<div>child changed</div><!--if-->`)
  1032. })
  1033. test('child reload with multiple instances in dynamic branch should keep parent reload stable', async () => {
  1034. const root = document.createElement('div')
  1035. const childId = 'test-dynamic-multi-child-reload'
  1036. const parentId = 'test-dynamic-multi-parent-reload'
  1037. const Child = defineVaporComponent({
  1038. __hmrId: childId,
  1039. setup() {
  1040. const msg = ref('child')
  1041. return { msg }
  1042. },
  1043. render: compileToFunction(`<div>{{ msg }}</div>`),
  1044. })
  1045. createRecord(childId, Child as any)
  1046. const { mount, component: Parent } = define({
  1047. __hmrId: parentId,
  1048. components: { Child },
  1049. setup() {
  1050. const ok = ref(true)
  1051. return { ok }
  1052. },
  1053. render: compileToFunction(
  1054. `<template v-if="ok"><Child/><Child/></template>`,
  1055. ),
  1056. }).create()
  1057. createRecord(parentId, Parent as any)
  1058. mount(root)
  1059. expect(root.textContent).toBe(`childchild`)
  1060. reload(childId, {
  1061. __vapor: true,
  1062. __hmrId: childId,
  1063. setup() {
  1064. const msg = ref('child changed')
  1065. return { msg }
  1066. },
  1067. render: compileToFunction(`<div>{{ msg }}</div>`),
  1068. })
  1069. expect(root.textContent).toBe(`child changedchild changed`)
  1070. reload(parentId, {
  1071. __vapor: true,
  1072. __hmrId: parentId,
  1073. components: { Child },
  1074. setup() {
  1075. const ok = ref(true)
  1076. return { ok }
  1077. },
  1078. render: compileToFunction(
  1079. `<template v-if="ok"><Child/><Child/></template>`,
  1080. ),
  1081. })
  1082. await nextTick()
  1083. expect(root.textContent).toBe(`child changedchild changed`)
  1084. })
  1085. test('child reload in teleport dynamic branch should not break subsequent parent reload', async () => {
  1086. const root = document.createElement('div')
  1087. const target = document.createElement('div')
  1088. document.body.appendChild(root)
  1089. document.body.appendChild(target)
  1090. const childId = 'test-teleport-dynamic-child-reload'
  1091. const parentId = 'test-teleport-dynamic-parent-reload'
  1092. const Child = defineVaporComponent({
  1093. __hmrId: childId,
  1094. setup() {
  1095. const msg = ref('child')
  1096. return { msg }
  1097. },
  1098. render: compileToFunction(`<div>{{ msg }}</div>`),
  1099. })
  1100. createRecord(childId, Child as any)
  1101. const { mount, component: Parent } = define({
  1102. __hmrId: parentId,
  1103. components: { Child },
  1104. setup() {
  1105. const ok = ref(true)
  1106. return { ok, target }
  1107. },
  1108. render: compileToFunction(
  1109. `<teleport :to="target"><template v-if="ok"><Child/><span>sibling</span></template></teleport>`,
  1110. ),
  1111. }).create()
  1112. createRecord(parentId, Parent as any)
  1113. mount(root)
  1114. expect(target.textContent).toBe(`childsibling`)
  1115. reload(childId, {
  1116. __vapor: true,
  1117. __hmrId: childId,
  1118. setup() {
  1119. const msg = ref('child changed')
  1120. return { msg }
  1121. },
  1122. render: compileToFunction(`<div>{{ msg }}</div>`),
  1123. })
  1124. expect(target.textContent).toBe(`child changedsibling`)
  1125. reload(parentId, {
  1126. __vapor: true,
  1127. __hmrId: parentId,
  1128. components: { Child },
  1129. setup() {
  1130. const ok = ref(true)
  1131. return { ok, target }
  1132. },
  1133. render: compileToFunction(
  1134. `<teleport :to="target"><template v-if="ok"><Child/><span>sibling</span></template></teleport>`,
  1135. ),
  1136. })
  1137. await nextTick()
  1138. expect(target.textContent).toBe(`child changedsibling`)
  1139. })
  1140. // Vapor router-view has no render function (setup-only).
  1141. // When HMR rerender is triggered, the setup function is re-executed.
  1142. // Ensure provide() warning is suppressed.
  1143. test('rerender setup-only component', async () => {
  1144. const childId = 'test-child-reload-01'
  1145. const Child = defineVaporComponent({
  1146. __hmrId: childId,
  1147. render: compileToFunction(`<div>foo</div>`),
  1148. })
  1149. createRecord(childId, Child as any)
  1150. // without a render function
  1151. const Parent = defineVaporComponent({
  1152. setup() {
  1153. provide('foo', 'bar')
  1154. return createComponent(Child)
  1155. },
  1156. })
  1157. const { html } = define({
  1158. setup() {
  1159. return createComponent(Parent)
  1160. },
  1161. }).render()
  1162. expect(html()).toBe('<div>foo</div>')
  1163. // will trigger parent rerender
  1164. reload(childId, {
  1165. __hmrId: childId,
  1166. render: compileToFunction(`<div>bar</div>`),
  1167. })
  1168. await nextTick()
  1169. expect(html()).toBe('<div>bar</div>')
  1170. expect('provide() can only be used inside setup()').not.toHaveBeenWarned()
  1171. })
  1172. describe('switch vapor/vdom modes', () => {
  1173. test('vapor -> vdom', async () => {
  1174. const id = 'vapor-to-vdom'
  1175. const Comp = {
  1176. __vapor: true,
  1177. __hmrId: id,
  1178. render() {
  1179. return template('<div>foo</div>')()
  1180. },
  1181. }
  1182. createRecord(id, Comp)
  1183. const App = {
  1184. render() {
  1185. return h(Comp as any)
  1186. },
  1187. }
  1188. const root = document.createElement('div')
  1189. const app = createApp(App)
  1190. app.use(vaporInteropPlugin)
  1191. app.mount(root)
  1192. expect(root.innerHTML).toBe('<div>foo</div>')
  1193. // switch to vdom
  1194. reload(id, {
  1195. __hmrId: id,
  1196. render() {
  1197. return h('div', 'bar')
  1198. },
  1199. })
  1200. await nextTick()
  1201. expect(root.innerHTML).toBe('<div>bar</div>')
  1202. })
  1203. test('vdom -> vapor', async () => {
  1204. const id = 'vdom-to-vapor'
  1205. const Comp = {
  1206. __hmrId: id,
  1207. render() {
  1208. return h('div', 'foo')
  1209. },
  1210. }
  1211. createRecord(id, Comp)
  1212. const App = {
  1213. render() {
  1214. return h(Comp)
  1215. },
  1216. }
  1217. const root = document.createElement('div')
  1218. const app = createApp(App)
  1219. app.use(vaporInteropPlugin)
  1220. app.mount(root)
  1221. expect(root.innerHTML).toBe('<div>foo</div>')
  1222. // switch to vapor
  1223. reload(id, {
  1224. __vapor: true,
  1225. __hmrId: id,
  1226. render() {
  1227. return template('<div>bar</div>')()
  1228. },
  1229. })
  1230. await nextTick()
  1231. expect(root.innerHTML).toBe('<div>bar</div>')
  1232. })
  1233. })
  1234. })