patchAttrs.spec.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. })
  24. test('attributes', () => {
  25. const el = document.createElement('div')
  26. patchProp(el, 'foo', null, 'a')
  27. expect(el.getAttribute('foo')).toBe('a')
  28. patchProp(el, 'foo', 'a', null)
  29. expect(el.getAttribute('foo')).toBe(null)
  30. })
  31. // #949
  32. test('onxxx but non-listener attributes', () => {
  33. const el = document.createElement('div')
  34. patchProp(el, 'onwards', null, 'a')
  35. expect(el.getAttribute('onwards')).toBe('a')
  36. patchProp(el, 'onwards', 'a', null)
  37. expect(el.getAttribute('onwards')).toBe(null)
  38. })
  39. })