Skip to content

Commit e2fcfbb

Browse files
committed
Add tests for spacing
1 parent 8e4ea24 commit e2fcfbb

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
lines changed

src/bin/css_to_ts/css_to_ts.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { generateGetColorDecisionsTsCode } from "./colorDecisions";
33
import { generateGetColorOptionsTsCode } from "./colorOptions";
44
import { getProjectRoot } from "../tools/getProjectRoot";
55
import { generateTypographyTsCode } from "./typography";
6+
import { generateSpacingTsCode } from "./spacing";
67
import * as fs from "fs";
78
import { join as pathJoin, basename as pathBasename, relative as pathRelative } from "path";
89

@@ -62,11 +63,6 @@ fs.writeFileSync(
6263
Buffer.from([warningMessage, ``, generateBreakpointsTsCode(rawCssCode)].join("\n"), "utf8")
6364
);
6465

65-
fs.writeFileSync(
66-
pathJoin(generatedDirPath, "typography.ts"),
67-
Buffer.from([warningMessage, ``, generateTypographyTsCode(rawCssCode)].join("\n"), "utf8")
68-
);
69-
7066
fs.writeFileSync(
7167
pathJoin(generatedDirPath, "typography.ts"),
7268
Buffer.from(
@@ -80,3 +76,8 @@ fs.writeFileSync(
8076
"utf8"
8177
)
8278
);
79+
80+
fs.writeFileSync(
81+
pathJoin(generatedDirPath, "spacing.ts"),
82+
Buffer.from([warningMessage, ``, generateSpacingTsCode(rawCssCode), ``].join("\n"), "utf8")
83+
);

src/bin/css_to_ts/spacing.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,20 @@ export const parseSpacing = memoize((rawCssCode: string): SpacingTokenAndValue[]
8888

8989
return spacingTokenAndValues;
9090
});
91+
92+
export function generateSpacingTsCode(rawCssCode: string) {
93+
const spacingTokenAndValues = parseSpacing(rawCssCode);
94+
95+
return [
96+
`export const spacingTokenByValue= ${JSON.stringify(
97+
Object.fromEntries(spacingTokenAndValues.map(({ token, value }) => [token, value])),
98+
null,
99+
4
100+
)};`,
101+
``,
102+
`type SpacingTokenByValue = typeof spacingTokenByValue;`,
103+
``,
104+
`export type SpacingToken = keyof SpacingTokenByValue;`,
105+
``
106+
].join("\n");
107+
}

src/lib/spacing.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
/* eslint-disable @typescript-eslint/ban-types */
22
import { assert } from "tsafe/assert";
33
import { capitalize } from "tsafe/capitalize";
4-
5-
const spacingTokenByValue = {
6-
"1v": "0.25rem",
7-
"1w": "0.50rem",
8-
"3v": "0.75rem",
9-
"2w": "1rem"
10-
} as const;
11-
12-
type SpacingTokenByValue = typeof spacingTokenByValue;
13-
14-
export type SpacingToken = keyof SpacingTokenByValue;
4+
import { spacingTokenByValue } from "./generatedFromCss/spacing";
5+
import type { SpacingToken, SpacingTokenByValue } from "./generatedFromCss/spacing";
156

167
export function spacing<T extends SpacingToken>(token: T): SpacingTokenByValue[T];
178
export function spacing<
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { it, expect } from "vitest";
2+
import { generateSpacingTsCode } from "../../../../src/bin/css_to_ts/spacing";
3+
4+
export default () =>
5+
it("Generates spacing TS code", () => {
6+
const rawCssCode = `
7+
.fr-m-7v {
8+
margin: 1.75rem !important;
9+
}
10+
11+
.fr-m-12v,
12+
.fr-m-6w {
13+
margin: 3rem !important;
14+
}
15+
16+
@media (min-width: 36em) { }
17+
18+
@media (min-width: 48em) { }
19+
20+
@media (min-width: 62em) { }
21+
22+
@media (min-width: 78em) { }
23+
`.replace(/^\n/, "");
24+
25+
const got = generateSpacingTsCode(rawCssCode);
26+
27+
const expected = `
28+
export const spacingTokenByValue= {
29+
"7v": "1.75rem",
30+
"12v": "3rem",
31+
"6w": "3rem"
32+
};
33+
34+
type SpacingTokenByValue = typeof spacingTokenByValue;
35+
36+
export type SpacingToken = keyof SpacingTokenByValue;
37+
`.replace(/^\n/, "");
38+
39+
expect(got).toBe(expected);
40+
});

test/behavior/bin/spacing/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import testCases from "cases-of-test";
22
import _0 from "./parseSpacing.test";
3+
import _1 from "./generateSpacingTsCode.test";
34

45
export default () =>
56
testCases({
6-
"tests": [[_0]]
7+
"tests": [[_0], [_1]]
78
});

0 commit comments

Comments
 (0)