|
1 |
| -const postcss = require('postcss') |
2 |
| -const { replaceSymbols } = require('../src') |
3 |
| - |
4 |
| -const run = (input, replacements, expected) => { |
5 |
| - return postcss([css => replaceSymbols(css, replacements)]) |
6 |
| - .process(input) |
7 |
| - .then(result => { |
8 |
| - expect(result.css).toEqual(expected) |
| 1 | +/* eslint-env jest */ |
| 2 | +import postcss from "postcss"; |
| 3 | +import { replaceSymbols } from "../src"; |
| 4 | + |
| 5 | +const replace = (input, replacements) => { |
| 6 | + const ast = postcss.parse(input); |
| 7 | + replaceSymbols(ast, replacements); |
| 8 | + return ast.toString(); |
| 9 | +}; |
| 10 | + |
| 11 | +test("return empty CSS unchanged", () => { |
| 12 | + expect(replace("", {})).toEqual(""); |
| 13 | +}); |
| 14 | + |
| 15 | +test("not change class names", () => { |
| 16 | + expect(replace(".foo { color: red }", { foo: "bar" })).toEqual( |
| 17 | + ".foo { color: red }" |
| 18 | + ); |
| 19 | +}); |
| 20 | + |
| 21 | +test("not change property names", () => { |
| 22 | + expect(replace(".foo { color: red }", { color: "background" })).toEqual( |
| 23 | + ".foo { color: red }" |
| 24 | + ); |
| 25 | +}); |
| 26 | + |
| 27 | +test("change declaration values", () => { |
| 28 | + expect(replace(".foo { color: red }", { red: "blue" })).toEqual( |
| 29 | + ".foo { color: blue }" |
| 30 | + ); |
| 31 | +}); |
| 32 | + |
| 33 | +test("should change media queries", () => { |
| 34 | + expect( |
| 35 | + replace("@media small { .foo { color: red } }", { |
| 36 | + small: "(max-width: 599px)" |
9 | 37 | })
|
10 |
| -} |
11 |
| - |
12 |
| -describe('replace-symbols', () => { |
13 |
| - it('should return empty CSS unchanged', () => { |
14 |
| - return run('', {}, '') |
15 |
| - }) |
16 |
| - |
17 |
| - it('should not change unless there are translations', () => { |
18 |
| - return run('.foo { color: red }', {}, '.foo { color: red }') |
19 |
| - }) |
20 |
| - |
21 |
| - it('should not change class names', () => { |
22 |
| - return run('.foo { color: red }', { foo: 'bar' }, '.foo { color: red }') |
23 |
| - }) |
24 |
| - |
25 |
| - it('should not change property names', () => { |
26 |
| - return run( |
27 |
| - '.foo { color: red }', |
28 |
| - { color: 'background' }, |
29 |
| - '.foo { color: red }' |
30 |
| - ) |
31 |
| - }) |
32 |
| - |
33 |
| - it('should change declaration values', () => { |
34 |
| - return run('.foo { color: red }', { red: 'blue' }, '.foo { color: blue }') |
35 |
| - }) |
36 |
| - |
37 |
| - it('should change symbols within declaration values', () => { |
38 |
| - return run( |
39 |
| - '.foo { box-shadow: 0 0 0 4px red }', |
40 |
| - { red: 'blue' }, |
41 |
| - '.foo { box-shadow: 0 0 0 4px blue }' |
42 |
| - ) |
43 |
| - }) |
44 |
| - |
45 |
| - it('should change multiple symbols within declaration values', () => { |
46 |
| - return run( |
47 |
| - '.foo { box-shadow: top left blur spread color }', |
48 |
| - { top: '1px', left: '2px', blur: '3px', spread: '4px', color: 'red' }, |
49 |
| - '.foo { box-shadow: 1px 2px 3px 4px red }' |
50 |
| - ) |
51 |
| - }) |
52 |
| - |
53 |
| - it('should change complex symbols, if you feel like trolling yourself', () => { |
54 |
| - return run( |
55 |
| - '.foo { box-shadow: 1px 0.5em 3px $sass-a #f00 }', |
56 |
| - { |
57 |
| - '1px': '1rem', |
58 |
| - '0.5em': '10px', |
59 |
| - '3px': '$sass-b', |
60 |
| - '$sass-a': '4px', |
61 |
| - '#f00': 'green' |
62 |
| - }, |
63 |
| - '.foo { box-shadow: 1rem 10px $sass-b 4px green }' |
64 |
| - ) |
65 |
| - }) |
66 |
| - |
67 |
| - it('should be able to rewrite variables', () => { |
68 |
| - return run( |
69 |
| - '.foo { color: var(--red) }', |
70 |
| - { '--red': '--blue' }, |
71 |
| - '.foo { color: var(--blue) }' |
72 |
| - ) |
73 |
| - }) |
74 |
| - |
75 |
| - it('should not replace half a variable', () => { |
76 |
| - return run( |
77 |
| - '.foo { color: colors.red; background: red.blue; }', |
78 |
| - { red: 'green', blue: 'white' }, |
79 |
| - '.foo { color: colors.red; background: red.blue; }' |
80 |
| - ) |
81 |
| - }) |
82 |
| - |
83 |
| - it('should not replace a replacement', () => { |
84 |
| - return run( |
85 |
| - '.foo { background: blue; color: red }', |
86 |
| - { red: 'blue', blue: 'green' }, |
87 |
| - '.foo { background: green; color: blue }' |
88 |
| - ) |
89 |
| - }) |
90 |
| - |
91 |
| - it('should not get trolled by me', () => { |
92 |
| - return run( |
93 |
| - '.foo { color: white }', |
94 |
| - { white: 'lightblue', blue: 'green' }, |
95 |
| - '.foo { color: lightblue }' |
96 |
| - ) |
97 |
| - return run( |
98 |
| - '.foo { color: white }', |
99 |
| - { white: 'light blue', blue: 'green' }, |
100 |
| - '.foo { color: light blue }' |
101 |
| - ) |
102 |
| - }) |
103 |
| - |
104 |
| - it('should change media queries', () => { |
105 |
| - return run( |
106 |
| - '@media small { .foo { color: red } }', |
107 |
| - { small: '(max-width: 599px)' }, |
108 |
| - '@media (max-width: 599px) { .foo { color: red } }' |
109 |
| - ) |
110 |
| - }) |
111 |
| -}) |
| 38 | + ).toEqual("@media (max-width: 599px) { .foo { color: red } }"); |
| 39 | +}); |
0 commit comments