Added extra tests

This commit is contained in:
Thom Werring 2023-10-22 15:51:50 +02:00
parent 7aca1b737d
commit 6816187fee
4 changed files with 156 additions and 9 deletions

View file

@ -5,23 +5,27 @@ import { SkillsService } from "@/skills/skills.service";
import { ExperiencesService } from "@/experiences/experiences.service"; import { ExperiencesService } from "@/experiences/experiences.service";
import { SkillDto } from "@/skills/skills.types"; import { SkillDto } from "@/skills/skills.types";
import { ExperienceDto } from "@/experiences/experiences.types"; import { ExperienceDto } from "@/experiences/experiences.types";
import { EducationService } from "@/education/education.service";
import { EducationDto } from "@/education/education.types";
describe("AppController", () => { describe("AppController", () => {
let appController: AppController; let appController: AppController;
let appService: AppService; let appService: AppService;
let skillsService: SkillsService; let skillsService: SkillsService;
let experiencesService: ExperiencesService; let experiencesService: ExperiencesService;
let educationService: EducationService;
beforeEach(async () => { beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({ const app: TestingModule = await Test.createTestingModule({
controllers: [AppController], controllers: [AppController],
providers: [AppService, SkillsService, ExperiencesService], providers: [AppService, SkillsService, ExperiencesService, EducationService],
}).compile(); }).compile();
appController = app.get<AppController>(AppController); appController = app.get<AppController>(AppController);
appService = app.get<AppService>(AppService); appService = app.get<AppService>(AppService);
skillsService = app.get<SkillsService>(SkillsService); skillsService = app.get<SkillsService>(SkillsService);
experiencesService = app.get<ExperiencesService>(ExperiencesService); experiencesService = app.get<ExperiencesService>(ExperiencesService);
educationService = app.get<EducationService>(EducationService);
}); });
describe("root", () => { describe("root", () => {
@ -71,4 +75,25 @@ describe("AppController", () => {
expect(experiencesService.getMany).toBeCalled(); expect(experiencesService.getMany).toBeCalled();
}); });
}); });
describe("getEducation", () => {
it("Should return an Array of EducationDtos", () => {
const result: EducationDto[] = EducationDto.asDto([
{
institute: "Massachusetts Institute of Technology",
city: "Cambridge",
url: "https://www.mit.edu/",
course: "Neuroscience",
level: "PHD",
startDate: new Date(2023, 2),
endDate: new Date(2023, 3),
description: "Got my PHD in neuroscience",
},
]);
jest.spyOn(educationService, "getMany").mockImplementation(() => result);
expect(appController.getEducation()).toBe(result);
expect(educationService.getMany).toBeCalled();
});
});
}); });

View file

@ -1,5 +1,6 @@
import { Test, TestingModule } from "@nestjs/testing"; import { Test, TestingModule } from "@nestjs/testing";
import { EducationService } from "src/education/education.service"; import { EducationService } from "@/education/education.service";
import { education } from "@/education/education";
describe("SkillsService", () => { describe("SkillsService", () => {
let service: EducationService; let service: EducationService;
@ -9,6 +10,30 @@ describe("SkillsService", () => {
providers: [EducationService], providers: [EducationService],
}).compile(); }).compile();
education.splice(0);
education.push({
institute: "Oxford University",
city: "Oxford",
url: "https://www.ox.ac.uk/",
course: "English",
level: "Masters",
startDate: new Date(2023, 0),
endDate: new Date(2023, 1),
description: "Got my english degree",
});
education.push({
institute: "Massachusetts Institute of Technology",
city: "Cambridge",
url: "https://www.mit.edu/",
course: "Neuroscience",
level: "PHD",
startDate: new Date(2023, 2),
endDate: new Date(2023, 3),
description: "Got my PHD in neuroscience",
});
service = module.get<EducationService>(EducationService); service = module.get<EducationService>(EducationService);
}); });
@ -21,12 +46,20 @@ describe("SkillsService", () => {
expect(service.getMany).toBeDefined(); expect(service.getMany).toBeDefined();
}); });
it("should return an array", () => { it("should return an array with 2 entries", () => {
expect(Array.isArray(service.getMany())).toBe(true); const education = service.getMany();
expect(Array.isArray(education)).toBe(true);
expect(education.length).toBe(2);
}); });
it("should return an array", () => { it("should return an array with 1 entry", () => {
expect(Array.isArray(service.getMany())).toBe(true); const education = service.getMany({
level: "PHD",
});
expect(Array.isArray(education)).toBe(true);
expect(education.length).toBe(1);
}); });
}); });
}); });

