patchProps.spec.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { patchProp } from '../src/patchProp'
  2. import { render, h } from '../src'
  3. describe('runtime-dom: props patching', () => {
  4. test('basic', () => {
  5. const el = document.createElement('div')
  6. patchProp(el, 'id', null, 'foo')
  7. expect(el.id).toBe('foo')
  8. patchProp(el, 'id', null, null)
  9. expect(el.id).toBe('')
  10. })
  11. test('value', () => {
  12. const el = document.createElement('input')
  13. patchProp(el, 'value', null, 'foo')
  14. expect(el.value).toBe('foo')
  15. patchProp(el, 'value', null, null)
  16. expect(el.value).toBe('')
  17. const obj = {}
  18. patchProp(el, 'value', null, obj)
  19. expect(el.value).toBe(obj.toString())
  20. expect((el as any)._value).toBe(obj)
  21. })
  22. test('boolean prop', () => {
  23. const el = document.createElement('select')
  24. patchProp(el, 'multiple', null, '')
  25. expect(el.multiple).toBe(true)
  26. patchProp(el, 'multiple', null, null)
  27. expect(el.multiple).toBe(false)
  28. })
  29. test('innerHTML unmount prev children', () => {
  30. const fn = jest.fn()
  31. const comp = {
  32. render: () => 'foo',
  33. unmounted: fn
  34. }
  35. const root = document.createElement('div')
  36. render(h('div', null, [h(comp)]), root)
  37. expect(root.innerHTML).toBe(`<div>foo</div>`)
  38. render(h('div', { innerHTML: 'bar' }), root)
  39. expect(root.innerHTML).toBe(`<div>bar</div>`)
  40. expect(fn).toHaveBeenCalled()
  41. })
  42. // #954
  43. test('(svg) innerHTML unmount prev children', () => {
  44. const fn = jest.fn()
  45. const comp = {
  46. render: () => 'foo',
  47. unmounted: fn
  48. }
  49. const root = document.createElement('div')
  50. render(h('div', null, [h(comp)]), root)
  51. expect(root.innerHTML).toBe(`<div>foo</div>`)
  52. render(h('svg', { innerHTML: '<g></g>' }), root)
  53. expect(root.innerHTML).toBe(`<svg><g></g></svg>`)
  54. expect(fn).toHaveBeenCalled()
  55. })
  56. test('textContent unmount prev children', () => {
  57. const fn = jest.fn()
  58. const comp = {
  59. render: () => 'foo',
  60. unmounted: fn
  61. }
  62. const root = document.createElement('div')
  63. render(h('div', null, [h(comp)]), root)
  64. expect(root.innerHTML).toBe(`<div>foo</div>`)
  65. render(h('div', { textContent: 'bar' }), root)
  66. expect(root.innerHTML).toBe(`<div>bar</div>`)
  67. expect(fn).toHaveBeenCalled()
  68. })
  69. })