如何通过情感主题进行单元测试?

时间:2019-02-06 13:29:33

标签: reactjs unit-testing enzyme emotion

需要使用情绪ThemeProvider或withTheme API传递主题以正确测试我的某些组件。

实际上,我发现“样式组件”存在相同的问题,它描述了here。根据VladimirPesterev的评论,我得到了这个API包装器:

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不是函数

看起来像过时的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)
}

更新 根据{{​​3}}的答案,我结束了以下代码段:

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

1 个答案:

答案 0 :(得分:0)

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

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>