|
5 | 5 | HttpTestingController,
|
6 | 6 | } from "@angular/common/http/testing";
|
7 | 7 | import { COURSES } from "../../../../server/db-data";
|
| 8 | +import { Course } from "../model/course"; |
| 9 | +import { HttpErrorResponse } from "@angular/common/http"; |
8 | 10 |
|
9 | 11 | describe("CoursesService", () => {
|
10 | 12 | let coursesService: CoursesService;
|
@@ -39,8 +41,86 @@ describe("CoursesService", () => {
|
39 | 41 | // We expect the request to be a GET request
|
40 | 42 | expect(request.request.method).toEqual("GET");
|
41 | 43 |
|
| 44 | + // Execute the http request |
42 | 45 | // We should specify which data should be returned
|
43 | 46 | // This is some test data
|
44 | 47 | request.flush({ payload: Object.values(COURSES) });
|
45 | 48 | });
|
| 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 | + }); |
46 | 126 | });
|
0 commit comments