Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/__tests__/react-native-api.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { FlatList, Image, Modal, ScrollView, Switch, Text, TextInput, View } from 'react-native';

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

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

expect(screen.toJSON()).toMatchInlineSnapshot(`
expect(
mapJsonProps(screen.toJSON(), {
style: '/* Intentional excluded */',
}),
).toMatchInlineSnapshot(`
<RCTSwitch
accessibilityRole="switch"
onChange={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
style={
{
"height": 31,
"width": 51,
}
}
style="/* Intentional excluded */"
testID="test"
value={true}
/>
Expand Down
32 changes: 32 additions & 0 deletions src/test-utils/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { ReactTestRendererJSON } from 'react-test-renderer';

type JsonPropsMapper = {
[key: string]: unknown;
};

export function mapJsonProps<T extends ReactTestRendererJSON | ReactTestRendererJSON[] | null>(
element: T,
mapper: JsonPropsMapper,
): T {
if (Array.isArray(element)) {
return element.map((e) => mapJsonProps(e, mapper)) as T;
}

if (!element) {
return element;
}

const resultProps = { ...element.props };
Object.keys(mapper).forEach((key) => {
if (key in element.props) {
resultProps[key] = mapper[key];
}
});

const resultElement = { ...element, props: resultProps };
Object.defineProperty(resultElement, '$$typeof', {
value: Symbol.for('react.test.json'),
});

return resultElement;
}