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