forked from nativewind/react-native-css
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstyled-ref-forwarding.test.tsx
More file actions
107 lines (91 loc) · 2.48 KB
/
styled-ref-forwarding.test.tsx
File metadata and controls
107 lines (91 loc) · 2.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
/* eslint-disable */
// @ts-nocheck
import React from "react";
import { View } from "react-native";
import { render, screen } from "@testing-library/react-native";
import { registerCSS, testID } from "react-native-css/jest";
import { styled } from "react-native-css/runtime";
const children = undefined;
// Mock SVG component that mimics react-native-svg behavior
const MockSvg = React.forwardRef<any, any>((props: any, ref: any) => {
return <View ref={ref} {...props} testID={props.testID ?? "mock-svg"} />;
});
MockSvg.displayName = "MockSvg";
describe("styled() ref forwarding and deprecated property support", () => {
test("should work with explicit nativeStyleMapping", () => {
registerCSS(`
.svg-explicit {
height: 24px;
width: 24px;
fill: red;
}
`);
const StyledSvg = styled(MockSvg, {
className: {
target: "style",
nativeStyleMapping: {
height: "height",
width: "width",
fill: "fill",
},
},
});
render(<StyledSvg testID={testID} className="svg-explicit" />);
const component = screen.getByTestId(testID);
expect(component.props).toEqual(
expect.objectContaining({
testID,
children,
height: 24,
width: 24,
fill: "#f00",
style: {},
}),
);
});
test("should properly forward refs", () => {
registerCSS(`
.ref-test {
height: 16px;
width: 16px;
}
`);
const StyledSvg = styled(MockSvg, {
className: {
target: "style",
nativeStyleMapping: {
height: "height",
width: "width",
},
},
});
const ref = React.createRef<any>();
render(<StyledSvg ref={ref} testID={testID} className="ref-test" />);
// Ref should be properly forwarded
expect(ref.current).toBeTruthy();
expect(ref.current.props.testID).toBe(testID);
});
test("should work without explicit mapping for regular components", () => {
registerCSS(`
.regular {
padding: 10px;
background-color: green;
}
`);
const StyledView = styled(View, {
className: "style",
});
render(<StyledView testID={testID} className="regular" />);
const component = screen.getByTestId(testID);
expect(component.props).toEqual(
expect.objectContaining({
testID,
children,
style: {
padding: 10,
backgroundColor: "#008000",
},
}),
);
});
});