UNPKG

1.38 kBJavaScriptView Raw
1import React from 'react'
2import { ThemeProvider } from 'styled-components'
3import * as enzyme from 'enzyme'
4import Adapter from 'enzyme-adapter-react-16'
5import 'jest-enzyme'
6
7import { theme } from './core/theme'
8
9const localStorageMock = {
10 getItem: jest.fn(),
11 setItem: jest.fn(),
12 clear: jest.fn(),
13}
14
15const shallowWithTheme = (component) => {
16 const context = enzyme.shallow(<ThemeProvider theme={theme} />)
17 .instance()
18 .getChildContext()
19 return enzyme.shallow(component, { context })
20}
21
22const mountWithTheme = (component) => {
23 const context = enzyme.shallow(<ThemeProvider theme={theme} />)
24 .instance()
25 .getChildContext()
26
27 return enzyme.mount(component, {
28 context,
29 childContextTypes: ThemeProvider.childContextTypes,
30 })
31}
32
33const renderWithTheme = (component) => {
34 const context = enzyme.shallow(<ThemeProvider theme={theme} />)
35 .instance()
36 .getChildContext()
37
38 return enzyme.render(component, {
39 context,
40 childContextTypes: ThemeProvider.childContextTypes,
41 })
42}
43
44window.matchMedia = window.matchMedia || function() {
45return {
46matches : false,
47addListener : function() {},
48removeListener: function() {}
49};
50};
51
52global.localStorage = localStorageMock;
53global.shallowWithTheme = shallowWithTheme;
54global.mountWithTheme = mountWithTheme
55global.renderWithTheme = renderWithTheme
56
57enzyme.configure({ adapter: new Adapter() });