【发布时间】:2018-12-14 00:30:27
【问题描述】:
我有一个简单的函数,可以在单击事件时将组件的状态从 true 切换为 false。 这里是函数
public toggleAvailability(dayTime: string): void {
const isAvailable = this.state[dayTime] === false ? true : false;
this.setState(
{ [dayTime]: isAvailable },
() => {
const instructor = {
userId: this.props.userId,
availability: this.state.friEarlyAfternoon
};
this.props.updateInstructor(instructor);
});
}
我正在使用 Jest+Enzyme 进行单元测试,我正在尝试按如下方式测试我的切换功能:
describe('Method toggleAvailability()', () => {
function test_toggleAvailability(dayTime: string, currentState: boolean, toggledState: boolean): void {
beforeEach(() => {
wrapper.setState({
dayTime: currentState,
});
wrapper.instance().toggleAvailability(dayTime);
});
it(`Sets the state's ${dayTime} to ${toggledState}`, () => {
expect(wrapper.state().dayTime).toEqual(toggledState);
});
}
test_toggleAvailability('monEarlyMorning', false, true);
test_toggleAvailability('monEMorning', true, false);
});
这里有人有建议吗?
【问题讨论】:
标签: javascript reactjs unit-testing jestjs enzyme