Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Added null checks to Grades type and tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
gannonprudhomme committed May 4, 2020
1 parent 0470fc5 commit 46a8d3d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
29 changes: 29 additions & 0 deletions autoscheduler/frontend/src/tests/types/Grades.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Grades from '../../types/Grades';
import { Indexable, fetchMock } from '../util';

const correctArgs: Indexable = {
gpa: 0.0, A: 0, B: 0, C: 0, D: 0, F: 0, I: 0, S: 0, Q: 0, X: 0,
};

const createGrades = jest.fn((args) => new Grades(fetchMock(args)));

describe('Grades type', () => {
describe('throws an error', () => {
const nonNullableProps = ['gpa', 'A', 'B', 'C', 'D', 'F', 'I', 'S', 'Q', 'X'];
test.each(nonNullableProps)('when any of the properties are null %s', (prop) => {
// arrange
const badArgs = { ...correctArgs };
badArgs[prop] = null;

// act/assert
expect(() => createGrades(badArgs)).toThrow();
});
});

describe('does not throw an error', () => {
test('when all of the props are not-null', () => {
// arrange/act/assert
expect(() => createGrades(correctArgs)).not.toThrow();
});
});
});
12 changes: 12 additions & 0 deletions autoscheduler/frontend/src/types/Grades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ export default class Grades {
Q: number;
X: number;
constructor(init: GradesInit) {
// None of the properties can be null
if (init.gpa == null) { throw Error(`Grades.gpa is invalid: ${init.gpa}`); }
if (init.A == null) { throw Error(`Grades.A is invalid: ${init.A}`); }
if (init.B == null) { throw Error(`Grades.B is invalid: ${init.B}`); }
if (init.C == null) { throw Error(`Grades.C is invalid: ${init.C}`); }
if (init.D == null) { throw Error(`Grades.D is invalid: ${init.D}`); }
if (init.F == null) { throw Error(`Grades.F is invalid: ${init.F}`); }
if (init.I == null) { throw Error(`Grades.I is invalid: ${init.I}`); }
if (init.S == null) { throw Error(`Grades.S is invalid: ${init.S}`); }
if (init.Q == null) { throw Error(`Grades.Q is invalid: ${init.Q}`); }
if (init.X == null) { throw Error(`Grades.X is invalid: ${init.X}`); }

Object.assign(this, init);
}
}

0 comments on commit 46a8d3d

Please sign in to comment.