1
我有這個陣營組件如何測試一個React組件,隨着時間和Jest和酶一起更新?
export class Timer extends Component {
constructor(props) {
super(props);
this.state = {i : props.i};
}
componentDidMount(){
this.decrementCounter();
}
decrementCounter(){
if(this.state.i < 1){
return;
}
setTimeout(() => {
this.setState({i : this.state.i - 1})
this.decrementCounter()}, 1000);
}
render(){
return <span>{this.state.i}</span>
}}
我想表達這樣
jest.useFakeTimers();
it('should decrement timer ',() => {
const wrapper = shallow(<Timer i={10} />);
expect(wrapper.text()).toBe("10");
jest.runOnlyPendingTimers();
expect(wrapper.text()).toBe("9");
});
測試目前國內首家指望通過,但第二個失敗
Expected value to be (using ===):
"9"
Received:
"10"
我怎樣才能正確地測試這個組件?