x-bind-style.spec.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { beHidden, beVisible, haveText, beChecked, haveAttribute, haveClasses, haveValue, notBeChecked, notHaveAttribute, notHaveClasses, test, html } from '../../utils'
  2. test('style attribute object binding',
  3. html`
  4. <div x-data>
  5. <span x-bind:style="{ color: 'red' }">I should be red</span>
  6. </div>
  7. `,
  8. ({ get }) => {
  9. get('span').should(haveAttribute('style', 'color: red;'))
  10. }
  11. )
  12. test('style attribute object binding using camelCase syntax',
  13. html`
  14. <div x-data>
  15. <span x-bind:style="{ backgroundColor: 'red' }">I should be red</span>
  16. </div>
  17. `,
  18. ({ get }) => {
  19. get('span').should(haveAttribute('style', 'background-color: red;'))
  20. }
  21. )
  22. test('style attribute object binding using kebab-case syntax',
  23. html`
  24. <div x-data>
  25. <span x-bind:style="{ 'background-color': 'red' }">I should be red</span>
  26. </div>
  27. `,
  28. ({ get }) => {
  29. get('span').should(haveAttribute('style', 'background-color: red;'))
  30. }
  31. )
  32. test('style attribute object binding with CSS variable',
  33. html`
  34. <div x-data x-bind:style="{ '--MyCSS-Variable': 0.25 }">
  35. <span style="opacity: var(--MyCSS-Variable);">I should be hardly visible</span>
  36. </div>
  37. `,
  38. ({ get }) => {
  39. get('div').should(haveAttribute('style', '--MyCSS-Variable:0.25;'))
  40. }
  41. )
  42. test('style attribute object bindings are merged with existing styles',
  43. html`
  44. <div x-data>
  45. <span style="display: block" x-bind:style="{ color: 'red' }">I should be red</span>
  46. </div>
  47. `,
  48. ({ get }) => {
  49. get('span').should(haveAttribute('style', 'display: block; color: red;'))
  50. }
  51. )
  52. test('CSS custom properties are set',
  53. html`
  54. <div x-data="{custom_color: '#f00'}">
  55. <span style="color: var(--custom-prop)" x-bind:style="{ '--custom-prop': custom_color }">I should be red</span>
  56. </div>
  57. `,
  58. ({ get }) => {
  59. get('span').should(haveAttribute('style', 'color: var(--custom-prop); --custom-prop:#f00;'))
  60. }
  61. )
  62. test('existing CSS custom properties are preserved',
  63. html`
  64. <div x-data="{link: 'var(--custom-prop-a)'}">
  65. <span style="color: var(--custom-prop-b); --custom-prop-a: red" x-bind:style="{ '--custom-prop-b': link }">I should be red</span>
  66. </div>
  67. `,
  68. ({ get }) => {
  69. get('span').should(haveAttribute('style', 'color: var(--custom-prop-b); --custom-prop-a: red; --custom-prop-b:var(--custom-prop-a);'))
  70. }
  71. )