1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { of, throwError } from 'rxjs';
import { createMockInternalUserService } from '../internal-user-service/internal-user.service.mock';
import { UserLoginComponent } from './user-login.component';
import { InternalUserService } from '../internal-user-service/internal-user.service';
import { UserInfo } from '../entities';
import { MatCheckboxModule } from '@angular/material';
describe('UserLoginComponent', () => {
let component: UserLoginComponent;
let fixture: ComponentFixture<UserLoginComponent>;
let mockInternalUserService: jasmine.SpyObj<InternalUserService>;
beforeEach(async(() => {
mockInternalUserService = createMockInternalUserService();
// mock property
(<any>mockInternalUserService).currentUserInfo = null;
TestBed.configureTestingModule({
declarations: [UserLoginComponent],
providers: [
{ provide: InternalUserService, useValue: mockInternalUserService }
],
imports: [ReactiveFormsModule, MatCheckboxModule],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(UserLoginComponent);
component = fixture.componentInstance;
});
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;
const rememberMeCheckbox = fixture.debugElement.query(By.css('input[type=checkbox]')).nativeElement as HTMLInputElement;
usernameInput.value = 'user';
usernameInput.dispatchEvent(new Event('input'));
passwordInput.value = 'user';
passwordInput.dispatchEvent(new Event('input'));
rememberMeCheckbox.dispatchEvent(new MouseEvent('click'));
fixture.detectChanges();
expect(component.form.value).toEqual({
username: 'user',
password: 'user',
rememberMe: true
});
});
it('login should work well', () => {
fixture.detectChanges();
const mockValue = {
username: 'user',
password: 'user',
rememberMe: true
};
mockInternalUserService.tryLogin.withArgs(mockValue).and.returnValue(of(<UserInfo>{ username: 'user', roles: ['user'] }));
component.form.setValue(mockValue);
component.onLoginButtonClick();
expect(mockInternalUserService.tryLogin).toHaveBeenCalledWith(mockValue);
expect(mockInternalUserService.userRouteNavigate).toHaveBeenCalledWith(['success', { fromlogin: 'true' }]);
});
describe('message display', () => {
it('nologin reason should display', () => {
fixture.detectChanges();
component.message = 'nologin';
fixture.detectChanges();
expect((fixture.debugElement.query(By.css('p')).nativeElement as
HTMLParagraphElement).textContent).toBe('You haven\'t login.');
});
it('invalid login reason should display', () => {
fixture.detectChanges();
component.message = 'invalidlogin';
fixture.detectChanges();
expect((fixture.debugElement.query(By.css('p')).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',
rememberMe: false
};
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')).nativeElement as
HTMLParagraphElement).textContent).toBe(customMessage);
});
});
});
|