如何将情感主题传递给单元测试?

How to pass emotion theme to unit tests?

需要通过 theme 来正确测试我的一些组件,使用 emotion ThemeProvider 或 withTheme API。

实际上,我在 'styled-components' 中发现了同样的问题,它描述了 here。根据 VladimirPesterev 的评论,我得到了这个 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);
}

当我在测试中使用这些助手时,出现错误:

TypeError: wrapper.instance is not a function

看起来已经过时了 API。上面链接的主题中有 arka-na 的另一个解决方案,但我不知道如何将其应用于情感:

import { ThemeConsumer } from 'styled-components'
import defaultTheme from '../somewhere/theme'

export const shallowWithTheme = (children, theme = defaultTheme) => 
{
   ThemeConsumer._currentValue = theme
   return shallow(children)
}

更新 根据 的回答,我得到了这个片段:

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

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

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>