1
我想測試登錄組件使用摩卡什麼是用於測試異步的最佳方式作出反應成分
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: '[email protected]',
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請求檢查用戶名和密碼(將SuperAgent)反應。
問題是我不想使用setTimeout函數,我喜歡在ajax請求完成時使用回調或承諾。可能嗎 ?