I am trying to test a Redux action and need assistance with testing an action with side-effects.
Here is my action :
export function login(email, password) {
return dispatch => {
dispatch(setLoginSuccess(false));
loginApi(email, password, error => {
dispatch(setLoginPending(false));
if (!error) {
dispatch(setLoginSuccess(true));
} else {
dispatch(setLoginError(error));
}
});
}
}
Below is the loginApi function to authenticate user :
export function loginApi(email, password, callback) {
if (email === 'test#test.com' && password == '123') {
return callback(null);
} else {
return callback(new Error('Please provide valid email and password'));
}
};
Additionally, I am facing an issue while simulating a form submit in my component with Enzyme and Jest.
Here is the code for the same :
render() {
let {email, password, emailValid} = this.state;
let {isLoginPending, isLoginSuccess, loginError} = this.props;
return (
<div className="col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2 col-xs-10 col-xs-offset-1">
<h3 className="text-center">Login</h3>
<form className="login-form" onSubmit={this.handleSubmit.bind(this)}>
<div className={emailValid? "form-group has-success" : (emailValid == undefined)? "form-group": "form-group has-error"}>
<label>Email address</label>
<input type="email" name="email" className="form-control" ref="userEmail"
placeholder="Enter your email" onChange={this.handleChange.bind(this)}/>
</div>
{/* Checking if email valid or not */}
{this.props.emailValid? "" : (this.props.emailValid == undefined)? "" : <p className="text-danger">Please provide a valid email!</p>}
<div className="form-group">
<label>Password</label>
<input type="password" name="password" className="form-control" ref="userPassword"
placeholder="Enter your password" onChange={this.handleChange.bind(this)}/>
</div>
<button type ="submit" className="btn btn-primary btn-block" disabled={!this.props.emailValid}>Get Started</button>
{/* Displaying error messages */}
{ loginError && <div className="auth-error-msg"><p className="text-danger">{loginError.message}</p></div> }
</form>
</div>
);
};
Here is the code for the handleSubmit event:
handleSubmit(e){
e.preventDefault();
this.props.login(this.refs.userEmail.value, this.refs.userPassword.value);
this.setState({
email: '',
password: ''
});
}
I am trying to simulate the Submit event in this way :
it('should render 1 error block on submitting invalid form', () => {
// Render a checkbox with label in the document
const spy = jest.fn();
const component = shallow(<Login login={spy}/>);
const form = component.find('form').simulate('submit');
});
But it currently throws an error as it cannot find preventDefault. How do I test this event?
I would recommend you to split the testing. Submitting the form and testing the actions are two separate things. For testing the action with jest, you need to dispatch the action to a mock store, and see which is the final state of the store. Something like this:
describe('actions', () => {
let store
beforeEach(() => {
store = mockStore({})
})
it('should dispatch the correct actions', () => {
const expectedActions = [
{ type: 'action1', ...arguments },
{ type: 'action2', ...arguments }
]
store.dispatch(login('user', 'password'))
expect(store.getActions()).toEqual(expectedActions)
})
})
you can do multiple test cases, adapting the expected actions to what you passed as parameters.
For creating a mock store, there are multiple packages that can do the job. Here is one example with support for thunks:
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)
export default mockStore
I personally would not spend too much effort testing that the form submits. At the end, that is a standard html thing, so I would instead focus on the components that you have built yourself.
And another tip: If you have gone through all the trouble of using redux, don't fall back to normal state. That setState that you have would be much more easily implemented, and tested, by using a normal reducer and getting that into your state as well.
Related
I've just started learning about react js and this is my first react js app. I'm using api to fetch the data. so far it works, but now I want to add a search keyword to the function that is acquired from a search bar component.
here's my code:
SearchBar.js
const SearchBar = ({ getUsers }) => {
return (
<div className="is-flex flex-align-items-center mb-3">
<input type="text" id="query" className="input search-input" placeholder="search keyword"/>
<Button className="search-btn ps-3 pe-3"
onClick={() => getUsers(document.querySelector('#query').value)}>
<FontAwesomeIcon icon={faMagnifyingGlass} />
</Button>
</div>
);
};
MasterUser.js
import { useState, useEffect } from "react";
import SearchBar from "./SearchBar";
const MasterUser = () => {
const [users, setUsers] = useState([]);
useEffect(() => {
getUsers();
}, []);
const getUsers = async (query='') => {
console.log('get users', query);
try {
let myurl = 'http://localhost:8080/users';
const response = await fetch(myurl);
const data = await response.json();
setUsers(data);
setIsLoading(false);
} catch (e) {
console.log(e.getMessage());
}
};
return (
<div>
<SearchBar onClick={getUsers}/>
</div>
);
};
when the app loads, the console log says get users <empty string> and it returns all the users as expected, but when I clicked on the search button (magnifyingGlass) it gives an error Uncaught TypeError: getUsers is not a function.
any help is appreciated..
<SearchBar onClick={getUsers}/>
You have named the prop onClick not getUsers. That's why you get that error.
Yeah, accessing dom element value using selectors (e.g. document.querySelector('#query').value) is also not typical react. Read about controlled form elements (save form element value in state).
Make your searchBar component more reactive like so
const SearchBar = ({ getUsers }) => {
const [searchValue,setSearchValue]=useState('');
return (
<div className="is-flex flex-align-items-center mb-3">
<input type="text" id="query" className="input search-input" placeholder="search keyword" value={searchValue} onChange={(e)=>setSearchValue(e.target.value)}/>
<Button className="search-btn ps-3 pe-3"
onClick={() => getUsers(searchValue)}>
<FontAwesomeIcon icon={faMagnifyingGlass} />
</Button>
</div>
);
};
I am trying to implement eye/eyeslash in on my Register form in React.
This is a function that's is responsible for changing visibility type and eye icon changing.
import React, { useState } from "react";
import { FontAwesomeIcon } from "#fortawesome/react-fontawesome";
export const usePasswordToggle = () => {
const [visible, setVisibility] = useState();
const Icon = <FontAwesomeIcon icon={visible ? "eye-slash" : "eye"} />;
const InputType = visible ? "text" : "password";
return [InputType, Icon];
};
I am trying to implement it in component responsible for registering.
import React, { Component, createRef } from "react";
import { usePasswordToggle } from "./usePasswordToggle";
class Register1 extends React.Component {
EmailR = createRef();
UsernameR = createRef();
PasswordR = createRef();
PasswordConfirmR = createRef();
constructor(props) {
super();
this.state = {
message: "",
password: "",
confirmPassword: "",
};
}
handleSubmit = (event) => {
// alert(this.PasswordR.current.value);
// alert(this.PasswordConfirmR.current.value);
if (this.PasswordR.current.value !== this.PasswordConfirmR.current.value) {
alert("The passwords doesn't match");
return false; // The form won't submit
} else {
alert("The passwords do match");
return true; // The form will submit
}
};
onCreateAccount = () => {
let loginInfo = {
Username: this.UsernameR.current.value,
Email: this.EmailR.current.value,
Password: this.PasswordR.current.value,
};
fetch("http://localhost:5000/api/authenticate/register", {
method: "POST",
headers: { "Content-type": "application/json" },
body: JSON.stringify(loginInfo),
})
.then((r) => r.json())
.then((res) => {
if (res) {
this.setState({
message:
"New Account is Created Successfully. Check your email to verify Account.",
});
}
});
};
render() {
return (
<div>
<h2 className="FormDescription">
{" "}
Please enter Account details for registration
</h2>
<div className="Form">
<p>
<label>
Email: <input type="text" ref={this.EmailR} />
</label>
</p>
<p>
<label>
Username: <input type="text" ref={this.UsernameR} />
</label>
</p>
<div>
<label>
Password:{" "}
<input type={usePasswordToggle.InputType} ref={this.PasswordR} />
</label>
<span className="password-toogle-icon">
{usePasswordToggle.Icon}
</span>
</div>
<p>
<label>
ReenterPassword:{" "}
<input type="password" ref={this.PasswordConfirmR} />{" "}
</label>
</p>
<button onClick={this.handleSubmit}> Create </button>
<p>{this.state.message}</p>
</div>
</div>
);
}
}
export default Register1;
My password is always visible, and eye icon is even not visible on the form (it should be inside my input field, but it is not).
Focus on this code snippet:
<div>
<label>
Password: <input type={usePasswordToggle.InputType} ref={this.PasswordR} />
</label>
<span className="password-toogle-icon">{usePasswordToggle.Icon}</span>
</div>
Any suggestion what is the problem?
Change this
const [visible, setVisibility] = useState();
to this
const [visible, setVisible] = useState(true);
as the official documentation here
First, add a default value to your useState, either true or false depending on which icon you want to render first.
Then, you should add a onClick method to your icon which will toggle the visibility state. You're setting the icon based on visible value, but you never toggle the value.
onClick={() => setVisibility(!visible)}
UPDATE
You also need to execute your Hook inside your main component (because yes, you wrote what React call a Hook), like so :
const [inputType, icon] = usePasswordToggle();
But doing so, you'll get an error from React that say you cannot use a Hook within a class component due to how they work.
Basically you need to change your Register1 component to be a functional component, and not a class anymore. Look here for a quick overview on how to : https://reactjs.org/docs/components-and-props.html
I am using React and Formik to handle my forms. In a component, I have a simple input text. That text is handled for error if being empty by formik. The problem is, I also want to asynchronously handle server validation (if the input.target.value already exist in the database).
Formik provides that functionality, but I am obviously doing something wrong because I get the following error.
Cannot read property .then of undefined
Code Sandbox here: https://codesandbox.io/s/nkm2zyy4z0
So far, I have done the following. According to formik documentation:
const asyncValidation = values =>
listItems.then(data => {
const errors ={};
if (data.includes(values.name)) {
errors.name = 'Username already exist';
}
if (!data.include(values.name) {
throw errors;
}
});
I also tried to create another iteration of the asyncValidation promise, as you see below:
const asyncValidation = values =>
new Promise((resolve, reject) => {
const errors = {};
if (listGroups.includes(values.name)) {
console.log(errors)
errors.email = 'Required';
}
if (!listGroups.includes(values.name)) {
console.log(errors)
reject(errors);
} else {
resolve();
}
});
But still, I get an error:
index.jsx:21 Uncaught (in promise) TypeError: Cannot read property 'name' of undefined
Not sure what to do. Name if a property of the values object. If I print on the console, the results, it will print this:
{name: "generalGroup1", description: ""}
description: ""
name: "generalGroup1"
__proto__: Object
Not sure what is wrong here...
Here, formik uses promises to handle this async functionality. I am a little perplex, on what error should be thrown. Maybe I made the mistake here, since I want to basically say. If the value matches something in the database, say that it already exists. If not don't throw any error.
Promises need to throw errors, so what should I do there, and how should I solve my console error.
I am also using a built in service to call the list of items from the API, in order to check against the value being on the input.
export const listItems = () => {
const options = {
method: httpMethod.GET,
url: endpoint.LIST_ITEMS
};
return Instance(options);
};
Below is the part of the component relevant to the input field:
class ItemDetailsForm extends React.Component {
static propTypes = {
...formPropTypes,
data: PropTypes.object
};
handleSubmit = values => {
const { id, onSubmit } = this.props;
onSubmit(id, values);
asyncValidation();
};
render() {
const { data } = this.props;
return (
<Formik
initialValues={{ ...data }}
onSubmit={this.handleSubmit}
validationSchema={validationSchema}
render={({ values, touched, errors, handleChange, handleBlur, handleSubmit }) => (
<form onSubmit={handleSubmit}>
<div className="row">
<div className="col-md-3">
<div className="form-group">
<label htmlFor="itemName">
Item name <span className="text-danger">*</span>
</label>
<input
type="text"
onChange={handleChange}
onBlur={handleBlur}
value={values.name}
name="name"
className={classNames('form-control', {
'is-invalid': errors.name && touched.name
})}
id="itemsName"
placeholder="Some Item"
/>
{!!errors.name && touched.name && (
<div className="text-danger">{errors.name}</div>
)}
</div>
<button className="btn btn-primary" type="submit">
Submit
</button>
</div>
</div>
</form>
)}
/>
);
}
}
export default ItemDetailsForm;
I have followed the formik docs, almost to the teeth, but something is obviously wrong. Can you help a bit. I am relatively new to programming, so if you could explain my mistake it would be great.
Below is my RequestAnInvite redux-form. The problem is when I submit the form, submitting is never changed to true. You can see I have a log below, which is always outputting false.
What am I doing wrong with redux-form to cause submitting to never set to true when I submit the form?
class RequestAnInvite extends React.Component {
componentDidMount() {
this.props.dispatch(loadTitles());
}
handleSubmit(data) {
console.log(data);this.props.dispatch(requestInvitationsActions.createInvitationRequest(data));
}
render() {
const { handleSubmit, submitting } = this.props;
console.log('submitting: ' + submitting);
return (
<div className="container-fluid h-100">
<form onSubmit={handleSubmit(this.handleSubmit.bind(this))}>
<Field
name="email"
type="text"
component={renderField}
label="Email"
placeholder="xxx#acme.com"
/>
<p>submitting: {submitting}</p>
<div className="form-group form-group-actions">
<button type="submit" className="btn btn-primary" disabled={submitting}>
{submitting ? 'Requesting...' : 'Request an Invite'}
</button>
</div>
</form>
</div>
);
}
}
RequestAnInvite = reduxForm({
form: 'RequestAnInvite',
validate,
})(RequestAnInvite);
const mapStateToProps = state => {
return {
titles: state.titles
};
};
const mapDispatchToProps = (dispatch) => bindActionCreators({
...requestInvitationsActions,
}, dispatch)
export default connect(mapStateToProps, mapDispatchToProps)(RequestAnInvite);
Update 1
handleSubmit(data) {
this.props.createInvitationRequest(data)
.then((response) => {
console.log(response)
}, (error) => {
});
}
From redux-form docs:
Whether or not your form is currently submitting. This prop will only work if you have passed an onSubmit function that returns a promise. It will be true until the promise is resolved or rejected.
Your handleSubmit is just dispatching an action so it has no way of knowing when it is submitting
When I am submiting the form submitSucceeded props goes true, pristine is also working fine but submitting props not change on submit form. I have attached related code. Please suggest me how I can fix this issue.
import React from 'react'
import { Field, reduxForm } from 'redux-form'
import FileInput from '../FileInput'
import 'react-widgets/dist/css/react-widgets.css';
import './reactForm.css';
const EditForm = (props) => {
const { handleSubmit, submitSucceeded, pristine, submitting, owners, cities, compound, avatarUrl, changeAvatar } = props;
return (
<form onSubmit={handleSubmit}>
<div className="row padding-20-0">
<div className="col-md-4">
<div className="box-upfile cursor" style={{backgroundImage: `url(${avatarUrl})`}} >
<div className="editImgComp" >
<i className="sprite-icon icon-030" onClick={()=>{changeAvatar(null); props.change('avatar', null)}}/>
<label html="imageBrowse">
<FileInput
onDone={(file)=> {changeAvatar(file.file); props.change("avatar", file.file)}}
type="file" className="hidden" id="imageBrowse"/>
<i className="sprite-icon icon-031"/>
</label>
</div>
</div>
</div>
</div>
<div className="row">
<div className="text-right col-xs-6">
{
submitSucceeded ?
<button type="button" className="btn ls-btn-red cursor" disabled={pristine || submitting || submitSucceeded}>
<i className='fa fa-circle-o-notch fa-spin'></i> Saving
</button>
:
<button type="submit" className="btn ls-btn-red cursor" disabled={pristine || submitting} onClick={handleSubmit} >Save</button>
}
</div>
</div>
</form>
)
}
export default reduxForm({
form: 'compoundForm' // a unique identifier for this form
})(EditForm)
Container:-
handleSubmit(data) {
this.props.dispatch(compoundSave(data));
}
Action:-
export function compoundSave(data) {
const id = data.id;
const config = {
method: 'put',
body: JSON.stringify({compound: data}),
};
return callApi('/v1/compounds/'+id, {}, config, compoundSaveRequest, compoundSaveSuccess, compoundSaveFailure);
}
Call Api method:-
`export function callApi(path, params, config, request, onRequestSuccess, onRequestFailure) {
const API_ROOT = 'http://api.dev.leasing.clicksandbox.com:8080';
const idToken = localStorage.getItem('id_token');
let url = API_ROOT+path;
url = buildUrlWithQueryString(url, params);
return dispatch => {
dispatch(request);
return fetch(url, config)
.then(checkStatus)
.then(parseJSON)
.then((json) => {
if (!json.success) { // (response.status < 200 || response.status > 300)
json.error &&
Toastr.error(json.error);
dispatch(onRequestFailure(json));
} else {
json.message &&
Toastr.success(json.message);
dispatch(onRequestSuccess(json));
}
}).catch((error) => {
const exceptionMessage = {
success: false,
error: "Something went wrong!"
}
dispatch(onRequestFailure(exceptionMessage));
});
};
}`
Please let me know if I need to explain more.
For any one else arriving here a year later like me looking for answers, and without seeing the Container code... i can infer that the problem is that you defined the method handleSubmit in the Container and send it as a prop to EditForm. The problem with that is that a Component that has the reduxForm() HOC applied, in this case EditForm, will generate its own handleSubmit prop, and therefore a conflict arises, leading to wrong behaviour of the form when submitting.
In order to fix the issue, you should have send the prop to EditForm with a different name say
<EditForm onSubmit={this.onSubmit} />
And then inside the EditForm component use it like:
...
<form onSubmit={handleSubmit(onSubmit)}>
That way the prop submitting of the reduxForm component will work if the submit handler returns a promise.
you should call handleSubmit() method with handler passed to component :
<form onSubmit={handleSubmit(this.props.onSubmit)}>