Advertisement
Guest User

Untitled

a guest
May 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { DebugElement } from '@angular/core';
  2. import { async, ComponentFixture, TestBed } from '@angular/core/testing';
  3. import { By } from '@angular/platform-browser';
  4. import { ReactiveFormsModule, FormGroup, AbstractControl } from '@angular/forms';
  5. import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
  6. import { TextMaskModule } from 'angular2-text-mask';
  7. import { BoletoValidatorComponent } from './boleto-validator.component';
  8. import { TouchHelperService } from '@app/shared/_services/touch-helper.service';
  9. import { Boleto2Service } from '@app/boleto/services/boleto2.service';
  10. import { ApiService } from '@app/shared/_services/api.service';
  11. import { JwtService } from '@app/shared/_services/jwt.service';
  12. import { Observable, Subscriber } from 'rxjs';
  13.  
  14. export const submitMock = (type: string) => Observable.create((observer$: Subscriber<any>) => {
  15.   if (type == 'error') {
  16.     observer$.error(new Error('Internal Server Error'));
  17.   } else if (type == 'success') {
  18.     observer$.next({
  19.       codigo: 200,
  20.       dados: {
  21.         Banco: 'XPTO',
  22.         IdValidacao: 'xxxxxx',
  23.         IsCartaoCredito: false,
  24.         IsConsorcio: false,
  25.         LinhaDigitavel: '62390001172100004672843846528412477770000352984',
  26.         Validade: '22/01/2019',
  27.         Valor: 'R$ 3.529,84'
  28.       },
  29.       mensagem: null
  30.     });
  31.   }
  32.   observer$.complete();
  33. });
  34.  
  35. describe('Component: BoletoValidatorComponent', () => {
  36.   let fixture: ComponentFixture<BoletoValidatorComponent>;
  37.   let boletoValidator: BoletoValidatorComponent;
  38.   let debugElement: DebugElement;
  39.   let formBoleto: FormGroup;
  40.   let barcodeInput: AbstractControl;
  41.  
  42.   // setup
  43.   beforeEach(async(() => {
  44.     TestBed.configureTestingModule({
  45.       imports: [ ReactiveFormsModule, TextMaskModule, HttpClientTestingModule ],
  46.       declarations: [ BoletoValidatorComponent ],
  47.       providers: [ Boleto2Service, ApiService, JwtService, TouchHelperService ]
  48.     });
  49.  
  50.     fixture = TestBed.createComponent(BoletoValidatorComponent);
  51.     boletoValidator = fixture.componentInstance;
  52.     debugElement = fixture.debugElement;
  53.  
  54.     fixture.detectChanges();
  55.  
  56.     // set some elements
  57.     formBoleto = boletoValidator.formBoleto;
  58.     barcodeInput = formBoleto.get('barcode');
  59.   }));
  60.  
  61.   it('should create', () => {
  62.     expect(boletoValidator).toBeTruthy();
  63.   });
  64.  
  65.   it('should the button is blocked if do not fill the barcode input', () => {
  66.     const button = debugElement.query(By.css('.form__btn')).nativeElement;
  67.     barcodeInput.setValue('');
  68.     fixture.detectChanges();
  69.     expect(button.disabled).toBeTruthy();
  70.   });
  71.  
  72.   it('should the button is enable if fill the barcode input', () => {
  73.     const button = debugElement.query(By.css('.form__btn')).nativeElement;
  74.     barcodeInput.setValue('Linha Digitável');
  75.     fixture.detectChanges();
  76.     expect(button.disabled).toBeFalsy();
  77.   });
  78.  
  79.   it('should get error message if the boleto format it was invalid on form submission', () => {
  80.     barcodeInput.setValue('62390001172100004602514710550295174790000029519');
  81.     boletoValidator.submit(new Event(''));
  82.     fixture.detectChanges();
  83.  
  84.     expect(boletoValidator.errorMessage).toEqual('Boleto inválido, por favor verifique a digitação!');
  85.   });
  86.  
  87.   it('should hide invalid format error message when typing', () => {
  88.  
  89.     // force the error message to appear
  90.     barcodeInput.setValue('62390001172100004602514710550295174790000029519');
  91.     boletoValidator.submit(new Event(''));
  92.     fixture.detectChanges();
  93.  
  94.     // hide message when user typing
  95.     const inputElement = debugElement.query(By.css('.form__input'));
  96.     inputElement.triggerEventHandler('input', { target: inputElement.nativeElement });
  97.  
  98.     fixture.detectChanges();
  99.     fixture.whenStable().then(() => {
  100.       expect(boletoValidator.errorMessage).toBeUndefined();
  101.     });
  102.   });
  103.  
  104.   it('should get error message when the barcode input is already filled and empty', () => {
  105.     const inputElement = debugElement.query(By.css('.form__input'));
  106.  
  107.     inputElement.triggerEventHandler('input', { target: inputElement.nativeElement });
  108.     fixture.detectChanges();
  109.  
  110.     expect(boletoValidator.errorMessage).toEqual('Insirá o código de barras do boleto.');
  111.   });
  112.  
  113.   it('should show the error element when has error message', () => {
  114.     boletoValidator.errorMessage = 'Testing';
  115.     fixture.detectChanges();
  116.     expect(debugElement.query(By.css('.error-msg')).nativeElement).not.toBeNull();
  117.   });
  118.  
  119.   it('should call submit method when user submit the form', () => {
  120.     const submitSpy = spyOn(boletoValidator, 'submit');
  121.     debugElement.query(By.css('.boleto-validator__form')).triggerEventHandler('ngSubmit', null);
  122.     expect(submitSpy).toHaveBeenCalledTimes(1);
  123.   });
  124.  
  125.   it('should change the icon when get ERROR from server', () => {
  126.     const $icon = debugElement.query(By.css('.icon-boleto')).nativeElement;
  127.     submitMock('error').subscribe(null,
  128.       error => {
  129.         boletoValidator.errorMessage = 'Error Message';
  130.         fixture.detectChanges();
  131.         fixture.whenStable().then(() => {
  132.           expect($icon.classList.contains('icon-boleto--valid')).toBeFalsy();
  133.           expect($icon.classList.contains('icon-boleto--invalid')).toBeTruthy();
  134.         });
  135.       }
  136.     );
  137.   });
  138.  
  139.   it('should change the icon when get SUCCESS from server', () => {
  140.     const $icon = debugElement.query(By.css('.icon-boleto')).nativeElement;
  141.     submitMock('success').subscribe(
  142.       success => {
  143.         boletoValidator.boletoData = success.dados;
  144.  
  145.         fixture.detectChanges();
  146.         fixture.whenStable().then(() => {
  147.           expect($icon.classList.contains('icon-boleto--invalid')).toBeFalsy();
  148.           expect($icon.classList.contains('icon-boleto--valid')).toBeTruthy();
  149.         });
  150.       }
  151.     );
  152.   });
  153.  
  154.   it('should render success message when server returns success', () => {
  155.     submitMock('success').subscribe(
  156.       success => {
  157.         boletoValidator.boletoData = success.dados;
  158.         fixture.detectChanges();
  159.         fixture.whenStable().then(() =>
  160.           expect(debugElement.query(By.css('.form__success-info')).nativeElement).not.toBeNull()
  161.         );
  162.       }
  163.     );
  164.   });
  165. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement