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
+ } ) ;
0 commit comments