-
Notifications
You must be signed in to change notification settings - Fork 49
/
Storyshots.test.js
80 lines (70 loc) · 1.95 KB
/
Storyshots.test.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import initStoryshots, {
multiSnapshotWithOptions,
} from '@storybook/addon-storyshots';
import { createSerializer } from 'enzyme-to-json';
import MockDate from 'mockdate';
Enzyme.configure({ adapter: new Adapter() });
/* makeStyle + useStyle hook, <Box> and withStyle HOC */
const MAKE_STYLE_REGEXP = /((?:makeStyles|MuiBox|Component|WithStyles|ForwardRef)\W.+?)-\d+/g;
function removeMaterialUIInternals(json) {
// Remove props we don't want to snapshot
if (json.props) {
['classes'].forEach(key => {
delete json.props[key];
});
}
// Remove makeStyle className serial numbers
if (json.props?.className?.match(MAKE_STYLE_REGEXP)) {
json = {
...json,
props: {
...json.props,
className: json.props.className.replace(MAKE_STYLE_REGEXP, '$1'),
},
};
}
// Remove Portal containerInfo
if (json.type === 'Portal') {
return {
...json,
props: {
...json.props,
containerInfo: undefined,
},
};
}
// Remove all props of these components
if (['Transition', 'TrapFocus'].includes(json.type)) {
return {
...json,
props: {},
};
}
// Skip HOC components (single children) or excessive wrapper
if (
json.type?.match(
/^(ForwardRef|WithStyles|ThemeProvider|Styled|MockedProvider|ApolloProvider)/
)
) {
// When skipping HOC or wrapper, the first children are usually setups (such as <CssBaseline>),
// we should ignore together
return (
json.children &&
removeMaterialUIInternals(json.children[json.children.length - 1])
);
}
return json;
}
initStoryshots({
test: arg => {
MockDate.set('2020-01-01');
multiSnapshotWithOptions({
renderer: mount,
})(arg);
MockDate.reset();
},
snapshotSerializers: [createSerializer({ map: removeMaterialUIInternals })],
});
Element.prototype.scrollTo = () => {};