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.

344 lines
12 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. // Jasmine unit tests
  2. // To run tests, run these commands from the project root:
  3. // 1. `npm install -g jasmine-node`
  4. // 2. `jasmine-node spec`
  5. /* global describe, it, expect */
  6. 'use strict';
  7. var postcss = require('postcss');
  8. var pxToViewport = require('..');
  9. var basicCSS = '.rule { font-size: 15px }';
  10. var { filterPropList } = require('../src/prop-list-matcher');
  11. describe('px-to-viewport', function() {
  12. it('should work on the readme example', function () {
  13. var input = 'h1 { margin: 0 0 20px; font-size: 32px; line-height: 2; letter-spacing: 1px; }';
  14. var output = 'h1 { margin: 0 0 6.25vw; font-size: 10vw; line-height: 2; letter-spacing: 1px; }';
  15. var processed = postcss(pxToViewport()).process(input).css;
  16. expect(processed).toBe(output);
  17. });
  18. it('should replace the px unit with vw', function () {
  19. var processed = postcss(pxToViewport()).process(basicCSS).css;
  20. var expected = '.rule { font-size: 4.6875vw }';
  21. expect(processed).toBe(expected);
  22. });
  23. it('should handle < 1 values and values without a leading 0', function () {
  24. var rules = '.rule { margin: 0.5rem .5px -0.2px -.2em }';
  25. var expected = '.rule { margin: 0.5rem 0.15625vw -0.0625vw -.2em }';
  26. var options = {
  27. minPixelValue: 0
  28. };
  29. var processed = postcss(pxToViewport(options)).process(rules).css;
  30. expect(processed).toBe(expected);
  31. });
  32. it('should remain unitless if 0', function () {
  33. var expected = '.rule { font-size: 0px; font-size: 0; }';
  34. var processed = postcss(pxToViewport()).process(expected).css;
  35. expect(processed).toBe(expected);
  36. });
  37. });
  38. describe('value parsing', function() {
  39. it('should not replace values in double quotes or single quotes', function () {
  40. var options = {
  41. propList: ['*']
  42. };
  43. var rules = '.rule { content: \'16px\'; font-family: "16px"; font-size: 16px; }';
  44. var expected = '.rule { content: \'16px\'; font-family: "16px"; font-size: 5vw; }';
  45. var processed = postcss(pxToViewport(options)).process(rules).css;
  46. expect(processed).toBe(expected);
  47. });
  48. it('should not replace values in `url()`', function () {
  49. var rules = '.rule { background: url(16px.jpg); font-size: 16px; }';
  50. var expected = '.rule { background: url(16px.jpg); font-size: 5vw; }';
  51. var processed = postcss(pxToViewport()).process(rules).css;
  52. expect(processed).toBe(expected);
  53. });
  54. it('should not replace values with an uppercase P or X', function () {
  55. var rules = '.rule { margin: 12px calc(100% - 14PX); height: calc(100% - 20px); font-size: 12Px; line-height: 16px; }';
  56. var expected = '.rule { margin: 3.75vw calc(100% - 14PX); height: calc(100% - 6.25vw); font-size: 12Px; line-height: 5vw; }';
  57. var processed = postcss(pxToViewport()).process(rules).css;
  58. expect(processed).toBe(expected);
  59. });
  60. });
  61. describe('unitToConvert', function() {
  62. it('should ignore non px values by default', function () {
  63. var expected = '.rule { font-size: 2em }';
  64. var processed = postcss(pxToViewport()).process(expected).css;
  65. expect(processed).toBe(expected);
  66. });
  67. it('should convert only values described in options', function () {
  68. var rules = '.rule { font-size: 5em; line-height: 2px }';
  69. var expected = '.rule { font-size: 1.5625vw; line-height: 2px }';
  70. var options = {
  71. unitToConvert: 'em'
  72. };
  73. var processed = postcss(pxToViewport(options)).process(rules).css;
  74. expect(processed).toBe(expected);
  75. });
  76. });
  77. describe('viewportWidth', function() {
  78. it('should should replace using 320px by default', function() {
  79. var expected = '.rule { font-size: 4.6875vw }';
  80. var processed = postcss(pxToViewport()).process(basicCSS).css;
  81. expect(processed).toBe(expected);
  82. });
  83. it('should replace using viewportWidth from options', function() {
  84. var expected = '.rule { font-size: 3.125vw }';
  85. var options = {
  86. viewportWidth: 480
  87. }
  88. var processed = postcss(pxToViewport(options)).process(basicCSS).css;
  89. expect(processed).toBe(expected);
  90. })
  91. });
  92. describe('unitPrecision', function () {
  93. it('should replace using a decimal of 2 places', function () {
  94. var expected = '.rule { font-size: 4.69vw }';
  95. var options = {
  96. unitPrecision: 2
  97. };
  98. var processed = postcss(pxToViewport(options)).process(basicCSS).css;
  99. expect(processed).toBe(expected);
  100. });
  101. });
  102. describe('viewportUnit', function() {
  103. it('should replace using unit from options', function() {
  104. var rules = '.rule { margin-top: 15px }';
  105. var expected = '.rule { margin-top: 4.6875vh }';
  106. var options = {
  107. viewportUnit: 'vh'
  108. };
  109. var processed = postcss(pxToViewport(options)).process(rules).css;
  110. expect(processed).toBe(expected);
  111. });
  112. });
  113. describe('fontViewportUnit', function() {
  114. it('should replace only font-size using unit from options', function() {
  115. var rules = '.rule { margin-top: 15px; font-size: 8px; }';
  116. var expected = '.rule { margin-top: 4.6875vw; font-size: 2.5vmax; }';
  117. var options = {
  118. fontViewportUnit: 'vmax'
  119. };
  120. var processed = postcss(pxToViewport(options)).process(rules).css;
  121. expect(processed).toBe(expected);
  122. });
  123. });
  124. describe('selectorBlackList', function () {
  125. it('should ignore selectors in the selector black list', function () {
  126. var rules = '.rule { font-size: 15px } .rule2 { font-size: 15px }';
  127. var expected = '.rule { font-size: 4.6875vw } .rule2 { font-size: 15px }';
  128. var options = {
  129. selectorBlackList: ['.rule2']
  130. };
  131. var processed = postcss(pxToViewport(options)).process(rules).css;
  132. expect(processed).toBe(expected);
  133. });
  134. it('should ignore every selector with `body$`', function () {
  135. var rules = 'body { font-size: 16px; } .class-body$ { font-size: 16px; } .simple-class { font-size: 16px; }';
  136. var expected = 'body { font-size: 5vw; } .class-body$ { font-size: 16px; } .simple-class { font-size: 5vw; }';
  137. var options = {
  138. selectorBlackList: ['body$']
  139. };
  140. var processed = postcss(pxToViewport(options)).process(rules).css;
  141. expect(processed).toBe(expected);
  142. });
  143. it('should only ignore exactly `body`', function () {
  144. var rules = 'body { font-size: 16px; } .class-body { font-size: 16px; } .simple-class { font-size: 16px; }';
  145. var expected = 'body { font-size: 16px; } .class-body { font-size: 5vw; } .simple-class { font-size: 5vw; }';
  146. var options = {
  147. selectorBlackList: [/^body$/]
  148. };
  149. var processed = postcss(pxToViewport(options)).process(rules).css;
  150. expect(processed).toBe(expected);
  151. });
  152. });
  153. describe('mediaQuery', function () {
  154. it('should replace px in media queries', function () {
  155. var options = {
  156. mediaQuery: true
  157. };
  158. var processed = postcss(pxToViewport(options)).process('@media (min-width: 500px) { .rule { font-size: 16px } }').css;
  159. var expected = '@media (min-width: 156.25vw) { .rule { font-size: 5vw } }';
  160. expect(processed).toBe(expected);
  161. });
  162. });
  163. describe('propList', function () {
  164. it('should only replace properties in the prop list', function () {
  165. var css = '.rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }';
  166. var expected = '.rule { font-size: 5vw; margin: 5vw; margin-left: 5px; padding: 5px; padding-right: 5vw }';
  167. var options = {
  168. propList: ['*font*', 'margin*', '!margin-left', '*-right', 'pad']
  169. };
  170. var processed = postcss(pxToViewport(options)).process(css).css;
  171. expect(processed).toBe(expected);
  172. });
  173. it('should only replace properties in the prop list with wildcard', function () {
  174. var css = '.rule { font-size: 16px; margin: 16px; margin-left: 5px; padding: 5px; padding-right: 16px }';
  175. var expected = '.rule { font-size: 16px; margin: 5vw; margin-left: 5px; padding: 5px; padding-right: 16px }';
  176. var options = {
  177. propList: ['*', '!margin-left', '!*padding*', '!font*']
  178. };
  179. var processed = postcss(pxToViewport(options)).process(css).css;
  180. expect(processed).toBe(expected);
  181. });
  182. it('should replace all properties when prop list is not given', function () {
  183. var rules = '.rule { margin: 16px; font-size: 15px }';
  184. var expected = '.rule { margin: 5vw; font-size: 4.6875vw }';
  185. var processed = postcss(pxToViewport()).process(rules).css;
  186. expect(processed).toBe(expected);
  187. });
  188. });
  189. describe('minPixelValue', function () {
  190. it('should not replace values below minPixelValue', function () {
  191. var options = {
  192. propWhiteList: [],
  193. minPixelValue: 2
  194. };
  195. var rules = '.rule { border: 1px solid #000; font-size: 16px; margin: 1px 10px; }';
  196. var expected = '.rule { border: 1px solid #000; font-size: 5vw; margin: 1px 3.125vw; }';
  197. var processed = postcss(pxToViewport(options)).process(rules).css;
  198. expect(processed).toBe(expected);
  199. });
  200. });
  201. describe('exclude', function () {
  202. var rules = '.rule { border: 1px solid #000; font-size: 16px; margin: 1px 10px; }';
  203. var covered = '.rule { border: 1px solid #000; font-size: 5vw; margin: 1px 3.125vw; }'
  204. it('when using regex at the time, the style should not be overwritten.', function () {
  205. var options = {
  206. exclude: /node_modules/
  207. }
  208. var processed = postcss(pxToViewport(options)).process(rules, {
  209. from: '/node_modules/main.css'
  210. }).css;
  211. expect(processed).toBe(rules);
  212. });
  213. it('when using regex at the time, the style should be overwritten.', function () {
  214. var options = {
  215. exclude: /node_modules/
  216. }
  217. var processed = postcss(pxToViewport(options)).process(rules, {
  218. from: '/example/main.css'
  219. }).css;
  220. expect(processed).toBe(covered);
  221. });
  222. it('when using array at the time, the style should not be overwritten.', function () {
  223. var options = {
  224. exclude: [/node_modules/, /exclude/]
  225. }
  226. var processed = postcss(pxToViewport(options)).process(rules, {
  227. from: '/exclude/main.css'
  228. }).css;
  229. expect(processed).toBe(rules);
  230. });
  231. it('when using array at the time, the style should be overwritten.', function () {
  232. var options = {
  233. exclude: [/node_modules/, /exclude/]
  234. }
  235. var processed = postcss(pxToViewport(options)).process(rules, {
  236. from: '/example/main.css'
  237. }).css;
  238. expect(processed).toBe(covered);
  239. });
  240. });
  241. describe('filter-prop-list', function () {
  242. it('should find "exact" matches from propList', function () {
  243. var propList = ['font-size', 'margin', '!padding', '*border*', '*', '*y', '!*font*'];
  244. var expected = 'font-size,margin';
  245. expect(filterPropList.exact(propList).join()).toBe(expected);
  246. });
  247. it('should find "contain" matches from propList and reduce to string', function () {
  248. var propList = ['font-size', '*margin*', '!padding', '*border*', '*', '*y', '!*font*'];
  249. var expected = 'margin,border';
  250. expect(filterPropList.contain(propList).join()).toBe(expected);
  251. });
  252. it('should find "start" matches from propList and reduce to string', function () {
  253. var propList = ['font-size', '*margin*', '!padding', 'border*', '*', '*y', '!*font*'];
  254. var expected = 'border';
  255. expect(filterPropList.startWith(propList).join()).toBe(expected);
  256. });
  257. it('should find "end" matches from propList and reduce to string', function () {
  258. var propList = ['font-size', '*margin*', '!padding', 'border*', '*', '*y', '!*font*'];
  259. var expected = 'y';
  260. expect(filterPropList.endWith(propList).join()).toBe(expected);
  261. });
  262. it('should find "not" matches from propList and reduce to string', function () {
  263. var propList = ['font-size', '*margin*', '!padding', 'border*', '*', '*y', '!*font*'];
  264. var expected = 'padding';
  265. expect(filterPropList.notExact(propList).join()).toBe(expected);
  266. });
  267. it('should find "not contain" matches from propList and reduce to string', function () {
  268. var propList = ['font-size', '*margin*', '!padding', '!border*', '*', '*y', '!*font*'];
  269. var expected = 'font';
  270. expect(filterPropList.notContain(propList).join()).toBe(expected);
  271. });
  272. it('should find "not start" matches from propList and reduce to string', function () {
  273. var propList = ['font-size', '*margin*', '!padding', '!border*', '*', '*y', '!*font*'];
  274. var expected = 'border';
  275. expect(filterPropList.notStartWith(propList).join()).toBe(expected);
  276. });
  277. it('should find "not end" matches from propList and reduce to string', function () {
  278. var propList = ['font-size', '*margin*', '!padding', '!border*', '*', '!*y', '!*font*'];
  279. var expected = 'y';
  280. expect(filterPropList.notEndWith(propList).join()).toBe(expected);
  281. });
  282. });