Skip to content

Commit ffe2a9a

Browse files
test(compiler): assert the attribute query [class=…] emits
The commit before this one is named `test(compiler)` and put its cases in src/__tests__/native/attributes.test.tsx, so the compiler plane had no coverage of a fix(compiler) change. A runtime test can only observe the RESULT of evaluating the query, which makes a wrong prop name and a wrong evaluation the same red. Seven cases over the emitted stylesheet: the four operators `class` reaches, plus a data-* and a plain attribute that must keep their own channels. Reverting selector-builder.ts reddens exactly the four class cases and leaves the other two green, so the set discriminates rather than merely coupling to the change.
1 parent c99d320 commit ffe2a9a

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { compile } from "react-native-css/compiler";
2+
3+
/**
4+
* The attribute query the compiler emits for one class's first rule.
5+
*
6+
* The runtime suite can only observe the RESULT of evaluating this, so a wrong
7+
* prop name and a wrong evaluation are the same red there. This reads the
8+
* emitted query itself, which is the half the runtime cannot see.
9+
*/
10+
function attributeQueryFor(css: string, className: string): unknown {
11+
const rules = compile(css).stylesheet().s;
12+
const entry = rules?.find(([name]) => name === className);
13+
if (!entry) {
14+
throw new Error(
15+
`the compiler emitted no rule for .${className} — the stylesheet carries ${JSON.stringify(rules?.map(([name]) => name))}`,
16+
);
17+
}
18+
const [, declarations] = entry;
19+
return declarations.map((declaration) => declaration.aq);
20+
}
21+
22+
/**
23+
* `class` is the CSS attribute; `className` is the prop it arrives on in React
24+
* Native. Emitting `class` names a prop no element has, so every `[class…]`
25+
* selector matches nothing.
26+
*/
27+
const CLASS_ATTRIBUTE_CASES = [
28+
[
29+
"exact match",
30+
`.exact[class="a b"] { color: red }`,
31+
"exact",
32+
["a", "className", "=", "a b"],
33+
],
34+
[
35+
"whitespace-list match",
36+
`.list[class~="b"] { color: red }`,
37+
"list",
38+
["a", "className", "~=", "b"],
39+
],
40+
[
41+
"substring match",
42+
`.part[class*="b"] { color: red }`,
43+
"part",
44+
["a", "className", "*=", "b"],
45+
],
46+
["presence", `.present[class] { color: red }`, "present", ["a", "className"]],
47+
] as const;
48+
49+
describe("[class…] reads the prop the class list arrives on", () => {
50+
test("the census is not empty, so the cases below are not vacuous", () => {
51+
expect(CLASS_ATTRIBUTE_CASES.length).toBeGreaterThan(0);
52+
});
53+
54+
test.each(CLASS_ATTRIBUTE_CASES)("%s", (_label, css, className, expected) => {
55+
expect(attributeQueryFor(css, className)).toStrictEqual([[expected]]);
56+
});
57+
});
58+
59+
describe("every other attribute keeps its own channel", () => {
60+
test("a data-* attribute compiles to a dataSet query, not an attribute one", () => {
61+
expect(
62+
attributeQueryFor(`.d[data-state="open"] { color: red }`, "d"),
63+
).toStrictEqual([[["d", "state", "=", "open"]]]);
64+
});
65+
66+
test("a plain attribute is untouched by the class mapping", () => {
67+
expect(attributeQueryFor(`.p[disabled] { color: red }`, "p")).toStrictEqual(
68+
[[["a", "disabled"]]],
69+
);
70+
});
71+
});

0 commit comments

Comments
 (0)