Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {TestBed} from "@angular/core/testing";
- import {WorkerService} from "./worker.service";
- import {Worker} from "./models/worker";
- import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing";
- import {HttpClient} from "@angular/common/http";
- import {HttpErrorHandler} from "../http-error-handler.service";
- describe('WorkerService (with mocks)', () => {
- let httpClient: HttpClient;
- let httpTestingController: HttpTestingController;
- let workerService: WorkerService;
- beforeEach(() => {
- TestBed.configureTestingModule({
- imports: [ HttpClientTestingModule ],
- providers: [ WorkerService, HttpErrorHandler ]
- });
- httpClient = TestBed.get(HttpClient);
- httpTestingController = TestBed.get(HttpTestingController);
- workerService = TestBed.get(WorkerService);
- });
- afterEach(() => {
- httpTestingController.verify();
- });
- describe('#getWrokersInDelegation', () => {
- let expectedWorkers: Worker[];
- beforeEach(() => {
- workerService = TestBed.get(WorkerService);
- expectedWorkers = [
- { id: 1, name: 'Adam', surname: 'Kowalski', country: 'Niemcy', city: 'Berlin', client: { name: 'Daleki', address: 'testowy'}, startDate: new Date(Date.UTC(2017, 1, 1, 14, 0, 0)), endDate: new Date(Date.UTC(2017, 2, 2, 15, 0, 0))},
- { id: 2, name: 'Piotr', surname: 'Nowak', country: 'Słowacja', city: 'Bratysława', client: { name: 'Daleki2', address: 'testowy2'}, startDate: new Date(Date.UTC(2017, 5, 4, 14, 0, 0)), endDate: new Date(Date.UTC(2017, 8, 4, 15, 0, 0))}
- ] as Worker[];
- });
- it('should return expected heroes (called once)', () => {
- workerService.getWrokersInDelegation().subscribe(
- workers => expect(workers).toEqual(expectedWorkers, 'should return expected workers'),
- fail
- );
- const req = httpTestingController.expectOne(workerService.getWorkersInDelegationUrl);
- expect(req.request.method).toEqual('GET');
- req.flush(expectedWorkers);
- });
- it('shuld be OK returning no heroes', () => {
- workerService.getWrokersInDelegation().subscribe(
- workers => expect(workers.length).toEqual(0, 'should have empty heroes array'),
- fail
- );
- const req = httpTestingController.expectOne(workerService.getWorkersInDelegationUrl);
- req.flush([]); // Respond with no workers
- });
- it('should turn 404 into a user-friendly error', () => {
- const msg = 'Coś poszło nie tak; Spróbuj ponownie później'
- workerService.getWrokersInDelegation().subscribe(
- wokrers => fail('expected to fail'),
- error => expect(error).toEqual(msg, 'should be the same message')
- );
- const req = httpTestingController.expectOne(workerService.getWorkersInDelegationUrl);
- //respond with 404 and the error message in the body
- req.flush(msg, { status: 404, statusText: 'Not found' });
- });
- })
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement