hmr.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { HMRRuntime } from '../src/hmr'
  2. import '../src/hmr'
  3. import { ComponentOptions, InternalRenderFunction } from '../src/component'
  4. import {
  5. render,
  6. nodeOps,
  7. h,
  8. serializeInner,
  9. triggerEvent,
  10. TestElement,
  11. nextTick
  12. } from '@vue/runtime-test'
  13. import * as runtimeTest from '@vue/runtime-test'
  14. import { baseCompile } from '@vue/compiler-core'
  15. declare var __VUE_HMR_RUNTIME__: HMRRuntime
  16. const { createRecord, rerender, reload } = __VUE_HMR_RUNTIME__
  17. function compileToFunction(template: string) {
  18. const { code } = baseCompile(template)
  19. const render = new Function('Vue', code)(
  20. runtimeTest
  21. ) as InternalRenderFunction
  22. render._rc = true // isRuntimeCompiled
  23. return render
  24. }
  25. describe('hot module replacement', () => {
  26. test('inject global runtime', () => {
  27. expect(createRecord).toBeDefined()
  28. expect(rerender).toBeDefined()
  29. expect(reload).toBeDefined()
  30. })
  31. test('createRecord', () => {
  32. expect(createRecord('test1', {})).toBe(true)
  33. // if id has already been created, should return false
  34. expect(createRecord('test1', {})).toBe(false)
  35. })
  36. test('rerender', async () => {
  37. const root = nodeOps.createElement('div')
  38. const parentId = 'test2-parent'
  39. const childId = 'test2-child'
  40. const Child: ComponentOptions = {
  41. __hmrId: childId,
  42. render: compileToFunction(`<slot/>`)
  43. }
  44. createRecord(childId, Child)
  45. const Parent: ComponentOptions = {
  46. __hmrId: parentId,
  47. data() {
  48. return { count: 0 }
  49. },
  50. components: { Child },
  51. render: compileToFunction(
  52. `<div @click="count++">{{ count }}<Child>{{ count }}</Child></div>`
  53. )
  54. }
  55. createRecord(parentId, Parent)
  56. render(h(Parent), root)
  57. expect(serializeInner(root)).toBe(`<div>00</div>`)
  58. // Perform some state change. This change should be preserved after the
  59. // re-render!
  60. triggerEvent(root.children[0] as TestElement, 'click')
  61. await nextTick()
  62. expect(serializeInner(root)).toBe(`<div>11</div>`)
  63. // // Update text while preserving state
  64. // rerender(
  65. // parentId,
  66. // compileToFunction(
  67. // `<div @click="count++">{{ count }}!<Child>{{ count }}</Child></div>`
  68. // )
  69. // )
  70. // expect(serializeInner(root)).toBe(`<div>1!1</div>`)
  71. // Should force child update on slot content change
  72. rerender(
  73. parentId,
  74. compileToFunction(
  75. `<div @click="count++">{{ count }}!<Child>{{ count }}!</Child></div>`
  76. )
  77. )
  78. expect(serializeInner(root)).toBe(`<div>1!1!</div>`)
  79. // Should force update element children despite block optimization
  80. rerender(
  81. parentId,
  82. compileToFunction(
  83. `<div @click="count++">{{ count }}<span>{{ count }}</span>
  84. <Child>{{ count }}!</Child>
  85. </div>`
  86. )
  87. )
  88. expect(serializeInner(root)).toBe(`<div>1<span>1</span>1!</div>`)
  89. // Should force update child slot elements
  90. rerender(
  91. parentId,
  92. compileToFunction(
  93. `<div @click="count++">
  94. <Child><span>{{ count }}</span></Child>
  95. </div>`
  96. )
  97. )
  98. expect(serializeInner(root)).toBe(`<div><span>1</span></div>`)
  99. })
  100. test('reload', async () => {
  101. const root = nodeOps.createElement('div')
  102. const childId = 'test3-child'
  103. const unmountSpy = jest.fn()
  104. const mountSpy = jest.fn()
  105. const Child: ComponentOptions = {
  106. __hmrId: childId,
  107. data() {
  108. return { count: 0 }
  109. },
  110. unmounted: unmountSpy,
  111. render: compileToFunction(`<div @click="count++">{{ count }}</div>`)
  112. }
  113. createRecord(childId, Child)
  114. const Parent: ComponentOptions = {
  115. render: () => h(Child)
  116. }
  117. render(h(Parent), root)
  118. expect(serializeInner(root)).toBe(`<div>0</div>`)
  119. reload(childId, {
  120. __hmrId: childId,
  121. data() {
  122. return { count: 1 }
  123. },
  124. mounted: mountSpy,
  125. render: compileToFunction(`<div @click="count++">{{ count }}</div>`)
  126. })
  127. await nextTick()
  128. expect(serializeInner(root)).toBe(`<div>1</div>`)
  129. expect(unmountSpy).toHaveBeenCalledTimes(1)
  130. expect(mountSpy).toHaveBeenCalledTimes(1)
  131. })
  132. })