We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- React
- Forms
- Employee Validation
- Discussions
Employee Validation
Employee Validation
Sort by
recency
|
45 Discussions
|
Please Login in order to post a comment
There seems to be an error with test cases it self. while testing employeeID the name is passed as Use (which is not a 4 chars).
The test is outdate. so if you want to get current date and compare to the input date it will give you error. instead we should do this:
Guys, here's a workaround until the tests are fixed:
Install the
@sinonjs/fake-timers
packageimport React, { useEffect, useState } from "react";
function EmployeeValidationForm() { const [form, setForm] = useState({ name: "", email: "", employeeId: "", joiningDate: "" }) const [errors, setErrors] = useState({ isNameError: true, isEmailError: true, isEmployeeIdError: true, isDateError: true }); const [isButtonDisabled, setIsButtonDisabled] = useState(false); const [touched, setTouched] = useState({ name: false, email: false, employeeId: false, joiningDate: false });
useEffect(() => { if (Object.values(form).some(val => val === "" || val === null || val === undefined)) { setIsButtonDisabled(true) } else if (Object.values(errors).some(error => error === true)) { setIsButtonDisabled(true) } else { setIsButtonDisabled(false) } }, [form, errors])
const handleOnChange = (e) => { setForm({ ...form, [e.target.name]: e.target.value })
}
const handleOnSubmit = () => { setForm({ name: "", email: "", employeeId: "", joiningDate: "" }) setErrors({ isNameError: true, isEmailError: true, isEmployeeIdError: true, isDateError: true }) }
const handleBlur = (e) => { setTouched({ ...touched, [e.target.name]: true }); };
return ( {errors.isNameError && Name must be at least 4 characters long and only contain letters and spaces
} {errors.isEmailError && Email must be a valid email address} {errors.isEmployeeIdError && Employee ID must be exactly 6 digits} {errors.isDateError && Joining Date cannot be in the future} Submit ); }export default EmployeeValidationForm;
In the test, the join date “2025-04-12” is used as a future date, but we’ve already passed that day 😄. It would be better to use a dynamic value instead of a hardcoded one.