Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/__tests__/compiler/important.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { render } from "@testing-library/react-native";
import { compile } from "react-native-css/compiler";
import { Pressable } from "react-native-css/components/Pressable";
import { registerCSS, testID } from "react-native-css/jest";
import { Specificity } from "react-native-css/utilities";

// !important selects which of the runtime's two merge passes a class takes, so a callback style is checked through both

const PLAIN = `.bg-red { background-color: red; }`;
const IMPORTANT = `.bg-red\\! { background-color: red !important; }`;

function ruleFor(css: string) {
const rule = compile(css).stylesheet().s?.[0]?.[1]?.[0];

if (rule === undefined) {
throw new Error(`compiled no rule for ${css}`);
}

return rule;
}

test("!important sets the Important slot, and nothing else moves", () => {
const plain = ruleFor(PLAIN);
const important = ruleFor(IMPORTANT);

expect(plain.s[Specificity.Important]).toBeUndefined();
expect(important.s[Specificity.Important]).toBe(1);

expect(plain.d).toStrictEqual(important.d);
expect(plain.s[Specificity.ClassName]).toBe(
important.s[Specificity.ClassName],
);
});

test("the marker selects the merge pass, and the operand order is how you see it", () => {
registerCSS(`${PLAIN}\n${IMPORTANT}`);

expect(
render(
<Pressable
testID={testID}
className="bg-red"
style={() => ({ backgroundColor: "blue" })}
/>,
).getByTestId(testID).props.style,
).toStrictEqual([{ backgroundColor: "#f00" }, { backgroundColor: "blue" }]);

// Reversed with the marker: the important declaration is rightmost and wins.
expect(
render(
<Pressable
testID={testID}
className="bg-red!"
style={() => ({ backgroundColor: "blue" })}
/>,
).getByTestId(testID).props.style,
).toStrictEqual([{ backgroundColor: "blue" }, { backgroundColor: "#f00" }]);
});

test("the callback ran on both routes rather than reaching the view unevaluated", () => {
registerCSS(`${PLAIN}\n${IMPORTANT}`);

for (const className of ["bg-red", "bg-red!"]) {
const style = render(
<Pressable
testID={testID}
className={className}
style={({ pressed }) => ({ opacity: pressed ? 0.5 : 1 })}
/>,
).getByTestId(testID).props.style as unknown[];

expect(style).toContainEqual({ opacity: 1 });

for (const entry of style) {
expect(typeof entry).not.toBe("function");
}
}
});
10 changes: 10 additions & 0 deletions src/__tests__/native/components.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ test("nativeStyleMapping with boolean true works alongside other styles", () =>
expect(component.props.style).toStrictEqual({ color: "#f00" });
});

test("nativeStyleMapping with boolean true takes an !important class's value over the element's", () => {
registerCSS(`.text-center { text-align: center !important; }`);

const component = render(
<TextInput testID={testID} className="text-center" textAlign="left" />,
).getByTestId(testID);

expect(component.props.textAlign).toBe("center");
});

test("nativeStyleMapping with boolean true on custom component", () => {
registerCSS(`.text-right { text-align: right; }`);

Expand Down
11 changes: 7 additions & 4 deletions src/__tests__/native/rightIsInline.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* runtime behavior with intentionally malformed data.
*/

import { StyleSheet } from "react-native";

import { render } from "@testing-library/react-native";
import { Text } from "react-native-css/components/Text";
import { View } from "react-native-css/components/View";
Expand Down Expand Up @@ -576,8 +578,8 @@ describe("rightIsInline - Important Style Interactions", () => {
/>,
).getByTestId(testID);

// Important overrides, VAR_SYMBOL filtered, literal kept
expect(component.props.style).toEqual({
// Important overrides, VAR_SYMBOL filtered, literal kept; the inline style stays a layer of its own
expect(StyleSheet.flatten(component.props.style)).toEqual({
color: "#f00",
fontSize: 12,
lineHeight: 24,
Expand All @@ -604,13 +606,14 @@ describe("rightIsInline - Important Style Interactions", () => {
<Text testID={testID} className="important-multi" style={inlineStyle} />,
).getByTestId(testID);

// All important properties override inline
expect(component.props.style).toEqual({
// All important properties override inline, which stays a layer of its own
expect(StyleSheet.flatten(component.props.style)).toEqual({
color: "#f00",
fontSize: 14,
fontWeight: "bold",
lineHeight: 24,
});
expect(component.props.style).toContain(inlineStyle);
});
});

Expand Down
104 changes: 104 additions & 0 deletions src/__tests__/native/style-layers-regressions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { Animated, View as RNView, StyleSheet } from "react-native";

import { act, render } from "@testing-library/react-native";
import { Pressable } from "react-native-css/components/Pressable";
import { ScrollView } from "react-native-css/components/ScrollView";
import { registerCSS, testID } from "react-native-css/jest";
import { styled } from "react-native-css/native";
import { VAR_SYMBOL } from "react-native-css/native/reactivity";

// Each case renders as it does in Nativewind v4, which applies the class, then the inline style, then !important

const CSS = `
.p-4 { padding: 16px; }
.bg-red\\! { background-color: red !important; }
`;

beforeEach(() => registerCSS(CSS));

test("an !important class on contentContainerClassName keeps the class's other values and the inline style", () => {
const component = render(
<ScrollView
testID={testID}
contentContainerClassName="bg-red! p-4"
contentContainerStyle={{ backgroundColor: "blue", margin: 2 }}
/>,
).getByTestId(testID);

expect(StyleSheet.flatten(component.props.contentContainerStyle)).toEqual({
padding: 16,
backgroundColor: "#f00",
margin: 2,
});
});

describe.each(["p-4", "p-4 bg-red!"])("className=%s", (className) => {
test("an inline Animated.Value stays live", () => {
const AnimatedView = styled(Animated.View, { className: "style" });
const opacity = new Animated.Value(0.5);

const component = render(
<AnimatedView
testID={testID}
className={className}
style={{ opacity }}
/>,
).getByTestId(testID);

act(() => {
opacity.setValue(0.9);
});

expect(StyleSheet.flatten(component.props.style)).toMatchObject({
padding: 16,
opacity: 0.9,
});
});

test("an inline style that holds no CSS variable reaches the component as the same object", () => {
const inline = { margin: 2 };

const component = render(
<RNViewStyled testID={testID} className={className} style={inline} />,
).getByTestId(testID);

// Layered, at any depth, never copied
expect([component.props.style].flat(Infinity)).toContain(inline);
});
});

const RNViewStyled = styled(RNView, { className: { target: "style" } });

test.each([
[[]],
[{ height: undefined }],
[{ [VAR_SYMBOL]: "inline", brand: "red" }],
])("the object form keeps the class's style beside an inline %p", (inline) => {
const component = render(
<RNViewStyled testID={testID} className="p-4" style={inline as never} />,
).getByTestId(testID);

expect(StyleSheet.flatten(component.props.style)).toMatchObject({
padding: 16,
});
expect(StyleSheet.flatten(component.props.style)).not.toHaveProperty("brand");
});

test("a Pressable's callback style runs between a class and an !important class", () => {
const component = render(
<Pressable
testID={testID}
className="p-4 bg-red!"
style={({ pressed }) => ({
opacity: pressed ? 0.5 : 1,
backgroundColor: "blue",
})}
/>,
).getByTestId(testID);

expect(StyleSheet.flatten(component.props.style)).toEqual({
padding: 16,
opacity: 1,
backgroundColor: "#f00",
});
});
Loading
Loading