nodeOps.spec.ts 3.7 KB

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