Skip to content

Commit 1b0041a

Browse files
committed
Testing http requests with paramters
Testing service get method that contains query parameters.
1 parent 6ea9f19 commit 1b0041a

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import {
44
HttpClientTestingModule,
55
HttpTestingController,
66
} from "@angular/common/http/testing";
7-
import { COURSES } from "../../../../server/db-data";
7+
import { COURSES, findLessonsForCourse } from "../../../../server/db-data";
88
import { Course } from "../model/course";
99
import { HttpErrorResponse } from "@angular/common/http";
10+
import { toNumber } from "cypress/types/lodash";
1011

1112
describe("CoursesService", () => {
1213
let coursesService: CoursesService;
@@ -114,8 +115,37 @@ describe("CoursesService", () => {
114115
});
115116
});
116117

118+
// Testing Http Request with Parameters
119+
it("should find a list of lessons", () => {
120+
// Setting up the service call
121+
coursesService.findLessons(12).subscribe({
122+
next: (lessons) => {
123+
// Expectations
124+
// These are the expectations we expect to have when this request is completed
125+
expect(lessons).toBeTruthy();
126+
expect(lessons.length).toBe(3);
127+
},
128+
});
129+
130+
// Mocking the http request
131+
const request = httpTestingController.expectOne(
132+
(request) => request.url == "/api/lessons"
133+
);
117134

118-
135+
// Expecations for the request
136+
expect(request.request.method).toEqual("GET");
137+
// Check if the request params include the correct params
138+
expect(request.request.params.get("courseId")).toEqual("12");
139+
expect(request.request.params.get("filter")).toEqual("");
140+
expect(request.request.params.get("sortOrder")).toEqual("asc");
141+
expect(request.request.params.get("pageNumber")).toEqual("0");
142+
expect(request.request.params.get("pageSize")).toEqual("3");
143+
144+
// Trigger the request
145+
request.flush({
146+
payload: findLessonsForCourse(12).slice(0, 3),
147+
});
148+
});
119149

120150
// After each test this will be executed
121151
afterEach(() => {

0 commit comments

Comments
 (0)