How to test fetch result in componentDidMount

427 Views Asked by At

I need to test if the state was set after promise resolved in componentDidMount

class Todos extends React.Component {
    constructor(props) {
        super(props);
        this.state = {};
    }
    componentDidMount() {
        const result = todoStore.fetch();
        mobx.when(
          () => result.state !== mobxUtils.PENDING,
          () => (this.setState({todos: result.value}))
        )

    }
    render() {
        const { todos } = this.state;
        return <div>
            {todos && <ul>
                {todos.map(t => <li>{t.title}</li>)}
            </ul>}
        </div>
    }
}

So far I have..

const wrapper = shallow(<Todos />);

// TODO Need wait until promise is resolved. But how?
// Maybe listen the setState method ?
// PLEASE don't use setTimout.

assert(wrapper.state('todos'))

See the full example..

https://runkit.com/ridermansb/testing-fetch-result-in-componentdidmount/1.0.0

0

There are 0 best solutions below