|
| 1 | +import {TestBed} from '@angular/core/testing'; |
| 2 | +import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; |
| 3 | +import {CoursesService} from './courses.service'; |
| 4 | +import {HttpClient, HttpErrorResponse} from '@angular/common/http'; |
| 5 | +import {COURSES} from '../../../server/db-data'; |
| 6 | + |
| 7 | + |
| 8 | +describe('CoursesService', () => { |
| 9 | + |
| 10 | + let httpClient: HttpClient; |
| 11 | + let httpTestingController: HttpTestingController; |
| 12 | + let coursesService: CoursesService; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + |
| 16 | + TestBed.configureTestingModule({ |
| 17 | + imports: [HttpClientTestingModule], |
| 18 | + providers: [ |
| 19 | + CoursesService |
| 20 | + ] |
| 21 | + }); |
| 22 | + |
| 23 | + httpClient = TestBed.get(HttpClient); |
| 24 | + httpTestingController = TestBed.get(HttpTestingController); |
| 25 | + coursesService = TestBed.get(CoursesService); |
| 26 | + |
| 27 | + }); |
| 28 | + |
| 29 | + |
| 30 | + it('should retrieve all courses', () => { |
| 31 | + |
| 32 | + coursesService.findAllCourses() |
| 33 | + .subscribe(courses => { |
| 34 | + |
| 35 | + expect(<any>courses).toBeTruthy('No value returned'); |
| 36 | + expect(courses.length).toBe(12, `number of courses not correct`); |
| 37 | + |
| 38 | + const course = courses.find(course => course.id == 12); |
| 39 | + |
| 40 | + expect(course.titles.description).toBe('Angular Testing Course', |
| 41 | + `The name of the course is incorrect`); |
| 42 | + |
| 43 | + }); |
| 44 | + |
| 45 | + const req = httpTestingController.expectOne('/api/courses'); |
| 46 | + expect(req.request.method).toEqual('GET'); |
| 47 | + |
| 48 | + req.flush({payload: Object.values(COURSES)}); |
| 49 | + |
| 50 | + }); |
| 51 | + |
| 52 | + |
| 53 | + it('should save a course', () => { |
| 54 | + |
| 55 | + coursesService.saveCourse(12, {titles: {description: 'Testing Course'}}) |
| 56 | + .subscribe(); |
| 57 | + |
| 58 | + const req = httpTestingController.expectOne('/api/courses/12'); |
| 59 | + expect(req.request.method).toEqual('PUT'); |
| 60 | + |
| 61 | + req.flush({}); |
| 62 | + |
| 63 | + }); |
| 64 | + |
| 65 | + |
| 66 | + it('should error the returned observable if request fails', () => { |
| 67 | + |
| 68 | + const errorMessage = 'Internal Server Error'; |
| 69 | + |
| 70 | + coursesService.saveCourse(12, {titles: {description: 'Testing Course'}}) |
| 71 | + .subscribe(() => { |
| 72 | + |
| 73 | + fail('The returned observable should have errored out.'); |
| 74 | + |
| 75 | + }, |
| 76 | + (error: HttpErrorResponse) => { |
| 77 | + expect(error.status).toEqual(500, 'status'); |
| 78 | + expect(error.error).toEqual(errorMessage, 'message'); |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + |
| 83 | + const req = httpTestingController.expectOne('/api/courses/12'); |
| 84 | + |
| 85 | + // Respond with mock error |
| 86 | + req.flush(errorMessage, {status: 500, statusText: 'Error'}); |
| 87 | + |
| 88 | + }); |
| 89 | + |
| 90 | + |
| 91 | + afterEach(() => { |
| 92 | + |
| 93 | + httpTestingController.verify(); |
| 94 | + |
| 95 | + }); |
| 96 | + |
| 97 | +}); |
0 commit comments