Skip to content
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

feat: adjusted grade field validation #381

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
exports[`AdjustedGradeInput component render snapshot 1`] = `
<span>
<Form.Control
max=""
min="0"
name="adjustedGradeValue"
onChange={[MockFunction hook.onChange]}
type="text"
type="number"
value="test-value"
/>
some-hint-text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ const useAdjustedGradeInputData = () => {
const hintText = possibleGrade && ` ${getLocalizedSlash()} ${possibleGrade}`;

const onChange = ({ target }) => {
setModalState({ adjustedGradeValue: target.value });
let adjustedGradeValue;
switch (true) {
case target.value < 0:
adjustedGradeValue = 0;
break;
case possibleGrade && target.value > possibleGrade:
adjustedGradeValue = possibleGrade;
break;
default:
adjustedGradeValue = target.value;
}
setModalState({ adjustedGradeValue });
};

return {
value,
onChange,
hintText,
possibleGrade,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,26 @@ describe('useAdjustedGradeInputData hook', () => {
});
});
describe('onChange', () => {
it('sets modal state with event target value', () => {
const testValue = 'test-value';
it('sets modal state with event target value when it is less than possibleGrade', () => {
const testValue = possibleGrade - 1;
out.onChange({ target: { value: testValue } });
expect(setModalState).toHaveBeenCalledWith({ adjustedGradeValue: testValue });
});
it('sets modal state with event target value when it is equal to possibleGrade', () => {
const testValue = possibleGrade;
out.onChange({ target: { value: testValue } });
expect(setModalState).toHaveBeenCalledWith({ adjustedGradeValue: testValue });
});
it('sets modal state to possibleGrade when event target value is greater than possibleGrade', () => {
const testValue = possibleGrade + 1;
out.onChange({ target: { value: testValue } });
expect(setModalState).toHaveBeenCalledWith({ adjustedGradeValue: possibleGrade });
});
it('sets modal state to 0 when event target value is less than 0', () => {
const testValue = -1;
out.onChange({ target: { value: testValue } });
expect(setModalState).toHaveBeenCalledWith({ adjustedGradeValue: 0 });
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ export const AdjustedGradeInput = () => {
value,
onChange,
hintText,
possibleGrade,
} = useAdjustedGradeInputData();
return (
<span>
<Form.Control
type="text"
type="number"
name="adjustedGradeValue"
min="0"
max={possibleGrade || ''}
value={value}
onChange={onChange}
/>
Expand Down
Loading