/
css-modules-test.ts
381 lines (356 loc) · 11.4 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { test, expect } from "@playwright/test";
import type * as Playwright from "@playwright/test";
import { PlaywrightFixture } from "./helpers/playwright-fixture";
import type { Fixture, AppFixture } from "./helpers/create-fixture";
import {
createAppFixture,
createFixture,
css,
js,
} from "./helpers/create-fixture";
test.describe("CSS Modules", () => {
// TODO: Finish all todo tests
let testTodo = (
title: string,
testFunction?: (
args: { page: Playwright.Page },
testInfo: Playwright.TestInfo
) => any
) => {};
let fixture: Fixture;
let appFixture: AppFixture;
test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/root.jsx": js`
import { Links, Outlet, Scripts } from "@remix-run/react";
import { cssModulesStylesheetUrl } from "@remix-run/css-modules";
import stylesHref from "~/styles.css";
export function links() {
return [
{ rel: "stylesheet", href: stylesHref },
{ rel: "stylesheet", href: cssModulesStylesheetUrl },
];
}
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/page-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/page-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/routes/layout.jsx": js`
import { Outlet } from "@remix-run/react";
import { Container } from "~/lib/container";
import { Heading } from "~/lib/heading";
import { Text } from "~/lib/text";
export default function() {
return (
<div>
<Container>
<Heading level={1}>Layout</Heading>
<Outlet />
</Container>
</div>
);
}
`,
"app/routes/layout/one.jsx": js`
import { Heading } from "~/lib/heading";
import { Input } from "~/lib/input";
export default function() {
return (
<div>
<Heading level={2}>Subpage 1</Heading>
<Input name="email" type="email" defaultValue="hi@remix.run" />
</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/container.jsx": js`
import styles from "~/lib/container.module.css";
export function Container(props) {
return (
<div
{...props}
data-ui-container=""
className={(styles.container + " " + (props.className || "")).trim()}
/>
);
}
`,
"app/lib/container.module.css": css`
.container {
display: block;
max-width: 1000px;
margin: 0 auto;
}
`,
"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/input.jsx": js`
import styles from "./input.module.css";
export function Input(props) {
return (
<input
{...props}
data-ui-input=""
className={(styles.input + " " + (props.className || "")).trim()}
/>
);
}
`,
"app/lib/input.module.css": css`
.input {
width: 100%;
outline-offset: 2px;
appearance: none;
border-radius: 3px;
border: 1px solid #000;
}
`,
"app/lib/text.jsx": js`
import styles from "./text.module.css";
export function Text({ as: Comp = "span", ...props }) {
return (
<Comp
{...props}
data-ui-text=""
className={props.className ? props.className + " " + styles.text : 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";
import { Text } from "~/lib/text";
export function Heading({ level = 2, ...props }) {
return (
<Text
{...props}
as={"h" + level}
data-ui-heading=""
data-ui-heading-level={level}
className={props.className ? props.className + " " + styles.heading : styles.heading}
/>
);
}
`,
"app/lib/heading.module.css": css`
.heading {
font-weight: bold;
}
.heading[data-heading-level="1"] {
font-size: 3rem;
}
.heading[data-heading-level="2"] {
font-size: 2rem;
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.afterAll(async () => {
await appFixture.close();
});
test("server renders with hashed classnames", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
let badge = await app.getElement("[data-ui-badge]");
let badgeClasses = badge.attr("class").split(" ");
let found: string[] = [];
for (let className of badgeClasses) {
if (/^badge-module__[\w-]+_badge$/.test(className)) {
found.push(className);
}
}
expect(found.length).toBe(1);
});
test("composes from global classname", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/page-a");
let button = await app.getElement("[data-ui-button]");
expect(
button
.attr("class")
// .button composes the global .reset--button class
.includes("reset--button")
).toBe(true);
});
test("composes from locally scoped classname", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/page-b");
let button = await app.getElement("[data-ui-button]");
let buttonClasses = button.attr("class").split(" ");
let found: string[] = [];
for (let className of buttonClasses) {
if (
/^button-module__[\w-]+_button$/.test(className) ||
/^button-module__[\w-]+_buttonRed$/.test(className)
) {
found.push(className);
}
}
expect(found.length).toBe(2);
});
// TODO: Feature not implemented yet
testTodo("composes from imported module classname");
test("composes :global selector with :local selector", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
let badge = await app.getElement("[data-ui-badge]");
let buttonClasses = badge.attr("class").split(" ");
let found: string[] = [];
for (let className of buttonClasses) {
if (
/^badge-module__[\w-]+_badge$/.test(className) ||
// .badge composes the global .bold class
className === "bold"
) {
found.push(className);
}
}
expect(found.length).toBe(2);
});
testTodo("keeps declarations in the correct order", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/page-a");
// let heading = await app.getElement("[data-ui-heading]"); A couple of
// approaches:
// - get the stylesheet text and check the order
// - check that the heading itself has computed styles from `Text` that are
// not overridden
// - check that the heading does NOT have computed styles from `Text` that
// are overridden
});
test.describe("No javascript", () => {
test.use({ javaScriptEnabled: false });
test("loads the CSS Modules stylesheet", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
let cssResponses = app.collectResponses((url) =>
url.pathname.endsWith(".css")
);
await app.goto("/");
expect(cssResponses.length).toEqual(2);
});
});
});