Skip to content
Merged
348 changes: 348 additions & 0 deletions apps/backend/src/applications/application.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@
expect(repository.find).toHaveBeenCalled();
expect(result).toEqual(mockApplications);
});

it('should return an empty array if the repo returns one', async () => {
mockRepository.find.mockResolvedValue([]);

const result = await service.findAll();

expect(repository.find).toHaveBeenCalled();
expect(result).toEqual([]);
});

it('should pass along any repo errors without information loss', async () => {
mockRepository.find.mockRejectedValue(
new Error('There was a problem retrieving the info'),
);

await expect(service.findAll()).rejects.toThrow(
`There was a problem retrieving the info`,
);
});
});

describe('findById', () => {
Expand Down Expand Up @@ -115,6 +134,68 @@
where: { appId: nonExistentId },
});
});

// TODO: Address this in codebase so it passes.
// Note: Adding .skip for now so it doesn't confuse people in their develop then tests all pass work cycle
it.skip('should not return an application from the repo if the id is not the same as asked for', async () => {
const mockApplication: Application = {
appId: 1,
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Monday, Tuesday',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-456-7890',
school: School.HARVARD_MEDICAL_SCHOOL,
referred: false,
referredEmail: null,
weeklyHours: 20,
};

mockRepository.findOne.mockResolvedValue(mockApplication);

const result = await service.findById(10);

Check warning on line 160 in apps/backend/src/applications/application.service.spec.ts

View workflow job for this annotation

GitHub Actions / pre-deploy

'result' is assigned a value but never used

expect(repository.findOne).toHaveBeenCalledWith({ where: { appId: 10 } });
expect(repository.findOne).toThrow();
});

it('should handle returning an application with no changes when optional fields are ommitted', async () => {
const mockApplication: Application = {
appId: 1,
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Monday, Tuesday',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-456-7890',
school: School.HARVARD_MEDICAL_SCHOOL,
weeklyHours: 20,
};

mockRepository.findOne.mockResolvedValue(mockApplication);

const result = await service.findById(1);

expect(repository.findOne).toHaveBeenCalledWith({ where: { appId: 1 } });
expect(result).toEqual(mockApplication);
});

it('should pass along any repo errors without information loss', async () => {
mockRepository.findOne.mockRejectedValue(
new Error('There was a problem retrieving the info'),
);

await expect(service.findById(1)).rejects.toThrow(
new Error(`There was a problem retrieving the info`),
);
});
});

describe('create', () => {
Expand Down Expand Up @@ -147,6 +228,197 @@
expect(repository.save).toHaveBeenCalled();
expect(result).toEqual(savedApplication);
});

it('should pass along any repo errors without information loss', async () => {
mockRepository.save.mockRejectedValue(
new Error('There was a problem retrieving the info'),
);
const mockApplication: CreateApplicationDto = {
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Monday, Tuesday',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-456-7890',
school: School.HARVARD_MEDICAL_SCHOOL,
weeklyHours: 20,
};

await expect(service.create(mockApplication)).rejects.toThrow(
new Error(`There was a problem retrieving the info`),
);
});

// TODO: Address this in codebase so it passes.
// Note: Adding .skip for now so it doesn't confuse people in their develop then tests all pass work cycle
it.skip('should not accept an invalid daysAvailable that is not in days of the week', async () => {
const createApplicationDto: CreateApplicationDto = {
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Christmas, Thanksgiving',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-456-7890',
school: School.HARVARD_MEDICAL_SCHOOL,
referred: false,
referredEmail: null,
weeklyHours: 20,
};

const savedApplication: Application = {
appId: 1,
...createApplicationDto,
};

mockRepository.save.mockResolvedValue(savedApplication);
await expect(service.create(createApplicationDto)).rejects.toThrow();
});

// TODO: Address this in codebase so it passes.
// Note: Adding .skip for now so it doesn't confuse people in their develop then tests all pass work cycle
it.skip('should not accept a phone number that is too long', async () => {
const createApplicationDto: CreateApplicationDto = {
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Monday, Tuesday',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-456-78901231',
school: School.HARVARD_MEDICAL_SCHOOL,
referred: false,
referredEmail: null,
weeklyHours: 20,
};

const savedApplication: Application = {
appId: 1,
...createApplicationDto,
};

mockRepository.save.mockResolvedValue(savedApplication);
await expect(service.create(createApplicationDto)).rejects.toThrow();
});

