Skip to content

Commit f3b7f93

Browse files
committedJun 3, 2017
Split replace value symbols test
1 parent 3442cc3 commit f3b7f93

File tree

2 files changed

+96
-110
lines changed

2 files changed

+96
-110
lines changed
 

‎test/replaceSymbols.test.js

+38-110
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,39 @@
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)"
937
})
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+
});

‎test/replaceValueSymbols.test.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* eslint-env jest */
2+
import { replaceValueSymbols as replace } from "../src";
3+
4+
test("not change empty css", () => {
5+
expect(replace("", {})).toEqual("");
6+
});
7+
8+
test("not change unless there are translations", () => {
9+
expect(replace("red", {})).toEqual("red");
10+
});
11+
12+
test("change symbols within values", () => {
13+
expect(replace("0 0 0 4px red", { red: "blue" })).toEqual("0 0 0 4px blue");
14+
});
15+
16+
test("change multiple symbols within values", () => {
17+
expect(
18+
replace("top left blur spread color", {
19+
top: "1px",
20+
left: "2px",
21+
blur: "3px",
22+
spread: "4px",
23+
color: "red"
24+
})
25+
).toEqual("1px 2px 3px 4px red");
26+
});
27+
28+
test("change complex symbols, if you feel like trolling yourself", () => {
29+
expect(
30+
replace("1px 0.5em 3px $sass-a #f00", {
31+
"1px": "1rem",
32+
"0.5em": "10px",
33+
"3px": "$sass-b",
34+
"$sass-a": "4px",
35+
"#f00": "green"
36+
})
37+
).toEqual("1rem 10px $sass-b 4px green");
38+
});
39+
40+
test("rewrite custom properties", () => {
41+
expect(replace("var(--red)", { "--red": "--blue" })).toEqual("var(--blue)");
42+
});
43+
44+
test("not replace half a variable", () => {
45+
expect(
46+
replace("colors.red red.blue", {
47+
red: "green",
48+
blue: "white",
49+
colors: "weights"
50+
})
51+
).toEqual("colors.red red.blue");
52+
});
53+
54+
test("not replace a replacement", () => {
55+
expect(replace("blue red", { red: "blue", blue: "green" })).toEqual(
56+
"green blue"
57+
);
58+
});

0 commit comments

Comments
 (0)
Please sign in to comment.