hmr.spec.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // TODO: port tests from packages/runtime-core/__tests__/hmr.spec.ts
  2. import { type HMRRuntime, ref } from '@vue/runtime-dom'
  3. import { makeRender } from './_utils'
  4. import {
  5. child,
  6. createComponent,
  7. renderEffect,
  8. setText,
  9. template,
  10. } from '@vue/runtime-vapor'
  11. declare var __VUE_HMR_RUNTIME__: HMRRuntime
  12. const { createRecord, reload } = __VUE_HMR_RUNTIME__
  13. const define = makeRender()
  14. describe('hot module replacement', () => {
  15. test('child reload + parent reload', async () => {
  16. const root = document.createElement('div')
  17. const childId = 'test1-child-reload'
  18. const parentId = 'test1-parent-reload'
  19. const { component: Child } = define({
  20. __hmrId: childId,
  21. setup() {
  22. const msg = ref('child')
  23. return { msg }
  24. },
  25. render(ctx) {
  26. const n0 = template(`<div> </div>`)()
  27. const x0 = child(n0 as any)
  28. renderEffect(() => setText(x0 as any, ctx.msg))
  29. return [n0]
  30. },
  31. })
  32. createRecord(childId, Child as any)
  33. const { mount, component: Parent } = define({
  34. __hmrId: parentId,
  35. setup() {
  36. const msg = ref('root')
  37. return { msg }
  38. },
  39. render(ctx) {
  40. const n0 = createComponent(Child)
  41. const n1 = template(`<div> </div>`)()
  42. const x0 = child(n1 as any)
  43. renderEffect(() => setText(x0 as any, ctx.msg))
  44. return [n0, n1]
  45. },
  46. }).create()
  47. createRecord(parentId, Parent as any)
  48. mount(root)
  49. expect(root.innerHTML).toMatchInlineSnapshot(
  50. `"<div>child</div><div>root</div>"`,
  51. )
  52. // reload child
  53. reload(childId, {
  54. __hmrId: childId,
  55. __vapor: true,
  56. setup() {
  57. const msg = ref('child changed')
  58. return { msg }
  59. },
  60. render(ctx: any) {
  61. const n0 = template(`<div> </div>`)()
  62. const x0 = child(n0 as any)
  63. renderEffect(() => setText(x0 as any, ctx.msg))
  64. return [n0]
  65. },
  66. })
  67. expect(root.innerHTML).toMatchInlineSnapshot(
  68. `"<div>child changed</div><div>root</div>"`,
  69. )
  70. // reload child again
  71. reload(childId, {
  72. __hmrId: childId,
  73. __vapor: true,
  74. setup() {
  75. const msg = ref('child changed2')
  76. return { msg }
  77. },
  78. render(ctx: any) {
  79. const n0 = template(`<div> </div>`)()
  80. const x0 = child(n0 as any)
  81. renderEffect(() => setText(x0 as any, ctx.msg))
  82. return [n0]
  83. },
  84. })
  85. expect(root.innerHTML).toMatchInlineSnapshot(
  86. `"<div>child changed2</div><div>root</div>"`,
  87. )
  88. // reload parent
  89. reload(parentId, {
  90. __hmrId: parentId,
  91. __vapor: true,
  92. setup() {
  93. const msg = ref('root changed')
  94. return { msg }
  95. },
  96. render(ctx: any) {
  97. const n0 = createComponent(Child)
  98. const n1 = template(`<div> </div>`)()
  99. const x0 = child(n1 as any)
  100. renderEffect(() => setText(x0 as any, ctx.msg))
  101. return [n0, n1]
  102. },
  103. })
  104. expect(root.innerHTML).toMatchInlineSnapshot(
  105. `"<div>child changed2</div><div>root changed</div>"`,
  106. )
  107. })
  108. })