【发布时间】:2016-04-12 12:23:30
【问题描述】:
我想用 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);
});
});
点击登录按钮时,ajax 请求检查用户名和密码(使用超级代理)。
问题是我不想使用 setTimeout 函数,我喜欢在 ajax 请求完成时使用回调或承诺。有可能吗?
【问题讨论】:
标签: testing reactjs mocha.js webpack