Skip to content

Commit f71b701

Browse files
chore: fix nightly test by excluding RCTSwitch style from snapshot (#1820)
1 parent 0922287 commit f71b701

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/__tests__/react-native-api.test.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import { FlatList, Image, Modal, ScrollView, Switch, Text, TextInput, View } from 'react-native';
33

44
import { render, screen } from '..';
5+
import { mapJsonProps } from '../test-utils/json';
56

67
const isReact19 = React.version.startsWith('19.');
78
const testGateReact19 = isReact19 ? test : test.skip;
@@ -109,18 +110,17 @@ test('React Native API assumption: <TextInput> with nested Text renders single h
109110
test('React Native API assumption: <Switch> renders a single host element', () => {
110111
render(<Switch testID="test" value={true} onChange={jest.fn()} />);
111112

112-
expect(screen.toJSON()).toMatchInlineSnapshot(`
113+
expect(
114+
mapJsonProps(screen.toJSON(), {
115+
style: '/* Intentional excluded */',
116+
}),
117+
).toMatchInlineSnapshot(`
113118
<RCTSwitch
114119
accessibilityRole="switch"
115120
onChange={[Function]}
116121
onResponderTerminationRequest={[Function]}
117122
onStartShouldSetResponder={[Function]}
118-
style={
119-
{
120-
"height": 31,
121-
"width": 51,
122-
}
123-
}
123+
style="/* Intentional excluded */"
124124
testID="test"
125125
value={true}
126126
/>

src/test-utils/json.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { ReactTestRendererJSON } from 'react-test-renderer';
2+
3+
type JsonPropsMapper = {
4+
[key: string]: unknown;
5+
};
6+
7+
export function mapJsonProps<T extends ReactTestRendererJSON | ReactTestRendererJSON[] | null>(
8+
element: T,
9+
mapper: JsonPropsMapper,
10+
): T {
11+
if (Array.isArray(element)) {
12+
return element.map((e) => mapJsonProps(e, mapper)) as T;
13+
}
14+
15+
if (!element) {
16+
return element;
17+
}
18+
19+
const resultProps = { ...element.props };
20+
Object.keys(mapper).forEach((key) => {
21+
if (key in element.props) {
22+
resultProps[key] = mapper[key];
23+
}
24+
});
25+
26+
const resultElement = { ...element, props: resultProps };
27+
Object.defineProperty(resultElement, '$$typeof', {
28+
value: Symbol.for('react.test.json'),
29+
});
30+
31+
return resultElement;
32+
}

0 commit comments

Comments
 (0)