// TODO: Address this in codebase so it passes.
// Note: Adding .skip for now so it doesn't confuse people in their develop then tests all pass work cycle
it.skip('should not accept a phone number that is too short', async () => {
const createApplicationDto: CreateApplicationDto = {
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Monday, Tuesday',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-4562',
school: School.HARVARD_MEDICAL_SCHOOL,
referred: false,
referredEmail: null,
weeklyHours: 20,
};

const savedApplication: Application = {
appId: 1,
...createApplicationDto,
};

mockRepository.save.mockResolvedValue(savedApplication);
await expect(service.create(createApplicationDto)).rejects.toThrow();
});

// TODO: Address this in codebase so it passes.
// Note: Adding .skip for now so it doesn't confuse people in their develop then tests all pass work cycle
it.skip('should not accept a phone number that is the right length but not in ###-###-#### format', async () => {
const createApplicationDto: CreateApplicationDto = {
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Monday, Tuesday',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-456-8-90',
school: School.HARVARD_MEDICAL_SCHOOL,
referred: false,
referredEmail: null,
weeklyHours: 20,
};

const savedApplication: Application = {
appId: 1,
...createApplicationDto,
};

mockRepository.save.mockResolvedValue(savedApplication);
await expect(service.create(createApplicationDto)).rejects.toThrow();
});

// TODO: Address this in codebase so it passes.
// Note: Adding .skip for now so it doesn't confuse people in their develop then tests all pass work cycle
it.skip('should not accept 0 weekly hours', async () => {
const createApplicationDto: CreateApplicationDto = {
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Monday, Tuesday',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-456-7890',
school: School.HARVARD_MEDICAL_SCHOOL,
referred: false,
referredEmail: null,
weeklyHours: 0,
};

const savedApplication: Application = {
appId: 1,
...createApplicationDto,
};

mockRepository.save.mockResolvedValue(savedApplication);
await expect(service.create(createApplicationDto)).rejects.toThrow();
});

// TODO: Address this in codebase so it passes.
// Note: Adding .skip for now so it doesn't confuse people in their develop then tests all pass work cycle
it.skip('should not accept negative weekly hours', async () => {
const createApplicationDto: CreateApplicationDto = {
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Monday, Tuesday',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-456-78901231',
school: School.HARVARD_MEDICAL_SCHOOL,
referred: false,
referredEmail: null,
weeklyHours: -5,
};

const savedApplication: Application = {
appId: 1,
...createApplicationDto,
};

mockRepository.save.mockResolvedValue(savedApplication);
await expect(service.create(createApplicationDto)).rejects.toThrow();
});
});

describe('update', () => {
Expand Down Expand Up @@ -242,6 +514,82 @@
});
expect(repository.save).not.toHaveBeenCalled();
});

it('should pass along any repo errors from retrieval without information loss when saving a new interest', async () => {
mockRepository.findOne.mockRejectedValue(
new Error('There was a problem retrieving the info'),
);

await expect(
service.update(1, { interest: InterestArea.HARM_REDUCTION }),
).rejects.toThrow(new Error(`There was a problem retrieving the info`));
});

it('should pass along any repo errors from retrieval without information loss when saving a new application status', async () => {
mockRepository.findOne.mockRejectedValue(
new Error('There was a problem retrieving the info'),
);

await expect(
service.update(1, { appStatus: AppStatus.IN_REVIEW }),
).rejects.toThrow(new Error(`There was a problem retrieving the info`));
});

it('should pass along any repo errors from saving the new info without information loss when saving a new interest', async () => {
const mockApplication: Application = {
appId: 1,
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Monday, Tuesday',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-456-7890',
school: School.HARVARD_MEDICAL_SCHOOL,
referred: false,
referredEmail: null,
weeklyHours: 20,
};

mockRepository.findOne.mockResolvedValue(mockApplication);
mockRepository.save.mockRejectedValue(
new Error('There was a problem retrieving the info'),
);

await expect(
service.update(1, { interest: InterestArea.HARM_REDUCTION }),
).rejects.toThrow(new Error(`There was a problem retrieving the info`));
});

it('should pass along any repo errors from saving the new info without information loss when saving a new application status', async () => {
const mockApplication: Application = {
appId: 1,
appStatus: AppStatus.APP_SUBMITTED,
daysAvailable: 'Monday, Tuesday',
experienceType: ExperienceType.BS,
fileUploads: [],
interest: InterestArea.NURSING,
license: null,
isInternational: false,
isLearner: false,
phone: '123-456-7890',
school: School.HARVARD_MEDICAL_SCHOOL,
referred: false,
referredEmail: null,
weeklyHours: 20,
};

mockRepository.findOne.mockResolvedValue(mockApplication);
mockRepository.save.mockRejectedValue(
new Error('There was a problem retrieving the info'),
);

await expect(
service.update(1, { appStatus: AppStatus.IN_REVIEW }),
).rejects.toThrow(new Error(`There was a problem retrieving the info`));
});
});

describe('delete', () => {
Expand Down
Loading
Loading