aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/app/user/user-login
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline/ClientApp/src/app/user/user-login')
-rw-r--r--Timeline/ClientApp/src/app/user/user-login/user-login.component.spec.ts74
-rw-r--r--Timeline/ClientApp/src/app/user/user-login/user-login.component.ts6
2 files changed, 62 insertions, 18 deletions
diff --git a/Timeline/ClientApp/src/app/user/user-login/user-login.component.spec.ts b/Timeline/ClientApp/src/app/user/user-login/user-login.component.spec.ts
index 3d431ce7..9c9ee1dc 100644
--- a/Timeline/ClientApp/src/app/user/user-login/user-login.component.spec.ts
+++ b/Timeline/ClientApp/src/app/user/user-login/user-login.component.spec.ts
@@ -1,28 +1,33 @@
import { NO_ERRORS_SCHEMA } from '@angular/core';
-import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
+import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';
-import { createMockInternalUserService } from '../internal-user-service/mock-internal-user-service';
-import { createMockActivatedRoute } from '../mock-activated-route';
-import { UserLoginComponent, LoginEvent } from './user-login.component';
+import { of, throwError } from 'rxjs';
+
+import { createMockInternalUserService } from '../internal-user-service/internal-user.service.mock';
+import { MockActivatedRoute } from '../../test-utilities/activated-route.mock';
+import { UserLoginComponent } from './user-login.component';
import { InternalUserService } from '../internal-user-service/internal-user.service';
+import { UserInfo } from '../entities';
describe('UserLoginComponent', () => {
let component: UserLoginComponent;
let fixture: ComponentFixture<UserLoginComponent>;
let mockInternalUserService: jasmine.SpyObj<InternalUserService>;
+ let mockActivatedRoute: MockActivatedRoute;
beforeEach(async(() => {
mockInternalUserService = createMockInternalUserService();
+ mockActivatedRoute = new MockActivatedRoute();
TestBed.configureTestingModule({
declarations: [UserLoginComponent],
providers: [
- {provide: InternalUserService, useValue: mockInternalUserService},
- {provide: ActivatedRoute, useValue:} // TODO: custom route snapshot param later.
- ]
+ { provide: InternalUserService, useValue: mockInternalUserService },
+ { provide: ActivatedRoute, useValue: mockActivatedRoute }
+ ],
imports: [ReactiveFormsModule],
schemas: [NO_ERRORS_SCHEMA]
})
@@ -32,14 +37,16 @@ describe('UserLoginComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(UserLoginComponent);
component = fixture.componentInstance;
- fixture.detectChanges();
});
it('should create', () => {
+ fixture.detectChanges();
expect(component).toBeTruthy();
});
it('reactive form should work well', () => {
+ fixture.detectChanges();
+
const usernameInput = fixture.debugElement.query(By.css('input[type=text]')).nativeElement as HTMLInputElement;
const passwordInput = fixture.debugElement.query(By.css('input[type=password]')).nativeElement as HTMLInputElement;
@@ -56,16 +63,57 @@ describe('UserLoginComponent', () => {
});
});
- it('login event should work well', fakeAsync(() => {
- let userCredential: LoginEvent;
- component.login.subscribe((e: LoginEvent) => { userCredential = e; });
+ it('login should work well', () => {
fixture.detectChanges();
+
const mockValue = {
username: 'user',
password: 'user'
};
+
+ mockInternalUserService.tryLogin.withArgs(mockValue).and.returnValue(of(<UserInfo>{ username: 'user', roles: ['user'] }));
+
component.form.setValue(mockValue);
component.onLoginButtonClick();
- expect(userCredential).toEqual(mockValue);
- }));
+
+ expect(mockInternalUserService.tryLogin).toHaveBeenCalledWith(mockValue);
+ expect(mockInternalUserService.userRouteNavigate).toHaveBeenCalledWith(['success', { reason: 'login' }]);
+ });
+
+ describe('message display', () => {
+ it('nologin reason should display', () => {
+ mockActivatedRoute.pushSnapshotWithParamMap({ reason: 'nologin' });
+ fixture.detectChanges();
+ expect(component.message).toBe('nologin');
+ expect((fixture.debugElement.query(By.css('p.mat-body')).nativeElement as
+ HTMLParagraphElement).textContent).toBe('You haven\'t login.');
+ });
+
+ it('invalid login reason should display', () => {
+ mockActivatedRoute.pushSnapshotWithParamMap({ reason: 'invalidlogin' });
+ fixture.detectChanges();
+ expect(component.message).toBe('invalidlogin');
+ expect((fixture.debugElement.query(By.css('p.mat-body')).nativeElement as
+ HTMLParagraphElement).textContent).toBe('Your login is no longer valid.');
+ });
+
+ it('custom error message should display', () => {
+ const customMessage = 'custom message';
+
+ fixture.detectChanges();
+
+ const mockValue = {
+ username: 'user',
+ password: 'user'
+ };
+ mockInternalUserService.tryLogin.withArgs(mockValue).and.returnValue(throwError(new Error(customMessage)));
+ component.form.setValue(mockValue);
+ component.onLoginButtonClick();
+
+ fixture.detectChanges();
+ expect(component.message).toBe(customMessage);
+ expect((fixture.debugElement.query(By.css('p.mat-body')).nativeElement as
+ HTMLParagraphElement).textContent).toBe(customMessage);
+ });
+ });
});
diff --git a/Timeline/ClientApp/src/app/user/user-login/user-login.component.ts b/Timeline/ClientApp/src/app/user/user-login/user-login.component.ts
index 082f879c..79a788de 100644
--- a/Timeline/ClientApp/src/app/user/user-login/user-login.component.ts
+++ b/Timeline/ClientApp/src/app/user/user-login/user-login.component.ts
@@ -1,4 +1,4 @@
-import { Component, Output, OnInit, EventEmitter } from '@angular/core';
+import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
@@ -6,10 +6,6 @@ import { InternalUserService } from '../internal-user-service/internal-user.serv
export type LoginMessage = 'nologin' | 'invalidlogin' | string;
-export class LoginEvent {
- username: string;
- password: string;
-}
@Component({
selector: 'app-user-login',