index.test.js

 avatar
unknown
javascript
4 years ago
6.2 kB
11
Indexable
const dayjs = require('dayjs');
const Person = require('./models/Person');
const Teacher = require('./models/Teacher');
const Student = require('./models/Student');

describe('Person', () => {
  test('should create a new Person object', () => {
    const person = new Person();
    expect(person).toBeTruthy();
  });

  describe('isAdult()', () => {
    const today = dayjs('2021-12-15').toISOString();

    test('should return true for 21-years-old', () => {
      const adultPerson = new Person({ dateOfBirth: dayjs('2000-11-12').toISOString() });
      expect(adultPerson.isAdult(today)).toEqual(true);
    });

    // edge case
    test('should return true for 18-years-old', () => {
      const adultPerson = new Person({ dateOfBirth: dayjs('2003-11-12').toISOString() });
      expect(adultPerson.isAdult(today)).toEqual(true);
    });

    // edge case
    test('should return false for 17-years-old, a week away from being an adult', () => {
      const teenPerson = new Person({ dateOfBirth: dayjs('2003-12-21').toISOString() });
      expect(teenPerson.isAdult(today)).toEqual(false);
    });

    test('should return false for 16-years-old', () => {
      const teenPerson = new Person({ dateOfBirth: dayjs('2005-11-12').toISOString() });
      expect(teenPerson.isAdult(today)).toEqual(false);
    });
  });

  describe('isFrom()', () => {
    const countryCode = 'BG';

    test(`should return true if from ${countryCode}`, () => {
      const localPerson = new Person({ birthplace: { city: 'Sofia', countryCode } })
      expect(localPerson.isFrom(countryCode)).toEqual(true);
    });

    test(`should return false if NOT from ${countryCode}`, () => {
      const southernPerson = new Person({ birthplace: { city: 'Sofia', countryCode: 'GR' } })
      expect(southernPerson.isFrom(countryCode)).toEqual(false);
    });
  });
});

describe('Teacher', () => {
  test('should create a new Teacher object', () => {
    const teacher = new Teacher();
    expect(teacher).toBeTruthy();
  });

  describe('isExpert()', () => {
    test('should return true for teacher with 7 years of XP', () => {
      const xpTeacher = new Teacher({ yearsOfExperience: 7 });
      expect(xpTeacher.isExpert()).toEqual(true);
    });

    // edge case
    test('should return true for teacher with 6 years of XP', () => {
      const xpTeacher = new Teacher({ yearsOfExperience: 6 });
      expect(xpTeacher.isExpert()).toEqual(true);
    });

    // edge case
    test('should return false for teacher with 5 years of XP', () => {
      const noobTeacher = new Teacher({ yearsOfExperience: 5 });
      expect(noobTeacher.isExpert()).toEqual(false);
    });

    test('should return false for teacher with 4 years of XP', () => {
      const noobTeacher = new Teacher({ yearsOfExperience: 4 });
      expect(noobTeacher.isExpert()).toEqual(false);
    });
  });

  test('performTest()', () => {
    const subject = 'math';
    const otherSubject = 'chemistry';
    const teacher = new Teacher({ subjectTaught: subject });
    const generateStudent = () => new Student({
      grades: {
        [subject]: [3, 4, 5],
        [otherSubject]: [2, 3]
      }
    });
    const students = new Array(3).fill(undefined).map(generateStudent);
    teacher.performTest(students);
    students.forEach(student => {
      expect(student.getGrades(subject).length).toEqual(4);
      expect(student.getGrades(otherSubject).length).toEqual(2);
    });
  });
});

describe('Student', () => {
  test('should create a new Student object', () => {
    const student = new Student();
    expect(student).toBeTruthy();
  });

  describe('isHighSchooler()', () => {
    test('should return true for 9-th grader', () => {
      const student = new Student({ grade: 9 });
      expect(student.isHighSchooler()).toEqual(true);
    });

    // edge case
    test('should return true for 8-th grader', () => {
      const student = new Student({ grade: 8 });
      expect(student.isHighSchooler()).toEqual(true);
    });

    // edge case
    test('should return true for 7-th grader', () => {
      const student = new Student({ grade: 7 });
      expect(student.isHighSchooler()).toEqual(false);
    });

    test('should return true for 6-th grader', () => {
      const student = new Student({ grade: 6 });
      expect(student.isHighSchooler()).toEqual(false);
    });
  });

  describe('demonstratesExcellence()', () => {
    test('should return true for student with only excellent marks', () => {
      const superExcellentStudent = new Student({
        grades: {
          math: [6, 6, 6],
          chemistry: [6, 6, 6]
        }
      });
      expect(superExcellentStudent.demonstratesExcellence()).toEqual(true);
    });

    // edge case
    test('should return true for student with 4 out of 5 excellent marks', () => {
      const excellentStudent = new Student({
        grades: {
          math: [6, 6],
          chemistry: [6, 5, 6]
        }
      });
      expect(excellentStudent.demonstratesExcellence()).toEqual(true);
    });

    test('should return false for student with 3 out of 5 excellent marks', () => {
      const almostExcellentStudent = new Student({
        grades: {
          math: [4, 6],
          chemistry: [6, 5, 6]
        }
      });
      expect(almostExcellentStudent.demonstratesExcellence()).toEqual(false);
    });

    test('should return false for student with no excellent marks', () => {
      const notExcellentStudent = new Student({
        grades: {
          math: [4, 3, 4],
          chemistry: [2, 3, 2]
        }
      });
      expect(notExcellentStudent.demonstratesExcellence()).toEqual(false);
    });
  });

  test('performTest()', () => {
    const subject = 'math';
    const otherSubject = 'chemistry';
    const student = new Student({
      grades: {
        [subject]: [5, 6, 6],
        [otherSubject]: [5, 5, 6, 5]
      }
    });
    student.performTest(subject);
    expect(student.getGrades(subject).length).toEqual(4);
    expect(student.getGrades(otherSubject).length).toEqual(4);
  });
});
Editor is loading...