nodeOps.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { nodeOps, svgNS } from '../src/nodeOps'
  2. describe('runtime-dom: node-ops', () => {
  3. test('the _value property should be cloned', () => {
  4. const el = nodeOps.createElement('input') as HTMLDivElement & {
  5. _value: any
  6. }
  7. el._value = 1
  8. const cloned = nodeOps.cloneNode!(el) as HTMLDivElement & { _value: any }
  9. expect(cloned._value).toBe(1)
  10. })
  11. test("the <select>'s multiple attr should be set in createElement", () => {
  12. const el = nodeOps.createElement('select', false, undefined, {
  13. multiple: ''
  14. }) as HTMLSelectElement
  15. const option1 = nodeOps.createElement('option') as HTMLOptionElement
  16. const option2 = nodeOps.createElement('option') as HTMLOptionElement
  17. option1.selected = true
  18. option2.selected = true
  19. nodeOps.insert(option1, el)
  20. nodeOps.insert(option2, el)
  21. expect(el.multiple).toBe(true)
  22. expect(option1.selected).toBe(true)
  23. expect(option2.selected).toBe(true)
  24. })
  25. describe('insertStaticContent', () => {
  26. test('fresh insertion', () => {
  27. const content = `<div>one</div><div>two</div>three`
  28. const parent = document.createElement('div')
  29. const nodes = nodeOps.insertStaticContent!(content, parent, null, false)
  30. expect(parent.innerHTML).toBe(content)
  31. expect(nodes.length).toBe(3)
  32. expect(nodes[0]).toBe(parent.firstChild)
  33. expect(nodes[nodes.length - 1]).toBe(parent.lastChild)
  34. })
  35. test('fresh insertion with anchor', () => {
  36. const content = `<div>one</div><div>two</div>three`
  37. const existing = `<div>existing</div>`
  38. const parent = document.createElement('div')
  39. parent.innerHTML = existing
  40. const anchor = parent.firstChild
  41. const nodes = nodeOps.insertStaticContent!(content, parent, anchor, false)
  42. expect(parent.innerHTML).toBe(content + existing)
  43. expect(nodes.length).toBe(3)
  44. expect(nodes[0]).toBe(parent.firstChild)
  45. expect(nodes[nodes.length - 1]).toBe(
  46. parent.childNodes[parent.childNodes.length - 2]
  47. )
  48. })
  49. test('fresh insertion as svg', () => {
  50. const content = `<text>hello</text><circle cx="100" cy="100" r="80"></circle>`
  51. const parent = document.createElementNS(svgNS, 'svg')
  52. const [first, last] = nodeOps.insertStaticContent!(
  53. content,
  54. parent,
  55. null,
  56. true
  57. )
  58. expect(parent.innerHTML).toBe(content)
  59. expect(first).toBe(parent.firstChild)
  60. expect(last).toBe(parent.lastChild)
  61. expect(first.namespaceURI).toMatch('svg')
  62. expect(last.namespaceURI).toMatch('svg')
  63. })
  64. test('fresh insertion as svg, with anchor', () => {
  65. const content = `<text>hello</text><circle cx="100" cy="100" r="80"></circle>`
  66. const existing = `<path></path>`
  67. const parent = document.createElementNS(svgNS, 'svg')
  68. parent.innerHTML = existing
  69. const anchor = parent.firstChild
  70. const [first, last] = nodeOps.insertStaticContent!(
  71. content,
  72. parent,
  73. anchor,
  74. true
  75. )
  76. expect(parent.innerHTML).toBe(content + existing)
  77. expect(first).toBe(parent.firstChild)
  78. expect(last).toBe(parent.childNodes[parent.childNodes.length - 2])
  79. expect(first.namespaceURI).toMatch('svg')
  80. expect(last.namespaceURI).toMatch('svg')
  81. })
  82. test('cached', () => {
  83. const content = `<div>one</div><div>two</div>three`
  84. const cacheParent = document.createElement('div')
  85. const nodes = nodeOps.insertStaticContent!(
  86. content,
  87. cacheParent,
  88. null,
  89. false
  90. )
  91. const parent = document.createElement('div')
  92. const clonedNodes = nodeOps.insertStaticContent!(
  93. ``,
  94. parent,
  95. null,
  96. false,
  97. nodes
  98. )
  99. expect(parent.innerHTML).toBe(content)
  100. expect(clonedNodes[0]).toBe(parent.firstChild)
  101. expect(clonedNodes[clonedNodes.length - 1]).toBe(parent.lastChild)
  102. expect(clonedNodes[0]).not.toBe(nodes[0])
  103. })
  104. })
  105. })