-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathjson.ts
More file actions
32 lines (26 loc) · 763 Bytes
/
json.ts
File metadata and controls
32 lines (26 loc) · 763 Bytes
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
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;
}