attr_spec.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var _ = require('../../../../src/util')
  2. var def = require('../../../../src/directives/attr')
  3. if (_.inBrowser) {
  4. describe('v-attr', function () {
  5. var el, dir
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. dir = {el: el}
  9. _.extend(dir, def)
  10. })
  11. it('normal attr', function () {
  12. dir.arg = 'test'
  13. dir.update('ok')
  14. expect(el.getAttribute('test')).toBe('ok')
  15. dir.update('again')
  16. expect(el.getAttribute('test')).toBe('again')
  17. dir.update(null)
  18. expect(el.hasAttribute('test')).toBe(false)
  19. dir.update(false)
  20. expect(el.hasAttribute('test')).toBe(false)
  21. dir.update(0)
  22. expect(el.getAttribute('test')).toBe('0')
  23. })
  24. it('should set property', function () {
  25. dir.el = document.createElement('input')
  26. dir.arg = 'value'
  27. dir.update('what')
  28. expect(dir.el.getAttribute('value')).toBe('what')
  29. expect(dir.el.value).toBe('what')
  30. })
  31. it('xlink', function () {
  32. var xlinkNS = 'http://www.w3.org/1999/xlink'
  33. dir.arg = 'xlink:special'
  34. dir.update('ok')
  35. expect(el.getAttributeNS(xlinkNS, 'special')).toBe('ok')
  36. dir.update('again')
  37. expect(el.getAttributeNS(xlinkNS, 'special')).toBe('again')
  38. dir.update(null)
  39. expect(el.hasAttributeNS(xlinkNS, 'special')).toBe(false)
  40. })
  41. it('object and xlink', function () {
  42. var xlinkNS = 'http://www.w3.org/1999/xlink'
  43. var obj1 = {special: 'ok', test: 'again'}
  44. var obj2 = {'xlink:href': '#', test: 'ok', empty: null}
  45. dir.update(obj2)
  46. expect(el.getAttributeNS(xlinkNS, 'href')).toBe('#')
  47. expect(el.getAttribute('test')).toBe('ok')
  48. expect(el.hasAttribute('empty')).toBe(false)
  49. dir.update(obj1)
  50. expect(el.hasAttributeNS(xlinkNS, 'href')).toBe(false)
  51. expect(el.getAttribute('special')).toBe('ok')
  52. expect(el.getAttribute('test')).toBe('again')
  53. obj1.test = null
  54. dir.update(obj1)
  55. expect(el.hasAttribute('test')).toBe(false)
  56. dir.update(obj2)
  57. expect(el.getAttributeNS(xlinkNS, 'href')).toBe('#')
  58. })
  59. })
  60. }