rendererStaticNode.spec.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { createStaticVNode, h, render } from '../src'
  2. describe('static vnode handling', () => {
  3. const content = `<div>hello</div><p>world</p>`
  4. const content2 = `<p>foo</p><div>bar</div><span>baz</span>`
  5. const s = createStaticVNode(content, 2)
  6. const s2 = createStaticVNode(content2, 3)
  7. test('should mount from string', () => {
  8. const root = document.createElement('div')
  9. render(h('div', [s]), root)
  10. expect(root.innerHTML).toBe(`<div>${content}</div>`)
  11. })
  12. test('should support reusing the same hoisted node', () => {
  13. const root = document.createElement('div')
  14. render(h('div', [s, s]), root)
  15. expect(root.innerHTML).toBe(`<div>${content}${content}</div>`)
  16. })
  17. // the rest only happens during HMR but needs to be correctly supported
  18. test('should update', () => {
  19. const root = document.createElement('div')
  20. render(h('div', [s]), root)
  21. expect(root.innerHTML).toBe(`<div>${content}</div>`)
  22. render(h('div', [s2]), root)
  23. expect(root.innerHTML).toBe(`<div>${content2}</div>`)
  24. })
  25. test('should move', () => {
  26. const root = document.createElement('div')
  27. render(h('div', [h('b'), s, h('b')]), root)
  28. expect(root.innerHTML).toBe(`<div><b></b>${content}<b></b></div>`)
  29. render(h('div', [s, h('b'), h('b')]), root)
  30. expect(root.innerHTML).toBe(`<div>${content}<b></b><b></b></div>`)
  31. render(h('div', [h('b'), h('b'), s]), root)
  32. expect(root.innerHTML).toBe(`<div><b></b><b></b>${content}</div>`)
  33. })
  34. test('should remove', () => {
  35. const root = document.createElement('div')
  36. render(h('div', [h('b'), s, h('b')]), root)
  37. expect(root.innerHTML).toBe(`<div><b></b>${content}<b></b></div>`)
  38. render(h('div', [h('b'), h('b')]), root)
  39. expect(root.innerHTML).toBe(`<div><b></b><b></b></div>`)
  40. render(h('div', [h('b'), h('b'), s]), root)
  41. expect(root.innerHTML).toBe(`<div><b></b><b></b>${content}</div>`)
  42. })
  43. })