mardi 12 avril 2016

What is the best way for testing asynchronous react component

I want to test login component in react using mocha

const loginView = require('./index');
const React = require('react');
const ReactDOM = require('react-dom');
const ReactTestUtils = require('react-addons-test-utils');
const chai = require('chai');
const jsdom = require('mocha-jsdom');
const injectTapEventPlugin = require('react-tap-event-plugin');
const nock = require('nock');
const api = require('../../configuration').api;

injectTapEventPlugin();
chai.should();

describe('login', () => {

  beforeEach(() => {
    jsdom();
  });

  it('show error dialog when username or password is invalid', (done) => {
    const login = ReactTestUtils.renderIntoDocument(React.createElement(loginView));

    nock(api).post('user/access-token').reply(200);

    login.setState({
      email: 'test@email.tld',
      password: 'wrong-password'
    });

    ReactTestUtils.Simulate.touchTap(ReactDOM.findDOMNode(login.refs.signin).firstChild);

    setTimeout(() => {
      login.state.showErrorDialog.should.equal(true);
      login.setState({
        showErrorDialog: false
      });
      done();
    }, 1500);
  });
});

When sign in button is click, the ajax request check username and password (using superagent).

The problem is I don't want to use setTimeout function, I like to use a callback or promise when ajax request is complete. Is it possible ?

Aucun commentaire:

Enregistrer un commentaire