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
|
import { Component } from '@angular/core';
import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Router, Event } from '@angular/router';
import { of, Observable } from 'rxjs';
import { delay } from 'rxjs/operators';
import { UserDialogComponent } from './user-dialog.component';
import { createMockInternalUserService } from '../internal-user-service/mock-internal-user-service';
import { InternalUserService, UserLoginState } from '../internal-user-service/internal-user.service';
@Component({
/* tslint:disable-next-line:component-selector*/
selector: 'mat-progress-spinner',
template: ''
})
class MatProgressSpinnerStubComponent { }
@Component({
/* tslint:disable-next-line:component-selector*/
selector: 'router-outlet',
template: ''
})
class RouterOutletStubComponent { }
describe('UserDialogComponent', () => {
let component: UserDialogComponent;
let fixture: ComponentFixture<UserDialogComponent>;
let mockInternalUserService: jasmine.SpyObj<InternalUserService>;
beforeEach(async(() => {
mockInternalUserService = createMockInternalUserService();
TestBed.configureTestingModule({
declarations: [UserDialogComponent, MatProgressSpinnerStubComponent, RouterOutletStubComponent],
providers: [{ provide: InternalUserService, useValue: mockInternalUserService },
{ // for the workaround
provide: Router, useValue: {
events: new Observable<Event>()
}
}]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(UserDialogComponent);
component = fixture.componentInstance;
});
it('progress spinner should work well', fakeAsync(() => {
mockInternalUserService.refreshAndGetUserState.and.returnValue(of(<UserLoginState>'nologin').pipe(delay(10)));
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('mat-progress-spinner'))).toBeTruthy();
tick(10);
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('mat-progress-spinner'))).toBeFalsy();
}));
it('nologin should work well', () => {
mockInternalUserService.refreshAndGetUserState.and.returnValue(of(<UserLoginState>'nologin'));
fixture.detectChanges();
expect(mockInternalUserService.refreshAndGetUserState).toHaveBeenCalled();
expect(mockInternalUserService.userRouteNavigate).toHaveBeenCalledWith(['login', { reason: 'nologin' }]);
});
it('invalid login should work well', () => {
mockInternalUserService.refreshAndGetUserState.and.returnValue(of(<UserLoginState>'invalidlogin'));
fixture.detectChanges();
expect(mockInternalUserService.refreshAndGetUserState).toHaveBeenCalled();
expect(mockInternalUserService.userRouteNavigate).toHaveBeenCalledWith(['login', { reason: 'invalidlogin' }]);
});
it('success should work well', () => {
mockInternalUserService.refreshAndGetUserState.and.returnValue(of(<UserLoginState>'success'));
fixture.detectChanges();
expect(mockInternalUserService.refreshAndGetUserState).toHaveBeenCalled();
expect(mockInternalUserService.userRouteNavigate).toHaveBeenCalledWith(['success', { reason: 'already' }]);
});
});
|