Guest User

Untitled

a guest
Nov 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. @Component({
  2. template: `
  3. <form [formGroup]="formGroup" (submit)="onSave()">
  4. Your name: <input type="text" formControlName="name">
  5. <button id="saveButton">Save</button>
  6. </form>
  7. `
  8. })
  9. export class FooComponent {
  10. public formGroup: FormGroup;
  11.  
  12. public onSave(): void {
  13. // save and route somewhere
  14. }
  15. }
  16.  
  17. describe('FooComponent', () => {
  18. let fixture, component, _router, routerSpy;
  19.  
  20. beforeAll(done => (async() => {
  21. TestBed.configureTestingModule({
  22. imports: [
  23. RouterTestingModule.withRoutes([]),
  24. FormsModule,
  25. ReactiveFormsModule
  26. ]
  27. });
  28.  
  29. fixture = TestBed.createComponent(FooComponent);
  30. component = fixture.componentInstance;
  31. _router = fixture.debugElement.injector.get(Router);
  32. routerSpy = spyOn(_router, 'navigate')
  33. fixture.detectChanges();
  34. })().then(done).catch(done.fail));
  35.  
  36. it('should save the form', () => {
  37. const saveButton = fixture.debugElement.query(By.css('#saveButton'));
  38. saveButton.triggerEventHandler('click', null);
  39. expect(routerSpy).toHaveBeenCalled();
  40.  
  41. // the test fails because the form is not actually submitted
  42. });
  43. });
  44.  
  45. <form [formGroup]="formGroup" (submit)="onSave()">
  46.  
  47. <button id="saveButton" (click)="onSave()">Save</button>
Add Comment
Please, Sign In to add comment