Skip to content

Commit 6ea9f19

Browse files
committed
Save Courses and Failed Http Reqeust
New test cases for saving a course successfully and when a http request fails
1 parent fc756cc commit 6ea9f19

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/app/courses/services/courses.services.spec.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
HttpTestingController,
66
} from "@angular/common/http/testing";
77
import { COURSES } from "../../../../server/db-data";
8+
import { Course } from "../model/course";
9+
import { HttpErrorResponse } from "@angular/common/http";
810

911
describe("CoursesService", () => {
1012
let coursesService: CoursesService;
@@ -39,8 +41,86 @@ describe("CoursesService", () => {
3941
// We expect the request to be a GET request
4042
expect(request.request.method).toEqual("GET");
4143

44+
// Execute the http request
4245
// We should specify which data should be returned
4346
// This is some test data
4447
request.flush({ payload: Object.values(COURSES) });
4548
});
49+
50+
it("should return course based on id", () => {
51+
coursesService.findCourseById(12).subscribe({
52+
next: (course) => {
53+
// First we expect to get some data back
54+
expect(course).toBeTruthy("No courses returned");
55+
// Check the id of the course returned
56+
expect(course.id).toBe(12);
57+
},
58+
});
59+
// We expect one request to be sent at this endpoint
60+
const request = httpTestingController.expectOne("/api/courses/12");
61+
// We expect the request to be a GET request
62+
expect(request.request.method).toEqual("GET");
63+
// We pass some test data to the request to trigger the request
64+
request.flush(COURSES[12]);
65+
});
66+
67+
it("should save course data successfully", () => {
68+
const changes: Partial<Course> = {
69+
titles: { description: "Testing Course" },
70+
};
71+
72+
coursesService.saveCourse(12, changes).subscribe({
73+
next: (course) => {
74+
expect(course.id).toBe(12);
75+
},
76+
});
77+
// This will create a http put request
78+
const request = httpTestingController.expectOne("/api/courses/12");
79+
// validate the type of request
80+
expect(request.request.method).toEqual("PUT");
81+
// Here we are validating the body of the put request sent to the server
82+
// Check if the data being passed to the endpoint is the data we specified
83+
expect(request.request.body.titles.description).toBe(
84+
changes.titles.description
85+
);
86+
87+
// This triggers the mock request
88+
// The data we passed to the request it the mock data
89+
// Note the data is the course data with the new changes added
90+
request.flush({ ...COURSES[12], ...changes });
91+
});
92+
93+
it("should give error if save course fails", () => {
94+
const changes: Partial<Course> = {
95+
titles: { description: "Testing Course" },
96+
};
97+
98+
coursesService.saveCourse(12, changes).subscribe({
99+
next: () => {
100+
fail("save course operation should have failed");
101+
},
102+
error: (error: HttpErrorResponse) => {
103+
expect(error.status).toBe(500);
104+
},
105+
});
106+
107+
// Now we trigger the http request
108+
const request = httpTestingController.expectOne("/api/courses/12");
109+
expect(request.request.method).toEqual("PUT");
110+
111+
request.flush("Save course failed", {
112+
status: 500,
113+
statusText: "Internal Server Error",
114+
});
115+
});
116+
117+
118+
119+
120+
// After each test this will be executed
121+
afterEach(() => {
122+
// verify there are no more requests
123+
// We want to make sure no other requests are being triggered
124+
httpTestingController.verify();
125+
});
46126
});

0 commit comments

Comments
 (0)