class_spec.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var _ = require('src/util')
  2. var def = require('src/directives/internal/class')
  3. describe(':class', function () {
  4. var el
  5. beforeEach(function () {
  6. el = document.createElement('div')
  7. })
  8. it('plain string', function () {
  9. el.className = 'haha'
  10. var dir = _.extend({ el: el }, def)
  11. dir.update('test')
  12. expect(el.className).toBe('haha test')
  13. dir.update('what now test')
  14. expect(el.className).toBe('haha what now test')
  15. dir.update('ok cool')
  16. expect(el.className).toBe('haha ok cool')
  17. dir.update()
  18. expect(el.className).toBe('haha')
  19. })
  20. it('object value', function () {
  21. el.className = 'hoho'
  22. var dir = _.extend({ el: el }, def)
  23. dir.update({
  24. a: true,
  25. b: false
  26. })
  27. expect(el.className).toBe('hoho a')
  28. dir.update({
  29. b: true
  30. })
  31. expect(el.className).toBe('hoho b')
  32. dir.update(null)
  33. expect(el.className).toBe('hoho')
  34. dir.update({
  35. 'a b': true,
  36. c: false
  37. })
  38. expect(el.className).toBe('hoho a b')
  39. dir.update({
  40. c: true
  41. })
  42. expect(el.className).toBe('hoho c')
  43. })
  44. it('array value', function () {
  45. el.className = 'a'
  46. var dir = _.extend({ el: el }, def)
  47. dir.update(['b', 'c'])
  48. expect(el.className).toBe('a b c')
  49. dir.update(['d', 'c'])
  50. expect(el.className).toBe('a d c')
  51. dir.update()
  52. expect(el.className).toBe('a')
  53. // test mutating array
  54. var arr = ['e', '']
  55. dir.update(arr)
  56. expect(el.className).toBe('a e')
  57. arr.length = 0
  58. arr.push('f')
  59. dir.update(arr)
  60. expect(el.className).toBe('a f')
  61. // test array with objects
  62. dir.update(['x', {y: true, z: true}])
  63. expect(el.className).toBe('a x y z')
  64. dir.update(['x', {y: true, z: false}])
  65. expect(el.className).toBe('a x y')
  66. dir.update(['f', {z: true}])
  67. expect(el.className).toBe('a f z')
  68. dir.update(['l', 'f', {n: true, z: true}])
  69. expect(el.className).toBe('a l f n z')
  70. dir.update(['x', {}])
  71. expect(el.className).toBe('a x')
  72. dir.update()
  73. expect(el.className).toBe('a')
  74. })
  75. })