-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from BUMETCS673/hw27_manage_profile_form_valid…
…ation validate manage profile form
- Loading branch information
Showing
4 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { validateProfileForm } from '../utils/validateProfileForm'; | ||
|
||
test('empty input form fields', () => { | ||
const formData = { | ||
name: '', gender: '', | ||
dob: { | ||
year: 1990, | ||
month: 1, | ||
day: 1 | ||
}, | ||
height: { | ||
feet: 0, | ||
inches: 0 | ||
} }; | ||
const result = validateProfileForm(formData); | ||
expect(result.isValid).toBe(false); | ||
}); | ||
|
||
test('name contains invalid characters', () => { | ||
const formData = { | ||
name: 'Test123', gender: 'female', | ||
dob: { | ||
year: 1990, | ||
month: 1, | ||
day: 1 | ||
}, | ||
height: { | ||
feet: 0, | ||
inches: 0 | ||
} }; | ||
const result = validateProfileForm(formData); | ||
expect(result.isValid).toBe(false); | ||
}); | ||
|
||
test('dob year is outside of valid range', () => { | ||
const formData = { | ||
name: 'Test Form', gender: 'female', | ||
dob: { | ||
year: 2030, | ||
month: 1, | ||
day: 1 | ||
}, | ||
height: { | ||
feet: 0, | ||
inches: 0 | ||
} }; | ||
const result = validateProfileForm(formData); | ||
expect(result.isValid).toBe(false); | ||
}); | ||
|
||
test('height (feet) is outside of valid range', () => { | ||
const formData = { | ||
name: 'Test Form', gender: 'female', | ||
dob: { | ||
year: 1997, | ||
month: 12, | ||
day: 31 | ||
}, | ||
height: { | ||
feet: 10, | ||
inches: 0 | ||
} }; | ||
const result = validateProfileForm(formData); | ||
expect(result.isValid).toBe(false); | ||
}); | ||
|
||
test('height (inches) is outside of valid range', () => { | ||
const formData = { | ||
name: 'Test Form', gender: 'female', | ||
dob: { | ||
year: 1997, | ||
month: 12, | ||
day: 31 | ||
}, | ||
height: { | ||
feet: 5, | ||
inches: -2 | ||
} }; | ||
const result = validateProfileForm(formData); | ||
expect(result.isValid).toBe(false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export function validateProfileForm(formData) { | ||
const { name, gender, dob, height } = formData; | ||
|
||
// Checking there is input data in each field | ||
if (!name || !gender || !dob || !height) { | ||
return { isValid: false, message: 'Form field(s) missing' }; | ||
} | ||
|
||
// Checking validity of name - con only contain alphabet or hyphens | ||
if (!/^[A-Za-z-\s]+$/.test(name)) { | ||
return { isValid: false, message: 'Name contains invalid characters. Only alphabetic characters, hyphens, and spaces allowed.' }; | ||
} | ||
|
||
// Checking validity of DOB year input | ||
if (dob.year < 1900 || dob.year > 2024) { | ||
return { isValid: false, message: 'DOB year not in valid range. Must be between 1900 and 2024.' }; | ||
} | ||
|
||
// Checking validity of height inputs | ||
if (height.feet < 0 || height.feet > 7) { | ||
return { isValid: false, message: 'Height (feet) not in valid range. Must be between 0 and 7.' }; | ||
} | ||
|
||
if (height.inches < 0 || height.inches > 12) { | ||
return { isValid: false, message: 'Height (inches) not in valid range. Must be between 0 and 12.' }; | ||
} | ||
|
||
// If all validations pass | ||
return { isValid: true, message: 'Form is valid' }; | ||
} |