hmr.spec.ts 4.0 KB

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