reactjs - 如何将测试用例写入反应js中UseEffect中的if Condition
问题描述
我是单元测试的新手,我尝试编写这个简单的测试用例但无法做到。有人可以帮我为useEffect中的if条件编写测试用例吗?
import React, { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
const Greet = () => {
const dispatch = useDispatch();
//I am getting data from redux file
const stateData = useSelector((state) => state.stateDatalist);
const countryData = useSelector((state) => state.CountryDatalist);
useEffect(() => {
if (stateData.length === 0 && countryData.length === 0) {
// calling another API data
dispatch(getDetails());
}
}, []);
return (
<div>
<h1>Hello</h1>
</div>
);
};
export default Greet;
解决方案
使用redux-mock-store创建具有模拟状态的模拟存储。每个测试用例提供不同的模拟状态来测试不同的代码分支。
例如
Greet.tsx
:
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
const getDetails = () => ({ type: 'GET_DETAIL' });
const Greet = () => {
const dispatch = useDispatch();
const stateData = useSelector((state: any) => state.stateDatalist);
const countryData = useSelector((state: any) => state.CountryDatalist);
useEffect(() => {
if (stateData.length === 0 && countryData.length === 0) {
dispatch(getDetails());
}
}, []);
return (
<div>
<h1>Hello</h1>
</div>
);
};
export default Greet;
Greet.test.tsx
:
import Greet from './Greet';
import createMockStore from 'redux-mock-store';
import { mount } from 'enzyme';
import React from 'react';
import { Provider } from 'react-redux';
const mockStore = createMockStore([]);
describe('68904328', () => {
test('should dispatch action for getting details', () => {
const state = {
stateDatalist: [],
CountryDatalist: [],
};
const store = mockStore(state);
mount(
<Provider store={store}>
<Greet />
</Provider>
);
expect(store.getActions()).toEqual([{ type: 'GET_DETAIL' }]);
});
test('should do nothing', () => {
const state = {
stateDatalist: [1],
CountryDatalist: [1],
};
const store = mockStore(state);
mount(
<Provider store={store}>
<Greet />
</Provider>
);
expect(store.getActions()).toEqual([]);
});
});
测试结果:
PASS examples/68904328/Greet.test.tsx (11.289 s)
68904328
✓ should dispatch action for getting details (44 ms)
✓ should do nothing (4 ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
Greet.tsx | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 12.109 s
包版本:
"react": "^16.14.0",
"jest": "^26.6.3",
"jest-enzyme": "^7.1.2",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.5",
"redux-mock-store": "^1.5.4",
推荐阅读
- angular - 带阴影的材质角文本框
- c# - 使用 Polly 时收到错误“请求消息已发送”
- excel - FormulaArray 连接和替换错误
- bash - 使用从 bash 中的 ascii 文件中读取的字符串创建多个循环
- ruby-on-rails - 我可以更有效地编写 IO 绑定的 RSpec 测试吗?
- amazon-web-services - 如何通过无服务器框架将 BatchWriteItem 权限附加到我的 IAM 角色策略?
- oop - 当特征需要的状态多于结构中包含的状态时,如何为结构实现特征?
- java - Java ZipCoder 异常
- javascript - sapui5 列表中的数据绑定没有给出数据
- matlab - 根据matlab中的一些值组合数据