Skip to content

Commit b461870

Browse files
author
Rodrigo Lins
committed
fix TTA-83: fixing tests
1 parent 24d945e commit b461870

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

src/app/guards/login-guard/login.guard.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ describe('LoginGuard', () => {
4646

4747
it('can activate the route when user is logged-in on Production', () => {
4848
loginGuard.isProduction = true;
49-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(true));
50-
loginGuard.canActivate().subscribe(isLogin => {
51-
expect(isLogin).toEqual(true);
49+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
50+
loginGuard.canActivate().subscribe(canActivate => {
51+
expect(canActivate).toEqual(true);
5252
});
5353
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
5454
});
5555

56+
5657
it('can activate the route when user is logged-in Locally', () => {
5758
loginGuard.isProduction = false;
5859
spyOn(loginService, 'isLogin').and.returnValue(of(true));
@@ -64,10 +65,10 @@ describe('LoginGuard', () => {
6465

6566
it('can not active the route and is redirected to login if user is not logged-in on Production', inject([Router], (router: Router) => {
6667
loginGuard.isProduction = true;
67-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(false));
68+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
6869
spyOn(router, 'navigate').and.stub();
69-
loginGuard.canActivate().subscribe(isLogin => {
70-
expect(isLogin).toEqual(false);
70+
loginGuard.canActivate().subscribe(canActivate => {
71+
expect(canActivate).toEqual(false);
7172
});
7273
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
7374
expect(router.navigate).toHaveBeenCalledWith(['login']);

src/app/modules/login/login.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe('LoginComponent', () => {
8686
});
8787

8888
it('should sign up or login with google if is not logged-in into the app on Production', inject([Router], (router: Router) => {
89-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(false));
89+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
9090
spyOn(azureAdB2CService, 'setCookies').and.returnValue();
9191
spyOn(azureAdB2CService, 'signIn').and.returnValue(of(() => {}));
9292
spyOn(featureToggleCookiesService, 'setCookies').and.returnValue(featureToggleCookiesService.setCookies());
@@ -115,7 +115,7 @@ describe('LoginComponent', () => {
115115
}));
116116

117117
it('should not sign-up or login with google if is already logged-in into the app on Production', inject([Router], (router: Router) => {
118-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(true));
118+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
119119
spyOn(router, 'navigate').and.stub();
120120
component.login();
121121
expect(azureAdB2CService.isLogin).toHaveBeenCalled();

src/app/modules/login/services/azure.ad.b2c.service.spec.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import { HttpClientTestingModule } from '@angular/common/http/testing';
21
import { inject, TestBed } from '@angular/core/testing';
32
import { Account, UserAgentApplication } from 'msal';
43
import { AzureAdB2CService } from './azure.ad.b2c.service';
54
import { CookieService } from 'ngx-cookie-service';
6-
import { of } from 'rxjs';
7-
85

96
describe('AzureAdB2CService', () => {
107
let service: AzureAdB2CService;
@@ -13,7 +10,7 @@ describe('AzureAdB2CService', () => {
1310

1411
beforeEach(() => {
1512
TestBed.configureTestingModule({
16-
imports: [HttpClientTestingModule],
13+
imports: [],
1714
});
1815
service = TestBed.inject(AzureAdB2CService);
1916
cookieService = TestBed.inject(CookieService);
@@ -49,12 +46,12 @@ describe('AzureAdB2CService', () => {
4946
it('on logout should call msal logout and verify if user localStorage is removed', () => {
5047
spyOn(UserAgentApplication.prototype, 'logout').and.returnValue();
5148
spyOn(cookieService, 'deleteAll');
52-
spyOn(localStorage, 'clear');
49+
spyOn(localStorage, 'removeItem').withArgs('user');
5350

5451
service.logout();
5552

5653
expect(cookieService.deleteAll).toHaveBeenCalled();
57-
expect(localStorage.clear).toHaveBeenCalled();
54+
expect(localStorage.removeItem).toHaveBeenCalledWith('user');
5855
expect(UserAgentApplication.prototype.logout).toHaveBeenCalled();
5956
});
6057

@@ -87,54 +84,56 @@ describe('AzureAdB2CService', () => {
8784
});
8885

8986
it('isLogin returns true if UserAgentApplication has a defined Account and token cookie exist', () => {
87+
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
9088
spyOn(cookieService, 'check').and.returnValue(true);
91-
spyOn(service, 'isValidToken').and.returnValue(of(true));
9289

93-
service.isLogin().subscribe(isLogin => {
94-
expect(isLogin).toEqual(true);
95-
});
90+
const isLogin = service.isLogin();
91+
92+
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
93+
expect(cookieService.check).toHaveBeenCalled();
94+
expect(isLogin).toEqual(true);
9695
});
9796

9897
it('isLogin returns false if UserAgentApplication has a defined Account and token cookie does not exist', () => {
99-
spyOn(service, 'isValidToken').and.returnValue(of(false));
98+
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
99+
spyOn(cookieService, 'check').and.returnValue(false);
100100

101+
const isLogin = service.isLogin();
101102

102-
service.isLogin().subscribe(isLogin => {
103-
expect(isLogin).toEqual(false);
104-
});
103+
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
104+
expect(cookieService.check).toHaveBeenCalled();
105+
expect(isLogin).toEqual(false);
105106
});
106107

107108
it('isLogin returns false if UserAgentApplication has a null value for Account', () => {
108109
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);
109-
spyOn(service, 'isValidToken').and.returnValue(of(false));
110110

111-
service.isLogin().subscribe(isLogin => {
112-
expect(isLogin).toEqual(false);
113-
});
111+
const isLogin = service.isLogin();
112+
113+
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
114+
expect(isLogin).toEqual(false);
114115
});
115116

116117
it('setTenantId should save a tenantId in local storage', () => {
117118
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
118-
spyOn(service, 'isValidToken').and.returnValue(of(true));
119+
spyOn(cookieService, 'check').and.returnValue(true);
119120
spyOn(localStorage, 'setItem').withArgs('tenant_id', '12345');
120121

121-
service.isLogin().subscribe(isLogin => {
122-
expect(isLogin).toEqual(true);
123-
});
124-
122+
const isLogin = service.isLogin();
125123
service.setTenantId();
126124

127125
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
126+
expect(cookieService.check).toHaveBeenCalled();
127+
expect(isLogin).toEqual(true);
128128
expect(localStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
129129
});
130130

131131
it('setTenantId should not save tenantId if login is false ', () => {
132-
spyOn(service, 'isValidToken').and.returnValue(of(false));
132+
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);
133133
spyOn(localStorage, 'setItem');
134-
135-
service.isLogin().subscribe(isLogin => {
136-
expect(isLogin).toEqual(false);
137-
});
134+
const isLogin = service.isLogin();
135+
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
136+
expect(isLogin).toEqual(false);
138137
expect(localStorage.setItem).not.toHaveBeenCalled();
139138
});
140139

src/app/modules/shared/components/user/user.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('UserComponent', () => {
5757

5858
it('onInit checks if isLogin and gets the name and set tenantIn in the storage', () => {
5959
component.isProduction = true;
60-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(true));
60+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
6161
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');
6262
spyOn(azureAdB2CService, 'getUserEmail').and.returnValue('Email');
6363
spyOn(azureAdB2CService, 'setTenantId');
@@ -70,7 +70,7 @@ describe('UserComponent', () => {
7070

7171
it('onInit does not get the name if isLogin false', () => {
7272
component.isProduction = true;
73-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(false));
73+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
7474
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');
7575
spyOn(azureAdB2CService, 'getUserEmail').and.returnValue('Email');
7676
spyOn(azureAdB2CService, 'setTenantId');

src/app/modules/time-clock/pages/time-clock.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ describe('TimeClockComponent', () => {
115115

116116
it('onInit checks if isLogin and gets the userName', () => {
117117
component.isProduction = true;
118-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(true));
118+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
119119
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');
120120
component.ngOnInit();
121121
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
@@ -124,7 +124,7 @@ describe('TimeClockComponent', () => {
124124

125125
it('onInit does not get the name if isLogin false', () => {
126126
component.isProduction = true;
127-
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(false));
127+
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
128128
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');
129129
component.ngOnInit();
130130
expect(azureAdB2CService.isLogin).toHaveBeenCalled();

0 commit comments

Comments
 (0)