Skip to content

Commit 75bfc44

Browse files
committed
Status after Section 2
1 parent c013ad8 commit 75bfc44

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

debug.log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1124/085332.182:ERROR:directory_reader_win.cc(43)] FindFirstFile: Das System kann den angegebenen Pfad nicht finden. (0x3)
2+
[1124/093447.442:ERROR:directory_reader_win.cc(43)] FindFirstFile: Das System kann den angegebenen Pfad nicht finden. (0x3)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { TestBed } from "@angular/core/testing";
2+
import { CalculatorService } from "./calculator.service";
3+
import { LoggerService } from "./logger.service";
4+
5+
describe("CalculatorService", () => {
6+
let loggerSpy: SpyObject<LoggerService>
7+
let calculator: CalculatorService
8+
9+
beforeEach(async function () {
10+
loggerSpy = jasmine.createSpyObj("LoggerService", ["log"]);
11+
12+
TestBed.configureTestingModule({
13+
providers: [
14+
CalculatorService,
15+
{
16+
provide: LoggerService,
17+
useValue: loggerSpy,
18+
}
19+
]
20+
})
21+
22+
23+
calculator = TestBed.inject(CalculatorService);
24+
});
25+
26+
it("should add two numbers", async function () {
27+
const result = calculator.add(2, 2);
28+
29+
expect(result).toBe(4);
30+
expect(loggerSpy.log).toHaveBeenCalledTimes(1);
31+
});
32+
33+
it("should substract two numbers", async function () {
34+
const result = calculator.subtract(2, 2);
35+
36+
expect(result).toBe(0, "unexpected substraction result");
37+
expect(loggerSpy.log).toHaveBeenCalledTimes(1);
38+
});
39+
40+
});
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { HttpErrorResponse } from "@angular/common/http";
2+
import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing";
3+
import { TestBed } from "@angular/core/testing";
4+
import { COURSES, findLessonsForCourse, LESSONS } from "../../../../server/db-data";
5+
import { Course } from "../model/course";
6+
import { CoursesService } from "./courses.service";
7+
8+
fdescribe("CoursesService", function () {
9+
let coursesService: CoursesService;
10+
let httpTestingController: HttpTestingController;
11+
12+
13+
beforeEach(async function () {
14+
TestBed.configureTestingModule({
15+
imports: [HttpClientTestingModule],
16+
providers: [
17+
CoursesService,
18+
]
19+
20+
})
21+
22+
coursesService = TestBed.inject(CoursesService);
23+
httpTestingController = TestBed.inject(HttpTestingController)
24+
});
25+
26+
afterEach(() => {
27+
28+
// Assert no other request are made by using `verify`
29+
httpTestingController.verify();
30+
31+
})
32+
33+
34+
it("should retrieve all courses", async function () {
35+
coursesService.findAllCourses()
36+
.subscribe(courses => {
37+
expect(courses).toBeTruthy("No courses returned");
38+
39+
expect(courses.length).toBe(12, "Incorrect number of courses");
40+
41+
const course = courses.find(course => course.id === 12);
42+
expect(course.titles.description).toEqual("Angular Testing Course");
43+
})
44+
45+
const req = httpTestingController.expectOne("/api/courses");
46+
expect(req.request.method).toEqual("GET");
47+
48+
// This provides data to a request and will result in the "subscribe-block" being run
49+
// This will synchronously run the "subscribe-block"!!
50+
req.flush({ "payload": Object.values(COURSES) });
51+
});
52+
53+
it("should find a course by its id", async function () {
54+
coursesService.findCourseById(12)
55+
.subscribe(course => {
56+
expect(course).toBeTruthy();
57+
expect(course.id).toBe(12);
58+
})
59+
60+
const req = httpTestingController.expectOne("/api/courses/12");
61+
expect(req.request.method).toBe("GET");
62+
63+
req.flush(COURSES[12]);
64+
});
65+
66+
67+
it("should save the course data", async function () {
68+
const changes: Partial<Course> = { titles: { description: "Testing Course" } };
69+
coursesService.saveCourse(12, changes).subscribe(course => {
70+
expect(course.id).toBe(12)
71+
});
72+
73+
const req = httpTestingController.expectOne("/api/courses/12");
74+
75+
expect((req.request.method)).toBe("PUT");
76+
expect(req.request.body.titles.description).toEqual(changes.titles.description);
77+
78+
req.flush({
79+
...COURSES[12],
80+
...changes,
81+
});
82+
});
83+
84+
it("should give an error if save course fails", async function () {
85+
const changes: Partial<Course> = { titles: { description: "Testing Course" } };
86+
87+
coursesService.saveCourse(12, changes).subscribe(
88+
() => fail("The save course operation should have failed."),
89+
(error: HttpErrorResponse) => {
90+
expect(error.status).toBe(500);
91+
}
92+
);
93+
94+
const req = httpTestingController.expectOne("/api/courses/12");
95+
expect(req.request.method).toBe("PUT");
96+
97+
req.flush("Save course failed", {
98+
status: 500,
99+
statusText: "Internal Server Error",
100+
});
101+
});
102+
103+
it("should find a list of lessons", async function () {
104+
coursesService.findLessons(12)
105+
.subscribe(lessons => {
106+
expect(lessons).toBeTruthy();
107+
108+
expect(lessons.length).toBe(3, "Wrong number of lessons returned");
109+
});
110+
111+
112+
const req = httpTestingController.expectOne(req => req.url === "/api/lessons");
113+
expect(req.request.method).toBe("GET");
114+
115+
expect(req.request.params.get("courseId")).toEqual("12");
116+
expect(req.request.params.get("filter")).toEqual("");
117+
expect(req.request.params.get("sortOrder")).toEqual("asc");
118+
expect(req.request.params.get("pageNumber")).toEqual("0");
119+
expect(req.request.params.get("pageSize")).toEqual("3", "Wrong param pageSize");
120+
121+
req.flush({
122+
payload: findLessonsForCourse(12).slice(0, 3),
123+
});
124+
});
125+
126+
});

src/typings.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ declare var module: NodeModule;
33
interface NodeModule {
44
id: string;
55
}
6+
7+
type SpyObject<T extends {}> = {
8+
[K in keyof T]: T[K] extends (...args: any[]) => any ? jasmine.Spy<T[K]> : T[K]
9+
}

0 commit comments

Comments
 (0)