Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component({
- template: `
- <form [formGroup]="formGroup" (submit)="onSave()">
- Your name: <input type="text" formControlName="name">
- <button id="saveButton">Save</button>
- </form>
- `
- })
- export class FooComponent {
- public formGroup: FormGroup;
- public onSave(): void {
- // save and route somewhere
- }
- }
- describe('FooComponent', () => {
- let fixture, component, _router, routerSpy;
- beforeAll(done => (async() => {
- TestBed.configureTestingModule({
- imports: [
- RouterTestingModule.withRoutes([]),
- FormsModule,
- ReactiveFormsModule
- ]
- });
- fixture = TestBed.createComponent(FooComponent);
- component = fixture.componentInstance;
- _router = fixture.debugElement.injector.get(Router);
- routerSpy = spyOn(_router, 'navigate')
- fixture.detectChanges();
- })().then(done).catch(done.fail));
- it('should save the form', () => {
- const saveButton = fixture.debugElement.query(By.css('#saveButton'));
- saveButton.triggerEventHandler('click', null);
- expect(routerSpy).toHaveBeenCalled();
- // the test fails because the form is not actually submitted
- });
- });
- <form [formGroup]="formGroup" (submit)="onSave()">
- <button id="saveButton" (click)="onSave()">Save</button>
Add Comment
Please, Sign In to add comment