style.spec.js 4.2 KB

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