-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcomponents.test.tsx
More file actions
116 lines (95 loc) · 3.42 KB
/
components.test.tsx
File metadata and controls
116 lines (95 loc) · 3.42 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
import {
Button as RNButton,
TextInput as RNTextInput,
type ButtonProps,
type TextInputProps,
} from "react-native";
import { render } from "@testing-library/react-native";
import { copyComponentProperties } from "react-native-css/components/copyComponentProperties";
import { TextInput } from "react-native-css/components/TextInput";
import { registerCSS, testID } from "react-native-css/jest";
import { useCssElement } from "react-native-css/native";
import type {
StyledConfiguration,
StyledProps,
} from "react-native-css/runtime.types";
test("Component preserves props when mapping specifies 'target: false'", () => {
registerCSS(`.sign-in { color: orange; }`);
const mapping: StyledConfiguration<typeof RNButton> = {
className: {
target: false,
nativeStyleMapping: {
color: "color",
},
},
};
const Button = copyComponentProperties(
RNButton,
(props: StyledProps<ButtonProps, typeof mapping>) => {
return useCssElement(RNButton, props, mapping);
},
);
const onPress = jest.fn();
const result = render(
<Button
testID={testID}
className="sign-in"
title="Sign In"
onPress={onPress}
/>,
);
expect(result.getByText("Sign In")).toBeTruthy();
const renderedElement = result.getByTestId(testID);
const renderedProps = renderedElement.props;
expect(renderedProps.testID).toBe(testID);
expect(renderedProps).not.toHaveProperty("className");
// the <Text> element in the RN button
// should apply an orange color to the element (because of the "sign-in" className)
const titleElement = result.getByText("Sign In");
expect(titleElement.props.style).toBeInstanceOf(Array);
expect(titleElement.props.style).toHaveLength(2);
expect(titleElement.props.style[1]).toEqual({ color: "#ffa500" });
});
test("nativeStyleMapping with boolean true extracts style prop using key name", () => {
registerCSS(`.text-center { text-align: center; }`);
const component = render(
<TextInput testID={testID} className="text-center" />,
).getByTestId(testID);
// textAlign should be extracted from style and mapped to the textAlign prop
expect(component.props.textAlign).toBe("center");
expect(component.props.style).not.toHaveProperty("textAlign");
});
test("nativeStyleMapping with boolean true works alongside other styles", () => {
registerCSS(`
.text-center { text-align: center; }
.text-red { color: red; }
`);
const component = render(
<TextInput testID={testID} className="text-center text-red" />,
).getByTestId(testID);
// textAlign extracted to prop, color stays in style
expect(component.props.textAlign).toBe("center");
expect(component.props.style).toStrictEqual({ color: "#f00" });
});
test("nativeStyleMapping with boolean true on custom component", () => {
registerCSS(`.text-right { text-align: right; }`);
const mapping: StyledConfiguration<typeof RNTextInput> = {
className: {
target: "style",
nativeStyleMapping: {
textAlign: true,
},
},
};
const StyledTextInput = copyComponentProperties(
RNTextInput,
(props: StyledProps<TextInputProps, typeof mapping>) => {
return useCssElement(RNTextInput, props, mapping);
},
);
const component = render(
<StyledTextInput testID={testID} className="text-right" />,
).getByTestId(testID);
expect(component.props.textAlign).toBe("right");
expect(component.props.style).not.toHaveProperty("textAlign");
});