style.spec.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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('string', 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('falsy number', done => {
  36. vm.styles = { opacity: 0 }
  37. waitForUpdate(() => {
  38. expect(vm.$el.style.opacity).toBe('0')
  39. }).then(done)
  40. })
  41. it('plain object', done => {
  42. vm.styles = { color: 'red' }
  43. waitForUpdate(() => {
  44. expect(vm.$el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
  45. }).then(done)
  46. })
  47. it('camelCase', done => {
  48. vm.styles = { marginRight: '10px' }
  49. waitForUpdate(() => {
  50. expect(vm.$el.style.marginRight).toBe('10px')
  51. }).then(done)
  52. })
  53. it('remove if falsy value', done => {
  54. vm.$el.style.color = 'red'
  55. waitForUpdate(() => {
  56. vm.styles = { color: null }
  57. }).then(() => {
  58. expect(vm.$el.style.color).toBe('')
  59. }).then(done)
  60. })
  61. it('ignore unsupported property', done => {
  62. vm.styles = { foo: 'bar' }
  63. waitForUpdate(() => {
  64. expect(vm.$el.style.foo).not.toBe('bar')
  65. }).then(done)
  66. })
  67. it('auto prefix', done => {
  68. const prop = checkPrefixedProp('transform')
  69. const val = 'scale(0.5)'
  70. vm.styles = { transform: val }
  71. waitForUpdate(() => {
  72. expect(vm.$el.style[prop]).toBe(val)
  73. }).then(done)
  74. })
  75. it('object with multiple entries', done => {
  76. vm.$el.style.color = 'red'
  77. vm.styles = {
  78. marginLeft: '10px',
  79. marginRight: '15px'
  80. }
  81. waitForUpdate(() => {
  82. expect(vm.$el.style.getPropertyValue('color')).toBe('red')
  83. expect(vm.$el.style.getPropertyValue('margin-left')).toBe('10px')
  84. expect(vm.$el.style.getPropertyValue('margin-right')).toBe('15px')
  85. vm.styles = {
  86. color: 'blue',
  87. padding: null
  88. }
  89. }).then(() => {
  90. expect(vm.$el.style.getPropertyValue('color')).toBe('blue')
  91. expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
  92. expect(vm.$el.style.getPropertyValue('margin-left')).toBeFalsy()
  93. expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
  94. // handle falsy value
  95. vm.styles = null
  96. }).then(() => {
  97. expect(vm.$el.style.getPropertyValue('color')).toBeFalsy()
  98. expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
  99. expect(vm.$el.style.getPropertyValue('margin-left')).toBeFalsy()
  100. expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
  101. }).then(done)
  102. })
  103. it('array of objects', done => {
  104. vm.$el.style.padding = '10px'
  105. vm.styles = [{ color: 'red' }, { marginRight: '20px' }]
  106. waitForUpdate(() => {
  107. expect(vm.$el.style.getPropertyValue('color')).toBe('red')
  108. expect(vm.$el.style.getPropertyValue('margin-right')).toBe('20px')
  109. expect(vm.$el.style.getPropertyValue('padding')).toBe('10px')
  110. vm.styles = [{ color: 'blue' }, { padding: null }]
  111. }).then(() => {
  112. expect(vm.$el.style.getPropertyValue('color')).toBe('blue')
  113. expect(vm.$el.style.getPropertyValue('margin-right')).toBeFalsy()
  114. expect(vm.$el.style.getPropertyValue('padding')).toBeFalsy()
  115. }).then(done)
  116. })
  117. it('updates objects deeply', done => {
  118. vm.styles = { display: 'none' }
  119. waitForUpdate(() => {
  120. expect(vm.$el.style.display).toBe('none')
  121. vm.styles.display = 'block'
  122. }).then(() => {
  123. expect(vm.$el.style.display).toBe('block')
  124. }).then(done)
  125. })
  126. it('background size with only one value', done => {
  127. vm.styles = { backgroundSize: '100%' }
  128. waitForUpdate(() => {
  129. expect(vm.$el.style.cssText.replace(/\s/g, '')).toMatch(/background-size:100%(auto)?;/)
  130. }).then(done)
  131. })
  132. it('should work with interpolation', done => {
  133. vm.styles = { fontSize: `${vm.fontSize}px` }
  134. waitForUpdate(() => {
  135. expect(vm.$el.style.fontSize).toBe('16px')
  136. }).then(done)
  137. })
  138. })