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.

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