Advertisement
Guest User

Untitled

a guest
May 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { TestBed, inject, async } from '@angular/core/testing';
  2. import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
  3. import { Boleto2Service } from './boleto2.service';
  4. import { JwtService } from '@app/shared/_services/jwt.service';
  5. import { ApiService } from '@app/shared/_services/api.service';
  6. import { BoletoValidatorRequest } from '../model/boletoValidator';
  7.  
  8. describe('Service: Boleto2Service', () => {
  9.  
  10.   let url;
  11.  
  12.   beforeEach(() => {
  13.     TestBed.configureTestingModule({
  14.       imports: [ HttpClientTestingModule ],
  15.       providers: [ Boleto2Service , ApiService, JwtService ]
  16.     });
  17.  
  18.     url = 'https://localhost:3000/validar';
  19.  
  20.   });
  21.  
  22.   it('should be created', inject([Boleto2Service], (service: Boleto2Service) => {
  23.     expect(service).toBeTruthy();
  24.   }));
  25.  
  26.   it('should get boleto information', async(
  27.     inject([ HttpTestingController, Boleto2Service, ApiService ],
  28.     (httpMock: HttpTestingController, service: Boleto2Service, api: ApiService) => {
  29.       const mockSuccessResponse = {
  30.         'codigo': 200,
  31.         'dados': {
  32.           'Banco': 'XPTO',
  33.           'IdValidacao': 'xxxxxxxxx',
  34.           'IsCartaoCredito': false,
  35.           'IsConsorcio': false,
  36.           'LinhaDigitavel': '62390001172100004672843846528412477770000352984',
  37.           'Validade': '22/01/2019',
  38.           'Valor': 'R$ 3.529,84'
  39.         },
  40.         'mensagem': null
  41.       };
  42.  
  43.       const data: BoletoValidatorRequest = {
  44.         'LinhaDigitavel': '62390.00117 21000.046728 43846.528412 4 77770000352984',
  45.         'csrf-token': '70-5E-37-8F-51-8B-61-A0-DF-B3-F8-0B-64-53-22-17-32-19-01-E7-6C-FB-BD-E6-E7-A3-AF-E9-B7-64'
  46.       };
  47.  
  48.       service.ticketValidator(data).subscribe(result =>
  49.         expect(result).toBe(mockSuccessResponse)
  50.       );
  51.  
  52.       const requestMock = httpMock.expectOne({
  53.         url: url,
  54.         method: 'POST'
  55.       });
  56.  
  57.       requestMock.flush(mockSuccessResponse);
  58.  
  59.     }
  60.   )));
  61.  
  62.   it('should get error error Internal Server Error', (
  63.     inject([ HttpTestingController, Boleto2Service, ApiService ],
  64.       (httpMock: HttpTestingController, service: Boleto2Service, api: ApiService) => {
  65.  
  66.         const mockErrorResponse = {
  67.           'status': 500,
  68.           'statusText': 'Internal Server Error'
  69.         };
  70.  
  71.         const data: BoletoValidatorRequest = {
  72.           'LinhaDigitavel': '62390.00117 21000.046728 43846.528412 4 77770000352984',
  73.           'csrf-token': '70-5E-37-8F-51-8B-61-A0-DF-B3-F8-0B-64-53-22-17-32-19-01-E7-6C-FB-BD-E6-E7-A3-AF-E9-B7-64'
  74.         };
  75.  
  76.         service.ticketValidator(data).subscribe(null,
  77.           error => {
  78.             expect(error.status).toEqual(mockErrorResponse.status);
  79.             expect(error.statusText).toEqual(mockErrorResponse.statusText);
  80.           }
  81.         );
  82.  
  83.         const requestMock = httpMock.expectOne({
  84.           url: url,
  85.           method: 'POST'
  86.         });
  87.  
  88.         requestMock.flush(null, mockErrorResponse);
  89.  
  90.     }
  91.   )));
  92.  
  93. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement