x-bind-style.spec.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 bindings are merged with existing styles',
  33. html`
  34. <div x-data>
  35. <span style="display: block" x-bind:style="{ color: 'red' }">I should be red</span>
  36. </div>
  37. `,
  38. ({ get }) => {
  39. get('span').should(haveAttribute('style', 'display: block; color: red;'))
  40. }
  41. )
  42. test('CSS custom properties are set',
  43. html`
  44. <div x-data="{custom_color: '#f00'}">
  45. <span style="color: var(--custom-prop)" x-bind:style="{ '--custom-prop': custom_color }">I should be red</span>
  46. </div>
  47. `,
  48. ({ get }) => {
  49. get('span').should(haveAttribute('style', 'color: var(--custom-prop); --custom-prop:#f00;'))
  50. }
  51. )
  52. test('existing CSS custom properties are preserved',
  53. html`
  54. <div x-data="{link: 'var(--custom-prop-a)'}">
  55. <span style="color: var(--custom-prop-b); --custom-prop-a: red" x-bind:style="{ '--custom-prop-b': link }">I should be red</span>
  56. </div>
  57. `,
  58. ({ get }) => {
  59. get('span').should(haveAttribute('style', 'color: var(--custom-prop-b); --custom-prop-a: red; --custom-prop-b:var(--custom-prop-a);'))
  60. }
  61. )