Javascript 如何测试您不知道的函数';我无法直接访问
我正在尝试测试从Javascript 如何测试您不知道的函数';我无法直接访问,javascript,reactjs,jestjs,react-testing-library,Javascript,Reactjs,Jestjs,React Testing Library,我正在尝试测试从useReducer解构的dispatch函数 我的分派函数是在我的组件中创建的,因此,据我所知,我不能像通常那样expect(分派) 我的组件如下所示: const reducer = (state, action) => { switch (action.type) { case 'MY_ACTION': return { ...state, myError: action.payload }; } }; con
useReducer
解构的dispatch
函数
我的分派函数是在我的组件中创建的,因此,据我所知,我不能像通常那样expect(分派)
我的组件如下所示:
const reducer = (state, action) => {
switch (action.type) {
case 'MY_ACTION':
return { ...state, myError: action.payload };
}
};
const Container = ({ someProp, anotherProp, aThirdProp }) => {
// This hook only works at this level of my application
const onlyAvailableInsideContainer = useonlyAvailableInsideContainer();
// Due to the above my initial state needs to be created here
const initialState = {
initialStateArr: [],
someProp,
myError: null,
aThirdProp,
anotherProp,
onlyAvailableInsideContainer,
};
// Which means I need to extract my state and dispatch functions within the Container also
const [state, dispatch] = useReducer(reducer, initialState);
const { fieldProps, formProps, errors } = someHook(
hookConfig(state, dispatch, aThirdProp, onlyAvailableInsideContainer),
);
return (
<div className="dcx-hybrid">
<MyContext.Provider
value={{ state, dispatch, fieldProps, formProps, errors }}
>
<SomeChildComponent />
</MyContext.Provider>
</div>
);
};
const dispatch= jest.fn();
const useReducerMock= (reducer,initialState) => [initialState, dispatch];
jest.spyOn(React, 'useReducer').mockImplementation(useReducerMock);
试着像这样监视useReducer
:
const reducer = (state, action) => {
switch (action.type) {
case 'MY_ACTION':
return { ...state, myError: action.payload };
}
};
const Container = ({ someProp, anotherProp, aThirdProp }) => {
// This hook only works at this level of my application
const onlyAvailableInsideContainer = useonlyAvailableInsideContainer();
// Due to the above my initial state needs to be created here
const initialState = {
initialStateArr: [],
someProp,
myError: null,
aThirdProp,
anotherProp,
onlyAvailableInsideContainer,
};
// Which means I need to extract my state and dispatch functions within the Container also
const [state, dispatch] = useReducer(reducer, initialState);
const { fieldProps, formProps, errors } = someHook(
hookConfig(state, dispatch, aThirdProp, onlyAvailableInsideContainer),
);
return (
<div className="dcx-hybrid">
<MyContext.Provider
value={{ state, dispatch, fieldProps, formProps, errors }}
>
<SomeChildComponent />
</MyContext.Provider>
</div>
);
};
const dispatch= jest.fn();
const useReducerMock= (reducer,initialState) => [initialState, dispatch];
jest.spyOn(React, 'useReducer').mockImplementation(useReducerMock);
然后测试它:
expect(dispatch).toBeCalledWith({
type: 'MY_ACTION',
payload: 'Some error message.',
});
测试通话的结果,而不是通话本身。@HereticMonkey我已经这样做了-我的问题是我的覆盖率表中仍然有漏行:(你是怎么做到的?遗漏了哪些行?@jornsharpe的方式与上述方式非常相似。我检查是否存在呈现点击后/分派的元素。但在实际情况下,我仍然会在我的还原器中找到未覆盖的行。请确保你的测试正在执行函数的所有可能输入,或者在你的情况下,所有输入对生成子函数的可能输入的父函数。我强烈建议不要监视React的某些部分-不要模仿你没有的。@jonrsharpe你是对的,但他想测试函数调用,最好测试此操作的ui。