View file

@ -1,6 +1,8 @@
import { Test, TestingModule } from "@nestjs/testing"; import { Test, TestingModule } from "@nestjs/testing";
import { ExperiencesService } from "./experiences.service"; import { ExperiencesService } from "./experiences.service";
import { SkillsService } from "@/skills/skills.service"; import { SkillsService } from "@/skills/skills.service";
import { experiences } from "@/experiences/experiences";
import { skills } from "@/skills/skills";
describe("ExperiencesService", () => { describe("ExperiencesService", () => {
let service: ExperiencesService; let service: ExperiencesService;
@ -10,10 +12,77 @@ describe("ExperiencesService", () => {
providers: [ExperiencesService, SkillsService], providers: [ExperiencesService, SkillsService],
}).compile(); }).compile();
experiences.splice(0);
experiences.push({
name: "Employer A",
city: "City A",
url: "https://example.com/",
jobTitle: "Tester",
startDate: new Date(2023, 0),
endDate: new Date(2023, 1),
description: `Description of work`,
skills: [
{
category: "Testing",
},
],
});
experiences.push({
name: "Employer B",
city: "City B",
url: "https://example.com/",
jobTitle: "Developer",
startDate: new Date(2023, 1),
endDate: new Date(2023, 2),
description: `Description of work`,
skills: [
{
category: "Programming",
},
],
});
skills.splice(0);
skills.push({
name: "Writing tests",
description: "Writing tests",
category: "Testing",
});
skills.push({
name: "TDD",
description: "Test Driven Development",
category: "Testing",
});
skills.push({
name: "OOP",
description: "Object Orientated Programming",
category: "Programming style",
});
service = module.get<ExperiencesService>(ExperiencesService); service = module.get<ExperiencesService>(ExperiencesService);
}); });
it("should be defined", () => { it("should be defined", () => {
expect(service).toBeDefined(); expect(service).toBeDefined();
}); });
describe("getMany method", () => {
it("should be defined", () => {
expect(service.getMany).toBeDefined();
});
it("should return an array", () => {
expect(Array.isArray(service.getMany())).toBe(true);
});
it("should return 2 entries", () => {
expect(service.getMany().length).toBe(2);
});
it("should return 1 entries with 2 skills", () => {
const experiences = service.getMany({ jobTitle: "Tester" });
expect(experiences.length).toBe(1);
expect(experiences[0].skills.length).toBe(2);
});
});
}); });

View file

@ -1,5 +1,6 @@
import { Test, TestingModule } from "@nestjs/testing"; import { Test, TestingModule } from "@nestjs/testing";
import { SkillsService } from "./skills.service"; import { SkillsService } from "./skills.service";
import { skills } from "@/skills/skills";
describe("SkillsService", () => { describe("SkillsService", () => {
let service: SkillsService; let service: SkillsService;
@ -9,6 +10,22 @@ describe("SkillsService", () => {
providers: [SkillsService], providers: [SkillsService],
}).compile(); }).compile();
skills.splice(0);
skills.push({
name: "Writing tests",
description: "Writing tests",
category: "Testing",
});
skills.push({
name: "TDD",
description: "Test Driven Development",
category: "Testing",
});
skills.push({
name: "OOP",
description: "Object Orientated Programming",
category: "Programming style",
});
service = module.get<SkillsService>(SkillsService); service = module.get<SkillsService>(SkillsService);
}); });
@ -20,13 +37,16 @@ describe("SkillsService", () => {
it("should be defined", () => { it("should be defined", () => {
expect(service.getMany).toBeDefined(); expect(service.getMany).toBeDefined();
}); });
it("should return an array", () => { it("should return an array", () => {
expect(Array.isArray(service.getMany())).toBe(true); expect(Array.isArray(service.getMany())).toBe(true);
}); });
it("should return an array", () => { it("should return 3 entries", () => {
expect(Array.isArray(service.getMany())).toBe(true); expect(service.getMany().length).toBe(3);
});
it("should return 2 entries", () => {
expect(service.getMany({ category: "Testing" }).length).toBe(2);
}); });
}); });
}); });