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.

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