【发布时间】:2020-01-18 14:20:11
【问题描述】:
假设我有一个子组件,它需要两个矩对象作为道具,例如:
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
class ChildComponent extends React.Component {
constructor(props) {
super(props);
}
startAndEndDateOnSameDay() {
return this.props.startDate.isSame(this.props.endDate, 'date')
}
render() {
let formattedDate;
if(this.startAndEndDateOnSameDay()) {
formattedDate = this.props.startDate.format();
}
else {
formattedDate = this.props.endDate.fromNow();
}
return (
<div>{formattedDate}</div>
);
}
}
ChildComponent.propTypes = {
startDate: PropTypes.instanceOf(moment).isRequired,
endDate: PropTypes.instanceOf(moment).isRequired
}
export default ChildComponent;
还有一个父组件将两个矩对象传递给子组件,例如:
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import ChildComponent from './ChildComponent';
class ParentComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
startDate: moment(),
endDate: moment()
};
}
render() {
return (
<ChildComponent startDate={this.state.startDate} endDate={this.state.endDate}/>
);
}
}
export default ParentComponent;
我正在尝试用 jest 和酶来测试这些组件:
import React from 'react';
import { shallow } from 'enzyme';
import ParentComponent from '../components/ParentComponent';
describe('<ParentComponent />', () => {
let wrapper;
beforeAll(() => {
wrapper = shallow(<ParentComponent/>);
});
it('should render the component correctly', () => {
expect(wrapper).toMatchSnapshot();
});
});
和
import React from 'react';
import { shallow } from 'enzyme';
import ChildComponent from '../components/ChildComponent';
import moment from 'moment';
describe('<ChildComponent />', () => {
let wrapper;
beforeAll(() => {
wrapper = shallow(<ChildComponent startDate={moment()} endDate={moment()}/>);
});
it('should render the component correctly', () => {
expect(wrapper).toMatchSnapshot();
});
describe('when the the start date and end date are on the same day', () => {
it('should print a the formatted start date', () => {
expect(wrapper.text()).toEqual('mock format here');
});
});
describe('when the start and end date are not on the same day', () => {
it('should print the the end date from now', () => {
expect(wrapper.text()).toEqual('mock from now here');
});
});
});
如何模拟 moment.js 库函数以使我的测试正常工作?
我正在尝试在 __mocks__ 文件夹中创建一个手动模拟文件,该文件将模拟两个测试套件的时刻库函数。我目前遇到的问题:
- 如何从模拟文件中模拟
moment()构造函数以始终返回相同的日期,这样我的快照测试将始终通过? - 如何模拟
.isSame()、.format()和.fromNow()函数以始终返回相同的值,而不管当前时间如何?
到目前为止,我的以下测试文件破坏了我的所有测试。我正在关注文档here:
const moment = require('moment');
function isSame() {
return true;
}
function fromNow() {
return 'Tomorrow at 12:00 pm'
}
function format() {
return 'Sept 16th 19';
}
exports.isSame = isSame;
exports.fromNow = fromNow;
exports.format = format;
当我使用此文件时,我的组件中出现错误,提示 startDate 和 endDate 未定义。
【问题讨论】:
标签: reactjs mocking jestjs enzyme