-
Notifications
You must be signed in to change notification settings - Fork 0
[POLY-91] Add required field indicators and validation to listing creation #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
677bac1
feat: add required field indicators and inline validation to listing …
cole-hackman e621267
merge: resolve conflicts with latest dev
cole-hackman 1424689
fix: match Dispatch<SetStateAction> signature for onImagesChange
cole-hackman de7b945
fixes
jaydonkc fefbcd9
fix: address listing form validation review feedback
jaydonkc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
frontend/app/listings/__tests__/newListingValidation.test.ts
This file contains hidden or 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,86 @@ | ||
| import { | ||
| hasFieldErrors, | ||
| upsertFieldError, | ||
| validateDescription, | ||
| validateImages, | ||
| validateListingFields, | ||
| validatePrice, | ||
| validateTitle, | ||
| } from '../newListingValidation'; | ||
|
|
||
| describe('new listing validation', () => { | ||
| it('collects required field errors for an empty submission', () => { | ||
| expect( | ||
| validateListingFields({ | ||
| title: '', | ||
| description: ' ', | ||
| price: '', | ||
| images: [], | ||
| }) | ||
| ).toEqual({ | ||
| title: 'Title is required.', | ||
| description: 'Description is required.', | ||
| price: 'Price is required.', | ||
| images: 'At least 1 photo is required.', | ||
| }); | ||
| }); | ||
|
|
||
| it('treats trimmed valid values as valid', () => { | ||
| expect(validateTitle(' Desk lamp ')).toBeUndefined(); | ||
| expect(validateDescription(' Clean, lightly used. ')).toBeUndefined(); | ||
| expect(validatePrice(' 15.50 ')).toBeUndefined(); | ||
| expect(validateImages(['image-1'])).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('keeps invalid edits invalid until the value is actually corrected', () => { | ||
| expect(validateTitle('Hey')).toBe('Title must be at least 5 characters.'); | ||
| expect(validatePrice('.')).toBe('Enter a valid non-negative price.'); | ||
| }); | ||
|
|
||
| it('rejects titles longer than 100 characters', () => { | ||
| expect(validateTitle('a'.repeat(101))).toBe('Title must be 100 characters or less.'); | ||
| }); | ||
|
|
||
| it('rejects more than 8 photos', () => { | ||
| expect( | ||
| validateImages([ | ||
| 'image-1', | ||
| 'image-2', | ||
| 'image-3', | ||
| 'image-4', | ||
| 'image-5', | ||
| 'image-6', | ||
| 'image-7', | ||
| 'image-8', | ||
| 'image-9', | ||
| ]) | ||
| ).toBe('Maximum 8 photos allowed.'); | ||
| }); | ||
|
|
||
| it('removes cleared field keys so banner state reflects active errors only', () => { | ||
| const errors = validateListingFields({ | ||
| title: 'Hey', | ||
| description: '', | ||
| price: '', | ||
| images: [], | ||
| }); | ||
|
|
||
| const withoutTitle = upsertFieldError(errors, 'title', undefined); | ||
|
|
||
| expect(withoutTitle.title).toBeUndefined(); | ||
| expect(Object.prototype.hasOwnProperty.call(withoutTitle, 'title')).toBe(false); | ||
| expect(hasFieldErrors(withoutTitle)).toBe(true); | ||
|
|
||
| const noErrors = upsertFieldError( | ||
| upsertFieldError( | ||
| upsertFieldError(withoutTitle, 'description', undefined), | ||
| 'price', | ||
| undefined | ||
| ), | ||
| 'images', | ||
| undefined | ||
| ); | ||
|
|
||
| expect(hasFieldErrors(noErrors)).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.