hmr.spec.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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(`<div><slot/></div>`)
  43. }
  44. createRecord(childId)
  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)
  56. render(h(Parent), root)
  57. expect(serializeInner(root)).toBe(`<div>0<div>0</div></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>1<div>1</div></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!<div>1</div></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!<div>1!</div></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><div>1!</div></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><div><span>1</span></div></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)
  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. // #1156 - static nodes should retain DOM element reference across updates
  133. // when HMR is active
  134. test('static el reference', async () => {
  135. const root = nodeOps.createElement('div')
  136. const id = 'test-static-el'
  137. const template = `<div>
  138. <div>{{ count }}</div>
  139. <button @click="count++">++</button>
  140. </div>`
  141. const Comp: ComponentOptions = {
  142. __hmrId: id,
  143. data() {
  144. return { count: 0 }
  145. },
  146. render: compileToFunction(template)
  147. }
  148. createRecord(id)
  149. render(h(Comp), root)
  150. expect(serializeInner(root)).toBe(
  151. `<div><div>0</div><button>++</button></div>`
  152. )
  153. // 1. click to trigger update
  154. triggerEvent((root as any).children[0].children[1], 'click')
  155. await nextTick()
  156. expect(serializeInner(root)).toBe(
  157. `<div><div>1</div><button>++</button></div>`
  158. )
  159. // 2. trigger HMR
  160. rerender(
  161. id,
  162. compileToFunction(template.replace(`<button`, `<button class="foo"`))
  163. )
  164. expect(serializeInner(root)).toBe(
  165. `<div><div>1</div><button class="foo">++</button></div>`
  166. )
  167. })
  168. // #1157 - component should force full props update when HMR is active
  169. test('force update child component w/ static props', () => {
  170. const root = nodeOps.createElement('div')
  171. const parentId = 'test-force-props-parent'
  172. const childId = 'test-force-props-child'
  173. const Child: ComponentOptions = {
  174. __hmrId: childId,
  175. props: {
  176. msg: String
  177. },
  178. render: compileToFunction(`<div>{{ msg }}</div>`)
  179. }
  180. createRecord(childId)
  181. const Parent: ComponentOptions = {
  182. __hmrId: parentId,
  183. components: { Child },
  184. render: compileToFunction(`<Child msg="foo" />`)
  185. }
  186. createRecord(parentId)
  187. render(h(Parent), root)
  188. expect(serializeInner(root)).toBe(`<div>foo</div>`)
  189. rerender(parentId, compileToFunction(`<Child msg="bar" />`))
  190. expect(serializeInner(root)).toBe(`<div>bar</div>`)
  191. })
  192. // #1305 - component should remove class
  193. test('remove static class from parent', () => {
  194. const root = nodeOps.createElement('div')
  195. const parentId = 'test-force-class-parent'
  196. const childId = 'test-force-class-child'
  197. const Child: ComponentOptions = {
  198. __hmrId: childId,
  199. render: compileToFunction(`<div>child</div>`)
  200. }
  201. createRecord(childId)
  202. const Parent: ComponentOptions = {
  203. __hmrId: parentId,
  204. components: { Child },
  205. render: compileToFunction(`<Child class="test" />`)
  206. }
  207. createRecord(parentId)
  208. render(h(Parent), root)
  209. expect(serializeInner(root)).toBe(`<div class="test">child</div>`)
  210. rerender(parentId, compileToFunction(`<Child/>`))
  211. expect(serializeInner(root)).toBe(`<div>child</div>`)
  212. })
  213. test('rerender if any parent in the parent chain', () => {
  214. const root = nodeOps.createElement('div')
  215. const parent = 'test-force-props-parent-'
  216. const childId = 'test-force-props-child'
  217. const numberOfParents = 5
  218. const Child: ComponentOptions = {
  219. __hmrId: childId,
  220. render: compileToFunction(`<div>child</div>`)
  221. }
  222. createRecord(childId)
  223. const components: ComponentOptions[] = []
  224. for (let i = 0; i < numberOfParents; i++) {
  225. const parentId = `${parent}${i}`
  226. const parentComp: ComponentOptions = {
  227. __hmrId: parentId
  228. }
  229. components.push(parentComp)
  230. if (i === 0) {
  231. parentComp.render = compileToFunction(`<Child />`)
  232. parentComp.components = {
  233. Child
  234. }
  235. } else {
  236. parentComp.render = compileToFunction(`<Parent />`)
  237. parentComp.components = {
  238. Parent: components[i - 1]
  239. }
  240. }
  241. createRecord(parentId)
  242. }
  243. const last = components[components.length - 1]
  244. render(h(last), root)
  245. expect(serializeInner(root)).toBe(`<div>child</div>`)
  246. rerender(last.__hmrId!, compileToFunction(`<Parent class="test"/>`))
  247. expect(serializeInner(root)).toBe(`<div class="test">child</div>`)
  248. })
  249. })