nodeOps.spec.ts 3.9 KB

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