简体   繁体   English

React/Jest:测试 React Context 方法(结果:TypeError:setScore 不是函数)

[英]React/Jest: Testing React Context methods (Result: TypeError: setScore is not a function)

I'm using React Context API to create a game.我正在使用 React Context API 来创建游戏。

In one of my components (GameStatus) I pull in some methods from the provider:在我的一个组件(GameStatus)中,我从提供者那里提取了一些方法:

const context = React.useContext(MyContext);
const { setGameStart, setGameEnd, setScore } = context;

And in the component I invoke these three methods onClick of the start game button, which in turn sets the state back in the provider.在组件中,我调用了开始游戏按钮的 onClick 这三个方法,这反过来又将状态设置回提供程序中。

GameStatus Component游戏状态组件

import React from 'react';
import { MyContext } from './Provider';

const GameStatus = ({ gameStart, gameEnd, score, total, getPlayers }: { gameStart: boolean, gameEnd: boolean, getPlayers: () => void, score: number, total: number }) => {
    const context = React.useContext(MyContext);
    const { setGameStart, setGameEnd, setScore } = context;
    return (
        <>
            {!gameStart && (
                <button onClick={() => {
                    getPlayers();
                    setScore(0);
                    setGameStart(true);
                    setGameEnd(false);
                }}>
                    Start game
                </button>
            )}
            {gameEnd && (
                <p>Game end - You scored {score} out {total}</p>
            )}
        </>
    )
}

export default GameStatus;

Then in my test file below I want to test that when the start game button is clicked the game is started (check the DOM has removed the button and is now showing the game).然后在下面的测试文件中,我想测试单击开始游戏按钮时游戏是否启动(检查 DOM 已删除该按钮,现在正在显示游戏)。

But I'm not sure how to pull in the methods in to the test file as I get:但是我不确定如何将方法引入到测试文件中:

Result: TypeError: setScore is not a function结果:TypeError:setScore 不是函数

I tried just copying:我试着只是复制:

const context = React.useContext(MyContext);
const { setGameStart, setGameEnd, setScore } = context;

But then I get an invalid hook call as I can't use React hooks inside the test.但是后来我得到一个无效的钩子调用,因为我不能在测试中使用 React 钩子。

Any ideas?有任何想法吗? Or a better approach to testing this?还是更好的测试方法? Thanks谢谢

GameStatus Test游戏状态测试

import React from 'react';
import { shallow } from 'enzyme';
import { MyContext } from '../components/Provider';
import GameStatus from '../components/GameStatus';

test('should start the game', () => {
    const getPlayers = jest.fn();

    const uut = shallow(
        <MyContext.Provider>
            <GameStatus
                getPlayers={getPlayers}
            />
        </MyContext.Provider>
    ).dive().find('button');

    uut.simulate('click');

    console.log(uut.debug());
});

I'm not sure if this helps but since your first approach was to use hooks inside your test you could try to use a library for rendering hooks.我不确定这是否有帮助,但由于您的第一种方法是在测试中使用钩子,您可以尝试使用库来呈现钩子。 I'm not familiar with enzyme but I used React Hooks Testing Library in order to render my hooks inside my tests.我不熟悉酶,但我使用React Hooks 测试库来在我的测试中渲染我的钩子。 You can use it like that:你可以这样使用它:

let testYourHook= renderHook(yourHook);

testYourHook value is not dynamic, so you have to get the value everytime with: testYourHook值不是动态的,因此您必须每次都获取该值:

testYourHook.result.current

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM