Skip to content

Commit 418d92f

Browse files
committed
add tests
1 parent 95f48cd commit 418d92f

File tree

6 files changed

+3187
-90
lines changed

6 files changed

+3187
-90
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ language injection and can easily be setup to give you full syntax highlighting
122122
1. Go to intellij's `Settings`, and choose `Editor` > `Language Injections`
123123
2. Press the `+` button and choose `JS Tagged Literal Injection`
124124
3. Name your new injection setting to something memorable, like `cssModules`
125-
4. Set your "Language ID" to `CSS` (or `SASS` if your using that)
125+
4. Set your "Language ID" to `CSS` (or `SASS` if you're using that)
126126
5. Set "Places Patterns" to `+ taggedString("cssModules")`
127127
6. Leave all other options empty and click "OK"
128128
7. Enjoy css-in-js like a boss
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
presets: ["@babel/preset-env", "@babel/preset-react"],
3+
plugins: ["babel-plugin-css-to-module"],
34
};

package.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
{
22
"name": "inline-css-modules-react",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"main": "dist/inline-css-modules-react.umd.js",
55
"module": "dist/inline-css-modules-react.esm.js",
66
"type": "module",
77
"types": "dist/inline-css-modules-react.d.ts",
88
"scripts": {
99
"watch": "rollup --config --watch",
10+
"test": "jest",
1011
"build": "rm -rf ./dist && rollup --config",
1112
"prepublish": "npm run build"
1213
},
1314
"author": "Tony Lefler <[email protected]>",
1415
"license": "MIT",
16+
"repository": {
17+
"type": "git",
18+
"url": "https://github.com/geoctrl/babel-plugin-css-to-module"
19+
},
1520
"devDependencies": {
1621
"@babel/core": "^7.23.0",
1722
"@babel/preset-env": "^7.22.20",
1823
"@babel/preset-react": "^7.22.15",
1924
"@rollup/plugin-babel": "^6.0.3",
2025
"@rollup/plugin-typescript": "^11.1.4",
26+
"@testing-library/react": "^14.0.0",
27+
"@types/jest": "^29.5.5",
28+
"babel-plugin-css-to-module": "^0.2.0",
29+
"jest-environment-jsdom": "^29.7.0",
30+
"postcss": "^8.4.31",
2131
"prettier": "^3.0.3",
2232
"react": "^18.2.0",
33+
"react-dom": "^18.2.0",
2334
"rollup": "^3.29.4",
2435
"rollup-plugin-terser": "^7.0.2",
2536
"tslib": "^2.6.2",
@@ -28,5 +39,8 @@
2839
"peerDependencies": {
2940
"react": ">=16"
3041
},
31-
"packageManager": "[email protected]"
42+
"packageManager": "[email protected]",
43+
"dependencies": {
44+
"jest": "^29.7.0"
45+
}
3246
}

src/inline-css-modules-react.test.tsx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
import React from "react";
6+
import { screen, render } from "@testing-library/react";
7+
import {
8+
cssModules,
9+
useCssModules,
10+
} from "../dist/inline-css-modules-react.umd";
11+
12+
describe("cssModules and useCssModules", () => {
13+
test("default should not have styles", () => {
14+
render(<NoStyles />);
15+
const div = screen.getByText(text);
16+
expect(getComputedStyle(div).backgroundColor).toBeFalsy();
17+
});
18+
19+
test("should display styles from applied class", () => {
20+
render(<WithStyles />);
21+
const div = screen.getByText(text);
22+
expect(getComputedStyle(div).backgroundColor).toBe("red");
23+
});
24+
25+
test("style className should be the same as applied className", () => {
26+
render(<WithStyles />);
27+
const className = document.head
28+
.querySelector("style")
29+
.innerHTML.split("{")[0]
30+
.slice(1);
31+
expect(className).toBe(s.test);
32+
});
33+
34+
test("should only mount one style element", () => {
35+
render(
36+
<>
37+
<WithStyles />
38+
<WithStyles />
39+
</>,
40+
);
41+
const styles = document.head.querySelectorAll("style");
42+
expect(styles.length).toBe(1);
43+
});
44+
45+
test("should remove style after unmount", () => {
46+
const { unmount } = render(<WithStyles />);
47+
expect(document.head.querySelector("style")).toBeTruthy();
48+
unmount();
49+
expect(document.head.querySelector("style")).toBeFalsy();
50+
});
51+
});
52+
53+
// setup
54+
const text = "Hello, World!";
55+
56+
function NoStyles() {
57+
return <div>{text}</div>;
58+
}
59+
60+
function WithStyles() {
61+
useCssModules(css);
62+
return <div className={s.test}>{text}</div>;
63+
}
64+
65+
const { css, s } = cssModules`
66+
.test {
67+
background-color: red;
68+
}
69+
`;

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
],
1111
"moduleResolution": "node",
1212
"declaration": true,
13-
"allowJs": true
13+
"allowJs": true,
14+
"jsx": "react"
1415
},
1516
"include": [
1617
"src",

0 commit comments

Comments
 (0)