Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何将情绪主题传递给单元测试?_Reactjs_Unit Testing_Enzyme_Emotion - Fatal编程技术网

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

Reactjs 如何将情绪主题传递给单元测试?,reactjs,unit-testing,enzyme,emotion,Reactjs,Unit Testing,Enzyme,Emotion,需要通过主题来正确测试我的一些组件,使用情感ThemeProvider或主题API 事实上,我在“样式化组件”中发现了同样的问题,它描述道。根据VladimirPesterev的评论,我得到了这个API包装器: import * as React from 'react' import { shallow, mount, render } from 'enzyme' import { ThemeProvider } from 'emotion-theming' import theme from

需要通过主题来正确测试我的一些组件,使用情感ThemeProvider或主题API

事实上,我在“样式化组件”中发现了同样的问题,它描述道。根据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);
}
更新 根据我的回答,我最终得到了以下片段:

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

你使用哪种情绪? 您可以通过广播到上下文来创建它

Jesselper.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
        }
      );
    }
资料来源:

您不需要手动添加

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