class_spec.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 = 'foo'
  10. var dir = _.extend({ el: el }, def)
  11. dir.update('bar')
  12. expect(el.className).toBe('foo bar')
  13. dir.update('baz qux')
  14. expect(el.className).toBe('foo baz qux')
  15. dir.update('qux')
  16. expect(el.className).toBe('foo qux')
  17. dir.update()
  18. expect(el.className).toBe('foo')
  19. })
  20. it('object value', function () {
  21. el.className = 'foo'
  22. var dir = _.extend({ el: el }, def)
  23. dir.update({
  24. bar: true,
  25. baz: false
  26. })
  27. expect(el.className).toBe('foo bar')
  28. dir.update({
  29. baz: true
  30. })
  31. expect(el.className).toBe('foo baz')
  32. dir.update(null)
  33. expect(el.className).toBe('foo')
  34. dir.update({
  35. 'bar baz': true,
  36. qux: false
  37. })
  38. expect(el.className).toBe('foo bar baz')
  39. dir.update({
  40. qux: true
  41. })
  42. expect(el.className).toBe('foo qux')
  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 c d')
  51. dir.update(['w', 'x y z'])
  52. expect(el.className).toBe('a w x y z')
  53. dir.update()
  54. expect(el.className).toBe('a')
  55. // test mutating array
  56. var arr = ['e', '']
  57. dir.update(arr)
  58. expect(el.className).toBe('a e')
  59. arr.length = 0
  60. arr.push('f')
  61. dir.update(arr)
  62. expect(el.className).toBe('a f')
  63. // test array with objects
  64. dir.update(['x', { y: true, z: true }])
  65. expect(el.className).toBe('a x y z')
  66. dir.update(['x', { y: true, z: false }])
  67. expect(el.className).toBe('a x y')
  68. dir.update(['f', { z: true }])
  69. expect(el.className).toBe('a f z')
  70. dir.update(['l', 'f', { n: true, z: true }])
  71. expect(el.className).toBe('a f z l n')
  72. dir.update(['x', {}])
  73. expect(el.className).toBe('a x')
  74. dir.update()
  75. expect(el.className).toBe('a')
  76. })
  77. })