1

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);
}
4

1 回答 1

2

你使用哪个版本的情感?您可以通过广播到上下文来创建它。

jestHelper.js

import { channel, createBroadcast  } from 'emotion-theming';
    const broadcast = createBroadcast(defaultTheme);

    const defaultOptions = {
      theme: defaultTheme,
      context: {
        [channel]: broadcast
      },
      childContextTypes: {
        [channel]: PropTypes.object
      }
    };



    export function mount(component, options) {
      return enzymeMount(
        component,
        {
          ...defaultOptions,
          options
        }
      );
    }

来源:createBoradcast

您无需手动添加<ThemeProvider>

于 2019-02-06T21:06:57.267 回答