英文:
How to write jest test case for moveover and mouseleave for functional component in react?
问题
以下是您提供的代码的翻译部分:
React组件:
我是 jest 测试案例的新手,我想要为一个拥有鼠标悬停和鼠标离开事件的 React 组件编写 jest 测试案例。我不太确定如何为这些事件编写测试案例。我查看了一个StackOverflow的回答,它与我的要求非常相似 <https://stackoverflow.com/questions/49934975/how-to-simulate-mouse-over-event-on-a-div-using-enzyme-for-testing-a-react-appli>。
根据StackOverflow的回答,它是针对类组件编写的,但我需要它适用于函数组件。
以下是代码:
import React, { useState } from "react";
export const SearchMenu = () => <input />;
const Toggle = () => {
const [dropdownShown, setDropdownShown] = useState(false);
return (
<div
className="search-type"
onMouseOver={() => setDropdownShown(true)}
onMouseLeave={() => setDropdownShown(false)}
>
<h1>悬停在我上面以隐藏/显示输入</h1>
{dropdownShown && <SearchMenu />}
</div>
);
};
export default Toggle;
测试案例:
import React from "react";
import { shallow } from "enzyme";
import Toggle from "./Toggle";
import Enzyme from "enzyme";
import { SearchMenu } from "./Toggle";
describe("Toggle 组件", () => {
it("检查状态", () => {
const wrapper = shallow(<Toggle />);
expect(wrapper.find(<SearchMenu />).exists).toBeTruthy();
// 测试初始状态
expect(wrapper.state("dropdownShown")).toBe(true);
wrapper.simulate("mouseleave");
// 测试鼠标离开后的状态
expect(wrapper.state("dropdownShown")).toBe(false);
// 测试鼠标悬停后的状态
wrapper.simulate("mouseover");
expect(wrapper.state("dropdownShown")).toBe(true);
});
});
在功能组件中,您无法像类组件一样使用 state
属性,因此会出现错误。您需要使用 useState
钩子来管理状态。如果需要测试功能组件中的状态,可以通过模拟事件来触发状态更改并断言状态的值。希望这些信息对您有所帮助。
英文:
I'm newbie for jest test cases, i wanted to write jest test case for a react component which have mouseover and mouseleave event. i'm not sure how to write the test case for those events. I've gone through one stackoverflow answer, it is very similar to my requirement <https://stackoverflow.com/questions/49934975/how-to-simulate-mouse-over-event-on-a-div-using-enzyme-for-testing-a-react-appli>
As per the stackoverflow answer it is written for class component but i need it for functional component.
Here is the code:
import React, {useState} from "react";
export const SearchMenu = () => <input />;
const Toggle = () => {
const[dropdownShown, setDropdownShown] = useState(false);
return (
<div
className="search-type"
onMouseOver={() => setDropdownShown(true)}
onMouseLeave={() => setDropdownShown(false)}
>
<h1>Hover over me to hide/unhide the input</h1>
{dropdownShown && <SearchMenu />}
</div>
);
}
export default Toggle;
the test case:
import React from "react";
import { shallow } from "enzyme";
import Toggle from "./Toggle";
import Enzyme from "enzyme";
import { SearchMenu } from "./Toggle";
describe("Toggle Component", () => {
it("check state", () => {
const wrapper = shallow(<Toggle />);
expect(wrapper.find(<SearchMenu />).exists).toBeTruthy();
// Testing Initial State
expect(wrapper.state("dropdownShown")).toBe(true);
wrapper.simulate("mouseleave");
// Testing state after mouseleave
expect(wrapper.state("dropdownShown")).toBe(false);
// Testing state after mouseover
wrapper.simulate("mouseover");
expect(wrapper.state("dropdownShown")).toBe(true);
});
});
Here i'm getting error ShallowWrapper::state() can only be called on class component
Can anyone help me how to write this test case in functional component?
Thanks! in advance.
答案1
得分: 1
错误消息很清晰。
函数组件没有状态,也没有实例。
useState
不同于组件状态 - React 管理它;它不直接关联到实例。
因此,您可以测试在触发鼠标事件时将呈现什么。
例如:
Toggle.test.tsx
:
import React from 'react';
import { shallow } from 'enzyme';
import Toggle, { SearchMenu } from './Toggle';
describe('Toggle Component', () => {
it('check state', () => {
const wrapper = shallow(<Toggle />);
// 测试初始状态
expect(wrapper.find(SearchMenu).exists()).toBeFalse();
// 鼠标悬停后测试状态
wrapper.simulate('mouseover');
expect(wrapper.find(SearchMenu).exists()).toBeTrue();
// 鼠标离开后测试状态
wrapper.simulate('mouseleave');
expect(wrapper.find(SearchMenu).exists()).toBeFalse();
});
});
测试结果:
PASS stackoverflow/75952818/Toggle.test.tsx (10.562 s)
Toggle Component
✓ check state (29 ms)
------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files | 90 | 100 | 75 | 100 |
Toggle.tsx | 90 | 100 | 75 | 100 |
------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.354 s
Ran all test suites related to changed files.
英文:
The error message is clear.
> Functional components don’t have state, or an instance.
> useState
isn’t the same as component state - React manages that; it’s not directly tied to the instance.
So you can test what will be rendered when triggering the mouse event.
E.g.
Toggle.test.tsx
:
import React from 'react';
import { shallow } from 'enzyme';
import Toggle, { SearchMenu } from './Toggle';
describe('Toggle Component', () => {
it('check state', () => {
const wrapper = shallow(<Toggle />);
// Testing Initial State
expect(wrapper.find(SearchMenu).exists()).toBeFalse();
// Testing state after mouseover
wrapper.simulate('mouseover');
expect(wrapper.find(SearchMenu).exists()).toBeTrue();
// Testing state after mouseleave
wrapper.simulate('mouseleave');
expect(wrapper.find(SearchMenu).exists()).toBeFalse();
});
});
Test result:
PASS stackoverflow/75952818/Toggle.test.tsx (10.562 s)
Toggle Component
✓ check state (29 ms)
------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files | 90 | 100 | 75 | 100 |
Toggle.tsx | 90 | 100 | 75 | 100 |
------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.354 s
Ran all test suites related to changed files.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论