class_spec.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var _ = require('../../../../../src/util')
  2. var def = require('../../../../../src/directives/internal/class')
  3. if (_.inBrowser) {
  4. describe(':class', function () {
  5. var el
  6. beforeEach(function () {
  7. el = document.createElement('div')
  8. })
  9. it('plain string', function () {
  10. el.className = 'haha'
  11. var dir = _.extend({ el: el }, def)
  12. dir.update('test')
  13. expect(el.className).toBe('haha test')
  14. dir.update('what now test')
  15. expect(el.className).toBe('haha test now what')
  16. dir.update('ok cool')
  17. expect(el.className).toBe('haha cool ok')
  18. dir.update()
  19. expect(el.className).toBe('haha')
  20. })
  21. it('object value', function () {
  22. el.className = 'hoho'
  23. var dir = _.extend({ el: el }, def)
  24. dir.update({
  25. a: true,
  26. b: false
  27. })
  28. expect(el.className).toBe('hoho a')
  29. dir.update({
  30. b: true
  31. })
  32. expect(el.className).toBe('hoho b')
  33. dir.update(null)
  34. expect(el.className).toBe('hoho')
  35. })
  36. it('array value', function () {
  37. el.className = 'a'
  38. var dir = _.extend({ el: el }, def)
  39. dir.update(['b', 'c'])
  40. expect(el.className).toBe('a b c')
  41. dir.update(['d', 'c'])
  42. expect(el.className).toBe('a c d')
  43. dir.update()
  44. expect(el.className).toBe('a')
  45. // test mutating array
  46. var arr = ['e', '']
  47. dir.update(arr)
  48. expect(el.className).toBe('a e')
  49. arr.length = 0
  50. arr.push('f')
  51. dir.update(arr)
  52. expect(el.className).toBe('a f')
  53. })
  54. })
  55. }