nodeOps.spec.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.namespaceURI).toMatch('svg')
  58. expect(last.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.namespaceURI).toMatch('svg')
  76. expect(last.namespaceURI).toMatch('svg')
  77. })
  78. })
  79. })