2
0

hmr.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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, 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>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, 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. test('reload class component', async () => {
  133. const root = nodeOps.createElement('div')
  134. const childId = 'test4-child'
  135. const unmountSpy = jest.fn()
  136. const mountSpy = jest.fn()
  137. class Child {
  138. static __vccOpts: ComponentOptions = {
  139. __hmrId: childId,
  140. data() {
  141. return { count: 0 }
  142. },
  143. unmounted: unmountSpy,
  144. render: compileToFunction(`<div @click="count++">{{ count }}</div>`)
  145. }
  146. }
  147. createRecord(childId, Child)
  148. const Parent: ComponentOptions = {
  149. render: () => h(Child)
  150. }
  151. render(h(Parent), root)
  152. expect(serializeInner(root)).toBe(`<div>0</div>`)
  153. class UpdatedChild {
  154. static __vccOpts: ComponentOptions = {
  155. __hmrId: childId,
  156. data() {
  157. return { count: 1 }
  158. },
  159. mounted: mountSpy,
  160. render: compileToFunction(`<div @click="count++">{{ count }}</div>`)
  161. }
  162. }
  163. reload(childId, UpdatedChild)
  164. await nextTick()
  165. expect(serializeInner(root)).toBe(`<div>1</div>`)
  166. expect(unmountSpy).toHaveBeenCalledTimes(1)
  167. expect(mountSpy).toHaveBeenCalledTimes(1)
  168. })
  169. // #1156 - static nodes should retain DOM element reference across updates
  170. // when HMR is active
  171. test('static el reference', async () => {
  172. const root = nodeOps.createElement('div')
  173. const id = 'test-static-el'
  174. const template = `<div>
  175. <div>{{ count }}</div>
  176. <button @click="count++">++</button>
  177. </div>`
  178. const Comp: ComponentOptions = {
  179. __hmrId: id,
  180. data() {
  181. return { count: 0 }
  182. },
  183. render: compileToFunction(template)
  184. }
  185. createRecord(id, Comp)
  186. render(h(Comp), root)
  187. expect(serializeInner(root)).toBe(
  188. `<div><div>0</div><button>++</button></div>`
  189. )
  190. // 1. click to trigger update
  191. triggerEvent((root as any).children[0].children[1], 'click')
  192. await nextTick()
  193. expect(serializeInner(root)).toBe(
  194. `<div><div>1</div><button>++</button></div>`
  195. )
  196. // 2. trigger HMR
  197. rerender(
  198. id,
  199. compileToFunction(template.replace(`<button`, `<button class="foo"`))
  200. )
  201. expect(serializeInner(root)).toBe(
  202. `<div><div>1</div><button class="foo">++</button></div>`
  203. )
  204. })
  205. // #1157 - component should force full props update when HMR is active
  206. test('force update child component w/ static props', () => {
  207. const root = nodeOps.createElement('div')
  208. const parentId = 'test-force-props-parent'
  209. const childId = 'test-force-props-child'
  210. const Child: ComponentOptions = {
  211. __hmrId: childId,
  212. props: {
  213. msg: String
  214. },
  215. render: compileToFunction(`<div>{{ msg }}</div>`)
  216. }
  217. createRecord(childId, Child)
  218. const Parent: ComponentOptions = {
  219. __hmrId: parentId,
  220. components: { Child },
  221. render: compileToFunction(`<Child msg="foo" />`)
  222. }
  223. createRecord(parentId, Parent)
  224. render(h(Parent), root)
  225. expect(serializeInner(root)).toBe(`<div>foo</div>`)
  226. rerender(parentId, compileToFunction(`<Child msg="bar" />`))
  227. expect(serializeInner(root)).toBe(`<div>bar</div>`)
  228. })
  229. // #1305 - component should remove class
  230. test('remove static class from parent', () => {
  231. const root = nodeOps.createElement('div')
  232. const parentId = 'test-force-class-parent'
  233. const childId = 'test-force-class-child'
  234. const Child: ComponentOptions = {
  235. __hmrId: childId,
  236. render: compileToFunction(`<div>child</div>`)
  237. }
  238. createRecord(childId, Child)
  239. const Parent: ComponentOptions = {
  240. __hmrId: parentId,
  241. components: { Child },
  242. render: compileToFunction(`<Child class="test" />`)
  243. }
  244. createRecord(parentId, Parent)
  245. render(h(Parent), root)
  246. expect(serializeInner(root)).toBe(`<div class="test">child</div>`)
  247. rerender(parentId, compileToFunction(`<Child/>`))
  248. expect(serializeInner(root)).toBe(`<div>child</div>`)
  249. })
  250. test('rerender if any parent in the parent chain', () => {
  251. const root = nodeOps.createElement('div')
  252. const parent = 'test-force-props-parent-'
  253. const childId = 'test-force-props-child'
  254. const numberOfParents = 5
  255. const Child: ComponentOptions = {
  256. __hmrId: childId,
  257. render: compileToFunction(`<div>child</div>`)
  258. }
  259. createRecord(childId, Child)
  260. const components: ComponentOptions[] = []
  261. for (let i = 0; i < numberOfParents; i++) {
  262. const parentId = `${parent}${i}`
  263. const parentComp: ComponentOptions = {
  264. __hmrId: parentId
  265. }
  266. components.push(parentComp)
  267. if (i === 0) {
  268. parentComp.render = compileToFunction(`<Child />`)
  269. parentComp.components = {
  270. Child
  271. }
  272. } else {
  273. parentComp.render = compileToFunction(`<Parent />`)
  274. parentComp.components = {
  275. Parent: components[i - 1]
  276. }
  277. }
  278. createRecord(parentId, parentComp)
  279. }
  280. const last = components[components.length - 1]
  281. render(h(last), root)
  282. expect(serializeInner(root)).toBe(`<div>child</div>`)
  283. rerender(last.__hmrId!, compileToFunction(`<Parent class="test"/>`))
  284. expect(serializeInner(root)).toBe(`<div class="test">child</div>`)
  285. })
  286. // #3302
  287. test('rerender with Teleport', () => {
  288. const root = nodeOps.createElement('div')
  289. const target = nodeOps.createElement('div')
  290. const parentId = 'parent-teleport'
  291. const Child: ComponentOptions = {
  292. data() {
  293. return {
  294. // style is used to ensure that the div tag will be tracked by Teleport
  295. style: {},
  296. target
  297. }
  298. },
  299. render: compileToFunction(`
  300. <teleport :to="target">
  301. <div :style="style">
  302. <slot/>
  303. </div>
  304. </teleport>
  305. `)
  306. }
  307. const Parent: ComponentOptions = {
  308. __hmrId: parentId,
  309. components: { Child },
  310. render: compileToFunction(`
  311. <Child>
  312. <template #default>
  313. <div>1</div>
  314. </template>
  315. </Child>
  316. `)
  317. }
  318. createRecord(parentId, Parent)
  319. render(h(Parent), root)
  320. expect(serializeInner(root)).toBe(
  321. `<!--teleport start--><!--teleport end-->`
  322. )
  323. expect(serializeInner(target)).toBe(`<div style={}><div>1</div></div>`)
  324. rerender(
  325. parentId,
  326. compileToFunction(`
  327. <Child>
  328. <template #default>
  329. <div>1</div>
  330. <div>2</div>
  331. </template>
  332. </Child>
  333. `)
  334. )
  335. expect(serializeInner(root)).toBe(
  336. `<!--teleport start--><!--teleport end-->`
  337. )
  338. expect(serializeInner(target)).toBe(
  339. `<div style={}><div>1</div><div>2</div></div>`
  340. )
  341. })
  342. })