Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {TestBed} from "@angular/core/testing";
  2. import {WorkerService} from "./worker.service";
  3. import {Worker} from "./models/worker";
  4. import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing";
  5. import {HttpClient} from "@angular/common/http";
  6. import {HttpErrorHandler} from "../http-error-handler.service";
  7.  
  8. describe('WorkerService (with mocks)', () => {
  9.   let httpClient: HttpClient;
  10.   let httpTestingController: HttpTestingController;
  11.   let workerService: WorkerService;
  12.  
  13.   beforeEach(() => {
  14.     TestBed.configureTestingModule({
  15.       imports: [ HttpClientTestingModule ],
  16.       providers: [ WorkerService, HttpErrorHandler ]
  17.     });
  18.  
  19.     httpClient = TestBed.get(HttpClient);
  20.     httpTestingController = TestBed.get(HttpTestingController);
  21.     workerService = TestBed.get(WorkerService);
  22.   });
  23.  
  24.   afterEach(() => {
  25.     httpTestingController.verify();
  26.   });
  27.  
  28.  
  29.   describe('#getWrokersInDelegation', () => {
  30.     let expectedWorkers: Worker[];
  31.  
  32.     beforeEach(() => {
  33.       workerService = TestBed.get(WorkerService);
  34.       expectedWorkers = [
  35.         { 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))},
  36.         { 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))}
  37.       ] as Worker[];
  38.     });
  39.  
  40.     it('should return expected heroes (called once)', () => {
  41.       workerService.getWrokersInDelegation().subscribe(
  42.         workers => expect(workers).toEqual(expectedWorkers, 'should return expected workers'),
  43.         fail
  44.       );
  45.  
  46.       const req = httpTestingController.expectOne(workerService.getWorkersInDelegationUrl);
  47.       expect(req.request.method).toEqual('GET');
  48.  
  49.       req.flush(expectedWorkers);
  50.     });
  51.  
  52.     it('shuld be OK returning no heroes', () => {
  53.       workerService.getWrokersInDelegation().subscribe(
  54.         workers => expect(workers.length).toEqual(0, 'should have empty heroes array'),
  55.         fail
  56.       );
  57.  
  58.       const req = httpTestingController.expectOne(workerService.getWorkersInDelegationUrl);
  59.       req.flush([]); // Respond with no workers
  60.     });
  61.  
  62.     it('should turn 404 into a user-friendly error', () => {
  63.       const msg = 'Coś poszło nie tak; Spróbuj ponownie później'
  64.       workerService.getWrokersInDelegation().subscribe(
  65.         wokrers => fail('expected to fail'),
  66.         error => expect(error).toEqual(msg, 'should be the same message')
  67.       );
  68.  
  69.       const req = httpTestingController.expectOne(workerService.getWorkersInDelegationUrl);
  70.       //respond with 404 and the error message in the body
  71.       req.flush(msg, { status: 404, statusText: 'Not found' });
  72.     });
  73.   })
  74. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement