Skip to content

Commit caf065a

Browse files
committed
test: add calc test
1 parent 631fa72 commit caf065a

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

tests/calc.test.tsx

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import type { AbstractCalculator } from '../src';
2+
import { genCalc } from '../src';
3+
4+
describe('calculator', () => {
5+
const cases: [
6+
(
7+
calc: (num: number | AbstractCalculator) => AbstractCalculator,
8+
) => string | number,
9+
{ js: number; css: string },
10+
][] = [
11+
[
12+
// 1 + 1
13+
(calc) => calc(1).add(1).equal(),
14+
{
15+
js: 2,
16+
css: 'calc(1px + 1px)',
17+
},
18+
],
19+
[
20+
// (1 + 1) * 4
21+
(calc) => calc(1).add(1).mul(4).equal(),
22+
{
23+
js: 8,
24+
css: 'calc((1px + 1px) * 4)',
25+
},
26+
],
27+
[
28+
// (2 + 4) / 2 - 2
29+
(calc) => calc(2).add(4).div(2).sub(2).equal(),
30+
{
31+
js: 1,
32+
css: 'calc((2px + 4px) / 2 - 2px)',
33+
},
34+
],
35+
[
36+
// Bad case
37+
// (2 + 4) / (3 - 2) - 2
38+
(calc) => calc(2).add(4).div(calc(3).sub(2)).sub(2).equal(),
39+
{
40+
js: 4,
41+
css: 'calc((2px + 4px) / (3px - 2px) - 2px)',
42+
},
43+
],
44+
[
45+
// Bad case
46+
// 2 * (2 + 3)
47+
(calc) => calc(2).mul(calc(2).add(3)).equal(),
48+
{
49+
js: 10,
50+
css: 'calc(2px * (2px + 3px))',
51+
},
52+
],
53+
[
54+
// (1 + 2) * 3
55+
(calc) => calc(calc(1).add(2)).mul(3).equal(),
56+
{
57+
js: 9,
58+
css: 'calc((1px + 2px) * 3)',
59+
},
60+
],
61+
[
62+
// 1 + (2 - 1)
63+
(calc) => calc(1).add(calc(2).sub(1)).equal(),
64+
{
65+
js: 2,
66+
css: 'calc(1px + (2px - 1px))',
67+
},
68+
],
69+
[
70+
// 1 + 2 * 2
71+
(calc) => calc(1).add(calc(2).mul(2)).equal(),
72+
{
73+
js: 5,
74+
css: 'calc(1px + 2px * 2)',
75+
},
76+
],
77+
[
78+
// 5 - (2 - 1)
79+
(calc) => calc(5).sub(calc(2).sub(1)).equal(),
80+
{
81+
js: 4,
82+
css: 'calc(5px - (2px - 1px))',
83+
},
84+
],
85+
[
86+
// 2 * 6 / 3
87+
(calc) => calc(2).mul(6).div(3).equal(),
88+
{
89+
js: 4,
90+
css: 'calc(2px * 6 / 3)',
91+
},
92+
],
93+
[
94+
// 6 / 3 * 2
95+
(calc) => calc(6).div(3).mul(2).equal(),
96+
{
97+
js: 4,
98+
css: 'calc(6px / 3 * 2)',
99+
},
100+
],
101+
[
102+
// Bad case
103+
// 6 / (3 * 2)
104+
(calc) => calc(6).div(calc(3).mul(2)).equal(),
105+
{
106+
js: 1,
107+
css: 'calc(6px / (3px * 2))',
108+
},
109+
],
110+
[
111+
// 6
112+
(calc) => calc(6).equal(),
113+
{
114+
js: 6,
115+
css: '6px',
116+
},
117+
],
118+
[
119+
// 1000 + 100 without unit
120+
(calc) => calc(1000).add(100).equal({ unit: false }),
121+
{
122+
js: 1100,
123+
css: 'calc(1000 + 100)',
124+
},
125+
],
126+
];
127+
128+
cases.forEach(([exp, { js, css }], index) => {
129+
it(`js calc ${index + 1}`, () => {
130+
expect(exp(genCalc('js', new Set()))).toBe(js);
131+
});
132+
133+
it(`css calc ${index + 1}`, () => {
134+
expect(exp(genCalc('css', new Set()))).toBe(css);
135+
});
136+
});
137+
138+
it('css calc should work with string', () => {
139+
const calc = genCalc('css', new Set());
140+
expect(calc('var(--var1)').add('var(--var2)').equal()).toBe(
141+
'calc(var(--var1) + var(--var2))',
142+
);
143+
});
144+
145+
it('css calc var should skip zIndex', () => {
146+
const calc = genCalc('css', new Set(['--ant-z-index']));
147+
expect(calc('var(--ant-z-index)').add(93).equal()).toBe(
148+
'calc(var(--ant-z-index) + 93)',
149+
);
150+
});
151+
});

0 commit comments

Comments
 (0)