-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathformat.ts
42 lines (37 loc) · 1.06 KB
/
format.ts
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
import type { ReactTestRendererJSON } from 'react-test-renderer';
import prettyFormat, { NewPlugin, plugins } from 'pretty-format';
type MapPropsFunction = (
props: Record<string, unknown>,
node: ReactTestRendererJSON
) => Record<string, unknown>;
export type FormatOptions = {
mapProps?: MapPropsFunction;
};
const format = (
input: ReactTestRendererJSON | ReactTestRendererJSON[],
options: FormatOptions = {}
) =>
prettyFormat(input, {
plugins: [getCustomPlugin(options.mapProps), plugins.ReactElement],
highlight: true,
});
const getCustomPlugin = (mapProps?: MapPropsFunction): NewPlugin => {
return {
test: (val) => plugins.ReactTestComponent.test(val),
serialize: (val, config, indentation, depth, refs, printer) => {
let newVal = val;
if (mapProps && val.props) {
newVal = { ...val, props: mapProps(val.props, val) };
}
return plugins.ReactTestComponent.serialize(
newVal,
config,
indentation,
depth,
refs,
printer
);
},
};
};
export default format;