diff --git a/src/app/error.service.spec.ts b/src/app/error.service.spec.ts new file mode 100644 index 0000000..44dc01a --- /dev/null +++ b/src/app/error.service.spec.ts @@ -0,0 +1,138 @@ +import { HttpErrorResponse, HttpHeaders } from '@angular/common/http'; +import { HttpErrorHandler } from './error.service'; + +describe('HttpErrorHandler', () => { + let handler: HttpErrorHandler; + + beforeEach(() => { + handler = new HttpErrorHandler(); + spyOn(console, 'error'); + }); + + describe('createHandleError', () => { + it('should return a curried function bound to the service name', () => { + const handleError = handler.createHandleError('TestService'); + expect(typeof handleError).toBe('function'); + }); + }); + + describe('handleError', () => { + it('should handle a client-side ErrorEvent', (done) => { + const errorEvent = new ErrorEvent('Network error', { + message: 'simulated network error', + }); + const errorResponse = new HttpErrorResponse({ + error: errorEvent, + status: 0, + statusText: 'Unknown Error', + }); + + const errorFn = handler.handleError('TestService', 'testOp', []); + errorFn(errorResponse).subscribe({ + error: (msg) => { + expect(msg).toBe('simulated network error'); + expect(console.error).toHaveBeenCalledWith(errorResponse); + expect(console.error).toHaveBeenCalledWith( + 'TestService::testOp failed: simulated network error' + ); + done(); + }, + }); + }); + + it('should handle a server-side error with status code', (done) => { + const errorResponse = new HttpErrorResponse({ + error: 'Not Found', + status: 404, + statusText: 'Not Found', + }); + + const errorFn = handler.handleError('TestService', 'testOp', []); + errorFn(errorResponse).subscribe({ + error: (msg) => { + expect(msg).toBe('server returned code 404 with body "Not Found"'); + expect(console.error).toHaveBeenCalledWith(errorResponse); + done(); + }, + }); + }); + + it('should extract Spring MVC errorMessage from errors header', (done) => { + const headers = new HttpHeaders().set( + 'errors', + JSON.stringify([{ errorMessage: 'lastName must not be empty' }]) + ); + const errorResponse = new HttpErrorResponse({ + error: 'Bad Request', + status: 400, + statusText: 'Bad Request', + headers, + }); + + const errorFn = handler.handleError('TestService', 'testOp', []); + errorFn(errorResponse).subscribe({ + error: (msg) => { + expect(msg).toBe('lastName must not be empty'); + done(); + }, + }); + }); + + it('should fall back to default message when errors header has no errorMessage', (done) => { + const headers = new HttpHeaders().set( + 'errors', + JSON.stringify([{ field: 'lastName' }]) + ); + const errorResponse = new HttpErrorResponse({ + error: 'Bad Request', + status: 400, + statusText: 'Bad Request', + headers, + }); + + const errorFn = handler.handleError('TestService', 'testOp', []); + errorFn(errorResponse).subscribe({ + error: (msg) => { + expect(msg).toBe('server returned code 400 with body "Bad Request"'); + done(); + }, + }); + }); + + it('should fall back to default message when errors header is an empty array', (done) => { + const headers = new HttpHeaders().set('errors', JSON.stringify([])); + const errorResponse = new HttpErrorResponse({ + error: 'Bad Request', + status: 400, + statusText: 'Bad Request', + headers, + }); + + const errorFn = handler.handleError('TestService', 'testOp', []); + errorFn(errorResponse).subscribe({ + error: (msg) => { + expect(msg).toBe('server returned code 400 with body "Bad Request"'); + done(); + }, + }); + }); + + it('should use default parameter values when none are provided', (done) => { + const errorResponse = new HttpErrorResponse({ + error: 'Server Error', + status: 500, + statusText: 'Internal Server Error', + }); + + const errorFn = handler.handleError(); + errorFn(errorResponse).subscribe({ + error: (msg) => { + expect(console.error).toHaveBeenCalledWith( + '::operation failed: server returned code 500 with body "Server Error"' + ); + done(); + }, + }); + }); + }); +}); diff --git a/src/app/owners/owner.service.spec.ts b/src/app/owners/owner.service.spec.ts index af9eeac..b75501e 100644 --- a/src/app/owners/owner.service.spec.ts +++ b/src/app/owners/owner.service.spec.ts @@ -39,7 +39,6 @@ import { HttpErrorHandler } from '../error.service'; import { OwnerService } from './owner.service'; import { Owner } from './owner'; import { Type } from '@angular/core'; -import { defer } from 'rxjs'; describe('OwnerService', () => { let httpTestingController: HttpTestingController; @@ -171,29 +170,18 @@ describe('OwnerService', () => { expect(req.request.body).toEqual(null); }); - it('search for delete Owner', () => { - - const errorResponse = new HttpErrorResponse({ - error: '404 error', - status: 404, - statusText: 'Not Found' - }); - - httpClientSpy.get.and.returnValue(asyncError(errorResponse)); - - ownerService.getOwnerById(1).subscribe((owners) => { - fail('Should have failed with 404 error'), - (error: HttpErrorResponse) => { - expect(error.status).toEqual(404); - expect(error.error).toContain('404 error'); - }}); - - const req = httpTestingController.expectOne( - { method: 'GET', url:ownerService.entityUrl + '/1' }); + it('should handle error when getting owner by id', () => { + ownerService.getOwnerById(1).subscribe( + () => fail('expected an error, not owner'), + (errorMsg) => { + expect(errorMsg).toContain('404'); + } + ); - }); + const req = httpTestingController.expectOne( + ownerService.entityUrl + '/1' + ); + expect(req.request.method).toEqual('GET'); + req.flush('404 error', { status: 404, statusText: 'Not Found' }); + }); }); - -export function asyncError(errorObject: any) { - return defer(() => Promise.reject(errorObject)); -} diff --git a/src/app/pets/pet.service.spec.ts b/src/app/pets/pet.service.spec.ts index 69ef4a1..47b5503 100644 --- a/src/app/pets/pet.service.spec.ts +++ b/src/app/pets/pet.service.spec.ts @@ -1,43 +1,123 @@ -/* - * - * * Copyright 2016-2017 the original author or authors. - * * - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * - */ - -/* tslint:disable:no-unused-variable */ - - -/** - * @author Vitaliy Fedoriv - */ - -import { inject, TestBed, waitForAsync } from '@angular/core/testing'; -import {PetService} from './pet.service'; -import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; -import {HttpClient} from '@angular/common/http'; +import { TestBed } from '@angular/core/testing'; +import { + HttpClientTestingModule, + HttpTestingController, +} from '@angular/common/http/testing'; +import { HttpResponse } from '@angular/common/http'; +import { Type } from '@angular/core'; + +import { PetService } from './pet.service'; +import { Pet } from './pet'; +import { HttpErrorHandler } from '../error.service'; +import { environment } from '../../environments/environment'; describe('PetService', () => { + let httpTestingController: HttpTestingController; + let petService: PetService; + const entityUrl = environment.REST_API_URL + 'pets'; + + const testPet: Pet = { + id: 1, + name: 'Leo', + birthDate: '2010-09-07', + type: { id: 1, name: 'cat' }, + ownerId: 1, + owner: { + id: 1, + firstName: 'George', + lastName: 'Franklin', + address: '110 W. Liberty St.', + city: 'Madison', + telephone: '6085551023', + pets: [], + }, + visits: [], + }; + beforeEach(() => { TestBed.configureTestingModule({ - // Import the HttpClient mocking services imports: [HttpClientTestingModule], - providers: [PetService] + providers: [PetService, HttpErrorHandler], + }); + httpTestingController = TestBed.inject( + HttpTestingController as Type + ); + petService = TestBed.inject(PetService); + }); + + afterEach(() => { + httpTestingController.verify(); + }); + + it('should return expected pets (getPets)', () => { + const expectedPets: Pet[] = [testPet]; + + petService.getPets().subscribe( + (pets) => expect(pets).toEqual(expectedPets), + fail + ); + + const req = httpTestingController.expectOne(entityUrl); + expect(req.request.method).toEqual('GET'); + req.flush(expectedPets); + }); + + it('should return a pet by id (getPetById)', () => { + petService.getPetById(1).subscribe( + (pet) => expect(pet).toEqual(testPet), + fail + ); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('GET'); + req.flush(testPet); + }); + + it('should add a pet under the correct owner URL (addPet)', () => { + const newPet: Pet = { ...testPet, id: null }; + const ownersUrl = environment.REST_API_URL + 'owners/1/pets'; + + petService.addPet(newPet).subscribe( + (pet) => expect(pet).toEqual(newPet), + fail + ); + + const req = httpTestingController.expectOne(ownersUrl); + expect(req.request.method).toEqual('POST'); + expect(req.request.body).toEqual(newPet); + const expectedResponse = new HttpResponse({ + status: 201, + statusText: 'Created', + body: newPet, + }); + req.event(expectedResponse); + }); + + it('should update a pet (updatePet)', () => { + const updatedPet: Pet = { ...testPet, name: 'Leopold' }; + + petService.updatePet('1', updatedPet).subscribe( + (pet) => expect(pet).toEqual(updatedPet), + fail + ); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('PUT'); + expect(req.request.body).toEqual(updatedPet); + const expectedResponse = new HttpResponse({ + status: 204, + statusText: 'No Content', + body: updatedPet, }); + req.event(expectedResponse); }); - it('should ...', waitForAsync(inject([HttpTestingController], (petService: PetService, http: HttpClient) => { - expect(petService).toBeTruthy(); - }))); + it('should delete a pet (deletePet)', () => { + petService.deletePet('1').subscribe(); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('DELETE'); + expect(req.request.body).toEqual(null); + req.flush(null); + }); }); diff --git a/src/app/pettypes/pettype.service.spec.ts b/src/app/pettypes/pettype.service.spec.ts index 0105085..9c236a2 100644 --- a/src/app/pettypes/pettype.service.spec.ts +++ b/src/app/pettypes/pettype.service.spec.ts @@ -1,42 +1,109 @@ -/* - * - * * Copyright 2016-2017 the original author or authors. - * * - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * - */ - -/* tslint:disable:no-unused-variable */ - -/** - * @author Vitaliy Fedoriv - */ - -import { inject, TestBed, waitForAsync } from '@angular/core/testing'; -import {PetTypeService} from './pettype.service'; -import {HttpClient} from '@angular/common/http'; -import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; +import { + HttpClientTestingModule, + HttpTestingController, +} from '@angular/common/http/testing'; +import { HttpResponse } from '@angular/common/http'; +import { Type } from '@angular/core'; + +import { PetTypeService } from './pettype.service'; +import { PetType } from './pettype'; +import { HttpErrorHandler } from '../error.service'; +import { environment } from '../../environments/environment'; describe('PetTypeService', () => { + let httpTestingController: HttpTestingController; + let petTypeService: PetTypeService; + const entityUrl = environment.REST_API_URL + 'pettypes'; + + const testPetType: PetType = { + id: 1, + name: 'cat', + }; + beforeEach(() => { TestBed.configureTestingModule({ - // Import the HttpClient mocking services imports: [HttpClientTestingModule], - providers: [PetTypeService] + providers: [PetTypeService, HttpErrorHandler], + }); + httpTestingController = TestBed.inject( + HttpTestingController as Type + ); + petTypeService = TestBed.inject(PetTypeService); + }); + + afterEach(() => { + httpTestingController.verify(); + }); + + it('should return expected pet types (getPetTypes)', () => { + const expectedPetTypes: PetType[] = [testPetType]; + + petTypeService.getPetTypes().subscribe( + (petTypes) => expect(petTypes).toEqual(expectedPetTypes), + fail + ); + + const req = httpTestingController.expectOne(entityUrl); + expect(req.request.method).toEqual('GET'); + req.flush(expectedPetTypes); + }); + + it('should return a pet type by id (getPetTypeById)', () => { + petTypeService.getPetTypeById('1').subscribe( + (petType) => expect(petType).toEqual(testPetType), + fail + ); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('GET'); + req.flush(testPetType); + }); + + it('should add a pet type (addPetType)', () => { + const newPetType: PetType = { ...testPetType, id: null }; + + petTypeService.addPetType(newPetType).subscribe( + (petType) => expect(petType).toEqual(newPetType), + fail + ); + + const req = httpTestingController.expectOne(entityUrl); + expect(req.request.method).toEqual('POST'); + expect(req.request.body).toEqual(newPetType); + const expectedResponse = new HttpResponse({ + status: 201, + statusText: 'Created', + body: newPetType, + }); + req.event(expectedResponse); + }); + + it('should update a pet type (updatePetType)', () => { + const updatedPetType: PetType = { ...testPetType, name: 'dog' }; + + petTypeService.updatePetType('1', updatedPetType).subscribe( + (petType) => expect(petType).toEqual(updatedPetType), + fail + ); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('PUT'); + expect(req.request.body).toEqual(updatedPetType); + const expectedResponse = new HttpResponse({ + status: 204, + statusText: 'No Content', + body: updatedPetType, }); + req.event(expectedResponse); }); - it('should ...', waitForAsync(inject([HttpTestingController], (petTypeService: PetTypeService, http: HttpClient) => { - expect(petTypeService).toBeTruthy(); - }))); + it('should delete a pet type (deletePetType)', () => { + petTypeService.deletePetType('1').subscribe(); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('DELETE'); + expect(req.request.body).toEqual(null); + req.flush(null); + }); }); diff --git a/src/app/specialties/specialty.service.spec.ts b/src/app/specialties/specialty.service.spec.ts index fd008a3..15b7988 100644 --- a/src/app/specialties/specialty.service.spec.ts +++ b/src/app/specialties/specialty.service.spec.ts @@ -1,42 +1,109 @@ -/* - * - * * Copyright 2016-2017 the original author or authors. - * * - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * - */ - -/* tslint:disable:no-unused-variable */ - -/** - * @author Vitaliy Fedoriv - */ - -import { inject, TestBed, waitForAsync } from '@angular/core/testing'; -import {SpecialtyService} from './specialty.service'; -import {HttpClient} from '@angular/common/http'; -import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; +import { + HttpClientTestingModule, + HttpTestingController, +} from '@angular/common/http/testing'; +import { HttpResponse } from '@angular/common/http'; +import { Type } from '@angular/core'; + +import { SpecialtyService } from './specialty.service'; +import { Specialty } from './specialty'; +import { HttpErrorHandler } from '../error.service'; +import { environment } from '../../environments/environment'; describe('SpecialtyService', () => { + let httpTestingController: HttpTestingController; + let specialtyService: SpecialtyService; + const entityUrl = environment.REST_API_URL + 'specialties'; + + const testSpecialty: Specialty = { + id: 1, + name: 'radiology', + }; + beforeEach(() => { TestBed.configureTestingModule({ - // Import the HttpClient mocking services imports: [HttpClientTestingModule], - providers: [SpecialtyService] + providers: [SpecialtyService, HttpErrorHandler], + }); + httpTestingController = TestBed.inject( + HttpTestingController as Type + ); + specialtyService = TestBed.inject(SpecialtyService); + }); + + afterEach(() => { + httpTestingController.verify(); + }); + + it('should return expected specialties (getSpecialties)', () => { + const expectedSpecialties: Specialty[] = [testSpecialty]; + + specialtyService.getSpecialties().subscribe( + (specialties) => expect(specialties).toEqual(expectedSpecialties), + fail + ); + + const req = httpTestingController.expectOne(entityUrl); + expect(req.request.method).toEqual('GET'); + req.flush(expectedSpecialties); + }); + + it('should return a specialty by id (getSpecialtyById)', () => { + specialtyService.getSpecialtyById('1').subscribe( + (specialty) => expect(specialty).toEqual(testSpecialty), + fail + ); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('GET'); + req.flush(testSpecialty); + }); + + it('should add a specialty (addSpecialty)', () => { + const newSpecialty: Specialty = { ...testSpecialty, id: null }; + + specialtyService.addSpecialty(newSpecialty).subscribe( + (specialty) => expect(specialty).toEqual(newSpecialty), + fail + ); + + const req = httpTestingController.expectOne(entityUrl); + expect(req.request.method).toEqual('POST'); + expect(req.request.body).toEqual(newSpecialty); + const expectedResponse = new HttpResponse({ + status: 201, + statusText: 'Created', + body: newSpecialty, + }); + req.event(expectedResponse); + }); + + it('should update a specialty (updateSpecialty)', () => { + const updatedSpecialty: Specialty = { ...testSpecialty, name: 'surgery' }; + + specialtyService.updateSpecialty('1', updatedSpecialty).subscribe( + (specialty) => expect(specialty).toEqual(updatedSpecialty), + fail + ); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('PUT'); + expect(req.request.body).toEqual(updatedSpecialty); + const expectedResponse = new HttpResponse({ + status: 204, + statusText: 'No Content', + body: updatedSpecialty, }); + req.event(expectedResponse); }); - it('should ...', waitForAsync(inject([HttpTestingController], (specialtyService: SpecialtyService, http: HttpClient) => { - expect(specialtyService).toBeTruthy(); - }))); + it('should delete a specialty (deleteSpecialty)', () => { + specialtyService.deleteSpecialty('1').subscribe(); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('DELETE'); + expect(req.request.body).toEqual(null); + req.flush(null); + }); }); diff --git a/src/app/vets/vet.service.spec.ts b/src/app/vets/vet.service.spec.ts index adbb15e..aae85f9 100644 --- a/src/app/vets/vet.service.spec.ts +++ b/src/app/vets/vet.service.spec.ts @@ -1,42 +1,111 @@ -/* - * - * * Copyright 2016-2017 the original author or authors. - * * - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * - */ - -/* tslint:disable:no-unused-variable */ - -/** - * @author Vitaliy Fedoriv - */ - -import { inject, TestBed, waitForAsync } from '@angular/core/testing'; -import {VetService} from './vet.service'; -import {HttpClient} from '@angular/common/http'; -import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; +import { + HttpClientTestingModule, + HttpTestingController, +} from '@angular/common/http/testing'; +import { HttpResponse } from '@angular/common/http'; +import { Type } from '@angular/core'; + +import { VetService } from './vet.service'; +import { Vet } from './vet'; +import { HttpErrorHandler } from '../error.service'; +import { environment } from '../../environments/environment'; describe('VetService', () => { + let httpTestingController: HttpTestingController; + let vetService: VetService; + const entityUrl = environment.REST_API_URL + 'vets'; + + const testVet: Vet = { + id: 1, + firstName: 'James', + lastName: 'Carter', + specialties: [{ id: 1, name: 'radiology' }], + }; + beforeEach(() => { TestBed.configureTestingModule({ - // Import the HttpClient mocking services imports: [HttpClientTestingModule], - providers: [VetService] + providers: [VetService, HttpErrorHandler], + }); + httpTestingController = TestBed.inject( + HttpTestingController as Type + ); + vetService = TestBed.inject(VetService); + }); + + afterEach(() => { + httpTestingController.verify(); + }); + + it('should return expected vets (getVets)', () => { + const expectedVets: Vet[] = [testVet]; + + vetService.getVets().subscribe( + (vets) => expect(vets).toEqual(expectedVets), + fail + ); + + const req = httpTestingController.expectOne(entityUrl); + expect(req.request.method).toEqual('GET'); + req.flush(expectedVets); + }); + + it('should return a vet by id (getVetById)', () => { + vetService.getVetById('1').subscribe( + (vet) => expect(vet).toEqual(testVet), + fail + ); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('GET'); + req.flush(testVet); + }); + + it('should add a vet (addVet)', () => { + const newVet: Vet = { ...testVet, id: null }; + + vetService.addVet(newVet).subscribe( + (vet) => expect(vet).toEqual(newVet), + fail + ); + + const req = httpTestingController.expectOne(entityUrl); + expect(req.request.method).toEqual('POST'); + expect(req.request.body).toEqual(newVet); + const expectedResponse = new HttpResponse({ + status: 201, + statusText: 'Created', + body: newVet, + }); + req.event(expectedResponse); + }); + + it('should update a vet (updateVet)', () => { + const updatedVet: Vet = { ...testVet, firstName: 'Jim' }; + + vetService.updateVet('1', updatedVet).subscribe( + (vet) => expect(vet).toEqual(updatedVet), + fail + ); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('PUT'); + expect(req.request.body).toEqual(updatedVet); + const expectedResponse = new HttpResponse({ + status: 204, + statusText: 'No Content', + body: updatedVet, }); + req.event(expectedResponse); }); - it('should ...', waitForAsync(inject([HttpTestingController], (vetService: VetService, http: HttpClient) => { - expect(vetService).toBeTruthy(); - }))); + it('should delete a vet (deleteVet)', () => { + vetService.deleteVet('1').subscribe(); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('DELETE'); + expect(req.request.body).toEqual(null); + req.flush(null); + }); }); diff --git a/src/app/visits/visit.service.spec.ts b/src/app/visits/visit.service.spec.ts index da8c30e..67826df 100644 --- a/src/app/visits/visit.service.spec.ts +++ b/src/app/visits/visit.service.spec.ts @@ -1,42 +1,120 @@ -/* - * - * * Copyright 2016-2017 the original author or authors. - * * - * * Licensed under the Apache License, Version 2.0 (the "License"); - * * you may not use this file except in compliance with the License. - * * You may obtain a copy of the License at - * * - * * http://www.apache.org/licenses/LICENSE-2.0 - * * - * * Unless required by applicable law or agreed to in writing, software - * * distributed under the License is distributed on an "AS IS" BASIS, - * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * * See the License for the specific language governing permissions and - * * limitations under the License. - * - */ - -/* tslint:disable:no-unused-variable */ - -/** - * @author Vitaliy Fedoriv - */ - -import { inject, TestBed, waitForAsync } from '@angular/core/testing'; -import {VisitService} from './visit.service'; -import {HttpClient} from '@angular/common/http'; -import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; +import { TestBed } from '@angular/core/testing'; +import { + HttpClientTestingModule, + HttpTestingController, +} from '@angular/common/http/testing'; +import { HttpResponse } from '@angular/common/http'; +import { Type } from '@angular/core'; + +import { VisitService } from './visit.service'; +import { Visit } from './visit'; +import { HttpErrorHandler } from '../error.service'; +import { environment } from '../../environments/environment'; describe('VisitService', () => { + let httpTestingController: HttpTestingController; + let visitService: VisitService; + const entityUrl = environment.REST_API_URL + 'visits'; + + const testVisit: Visit = { + id: 1, + date: '2016-09-07', + description: 'rabies shot', + pet: { + id: 1, + name: 'Leo', + birthDate: '2010-09-07', + type: { id: 1, name: 'cat' }, + ownerId: 1, + owner: null, + visits: null, + }, + }; + beforeEach(() => { TestBed.configureTestingModule({ - // Import the HttpClient mocking services imports: [HttpClientTestingModule], - providers: [VisitService] + providers: [VisitService, HttpErrorHandler], + }); + httpTestingController = TestBed.inject( + HttpTestingController as Type + ); + visitService = TestBed.inject(VisitService); + }); + + afterEach(() => { + httpTestingController.verify(); + }); + + it('should return expected visits (getVisits)', () => { + const expectedVisits: Visit[] = [testVisit]; + + visitService.getVisits().subscribe( + (visits) => expect(visits).toEqual(expectedVisits), + fail + ); + + const req = httpTestingController.expectOne(entityUrl); + expect(req.request.method).toEqual('GET'); + req.flush(expectedVisits); + }); + + it('should return a visit by id (getVisitById)', () => { + visitService.getVisitById('1').subscribe( + (visit) => expect(visit).toEqual(testVisit), + fail + ); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('GET'); + req.flush(testVisit); + }); + + it('should add a visit under the correct owner/pet URL (addVisit)', () => { + const newVisit: Visit = { ...testVisit, id: null }; + const visitsUrl = environment.REST_API_URL + 'owners/1/pets/1/visits'; + + visitService.addVisit(newVisit).subscribe( + (visit) => expect(visit).toEqual(newVisit), + fail + ); + + const req = httpTestingController.expectOne(visitsUrl); + expect(req.request.method).toEqual('POST'); + expect(req.request.body).toEqual(newVisit); + const expectedResponse = new HttpResponse({ + status: 201, + statusText: 'Created', + body: newVisit, + }); + req.event(expectedResponse); + }); + + it('should update a visit (updateVisit)', () => { + const updatedVisit: Visit = { ...testVisit, description: 'updated desc' }; + + visitService.updateVisit('1', updatedVisit).subscribe( + (visit) => expect(visit).toEqual(updatedVisit), + fail + ); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('PUT'); + expect(req.request.body).toEqual(updatedVisit); + const expectedResponse = new HttpResponse({ + status: 204, + statusText: 'No Content', + body: updatedVisit, }); + req.event(expectedResponse); }); - it('should ...', waitForAsync(inject([HttpTestingController], (visitService: VisitService, http: HttpClient) => { - expect(visitService).toBeTruthy(); - }))); + it('should delete a visit (deleteVisit)', () => { + visitService.deleteVisit('1').subscribe(); + + const req = httpTestingController.expectOne(entityUrl + '/1'); + expect(req.request.method).toEqual('DELETE'); + expect(req.request.body).toEqual(null); + req.flush(null); + }); });