-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser.component.spec.ts
More file actions
104 lines (96 loc) · 4.04 KB
/
user.component.spec.ts
File metadata and controls
104 lines (96 loc) · 4.04 KB
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
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
import { of } from 'rxjs';
import { UserComponent } from './user.component';
import { AzureAdB2CService } from '../../../login/services/azure.ad.b2c.service';
import {AppRoutingModule} from '../../../../app-routing.module';
import { LoginService } from '../../../login/services/login.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { SocialAuthService } from 'angularx-social-login';
describe('UserComponent', () => {
let component: UserComponent;
let fixture: ComponentFixture<UserComponent>;
let azureAdB2CService: AzureAdB2CService;
let loginService: LoginService;
const azureAdB2CServiceStub = {
isLogin() {
return of(true);
},
signIn() {
return of();
},
};
const loginServiceStub = {
isLogin() {
return of(true);
},
signIn() {
return of();
},
};
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['authState']);
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [UserComponent],
imports: [AppRoutingModule, HttpClientTestingModule],
providers: [
{ providers: AzureAdB2CService, useValue: azureAdB2CServiceStub },
{ providers: LoginService, useValue: loginServiceStub },
{ provide: SocialAuthService, useValue: socialAuthServiceStub }
],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(UserComponent);
component = fixture.componentInstance;
fixture.detectChanges();
loginService = TestBed.inject(LoginService);
azureAdB2CService = TestBed.inject(AzureAdB2CService);
});
it('component should be created', () => {
expect(component).toBeTruthy();
});
it('onInit checks if isLogin and gets the name and set tenantIn in the storage', () => {
component.isProduction = true;
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');
spyOn(azureAdB2CService, 'getUserEmail').and.returnValue('Email');
spyOn(azureAdB2CService, 'setTenantId');
component.ngOnInit();
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
expect(azureAdB2CService.getName).toHaveBeenCalled();
expect(azureAdB2CService.getUserEmail).toHaveBeenCalled();
expect(azureAdB2CService.setTenantId).toHaveBeenCalled();
});
it('onInit does not get the name if isLogin false', () => {
component.isProduction = true;
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');
spyOn(azureAdB2CService, 'getUserEmail').and.returnValue('Email');
spyOn(azureAdB2CService, 'setTenantId');
component.ngOnInit();
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
expect(azureAdB2CService.getName).toHaveBeenCalledTimes(0);
expect(azureAdB2CService.getUserEmail).toHaveBeenCalledTimes(0);
expect(azureAdB2CService.setTenantId).not.toHaveBeenCalled();
});
it('onInit checks if isLogin and gets the name and set tenantIn in the storage Locally', () => {
component.isProduction = false;
spyOn(loginService, 'isLogin').and.returnValue(of(true));
spyOn(loginService, 'getName').and.returnValue('Name');
spyOn(loginService, 'getUserEmail').and.returnValue('Email');
component.ngOnInit();
expect(loginService.isLogin).toHaveBeenCalled();
expect(loginService.getName).toHaveBeenCalled();
expect(loginService.getUserEmail).toHaveBeenCalled();
});
it('onInit does not get the name if isLogin false Locally', () => {
component.isProduction = false;
spyOn(loginService, 'isLogin').and.returnValue(of(false));
spyOn(loginService, 'getName').and.returnValue('Name');
spyOn(loginService, 'getUserEmail').and.returnValue('Email');
component.ngOnInit();
expect(loginService.isLogin).toHaveBeenCalled();
expect(loginService.getName).toHaveBeenCalledTimes(0);
expect(loginService.getUserEmail).toHaveBeenCalledTimes(0);
});
});