/
css-modules-test.ts
300 lines (264 loc) · 8.12 KB
/
css-modules-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import {
createAppFixture,
createFixture,
collectResponses,
css,
js,
getElement,
} from "./helpers/create-fixture";
import type { Fixture, AppFixture } from "./helpers/create-fixture";
describe("rendering", () => {
let fixture: Fixture;
let app: AppFixture;
beforeAll(async () => {
fixture = await createFixture({
files: {
"app/root.jsx": js`
import { Links, Outlet, Scripts } from "remix";
import stylesHref from "~/styles.css";
import cssModuleStylesheetUrl from "@remix-run/css-modules";
export function links() {
return [
{ rel: "stylesheet", href: stylesHref },
{
rel: "stylesheet",
href: cssModuleStylesheetUrl,
"data-css-modules-link": "",
},
];
}
export default function Root() {
return (
<html>
<head>
<Links />
</head>
<body>
<div id="content">
<Outlet />
</div>
<Scripts />
</body>
</html>
)
}
`,
"app/styles.css": css`
.reset--button {
appearance: none;
display: inline-block;
border: none;
padding: 0;
text-decoration: none;
background: 0;
color: inherit;
font: inherit;
}
`,
"app/routes/index.jsx": js`
import { Badge } from "~/lib/badge";
export default function() {
return (
<div>
<h1>Index</h1>
<Badge>Hello</Badge>
</div>
);
}
`,
"app/routes/a.jsx": js`
import { Badge } from "~/lib/badge";
import { Button } from "~/lib/button";
import { Heading } from "~/lib/heading";
import { Text } from "~/lib/text";
export default function() {
return (
<div>
<Heading level={1}>Route A</Heading>
<Badge>Welcome</Badge>
<Text>This is really good information, eh?</Text>
<Button>Click Me</Button>
</div>
);
}
`,
"app/routes/b.jsx": js`
import { Button } from "~/lib/button";
import { Heading } from "~/lib/heading";
import { Text } from "~/lib/text";
export default function() {
return (
<div>
<Heading level={1}>Route B</Heading>
<Text>Here's a red button</Text>
<Button variant="red">Click Me</Button>
</div>
);
}
`,
"app/lib/button.jsx": js`
import styles from "./button.module.css";
export function Button({ variant, ...props }) {
return (
<Button
{...props}
data-ui-button=""
className={variant === "red" ? styles.buttonRed : styles.button}
/>
);
}
`,
"app/lib/button.module.css": css`
.button {
/* TODO: composes: text from "./text.module.css"; */
composes: reset--button from global;
padding: 0.5rem 1rem;
box-shadow: none;
background-color: dodgerblue;
color: white;
font-weight: bold;
}
.buttonRed {
composes: button;
background-color: red;
}
`,
"app/lib/badge.jsx": js`
import styles from "./badge.module.css";
export function Badge(props) {
return (
<div
{...props}
data-ui-badge=""
className={styles.badge + " bold"}
/>
);
`,
"app/lib/badge.module.css": css`
.badge {
/* TODO: composes: text from "./text.module.css"; */
display: inline-block;
padding: 0.25rem 0.5rem;
background-color: lightgray;
color: black;
font-size: 0.8rem;
}
:global(.bold):local(.badge) {
text-transform: uppercase;
}
`,
"app/lib/text.jsx": js`
import styles from "./text.module.css";
export function Text(props) {
return (
<span
{...props}
data-ui-text=""
className={styles.text}
/>
);
}
`,
"app/lib/text.module.css": css`
.text {
font-family: system-ui, sans-serif;
letter-spacing: -0.5px;
}
`,
"app/lib/heading.jsx": js`
import styles from "./heading.module.css";
export function Heading({ level = 2, ...props }) {
let Comp = "h" + level;
return (
<Comp
{...props}
data-ui-heading=""
data-ui-heading-level={level}
className={styles.heading}
/>
);
`,
"app/lib/heading.module.css": css`
.heading {
/* TODO: composes: text from "./text.module.css"; */
font-weight: bold;
}
.heading[data-heading-level="1"] {
font-size: 3rem;
}
.heading[data-heading-level="2"] {
font-size: 2rem;
}
`,
},
});
app = await createAppFixture(fixture);
});
afterAll(async () => {
await app.close();
});
it("loads the CSS Modules stylesheet", async () => {
await app.disableJavaScript();
let cssResponses = collectResponses(app.page, (url) =>
url.pathname.endsWith(".css")
);
await app.page.goto("/");
await app.page.waitForSelector("[data-css-modules-link]");
expect(cssResponses.length).toEqual(2);
});
it("server renders with hashed classnames", async () => {
let res = await app.page.goto("/");
let badge = getElement(await res.text(), "[data-ui-badge]");
let badgeClasses = badge.attr("class").split(" ");
let found: string[] = [];
for (let className of badgeClasses) {
if (/^badge__[\w-]+$/.test(className)) {
found.push(className);
}
}
expect(found.length).toBe(1);
});
it("composes from global classname", async () => {
let res = await app.page.goto("/a");
let button = getElement(await res.text(), "[data-ui-button]");
expect(
button
.attr("class")
// .button composes the global .reset--button class
.includes("reset--button")
).toBe(true);
});
it("composes from locally scoped classname", async () => {
let res = await app.page.goto("/b");
let button = getElement(await res.text(), "[data-ui-button]");
let buttonClasses = button.attr("class").split(" ");
let found: string[] = [];
for (let className of buttonClasses) {
if (
/^button__[\w-]+$/.test(className) ||
/^buttonRed__[\w-]+$/.test(className)
) {
found.push(className);
}
}
expect(found.length).toBe(2);
});
// TODO: Feature not implemented yet
// it.todo("composes from imported module classname", () => {});
it("composes :global selector with :local selector", async () => {
let res = await app.page.goto("/");
let badge = getElement(await res.text(), "[data-ui-badge]");
let buttonClasses = badge.attr("class").split(" ");
let found: string[] = [];
for (let className of buttonClasses) {
if (
/^badge__[\w-]+$/.test(className) ||
// .badge composes the global .bold class
className === "bold"
) {
found.push(className);
}
}
expect(found.length).toBe(2);
});
});