angular4-testing/project-manager/src/app/login/login.component.spec.ts

51 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-04-04 04:14:07 +02:00
import {TestBed} from "@angular/core/testing";
import {LoginComponent} from "./login.component";
import {RouterTestingModule} from "@angular/router/testing";
import {LoginService} from "../services/login-service/login-service";
import {FormsModule} from "@angular/forms";
import {MockEmptyClass} from "../mocks/mock-empty-class";
2017-04-04 04:14:07 +02:00
import Spy = jasmine.Spy;
2017-04-16 23:21:41 +02:00
describe('Login Component UI Driven Form Test with Spy', () => {
2017-04-04 04:14:07 +02:00
beforeEach(() => {
TestBed.configureTestingModule({
imports: [FormsModule, RouterTestingModule.withRoutes([])],
declarations: [LoginComponent],
providers: [
{provide: LoginService, useClass: MockEmptyClass}
2017-04-04 04:14:07 +02:00
],
})
});
it('should be possible to request login', () => {
const inputUser = 'testuser';
const inputPass = 'testpass';
const loginFixture = TestBed.createComponent(LoginComponent);
const loginInstance = loginFixture.componentInstance;
const loginElement = loginFixture.nativeElement;
loginFixture.autoDetectChanges(true);
// Login ausfuellen und absenden
loginFixture.whenStable().then(() => {
2017-04-05 02:43:17 +02:00
// Spy anmelden, die eigentliche Methode wird nicht mehr aufgerufen
2017-04-04 04:14:07 +02:00
const spy = spyOn(loginInstance, 'login');
// Formular Eingabe
loginElement.querySelector('div /deep/ div > #inputEmail').value = inputUser;
loginElement.querySelector('div /deep/ div > #inputPassword').value = inputPass;
// Trigger Submit - nicht möglich über click() oder HTTPNode submit()
// daher in Comp '@ViewChild(NgForm)' benoetigt
loginInstance.form.ngSubmit.emit();
// Spy auswerten
expect(spy).toHaveBeenCalledWith(inputUser, inputPass);
});
});
});