You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.4 KiB

4 years ago
  1. /* global describe, it */
  2. import postcss from 'postcss'
  3. import assert from 'assert'
  4. import constants from '../src'
  5. const test = (input, expected) => {
  6. let processor = postcss([constants])
  7. assert.equal(processor.process(input).css, expected)
  8. }
  9. describe('constants', () => {
  10. it('should pass through an empty string', () => {
  11. test('', '')
  12. })
  13. it('should export a constant', () => {
  14. test('@value red blue;', ':export {\n red: blue\n}')
  15. })
  16. it('gives an error when there is no semicolon between lines', () => {
  17. const input = '@value red blue\n@value green yellow'
  18. let processor = postcss([constants])
  19. const result = processor.process(input)
  20. const warnings = result.warnings()
  21. assert.equal(warnings.length, 1)
  22. assert.equal(warnings[0].text, 'Invalid value definition: red blue\n@value green yellow')
  23. })
  24. it('should export a more complex constant', () => {
  25. test('@value small (max-width: 599px);', ':export {\n small: (max-width: 599px)\n}')
  26. })
  27. it('should replace constants within the file', () => {
  28. test('@value blue red; .foo { color: blue; }', ':export {\n blue: red;\n}\n.foo { color: red; }')
  29. })
  30. it('should import and re-export a simple constant', () => {
  31. test('@value red from "./colors.css";', ':import("./colors.css") {\n i__const_red_0: red\n}\n:export {\n red: i__const_red_0\n}')
  32. })
  33. it('should import a simple constant and replace usages', () => {
  34. test('@value red from "./colors.css"; .foo { color: red; }', ':import("./colors.css") {\n i__const_red_1: red;\n}\n:export {\n red: i__const_red_1;\n}\n.foo { color: i__const_red_1; }')
  35. })
  36. it('should import and alias a constant and replace usages', () => {
  37. test('@value blue as red from "./colors.css"; .foo { color: red; }', ':import("./colors.css") {\n i__const_red_2: blue;\n}\n:export {\n red: i__const_red_2;\n}\n.foo { color: i__const_red_2; }')
  38. })
  39. it('should import multiple from a single file', () => {
  40. test(
  41. `@value blue, red from "./colors.css";
  42. .foo { color: red; }
  43. .bar { color: blue }`,
  44. `:import("./colors.css") {
  45. i__const_blue_3: blue;
  46. i__const_red_4: red;
  47. }
  48. :export {
  49. blue: i__const_blue_3;
  50. red: i__const_red_4;
  51. }
  52. .foo { color: i__const_red_4; }
  53. .bar { color: i__const_blue_3 }`)
  54. })
  55. it('should import from a definition', () => {
  56. test(
  57. '@value colors: "./colors.css"; @value red from colors;',
  58. ':import("./colors.css") {\n i__const_red_5: red\n}\n' +
  59. ':export {\n colors: "./colors.css";\n red: i__const_red_5\n}'
  60. )
  61. })
  62. it('should only allow values for paths if defined in the right order', () => {
  63. test(
  64. '@value red from colors; @value colors: "./colors.css";',
  65. ':import(colors) {\n i__const_red_6: red\n}\n' +
  66. ':export {\n red: i__const_red_6;\n colors: "./colors.css"\n}'
  67. )
  68. })
  69. it('should allow transitive values', () => {
  70. test(
  71. '@value aaa: red;\n@value bbb: aaa;\n.a { color: bbb; }',
  72. ':export {\n aaa: red;\n bbb: red;\n}\n.a { color: red; }'
  73. )
  74. })
  75. it('should allow transitive values within calc', () => {
  76. test(
  77. '@value base: 10px;\n@value large: calc(base * 2);\n.a { margin: large; }',
  78. ':export {\n base: 10px;\n large: calc(10px * 2);\n}\n.a { margin: calc(10px * 2); }'
  79. )
  80. })
  81. it('should preserve import order', () => {
  82. test(
  83. '@value a from "./a.css"; @value b from "./b.css";',
  84. ':import("./a.css") {\n i__const_a_7: a\n}\n' +
  85. ':import("./b.css") {\n i__const_b_8: b\n}\n' +
  86. ':export {\n a: i__const_a_7;\n b: i__const_b_8\n}'
  87. )
  88. })
  89. it('should allow custom-property-style names', () => {
  90. test(
  91. '@value --red from "./colors.css"; .foo { color: --red; }',
  92. ':import("./colors.css") {\n i__const___red_9: --red;\n}\n' +
  93. ':export {\n --red: i__const___red_9;\n}\n' +
  94. '.foo { color: i__const___red_9; }')
  95. })
  96. it('should allow all colour types', () => {
  97. test(
  98. '@value named: red; @value 3char #0f0; @value 6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1);\n' +
  99. '.foo { color: named; background-color: 3char; border-top-color: 6char; border-bottom-color: rgba; outline-color: hsla; }',
  100. ':export {\n named: red;\n 3char: #0f0;\n 6char: #00ff00;\n rgba: rgba(34, 12, 64, 0.3);\n hsla: hsla(220, 13.0%, 18.0%, 1);\n}\n' +
  101. '.foo { color: red; background-color: #0f0; border-top-color: #00ff00; border-bottom-color: rgba(34, 12, 64, 0.3); outline-color: hsla(220, 13.0%, 18.0%, 1); }')
  102. })
  103. it('should import multiple from a single file on multiple lines', () => {
  104. test(
  105. `@value (
  106. blue,
  107. red
  108. ) from "./colors.css";
  109. .foo { color: red; }
  110. .bar { color: blue }`,
  111. `:import("./colors.css") {
  112. i__const_blue_10: blue;
  113. i__const_red_11: red;
  114. }
  115. :export {
  116. blue: i__const_blue_10;
  117. red: i__const_red_11;
  118. }
  119. .foo { color: i__const_red_11; }
  120. .bar { color: i__const_blue_10 }`)
  121. })
  122. it('should allow definitions with commas in them', () => {
  123. test(
  124. '@value coolShadow: 0 11px 15px -7px rgba(0,0,0,.2),0 24px 38px 3px rgba(0,0,0,.14) ;\n' +
  125. '.foo { box-shadow: coolShadow; }',
  126. ':export {\n coolShadow: 0 11px 15px -7px rgba(0,0,0,.2),0 24px 38px 3px rgba(0,0,0,.14);\n}\n' +
  127. '.foo { box-shadow: 0 11px 15px -7px rgba(0,0,0,.2),0 24px 38px 3px rgba(0,0,0,.14); }')
  128. })
  129. it('should allow values with nested parantheses', () => {
  130. test(
  131. '@value aaa: color(red lightness(50%));',
  132. ':export {\n aaa: color(red lightness(50%))\n}'
  133. )
  134. })
  135. })