Skip to content

Commit c8c627a

Browse files
authored
Add tests (#1)
* feat: Add vitest * feat: Add tests for main functions * refactor: Move test files to test folder * Add throw error checking
1 parent 0e49c61 commit c8c627a

7 files changed

Lines changed: 155 additions & 5 deletions

File tree

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@
3333
},
3434
"scripts": {
3535
"build": "tsup",
36-
"test": "echo \"Error: no test specified\" && exit 1"
36+
"test": "vitest --config vitest.config.ts"
3737
},
3838
"dependencies": {
3939
"@emotion/unitless": "^0.10.0"
4040
},
4141
"devDependencies": {
4242
"@types/react": "^18.3.5",
4343
"tsup": "^8.3.0",
44-
"typescript": "^5.6.2"
44+
"typescript": "^5.6.2",
45+
"vitest": "^2.1.1"
4546
},
4647
"engines": {
4748
"node": ">=14.0.0"
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { describe, it, expect } from "vitest";
2+
3+
import { stringifyCSSProperties } from "../stringifyCSSProperties";
4+
5+
describe("stringifyCSSProperties", () => {
6+
it("returns string", () => {
7+
expect(stringifyCSSProperties({ color: "teal" })).toBeTypeOf("string");
8+
});
9+
10+
it("returns empty string for empty object input", () => {
11+
expect(stringifyCSSProperties({})).toBe("");
12+
});
13+
14+
it("throws error for string input", () => {
15+
//@ts-ignore
16+
expect(() => stringifyCSSProperties("")).toThrowError(
17+
"Invalid input: 'cssProperties' must be an object."
18+
);
19+
});
20+
21+
it("throws error for 'null' input", () => {
22+
//@ts-ignore
23+
expect(() => stringifyCSSProperties(null)).toThrowError(
24+
"Invalid input: 'cssProperties' must be an object."
25+
);
26+
});
27+
28+
it("throws error for wrong CSS-value", () => {
29+
expect(() =>
30+
//@ts-ignore
31+
stringifyCSSProperties({ color: { color: "teal" } })
32+
).toThrowError(
33+
"Invalid input: value of 'cssProperties' must be string or number."
34+
);
35+
});
36+
37+
it("doesn't change string CSS-value", () => {
38+
const expected = "color:teal; margin:20rem; padding:5px 10px;";
39+
const actual = stringifyCSSProperties({
40+
color: "teal",
41+
margin: "20rem",
42+
padding: "5px 10px",
43+
});
44+
45+
expect(actual).toBe(expected);
46+
});
47+
48+
it("converts CSS-prop name from camel to kebab case", () => {
49+
const expected =
50+
"margin-bottom:20px; background-color:teal; border-radius:30rem; font-family:sans-serif;";
51+
const actual = stringifyCSSProperties({
52+
marginBottom: "20px",
53+
backgroundColor: "teal",
54+
borderRadius: "30rem",
55+
fontFamily: "sans-serif",
56+
});
57+
58+
expect(actual).toBe(expected);
59+
});
60+
61+
it("adds 'px' to numeric CSS-value", () => {
62+
const expected = "margin:20px; padding:5px;";
63+
const actual = stringifyCSSProperties({ margin: 20, padding: 5 });
64+
65+
expect(actual).toBe(expected);
66+
});
67+
68+
it("doesn't add 'px' to unitless CSS-prop and '0' CSS-value", () => {
69+
const expected = "z-index:20; flex:1; opacity:0.5; margin:0; padding:0;";
70+
const actual = stringifyCSSProperties({
71+
zIndex: 20,
72+
flex: 1,
73+
opacity: 0.5,
74+
margin: 0,
75+
padding: 0,
76+
});
77+
78+
expect(actual).toBe(expected);
79+
});
80+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { stringifyStyleMap } from "../stringifyStyleMap";
4+
5+
describe("stringifyStyleMap", () => {
6+
const cssProperties = { color: "teal" };
7+
8+
it("returns string", () => {
9+
expect(stringifyStyleMap({ ".class": cssProperties })).toBeTypeOf("string");
10+
});
11+
12+
it("returns empty string for empty object input", () => {
13+
expect(stringifyStyleMap({})).toBe("");
14+
});
15+
16+
it("throws error for string input", () => {
17+
//@ts-ignore
18+
expect(() => stringifyStyleMap("")).toThrowError(
19+
"Invalid input: 'styleMap' must be an object."
20+
);
21+
});
22+
23+
it("throws error for 'null' input", () => {
24+
//@ts-ignore
25+
expect(() => stringifyStyleMap(null)).toThrowError(
26+
"Invalid input: 'styleMap' must be an object."
27+
);
28+
});
29+
30+
it("doesn't change CSS-selector string", () => {
31+
const expected =
32+
"header{color:teal;} .className{color:teal;} body ul > li{color:teal;}";
33+
const actual = stringifyStyleMap({
34+
header: cssProperties,
35+
".className": cssProperties,
36+
"body ul > li": cssProperties,
37+
});
38+
39+
expect(actual).toBe(expected);
40+
});
41+
42+
// TODO: add reducing feature to stringifyStyleMap
43+
it("doesn't reduce styles for empty cssProperties", () => {
44+
const expected = "header{color:teal;} main{} footer{color:teal;}";
45+
const actual = stringifyStyleMap({
46+
header: cssProperties,
47+
main: {},
48+
footer: cssProperties,
49+
});
50+
51+
expect(actual).toBe(expected);
52+
});
53+
});

src/utils/applyCssUnits.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function applyCssUnits(
77
) {
88
if (typeof value !== "string" && typeof value !== "number") {
99
throw new Error(
10-
"Invalid input: value of 'cssProperties' must be an string or number."
10+
"Invalid input: value of 'cssProperties' must be string or number."
1111
);
1212
}
1313

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,5 @@
108108
"skipLibCheck": true /* Skip type checking all .d.ts files. */
109109
},
110110
"include": ["src"],
111-
"exclude": ["node_modules", "dist"]
111+
"exclude": ["node_modules", "dist", "src/**/*.test.ts"]
112112
}

vitest.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
environment: "node",
6+
include: ["**/*.{test,spec}.?(c|m)[jt]s?(x)"],
7+
exclude: [
8+
"**/node_modules/**",
9+
"**/dist/**",
10+
"**/cypress/**",
11+
"**/.{idea,git,cache,output,temp}/**",
12+
"**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*",
13+
],
14+
},
15+
});

0 commit comments

Comments
 (0)