patchAttrs.spec.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { patchProp } from '../src/patchProp'
  2. import { xlinkNS } from '../src/modules/attrs'
  3. describe('runtime-dom: attrs patching', () => {
  4. test('xlink attributes', () => {
  5. const el = document.createElementNS('http://www.w3.org/2000/svg', 'use')
  6. patchProp(el, 'xlink:href', null, 'a', true)
  7. expect(el.getAttributeNS(xlinkNS, 'href')).toBe('a')
  8. patchProp(el, 'xlink:href', 'a', null, true)
  9. expect(el.getAttributeNS(xlinkNS, 'href')).toBe(null)
  10. })
  11. test('textContent attributes /w svg', () => {
  12. const el = document.createElementNS('http://www.w3.org/2000/svg', 'use')
  13. patchProp(el, 'textContent', null, 'foo', true)
  14. expect(el.attributes.length).toBe(0)
  15. expect(el.innerHTML).toBe('foo')
  16. })
  17. test('boolean attributes', () => {
  18. const el = document.createElement('input')
  19. patchProp(el, 'readonly', null, true)
  20. expect(el.getAttribute('readonly')).toBe('')
  21. patchProp(el, 'readonly', true, false)
  22. expect(el.getAttribute('readonly')).toBe(null)
  23. patchProp(el, 'readonly', false, '')
  24. expect(el.getAttribute('readonly')).toBe('')
  25. patchProp(el, 'readonly', '', 0)
  26. expect(el.getAttribute('readonly')).toBe(null)
  27. patchProp(el, 'readonly', 0, '0')
  28. expect(el.getAttribute('readonly')).toBe('')
  29. patchProp(el, 'readonly', '0', false)
  30. expect(el.getAttribute('readonly')).toBe(null)
  31. patchProp(el, 'readonly', false, 1)
  32. expect(el.getAttribute('readonly')).toBe('')
  33. patchProp(el, 'readonly', 1, undefined)
  34. expect(el.getAttribute('readonly')).toBe(null)
  35. })
  36. test('attributes', () => {
  37. const el = document.createElement('div')
  38. patchProp(el, 'foo', null, 'a')
  39. expect(el.getAttribute('foo')).toBe('a')
  40. patchProp(el, 'foo', 'a', null)
  41. expect(el.getAttribute('foo')).toBe(null)
  42. })
  43. // #949
  44. test('onxxx but non-listener attributes', () => {
  45. const el = document.createElement('div')
  46. patchProp(el, 'onwards', null, 'a')
  47. expect(el.getAttribute('onwards')).toBe('a')
  48. patchProp(el, 'onwards', 'a', null)
  49. expect(el.getAttribute('onwards')).toBe(null)
  50. })
  51. })