style.spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Vue from 'vue'
  2. function checkPrefixedProp (prop) {
  3. var el = document.createElement('div')
  4. var upper = prop.charAt(0).toUpperCase() + prop.slice(1)
  5. if (!(prop in el.style)) {
  6. var prefixes = ['Webkit', 'Moz', 'ms']
  7. var i = prefixes.length
  8. while (i--) {
  9. if ((prefixes[i] + upper) in el.style) {
  10. prop = prefixes[i] + upper
  11. }
  12. }
  13. }
  14. return prop
  15. }
  16. describe('Directive v-bind:style', () => {
  17. let vm
  18. beforeEach(() => {
  19. vm = new Vue({
  20. template: '<div :style="styles"></div>',
  21. data () {
  22. return {
  23. styles: {},
  24. fontSize: 16
  25. }
  26. }
  27. }).$mount()
  28. })
  29. it('plain object', done => {
  30. vm.styles = { color: 'red' }
  31. waitForUpdate(() => {
  32. expect(vm.$el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
  33. }).then(done)
  34. })
  35. it('camelCase', done => {
  36. vm.styles = { marginRight: '10px' }
  37. waitForUpdate(() => {
  38. expect(vm.$el.style.marginRight).toBe('10px')
  39. }).then(done)
  40. })
  41. it('remove if falsy value', done => {
  42. vm.$el.style.color = 'red'
  43. waitForUpdate(() => {
  44. vm.styles = { color: null }
  45. }).then(() => {
  46. expect(vm.$el.style.color).toBe('')
  47. }).then(done)
  48. })
  49. it('ignore unsupported property', done => {
  50. vm.styles = { foo: 'bar' }
  51. waitForUpdate(() => {
  52. expect(vm.$el.style.foo).not.toBe('bar')
  53. }).then(done)
  54. })
  55. it('auto prefix', done => {
  56. const prop = checkPrefixedProp('transform')
  57. const val = 'scale(0.5)'
  58. vm.styles = { transform: val }
  59. waitForUpdate(() => {
  60. expect(vm.$el.style[prop]).toBe(val)
  61. }).then(done)
  62. })
  63. it('object with multiple entries', done => {
  64. vm.$el.style.color = 'red'
  65. vm.styles = {
  66. marginLeft: '10px',
  67. marginRight: '15px'
  68. }
  69. waitForUpdate(() => {
  70. expect(vm.$el.style.getPropertyValue('color')).toBe('red')
  71. expect(vm.$el.style.getPropertyValue('margin-left')).toBe('10px')
  72. expect(vm.$el.style.getPropertyValue('margin-right')).toBe('15px')
  73. vm.styles = {
  74. color: 'blue',
  75. padding: null
  76. }
  77. }).then(() => {
  78. expect(vm.$el.style.getPropertyValue('color')).toBe('blue')
  79. expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
  80. expect(vm.$el.style.getPropertyValue('margin-left')).toBeFalsy()
  81. expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
  82. // handle falsy value
  83. vm.styles = null
  84. }).then(() => {
  85. expect(vm.$el.style.getPropertyValue('color')).toBeFalsy()
  86. expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
  87. expect(vm.$el.style.getPropertyValue('margin-left')).toBeFalsy()
  88. expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
  89. }).then(done)
  90. })
  91. it('array of objects', done => {
  92. vm.$el.style.padding = '10px'
  93. vm.styles = [{ color: 'red' }, { marginRight: '20px' }]
  94. waitForUpdate(() => {
  95. expect(vm.$el.style.getPropertyValue('color')).toBe('red')
  96. expect(vm.$el.style.getPropertyValue('margin-right')).toBe('20px')
  97. expect(vm.$el.style.getPropertyValue('padding')).toBe('10px')
  98. vm.styles = [{ color: 'blue' }, { padding: null }]
  99. }).then(() => {
  100. expect(vm.$el.style.getPropertyValue('color')).toBe('blue')
  101. expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
  102. expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
  103. }).then(done)
  104. })
  105. it('updates objects deeply', done => {
  106. vm.styles = { display: 'none' }
  107. waitForUpdate(() => {
  108. expect(vm.$el.style.display).toBe('none')
  109. vm.styles.display = 'block'
  110. }).then(() => {
  111. expect(vm.$el.style.display).toBe('block')
  112. }).then(done)
  113. })
  114. it('background size with only one value', done => {
  115. vm.styles = { backgroundSize: '100%' }
  116. waitForUpdate(() => {
  117. expect(vm.$el.style.cssText.replace(/\s/g, '')).toMatch(/background-size:100%(auto)?;/)
  118. }).then(done)
  119. })
  120. it('should work with interpolation', done => {
  121. vm.styles = { fontSize: `${vm.fontSize}px` }
  122. waitForUpdate(() => {
  123. expect(vm.$el.style.fontSize).toBe('16px')
  124. }).then(done)
  125. })
  126. })