index.test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. let postcss = require('postcss')
  2. let clamp = require('./')
  3. async function run (input, output, opts) {
  4. let result = await postcss([clamp(opts)]).process(input, {
  5. from: '/test.css'
  6. })
  7. expect(result.css).toEqual(output)
  8. expect(result.warnings()).toHaveLength(0)
  9. return result
  10. }
  11. it('handle simple transformation (only values)', async () => {
  12. await run(
  13. 'a{ width: clamp(10px, 64px, 80px); }',
  14. 'a{ width: max(10px, min(64px, 80px)); }'
  15. )
  16. })
  17. it('handle simple transformation (only values) with preserve', async () => {
  18. await run(
  19. 'a{ width: clamp(10px, 64px, 80px); }',
  20. 'a{ width: max(10px, min(64px, 80px)); width: clamp(10px, 64px, 80px); }',
  21. { preserve: true }
  22. )
  23. })
  24. it('handle transformation with functions', async () => {
  25. await run(
  26. 'a{ width: clamp(calc(100% - 10px), min(10px, 100%), max(40px, 4em)); }',
  27. 'a{ width: max(calc(100% - 10px), min(min(10px, 100%), max(40px, 4em))); }'
  28. )
  29. })
  30. it('handle transformation with functions with preserve', async () => {
  31. await run(
  32. 'a{ width: clamp(calc(100% - 10px), min(10px, 100%), max(40px, 4em)); }',
  33. 'a{ width: max(calc(100% - 10px), min(min(10px, 100%), max(40px, 4em))); ' +
  34. 'width: clamp(calc(100% - 10px), min(10px, 100%), max(40px, 4em)); }',
  35. { preserve: true }
  36. )
  37. })
  38. it('handle transformation with different units', async () => {
  39. await run(
  40. 'a{ width: clamp(10%, 2px, 4rem); }',
  41. 'a{ width: max(10%, min(2px, 4rem)); }'
  42. )
  43. })
  44. it('handle transformation with different units and preserve', async () => {
  45. await run(
  46. 'a{ width: clamp(10%, 2px, 4rem); }',
  47. 'a{ width: max(10%, min(2px, 4rem)); width: clamp(10%, 2px, 4rem); }',
  48. { preserve: true }
  49. )
  50. })
  51. it('transform only function with 3 parameters', async () => {
  52. await run(
  53. 'a{ width: clamp(10%, 2px, 4rem);' +
  54. '\nheight: clamp(10px, 20px, 30px, 40px); }',
  55. 'a{ width: max(10%, min(2px, 4rem));' +
  56. '\nheight: clamp(10px, 20px, 30px, 40px); }'
  57. )
  58. })
  59. it('transform only clamp function', async () => {
  60. await run(
  61. 'a{ width: clamp(10%, 2px, 4rem);\nheight: calc(10px + 100%); }',
  62. 'a{ width: max(10%, min(2px, 4rem));\nheight: calc(10px + 100%); }'
  63. )
  64. })
  65. it('precalculate second and third with the same unit (int values)', async () => {
  66. await run('a{ width: clamp(10%, 2px, 5px); }', 'a{ width: max(10%, 7px); }', {
  67. precalculate: true
  68. })
  69. })
  70. it('precalculate second and third with the same unit (float values)', async () => {
  71. await run(
  72. 'a{ width: clamp(10%, 2.5px, 5.1px); }',
  73. 'a{ width: max(10%, 7.6px); }',
  74. { precalculate: true }
  75. )
  76. })
  77. it('precalculate second and third with the same unit (float and int values)', async () => {
  78. await run(
  79. 'a{ width: clamp(10%, 2.5px, 5px); }',
  80. 'a{ width: max(10%, 7.5px); }',
  81. { precalculate: true }
  82. )
  83. })
  84. it('precalculate 2nd & 3rd with the same unit (float and int vals) & preserve', async () => {
  85. await run(
  86. 'a{ width: clamp(10%, 2.5px, 5px); }',
  87. 'a{ width: max(10%, 7.5px); width: clamp(10%, 2.5px, 5px); }',
  88. { precalculate: true, preserve: true }
  89. )
  90. })
  91. it('precalculate all values with the same unit (int values)', async () => {
  92. await run('a{ width: clamp(10px, 2px, 5px); }', 'a{ width: 17px; }', {
  93. precalculate: true
  94. })
  95. })
  96. it('precalculate all values with the same unit (float values)', async () => {
  97. await run(
  98. 'a{ width: clamp(10.4px, 2.11px, 5.9px); }',
  99. 'a{ width: 18.41px; }',
  100. { precalculate: true }
  101. )
  102. })
  103. it('precalculate all values with the same unit (int and float values)', async () => {
  104. await run('a{ width: clamp(10.4px, 2px, 5.9px); }', 'a{ width: 18.3px; }', {
  105. precalculate: true
  106. })
  107. })
  108. it('handle function with enable precalculation as third', async () => {
  109. await run(
  110. 'a{ width: clamp(10px, 2px, calc(10px + 100%)); }',
  111. 'a{ width: max(10px, min(2px, calc(10px + 100%))); }',
  112. { precalculate: true }
  113. )
  114. })
  115. it('handle function with enable precalculation as second', async () => {
  116. await run(
  117. 'a{ width: clamp(10px, calc(10px + 100%), 2px); }',
  118. 'a{ width: max(10px, min(calc(10px + 100%), 2px)); }',
  119. { precalculate: true }
  120. )
  121. })
  122. it('handle function with enable precalculation as first', async () => {
  123. await run(
  124. 'a{ width: clamp(calc(10px + 100%), 10px, 2px); }',
  125. 'a{ width: max(calc(10px + 100%), 12px); }',
  126. { precalculate: true }
  127. )
  128. })
  129. it('handle function with enable precalculation as all', async () => {
  130. await run(
  131. 'a{ width: clamp(calc(10px + 100%), calc(10rem + 200%), 10px); }',
  132. 'a{ width: max(calc(10px + 100%), min(calc(10rem + 200%), 10px)); }',
  133. { precalculate: true }
  134. )
  135. })
  136. it('handle not valid values', async () => {
  137. await run('a{ width: clamp(a, b, c); }', 'a{ width: max(a, min(b, c)); }', {
  138. precalculate: true
  139. })
  140. })
  141. it('handle not valid values with preserve', async () => {
  142. await run(
  143. 'a{ width: clamp(a, b, c); }',
  144. 'a{ width: max(a, min(b, c)); width: clamp(a, b, c); }',
  145. { precalculate: true, preserve: true }
  146. )
  147. })
  148. it('handle not valid values mixed with valid', async () => {
  149. await run(
  150. 'a{ width: clamp(a, 1px, 2em); }',
  151. 'a{ width: max(a, min(1px, 2em)); }',
  152. { precalculate: true }
  153. )
  154. })
  155. it('handle not valid values mixed with valid and preserve', async () => {
  156. await run(
  157. 'a{ width: clamp(a, 1px, 2em); }',
  158. 'a{ width: max(a, min(1px, 2em)); width: clamp(a, 1px, 2em); }',
  159. { precalculate: true, preserve: true }
  160. )
  161. })
  162. it('handle complex values', async () => {
  163. await run(
  164. 'a{ grid-template-columns: clamp(22rem, 40%, 32rem) minmax(0, 1fr); }',
  165. 'a{ grid-template-columns: max(22rem, min(40%, 32rem)) minmax(0, 1fr); }'
  166. )
  167. })
  168. it('handle multiple complex values', async () => {
  169. await run(
  170. 'a{ margin: clamp(1rem, 2%, 3rem) 4px clamp(5rem, 6%, 7rem) 8rem; }',
  171. 'a{ margin: max(1rem, min(2%, 3rem)) 4px max(5rem, min(6%, 7rem)) 8rem; }'
  172. )
  173. })
  174. it('handle calc', async () => {
  175. await run(
  176. 'a{ margin: 0 40px 0 calc(-1 * clamp(32px, 16vw, 64px)); }',
  177. 'a{ margin: 0 40px 0 calc(-1 * max(32px, min(16vw, 64px))); }'
  178. )
  179. })
  180. it('handle multiple calc', async () => {
  181. await run(
  182. 'a{ margin: calc(-1 * clamp(1px, 2vw, 3px)) calc(-1 * clamp(4px, 5vw, 6px)); }',
  183. 'a{ margin: calc(-1 * max(1px, min(2vw, 3px))) calc(-1 * max(4px, min(5vw, 6px))); }'
  184. )
  185. })
  186. it('handle nested clamp', async () => {
  187. await run(
  188. 'a{ font-size: clamp(clamp(1rem, 2vw, 3rem), 4vw, 5rem); }',
  189. 'a{ font-size: max(max(1rem, min(2vw, 3rem)), min(4vw, 5rem)); }'
  190. )
  191. })