bind_spec.js 1.9 KB

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