|
| 1 | +import { Button as RNButton, type ButtonProps } from "react-native"; |
| 2 | + |
| 3 | +import { render } from "@testing-library/react-native"; |
| 4 | +import { copyComponentProperties } from "react-native-css/components/copyComponentProperties"; |
| 5 | +import { registerCSS, testID } from "react-native-css/jest"; |
| 6 | +import { useCssElement } from "react-native-css/native"; |
| 7 | +import type { |
| 8 | + StyledConfiguration, |
| 9 | + StyledProps, |
| 10 | +} from "react-native-css/runtime.types"; |
| 11 | + |
| 12 | +test("Component preserves props when mapping specifies 'target: false'", () => { |
| 13 | + registerCSS(`.sign-in { color: orange; }`); |
| 14 | + |
| 15 | + const mapping: StyledConfiguration<typeof RNButton> = { |
| 16 | + className: { |
| 17 | + target: false, |
| 18 | + nativeStyleMapping: { |
| 19 | + color: "color", |
| 20 | + }, |
| 21 | + }, |
| 22 | + }; |
| 23 | + |
| 24 | + const Button = copyComponentProperties( |
| 25 | + RNButton, |
| 26 | + (props: StyledProps<ButtonProps, typeof mapping>) => { |
| 27 | + return useCssElement(RNButton, props, mapping); |
| 28 | + }, |
| 29 | + ); |
| 30 | + |
| 31 | + const onPress = jest.fn(); |
| 32 | + |
| 33 | + const result = render( |
| 34 | + <Button |
| 35 | + testID={testID} |
| 36 | + className="sign-in" |
| 37 | + title="Sign In" |
| 38 | + onPress={onPress} |
| 39 | + />, |
| 40 | + ); |
| 41 | + |
| 42 | + expect(result.getByText("Sign In")).toBeTruthy(); |
| 43 | + |
| 44 | + const renderedElement = result.getByTestId(testID); |
| 45 | + const renderedProps = renderedElement.props; |
| 46 | + |
| 47 | + expect(renderedProps.testID).toBe(testID); |
| 48 | + expect(renderedProps).not.toHaveProperty("className"); |
| 49 | + |
| 50 | + // the <Text> element in the RN button |
| 51 | + // should apply an orange color to the element (because of the "sign-in" className) |
| 52 | + const titleElement = result.getByText("Sign In"); |
| 53 | + expect(titleElement.props.style).toBeInstanceOf(Array); |
| 54 | + expect(titleElement.props.style).toHaveLength(2); |
| 55 | + expect(titleElement.props.style[1]).toEqual({ color: "#ffa500" }); |
| 56 | +}); |
0 commit comments