Skip to content

Commit c3fe1e6

Browse files
committed
Section 2: services
1 parent f4cfd27 commit c3fe1e6

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { COURSES, findLessonsForCourse } from "./../../../../server/db-data";
2+
import { TestBed } from "@angular/core/testing";
3+
import {
4+
HttpClientTestingModule,
5+
HttpTestingController,
6+
} from "@angular/common/http/testing";
7+
import { CoursesService } from "./courses.service";
8+
import { Course } from "../model/course";
9+
import { HttpErrorResponse } from "@angular/common/http";
10+
11+
describe("CoursesService", () => {
12+
let courseService: CoursesService;
13+
let httpTestingController: HttpTestingController;
14+
15+
beforeEach(() => {
16+
TestBed.configureTestingModule({
17+
imports: [HttpClientTestingModule],
18+
providers: [CoursesService],
19+
});
20+
21+
courseService = TestBed.inject(CoursesService);
22+
httpTestingController = TestBed.inject(HttpTestingController);
23+
});
24+
25+
afterEach(() => {
26+
httpTestingController.verify();
27+
});
28+
29+
it("should retrieve all courses", () => {
30+
courseService.findAllCourses().subscribe({
31+
next: (courses) => {
32+
expect(courses).toBeTruthy("No courses returned");
33+
expect(courses.length).toBe(12, "Incorrect number of courses");
34+
const course = courses.find((course) => course.id == 12);
35+
expect(course.titles.description).toBe("Angular Testing Course");
36+
},
37+
});
38+
39+
const req = httpTestingController.expectOne("/api/courses");
40+
expect(req.request.method).toEqual("GET");
41+
req.flush({ payload: Object.values(COURSES) });
42+
});
43+
44+
it("sould find a course by id", () => {
45+
courseService.findCourseById(12).subscribe({
46+
next: (course) => {
47+
expect(course).toBeTruthy("No course found");
48+
expect(course.id).toBe(12);
49+
expect(course.titles.description).toBe("Angular Testing Course");
50+
},
51+
});
52+
53+
const req = httpTestingController.expectOne("/api/courses/12");
54+
expect(req.request.method).toEqual("GET");
55+
req.flush(COURSES[12]);
56+
});
57+
58+
it("should save the course data", () => {
59+
const changes: Partial<Course> = {
60+
titles: { description: "Testing course" },
61+
};
62+
courseService.saveCourse(12, changes).subscribe({
63+
next: (course) => {
64+
expect(course.id).toBe(12);
65+
},
66+
});
67+
68+
const req = httpTestingController.expectOne("/api/courses/12");
69+
expect(req.request.method).toEqual("PUT");
70+
expect(req.request.body.titles.description).toEqual(
71+
changes.titles.description
72+
);
73+
74+
req.flush({
75+
...COURSES[12],
76+
...changes,
77+
});
78+
});
79+
80+
it("sould give an error if save course fails", () => {
81+
const changes: Partial<Course> = {
82+
titles: { description: "Testing course" },
83+
};
84+
85+
courseService.saveCourse(12, changes).subscribe({
86+
next: () => {
87+
fail("The save course should have failed");
88+
},
89+
error: (error: HttpErrorResponse) => {
90+
expect(error.status).toBe(500);
91+
},
92+
});
93+
const req = httpTestingController.expectOne("/api/courses/12");
94+
expect(req.request.method).toEqual("PUT");
95+
expect(req.request.body.titles.description).toEqual(
96+
changes.titles.description
97+
);
98+
99+
req.flush("Save course failed", {
100+
status: 500,
101+
statusText: "InternalServerError",
102+
});
103+
});
104+
105+
it("should find a list of lessons", () => {
106+
courseService.findLessons(12).subscribe({
107+
next: (lessons) => {
108+
expect(lessons).toBeTruthy();
109+
expect(lessons.length).toBe(3);
110+
},
111+
});
112+
113+
const req = httpTestingController.expectOne(
114+
(req) => req.url === "/api/lessons"
115+
);
116+
117+
expect(req.request.method).toEqual("GET");
118+
119+
expect(req.request.params.get("courseId")).toEqual("12");
120+
expect(req.request.params.get("filter")).toEqual("");
121+
expect(req.request.params.get("sortOrder")).toEqual("asc");
122+
expect(req.request.params.get("pageNumber")).toEqual("0");
123+
expect(req.request.params.get("pageSize")).toEqual("3");
124+
125+
req.flush({
126+
payload: findLessonsForCourse(12).slice(0, 3),
127+
});
128+
});
129+
});

0 commit comments

Comments
 (0)