用Jest测试React Hook

时间:2019-08-09 05:45:47

标签: jestjs enzyme react-hooks

我想使用jest + zyme测试appHook.jsx文件中存在的功能。请提供用于为此文件编写测试用例的流程。

appHook.jsx 

import { useContext } from 'react';
import { AppContext } from '../context/appContext';

const AppHook = () => {
  const [state, setState] = useContext(AppContext);

  function updateColors(colors) {
    setState(state => ({ ...state, customerSpecificColor: colors }));
  }

  function setDisplayCommonLoader(isDisplay) {
    setState(state => ({ ...state, displayCommonLoader: isDisplay }));
  }

  return {
    updateColors,
    customerSpecificColor: state.customerSpecificColor,
    setDisplayCommonLoader,
    displayCommonLoader: state.displayCommonLoader,
  };
};

export default AppHook;

appContext.jsx

import React, { useState } from 'react';

const AppContext = React.createContext([{}, () => {}]);

const AppProvider = props => {
  const [state, setState] = useState({

    customerSpecificColor: 'blue',
    displayCommonLoader: false
  });
  return (
    <AppContext.Provider value={[state, setState]}>
      {props.children}
    </AppContext.Provider>
  );
};

export { AppContext, AppProvider };

0 个答案:

没有答案