我正在使用Mocha,Chai,Karma,Sinon,Webpack进行单元测试。如何测试React-Redux连接组件?
我跟着这个链接来配置我的React-Redux代码的测试环境。
我可以成功地测试自己的行动和减速器javascript代码,但是当涉及到我的测试组件总是抛出一些错误。
import React from 'react';
import TestUtils from 'react/lib/ReactTestUtils'; //I like using the Test Utils, but you can just use the DOM API instead.
import chai from 'chai';
// import sinon from 'sinon';
import spies from 'chai-spies';
chai.use(spies);
let should = chai.should()
, expect = chai.expect;
import { PhoneVerification } from '../PhoneVerification';
let fakeStore = {
'isFetching': false,
'usernameSettings': {
'errors': {},
'username': 'sahil',
'isEditable': false
},
'emailSettings': {
'email': '[email protected]m',
'isEmailVerified': false,
'isEditable': false
},
'passwordSettings': {
'errors': {},
'password': 'showsomestarz',
'isEditable': false
},
'phoneSettings': {
'isEditable': false,
'errors': {},
'otp': null,
'isOTPSent': false,
'isOTPReSent': false,
'isShowMissedCallNumber': false,
'isShowMissedCallVerificationLink': false,
'missedCallNumber': null,
'timeLeftToVerify': null,
'_verifiedNumber': null,
'timers': [],
'phone': '',
'isPhoneVerified': false
}
}
function setup() {
console.log(PhoneVerification);
// PhoneVerification.componentDidMount = chai.spy();
let output = TestUtils.renderIntoDocument(<PhoneVerification {...fakeStore}/>);
return {
output
}
}
describe('PhoneVerificationComponent',() => {
it('should render properly', (done) => {
const { output } = setup();
expect(PhoneVerification.prototype.componentDidMount).to.have.been.called;
done();
})
});
下面的错误出现在上面的代码。
FAILED TESTS:
PhoneVerificationComponent
✖ should render properly
Chrome 48.0.2564 (Mac OS X 10.11.3)
Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
尝试从空子间谍变为chai间谍。
我该如何测试我的React-Redux Connected Components(智能组件)?
如何导出组件?是使用命名导出还是仅导出默认导入{PhoneVerification} ../ PhoneVerification';'是你的冒犯路线,当你这样做时,如果你没有进行命名导出,你得到undefined –
我正在使用命名导出 – Ayushya
我也有一个类似的设置,并得到类似的错误信息。对此有何进展?谢谢。 – FujiRoyale