-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add length validation to name fields and refactor error state logic
- Loading branch information
1 parent
c18b487
commit 6f95c86
Showing
11 changed files
with
183 additions
and
48 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
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
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
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,150 @@ | ||
|
||
describe('Show error state', () => { | ||
it('returns true when state is invalid', () => { | ||
const context = { | ||
messages: { | ||
rule_required: { | ||
value: 'Field is required.', | ||
visible: true | ||
} | ||
}, | ||
state: { | ||
invalid: true | ||
} | ||
}; | ||
|
||
const result = showErrorState(context); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns true when context contains at least 1 visible error message', () => { | ||
const context = { | ||
messages: { | ||
rule_required: { | ||
value: 'Field is required.', | ||
visible: false | ||
}, | ||
rule_length: { | ||
value: 'Invalid length.', | ||
visible: true | ||
} | ||
}, | ||
state: { | ||
invalid: true | ||
} | ||
}; | ||
|
||
const result = showErrorState(context); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('returns false when no visible error message is present', () => { | ||
const context = { | ||
messages: { | ||
rule_required: { | ||
value: 'Field is required.', | ||
visible: false | ||
} | ||
}, | ||
state: { | ||
invalid: true | ||
} | ||
}; | ||
|
||
const result = showErrorState(context); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
|
||
it('returns false when no error message is present', () => { | ||
const context = { | ||
messages: {}, | ||
state: { | ||
invalid: true | ||
} | ||
}; | ||
|
||
const result = showErrorState(context); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
|
||
it('returns false when error message is not visible', () => { | ||
const context = { | ||
messages: { | ||
rule_required: { | ||
value: 'Field is required.', | ||
visible: false | ||
} | ||
}, | ||
state: { | ||
invalid: true | ||
} | ||
}; | ||
|
||
const result = showErrorState(context); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
|
||
it('returns false when state is valid', () => { | ||
const context = { | ||
messages: {}, | ||
state: { | ||
invalid: false | ||
} | ||
}; | ||
|
||
const result = showErrorState(context); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('Get error message', () => { | ||
it('returns error message when context has error messages', () => { | ||
const context = { | ||
messages: { | ||
rule_required: { | ||
value: 'Field is required.', | ||
visible: true | ||
} | ||
} | ||
}; | ||
|
||
const result = getErrorMessage(context); | ||
|
||
expect(result).toBe('Field is required.'); | ||
}); | ||
|
||
it('returns undefined when context has no error messages', () => { | ||
const context = { | ||
messages: {} | ||
}; | ||
|
||
const result = getErrorMessage(context); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('returns first visible error message when context has multiple error messages', () => { | ||
const context = { | ||
messages: { | ||
rule_required: { | ||
value: 'Field is required.', | ||
visible: false | ||
}, | ||
rule_length: { | ||
value: 'Invalid length.', | ||
visible: true | ||
} | ||
} | ||
}; | ||
|
||
const result = getErrorMessage(context); | ||
|
||
expect(result).toBe('Invalid length.'); | ||
}); | ||
}); |
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,17 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function showErrorState(context: Record<string, any>): boolean { | ||
const messageKeys = Object.keys(context.messages) | ||
.filter(key => context.messages[key].visible); | ||
|
||
return context.state.invalid && | ||
messageKeys.length > 0 && | ||
context.messages[messageKeys[0]].visible; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function getErrorMessage(context: Record<string, any>): string | undefined { | ||
const messageKeys = Object.keys(context.messages) | ||
.filter(key => context.messages[key].visible); | ||
|
||
return messageKeys.length ? context.messages[messageKeys[0]].value : undefined; | ||
} |