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
|
49 Discussions
|
Please Login in order to post a comment
I believe some of the unit tests are incorrect, or the task description is not clear enough.
The task says “The user types...”, so validation is expected to run on typing (i.e. using onChange). However, the test changes values without triggering onChange, which causes the test to fail even when the code behaves as described.
import React, { useState } from "react";
const initialInputState = { name: '', email: '', employeeId: '', joiningDate: '', }
const initialErrorState = { name: true, email: true, employeeId: true, joiningDate: true, }
function EmployeeValidationForm() { const [inputs, setInputs] = useState(initialInputState); const [errors, setError] = useState(initialErrorState);
const isNameValid = (name) => { const isValid = name.length >= 4 && name.match(/^[A-Za-z ]+$/).length; setError(currentErrors => ({ ...currentErrors, name: !isValid, })) }
const isEmailValid = (email) => { const isValid = email.includes("@") && email.endsWith(".com"); setError((currentErrors) => ({ ...currentErrors, email: !isValid, })) }
const isEmployeeIdVaild = (employeeId) => { const isValid = employeeId.length === 6 && Number.isInteger(+employeeId); setError((currentErrors) => ({ ...currentErrors, employeeId: !isValid })) }
const isDateValid = (date) => { const today = new Date("2025-04-11"); const isValid = new Date(date) <= today; setError((currentErrors) => ({ ...currentErrors, joiningDate: !isValid, })) }
const handleChange = (e) => { setInputs((currentInputs) => ({ ...currentInputs, [e.target.name]: e.target.value }));
}
const handleSubmit = (e) => { e.preventDefault(); setInputs(initialInputState); setError(initialErrorState); }
to bypass the date issue causing the tests to fail, I did the following:
Please fix the test case for the feature year, its set to 2025-04-12 and expect to be a future.... Better set dynamic date converted or put like 2030.... and fix after 5 years again :D:D:D
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).