forked from sumup-oss/circuit-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.setup.js
58 lines (48 loc) · 1.67 KB
/
jest.setup.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
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import 'jest-enzyme';
import Enzyme, { shallow, render, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { createSerializer } from 'jest-emotion';
import { axe, toHaveNoViolations } from 'jest-axe';
import { create } from 'react-test-renderer';
import * as emotion from 'emotion';
import { ThemeProvider } from 'emotion-theming';
import { circuit } from './src/themes';
Enzyme.configure({ adapter: new Adapter() });
const renderWithTheme = renderFn => (component, ...rest) =>
renderFn(<ThemeProvider theme={circuit}>{component}</ThemeProvider>, rest);
const shallowWithTheme = tree => {
const context = shallow(<ThemeProvider theme={circuit} />)
.instance()
.getChildContext();
return shallow(tree, { context });
};
const mountWithTheme = tree => {
const context = shallow(<ThemeProvider theme={circuit} />)
.instance()
.getChildContext();
return mount(tree, {
context,
childContextTypes: ThemeProvider.childContextTypes
});
};
global.shallow = shallowWithTheme;
global.render = renderWithTheme(render);
global.create = renderWithTheme(create);
global.mount = mountWithTheme;
global.renderToHtml = renderWithTheme(renderToStaticMarkup);
global.axe = axe;
// This is defined by webpack in storybook builds using the DefinePlugin plugin.
global.STORYBOOK = false;
// Add custom matchers
expect.extend(toHaveNoViolations);
// Add a snapshot serializer that removes random hashes
// from emotion class names.
expect.addSnapshotSerializer(
createSerializer(emotion, {
classNameReplacer(className, index) {
return `circuit-${index}`;
}
})
);