我试图应用酶时遇到这个错误,我找不到任何相关问题.
这是test.js;
import React from 'react';
import AccountLoginForm from './LoginPage';
import sinon from 'sinon';
import { mount, shallow, configure } from 'enzyme';
import { expect } from 'chai';
import Adapter from 'enzyme-adapter-react-15';
import configureStore from 'redux-mock-store';
configure({ adapter: new Adapter() });
const mockStore = configureStore();
sinon.spy(AccountLoginForm.prototype, 'componentDidMount');
describe('<AccountLoginForm />', () => {
it('calls componentDidMount', () => {
const wrapper = shallow(<AccountLoginForm />, { context: { store: mockStore() } });
expect(Foo.prototype.componentDidMount.calledOnce).to.equal(true);
});
});
Run Code Online (Sandbox Code Playgroud)
这是LoginPage;
import React, { Component } from 'react'; …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的二维数组;
而且我需要使用map函数来显示与数组长度一样多的div元素。
我将状态保存到localStorage中,防止刷新页面后数据消失。
const conditions = JSON.parse(localStorage.getItem("condition")).map((condition, index) => {
return <div key={index} className="card bg-color-primary" style={{ height: "170px", width: "300px", borderColor: "#1E90FF", backgroundColor: "white", borderWidth: "2px" }}>
{_.isEmpty(this.state.rule.condition) ?
<div className="card-content" style={{ color: "black" }}>
<span className="card-title">New Condition</span>
<p>Click to edit</p>
</div> :
<div className="card-content" style={{ color: "black" }}>
<span className="card-title">{condition.property}</span>
<p>{condition.operator + " " + condition.value}</p>
</div>}
<div className="card-action">
<a href="javascript:;" style={{ color: "red" }}>OR</a>
<a href="javascript:;" style={{ color: "black" }}>AND</a>
<a onClick={this.handleOpen} …Run Code Online (Sandbox Code Playgroud) 我是测试 axios 调用的初学者并开始使用 axios-mock-adapter,但我不明白为什么我们使用 axios-mock-adapter。
mock.onPost('/api').reply(200, userData, headers);
Run Code Online (Sandbox Code Playgroud)
在这个代码片段中,请求是真的发送到服务器还是只是一个模拟?
因为如果我提供错误的凭据,它会以 200 状态响应,因为我在“回复”中识别它以返回 200。
那么如果我确定了响应状态,使用它的原因是什么?如果它真的不去服务器,这似乎是没有用的。
也许我错过了一些我不知道的东西,因为我是新手,但应该有人把这个问题放在我的脑海里。
我点击了提交按钮后试图重定向我的页面,为此,我的onSubmit方法看起来像;
onSubmit(e){
e.preventDefault();
if (this.isValid()){
this.setState({ errors: {}, isLoading: true });
this.props.userSignupRequest(this.state).then(
() => {
this.context.router.push('/');
},
( data ) => this.setState({ errors: data.response.data, isLoading: false })
);
}
}
Run Code Online (Sandbox Code Playgroud)
我的propTypes和contextTypes ;
SignupForm.propTypes = {
userSignupRequest: React.PropTypes.func.isRequired
}
SignupForm.contextTypes = {
router: React.PropTypes.object.isRequired
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,但它一直显示错误,而不是重定向.我也试过了browserHistory.push('/'),它也没用.
我犯的错误是;
Uncaught (in promise) TypeError: _this2.context.router.push is not a function
at props.userSignupRequest.then._this2.setState.errors
Run Code Online (Sandbox Code Playgroud)
正如我所说的,我也试过browserHistory喜欢;
import { browserHistory } from 'react-router';
Run Code Online (Sandbox Code Playgroud)
和onSubmit方法;
onSubmit(e){
e.preventDefault();
if (this.isValid()){
this.setState({ errors: …Run Code Online (Sandbox Code Playgroud) reactjs ×4
javascript ×3
unit-testing ×2
arrays ×1
axios ×1
enzyme ×1
jestjs ×1
mocha.js ×1
react-router ×1
redirect ×1