nodeOps.spec.ts 3.9 KB

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