2
0

style_spec.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var _ = require('../../../../../src/util')
  2. var def = require('../../../../../src/directives/internal/style')
  3. var Vue = require('../../../../../src/vue')
  4. function checkPrefixedProp (prop) {
  5. var el = document.createElement('div')
  6. var upper = prop.charAt(0).toUpperCase() + prop.slice(1)
  7. if (!(prop in el.style)) {
  8. var prefixes = ['Webkit', 'Moz', 'ms']
  9. var i = prefixes.length
  10. while (i--) {
  11. if ((prefixes[i] + upper) in el.style) {
  12. prop = prefixes[i] + upper
  13. }
  14. }
  15. }
  16. return prop
  17. }
  18. if (_.inBrowser) {
  19. describe(':style', function () {
  20. var el, dir
  21. beforeEach(function () {
  22. el = document.createElement('div')
  23. dir = { el: el }
  24. _.extend(dir, def)
  25. })
  26. it('plain CSS string', function () {
  27. dir.update('color:red;')
  28. expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
  29. })
  30. it('!important', function () {
  31. dir.update({
  32. color: 'red !important;'
  33. })
  34. expect(el.style.getPropertyPriority('color')).toBe('important')
  35. })
  36. it('camel case', function () {
  37. dir.update({
  38. marginLeft: '30px'
  39. })
  40. expect(el.style.marginLeft).toBe('30px')
  41. })
  42. it('remove on falsy value', function () {
  43. el.style.color = 'red'
  44. dir.update({
  45. color: null
  46. })
  47. expect(el.style.color).toBe('')
  48. })
  49. it('ignore unsupported property', function () {
  50. dir.update({
  51. unsupported: 'test'
  52. })
  53. expect(el.style.unsupported).not.toBe('test')
  54. })
  55. it('auto prefixing', function () {
  56. var prop = checkPrefixedProp('transform')
  57. var val = 'scale(0.5)'
  58. dir.update({
  59. transform: val
  60. })
  61. expect(el.style[prop]).toBe(val)
  62. })
  63. it('object with multiple fields', function () {
  64. el.style.padding = '10px'
  65. dir.update({
  66. color: 'red',
  67. marginRight: '30px'
  68. })
  69. expect(el.style.getPropertyValue('color')).toBe('red')
  70. expect(el.style.getPropertyValue('margin-right')).toBe('30px')
  71. expect(el.style.getPropertyValue('padding')).toBe('10px')
  72. dir.update({
  73. color: 'blue',
  74. padding: null
  75. })
  76. expect(el.style.getPropertyValue('color')).toBe('blue')
  77. expect(el.style.getPropertyValue('margin-right')).toBeFalsy()
  78. expect(el.style.getPropertyValue('padding')).toBeFalsy()
  79. })
  80. it('array of objects', function () {
  81. el.style.padding = '10px'
  82. dir.update([{color: 'red'}, {marginRight: '30px'}])
  83. expect(el.style.getPropertyValue('color')).toBe('red')
  84. expect(el.style.getPropertyValue('margin-right')).toBe('30px')
  85. expect(el.style.getPropertyValue('padding')).toBe('10px')
  86. dir.update([{color: 'blue'}, {padding: null}])
  87. expect(el.style.getPropertyValue('color')).toBe('blue')
  88. expect(el.style.getPropertyValue('margin-right')).toBeFalsy()
  89. expect(el.style.getPropertyValue('padding')).toBeFalsy()
  90. })
  91. it('updates object deep', function (done) {
  92. el.setAttribute(':style', 'divStyling')
  93. var vm = new Vue({
  94. el: el,
  95. data: {divStyling: { display: 'none'}}
  96. })
  97. expect(el.style.display).toBe('none')
  98. vm.divStyling.display = 'block'
  99. _.nextTick(function () {
  100. expect(el.style.display).toBe('block')
  101. done()
  102. })
  103. })
  104. })
  105. }