nodeOps.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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', false, 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!(content, parent, null, false)
  22. expect(parent.innerHTML).toBe(content)
  23. expect(nodes[0]).toBe(parent.firstChild)
  24. expect(nodes[1]).toBe(parent.lastChild)
  25. })
  26. test('fresh insertion with anchor', () => {
  27. const content = `<div>one</div><div>two</div>three`
  28. const existing = `<div>existing</div>`
  29. const parent = document.createElement('div')
  30. parent.innerHTML = existing
  31. const anchor = parent.firstChild
  32. const nodes = nodeOps.insertStaticContent!(content, parent, anchor, false)
  33. expect(parent.innerHTML).toBe(content + existing)
  34. expect(nodes[0]).toBe(parent.firstChild)
  35. expect(nodes[1]).toBe(parent.childNodes[parent.childNodes.length - 2])
  36. })
  37. test('fresh insertion as svg', () => {
  38. const content = `<text>hello</text><circle cx="100" cy="100" r="80"></circle>`
  39. const parent = document.createElementNS(svgNS, 'svg')
  40. const [first, last] = nodeOps.insertStaticContent!(
  41. content,
  42. parent,
  43. null,
  44. true
  45. )
  46. expect(parent.innerHTML).toBe(content)
  47. expect(first).toBe(parent.firstChild)
  48. expect(last).toBe(parent.lastChild)
  49. expect((first as Element).namespaceURI).toMatch('svg')
  50. expect((last as Element).namespaceURI).toMatch('svg')
  51. })
  52. test('fresh insertion as svg, with anchor', () => {
  53. const content = `<text>hello</text><circle cx="100" cy="100" r="80"></circle>`
  54. const existing = `<path></path>`
  55. const parent = document.createElementNS(svgNS, 'svg')
  56. parent.innerHTML = existing
  57. const anchor = parent.firstChild
  58. const [first, last] = nodeOps.insertStaticContent!(
  59. content,
  60. parent,
  61. anchor,
  62. true
  63. )
  64. expect(parent.innerHTML).toBe(content + existing)
  65. expect(first).toBe(parent.firstChild)
  66. expect(last).toBe(parent.childNodes[parent.childNodes.length - 2])
  67. expect((first as Element).namespaceURI).toMatch('svg')
  68. expect((last as Element).namespaceURI).toMatch('svg')
  69. })
  70. test('cached insertion', () => {
  71. const content = `<div>one</div><div>two</div>three`
  72. const existing = `<div>existing</div>`
  73. const parent = document.createElement('div')
  74. parent.innerHTML = existing
  75. const anchor = parent.firstChild
  76. const cached = document.createElement('div')
  77. cached.innerHTML = content
  78. const nodes = nodeOps.insertStaticContent!(
  79. content,
  80. parent,
  81. anchor,
  82. false,
  83. cached.firstChild,
  84. cached.lastChild
  85. )
  86. expect(parent.innerHTML).toBe(content + existing)
  87. expect(nodes[0]).toBe(parent.firstChild)
  88. expect(nodes[1]).toBe(parent.childNodes[parent.childNodes.length - 2])
  89. })
  90. })
  91. })