我是react redux
的新手。我正在尝试为看起来像的组件搜索功能编写单元测试用例,
constructor(props) {
super(props);
this.staate = {
search : ''
}
}
onInputChnage = (e) => {
this.setState({ search: e.target.value });
}
render() {
const { jobs: { content } } = this.props;
const { search } = this.state;
const filteredList = content.filter(job => (job.jdName && job.jdName.toLowerCase().includes(search.toLowerCase())) || (job.technology && job.technology.toLowerCase().includes(search.toLowerCase())));
return (
<input type="text"
id="searchJob"
className="form-control-sm border-0 flex-grow-1"
value={this.state.search}
placeholder="Technology / Job title"
onChange={this.onInputChnage} />
<i className="fa fa-search search-job"></i>
)
}
}
数据就像,
this.props.jobs = {
"content": [{
"id": "5b7d4a566c5fd00507501051",
"companyId": null,
"jdName": "Senior/ Lead UI Developer",
"jobDescription": null,
"technology": "java"
},{
"id": "5b7d4a566c5fd005075011",
"companyId": null,
"jdName": "ead UI Developer",
"jobDescription": null,
"technology": "angular"
}]
}
因此,在这里我要测试此组件搜索。我为此使用jest and enzymes
。有人可以帮我吗?
答案 0 :(得分:0)
这是解决方案:
index.tsx
:
import React from 'react';
interface ISearchComponentOwnProps {
jobs: { content: any };
}
interface ISearchComponentState {
search: string;
}
export class SearchComponent extends React.Component<ISearchComponentOwnProps, ISearchComponentState> {
constructor(props: ISearchComponentOwnProps) {
super(props);
this.state = {
search: ''
};
}
public render() {
const {
jobs: { content }
} = this.props;
const { search } = this.state;
const filteredList = content.filter(
job =>
(job.jdName && job.jdName.toLowerCase().includes(search.toLowerCase())) ||
(job.technology && job.technology.toLowerCase().includes(search.toLowerCase()))
);
return (
<div>
<input
type="text"
id="searchJob"
className="form-control-sm border-0 flex-grow-1"
value={this.state.search}
placeholder="Technology / Job title"
onChange={this.onInputChnage}
/>
<i className="fa fa-search search-job"></i>
<ul className="job-list">
{filteredList.map(job => {
return (
<li key={job.id}>
<span>jdName: {job.jdName}</span>
<span>technology: {job.technology}</span>
</li>
);
})}
</ul>
</div>
);
}
private onInputChnage = (e: React.ChangeEvent<HTMLInputElement>) => {
this.setState({ search: e.target.value });
}
}
单元测试:
index.spec.tsx
:
import React from 'react';
import { shallow } from 'enzyme';
import { SearchComponent } from '.';
describe('SearchComponent', () => {
const jobs = {
content: [
{
id: '5b7d4a566c5fd00507501051',
companyId: null,
jdName: 'Senior/ Lead UI Developer',
jobDescription: null,
technology: 'java'
},
{
id: '5b7d4a566c5fd005075011',
companyId: null,
jdName: 'ead UI Developer',
jobDescription: null,
technology: 'angular'
}
]
};
it('should search and filter job list correctly', () => {
const wrapper = shallow(<SearchComponent jobs={jobs}></SearchComponent>);
expect(wrapper.find('#searchJob')).toHaveLength(1);
const mockedChangeEvent = { target: { value: 'Lead' } };
expect(wrapper.state('search')).toBe('');
wrapper.find('#searchJob').simulate('change', mockedChangeEvent);
expect(wrapper.state('search')).toBe('Lead');
expect(wrapper.find('.job-list').children()).toHaveLength(1);
expect(wrapper.html()).toMatchSnapshot();
});
});
覆盖率100%的单元测试结果:
PASS src/stackoverflow/54456606/index.spec.tsx
SearchComponent
✓ should search and filter job list correctly (19ms)
-----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.tsx | 100 | 100 | 100 | 100 | |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 1 passed, 1 total
Time: 3.058s, estimated 5s
组件快照:
// Jest Snapshot v1
exports[`SearchComponent should search and filter job list correctly 1`] = `"<div><input type=\\"text\\" id=\\"searchJob\\" class=\\"form-control-sm border-0 flex-grow-1\\" value=\\"Lead\\" placeholder=\\"Technology / Job title\\"/><i class=\\"fa fa-search search-job\\"></i><ul class=\\"job-list\\"><li><span>jdName: Senior/ Lead UI Developer</span><span>technology: java</span></li></ul></div>"`;
以下是完整的演示:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/54456606