-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathclassName-with-style.test.tsx
More file actions
274 lines (226 loc) · 8.48 KB
/
className-with-style.test.tsx
File metadata and controls
274 lines (226 loc) · 8.48 KB
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
import { View as RNView } from "react-native";
import { render } from "@testing-library/react-native";
import { copyComponentProperties } from "react-native-css/components/copyComponentProperties";
import { FlatList } from "react-native-css/components/FlatList";
import { ScrollView } from "react-native-css/components/ScrollView";
import { Text } from "react-native-css/components/Text";
import { View } from "react-native-css/components/View";
import { registerCSS, testID } from "react-native-css/jest";
import {
useCssElement,
type StyledConfiguration,
type StyledProps,
} from "react-native-css/native";
test("className with inline style props should coexist when different properties", () => {
registerCSS(`.text-red { color: red; }`);
const component = render(
<Text testID={testID} className="text-red" style={{ fontSize: 16 }} />,
).getByTestId(testID);
// Both className and style props should be applied as array
expect(component.props.style).toStrictEqual([
{ color: "#f00" },
{ fontSize: 16 },
]);
});
test("className with inline style props should favor inline when same property", () => {
registerCSS(`.text-red { color: red; }`);
const component = render(
<Text testID={testID} className="text-red" style={{ color: "blue" }} />,
).getByTestId(testID);
// When same property exists, inline style should win
expect(component.props.style).toStrictEqual({ color: "blue" });
});
test("only className should not create array", () => {
registerCSS(`.text-red { color: red; }`);
const component = render(
<Text testID={testID} className="text-red" />,
).getByTestId(testID);
// Only className should be a flat object
expect(component.props.style).toStrictEqual({ color: "#f00" });
});
test("only inline style should not create array", () => {
const component = render(
<Text testID={testID} style={{ color: "blue" }} />,
).getByTestId(testID);
// Only inline style should be a flat object
expect(component.props.style).toStrictEqual({ color: "blue" });
});
test("important should overwrite the inline style", () => {
registerCSS(`.text-red\\! { color: red !important; }`);
const component = render(
<Text testID={testID} className="text-red!" style={{ color: "blue" }} />,
).getByTestId(testID);
expect(component.props.style).toStrictEqual({ color: "#f00" });
});
test("View with multiple className properties where inline style takes precedence", () => {
registerCSS(`
.px-4 { padding-left: 16px; padding-right: 16px; }
.pt-4 { padding-top: 16px; }
.mb-4 { margin-bottom: 16px; }
`);
const component = render(
<View
testID={testID}
className="px-4 pt-4 mb-4"
style={{ width: 300, paddingRight: 0 }}
/>,
).getByTestId(testID);
// Inline style should override paddingRight from px-4 class
// Other className styles should be preserved in array
expect(component.props.style).toStrictEqual([
{
paddingLeft: 16,
paddingRight: 16,
paddingTop: 16,
marginBottom: 16,
},
{
width: 300,
paddingRight: 0,
},
]);
});
test("FlatList: className should map to style", () => {
registerCSS(`.bg-red { background-color: red; }`);
const component = render(
<FlatList
testID={testID}
data={[]}
renderItem={() => null}
className="bg-red"
/>,
).getByTestId(testID);
expect(component.props.style).toStrictEqual({ backgroundColor: "#f00" });
});
/**
* Tests for style={undefined} not destroying computed className styles.
*
* Object.assign({}, left, right) copies all enumerable own properties from right,
* including those with value undefined. When a component passes style={undefined}
* (common when forwarding optional style props), the computed NativeWind styles
* from className are overwritten.
*
* PR #224 fixed the default ["style"] target path. These tests cover the remaining
* paths: non-"style" array targets (e.g. ["contentContainerStyle"]) and string targets.
*/
describe("style={undefined} should not destroy computed className styles", () => {
// Path A: config.target = ["style"] (fixed in PR #224)
test("View: className with style={undefined}", () => {
registerCSS(`.text-red { color: red; }`);
const component = render(
<Text testID={testID} className="text-red" style={undefined} />,
).getByTestId(testID);
expect(component.props.style).toStrictEqual({ color: "#f00" });
});
test("View: className with style={null}", () => {
registerCSS(`.text-red { color: red; }`);
const component = render(
<Text testID={testID} className="text-red" style={null} />,
).getByTestId(testID);
expect(component.props.style).toStrictEqual({ color: "#f00" });
});
// Path B: config.target = ["contentContainerStyle"] (non-"style" array target)
test("ScrollView: contentContainerClassName with contentContainerStyle={undefined}", () => {
registerCSS(`.bg-green { background-color: green; }`);
const component = render(
<ScrollView
testID={testID}
contentContainerClassName="bg-green"
contentContainerStyle={undefined}
/>,
).getByTestId(testID);
expect(component.props.contentContainerStyle).toStrictEqual({
backgroundColor: "#008000",
});
});
test("ScrollView: contentContainerClassName preserves styles with valid contentContainerStyle", () => {
registerCSS(`.bg-green { background-color: green; }`);
const component = render(
<ScrollView
testID={testID}
contentContainerClassName="bg-green"
contentContainerStyle={{ padding: 10 }}
/>,
).getByTestId(testID);
// Non-"style" targets: inline contentContainerStyle overwrites className styles
// (array coexistence is only implemented for the ["style"] target path)
expect(component.props.contentContainerStyle).toStrictEqual({
padding: 10,
});
});
test("ScrollView: contentContainerClassName without contentContainerStyle", () => {
registerCSS(`.bg-green { background-color: green; }`);
const component = render(
<ScrollView testID={testID} contentContainerClassName="bg-green" />,
).getByTestId(testID);
expect(component.props.contentContainerStyle).toStrictEqual({
backgroundColor: "#008000",
});
});
// Path B: FlatList with columnWrapperClassName (another non-"style" array target)
test("FlatList: contentContainerClassName with contentContainerStyle={undefined}", () => {
registerCSS(`.p-4 { padding: 16px; }`);
const component = render(
<FlatList
testID={testID}
data={[]}
renderItem={() => null}
contentContainerClassName="p-4"
contentContainerStyle={undefined}
/>,
).getByTestId(testID);
expect(component.props.contentContainerStyle).toStrictEqual({
padding: 16,
});
});
// Path B: custom styled() with string target (e.g. { className: { target: "style" } })
test("custom styled() with string target: style={undefined} preserves styles", () => {
registerCSS(`.bg-purple { background-color: purple; }`);
const mapping: StyledConfiguration<typeof RNView> = {
className: {
target: "style",
},
};
const StyledView = copyComponentProperties(
RNView,
(
props: StyledProps<React.ComponentProps<typeof RNView>, typeof mapping>,
) => {
return useCssElement(RNView, props, mapping);
},
);
const component = render(
<StyledView testID={testID} className="bg-purple" style={undefined} />,
).getByTestId(testID);
expect(component.props.style).toStrictEqual({ backgroundColor: "#800080" });
});
// Real-world: optional style prop forwarding
test("optional style prop forwarding preserves className styles", () => {
registerCSS(`
.p-4 { padding: 16px; }
.bg-white { background-color: white; }
`);
// Simulates a reusable component that forwards optional contentContainerStyle
function MyScrollView({
contentContainerStyle,
}: {
contentContainerStyle?: React.ComponentProps<
typeof ScrollView
>["contentContainerStyle"];
}) {
return (
<ScrollView
testID={testID}
contentContainerClassName="p-4 bg-white"
contentContainerStyle={contentContainerStyle}
/>
);
}
// Called without contentContainerStyle — implicitly undefined
const component = render(<MyScrollView />).getByTestId(testID);
expect(component.props.contentContainerStyle).toStrictEqual({
padding: 16,
backgroundColor: "#fff",
});
});
});