It's needed to pass theme to test correctly some of my components, using emotion ThemeProvider or withTheme API.
Actually, I found the same issue with 'styled-components', it described here. According VladimirPesterev comment, I got this API-wrapper:
import * as React from 'react'
import { shallow, mount, render } from 'enzyme'
import { ThemeProvider } from 'emotion-theming'
import theme from '../themes/default';
function wrapWithTheme (fn: Function, children: React.ReactChild, options: any): React.ReactChild {
const wrapper = fn(
<ThemeProvider theme={theme}>
{ children }
</ThemeProvider>,
options
)
return wrapper[fn.name]({
context: wrapper.instance().getChildContext(),
})
}
export function shallowWithTheme (component: React.ReactChild, options?: any) {
return wrapWithTheme(shallow, component, options);
}
export function mountWithTheme (component: React.ReactChild, options?: any) {
return wrapWithTheme(mount, component, options);
}
export function renderWithTheme (component: React.ReactChild, options?: any) {
return wrapWithTheme(render, component, options);
}
When I use those helpers in my tests, I get an error:
TypeError: wrapper.instance is not a function
Looks like it's outdated API. There is another solution from arka-na in topic linked above, but I have no idea how to adopt it to emotion:
import { ThemeConsumer } from 'styled-components'
import defaultTheme from '../somewhere/theme'
export const shallowWithTheme = (children, theme = defaultTheme) =>
{
ThemeConsumer._currentValue = theme
return shallow(children)
}
UPDATE According Yichaoz answer, I ended-up with this snippet:
import * as React from 'react'
import { shallow, mount, render } from 'enzyme'
import { channel, createBroadcast } from 'emotion-theming'
import * as PropTypes from 'prop-types';
import defaultTheme from '../themes/default';
const broadcast = createBroadcast(defaultTheme);
const defaultOptions = {
theme: defaultTheme,
context: {
[channel]: broadcast
},
childContextTypes: {
[channel]: PropTypes.object
}
};
function wrapWithTheme (fn: Function, component: React.ReactChild, options: any): React.ReactChild {
const mergedOptions = Object.assign({}, defaultOptions, options);
return fn(component, mergedOptions);
}
export function shallowWithTheme (component: React.ReactChild, options?: any) {
return wrapWithTheme(shallow, component, options);
}
export function mountWithTheme (component: React.ReactChild, options?: any) {
return wrapWithTheme(mount, component, options);
}
export function renderWithTheme (component: React.ReactChild, options?: any) {
return wrapWithTheme(render, component, options);
}