Skip to content
Prev Previous commit
Next Next commit
fix TTA-83: fixing tests
  • Loading branch information
Rodrigo Lins committed Aug 5, 2022
commit b461870dec0b779e533e0b40adbef0a24e599737
13 changes: 7 additions & 6 deletions src/app/guards/login-guard/login.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ describe('LoginGuard', () => {

it('can activate the route when user is logged-in on Production', () => {
loginGuard.isProduction = true;
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(true));
loginGuard.canActivate().subscribe(isLogin => {
expect(isLogin).toEqual(true);
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
loginGuard.canActivate().subscribe(canActivate => {
expect(canActivate).toEqual(true);
});
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
});


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

it('can not active the route and is redirected to login if user is not logged-in on Production', inject([Router], (router: Router) => {
loginGuard.isProduction = true;
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(false));
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
spyOn(router, 'navigate').and.stub();
loginGuard.canActivate().subscribe(isLogin => {
expect(isLogin).toEqual(false);
loginGuard.canActivate().subscribe(canActivate => {
expect(canActivate).toEqual(false);
});
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
expect(router.navigate).toHaveBeenCalledWith(['login']);
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/login/login.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('LoginComponent', () => {
});

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

it('should not sign-up or login with google if is already logged-in into the app on Production', inject([Router], (router: Router) => {
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(true));
spyOn(azureAdB2CService, 'isLogin').and.returnValue(true);
spyOn(router, 'navigate').and.stub();
component.login();
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
Expand Down
55 changes: 27 additions & 28 deletions src/app/modules/login/services/azure.ad.b2c.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { inject, TestBed } from '@angular/core/testing';
import { Account, UserAgentApplication } from 'msal';
import { AzureAdB2CService } from './azure.ad.b2c.service';
import { CookieService } from 'ngx-cookie-service';
import { of } from 'rxjs';


describe('AzureAdB2CService', () => {
let service: AzureAdB2CService;
Expand All @@ -13,7 +10,7 @@ describe('AzureAdB2CService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
imports: [],
});
service = TestBed.inject(AzureAdB2CService);
cookieService = TestBed.inject(CookieService);
Expand Down Expand Up @@ -49,12 +46,12 @@ describe('AzureAdB2CService', () => {
it('on logout should call msal logout and verify if user localStorage is removed', () => {
spyOn(UserAgentApplication.prototype, 'logout').and.returnValue();
spyOn(cookieService, 'deleteAll');
spyOn(localStorage, 'clear');
spyOn(localStorage, 'removeItem').withArgs('user');

service.logout();

expect(cookieService.deleteAll).toHaveBeenCalled();
expect(localStorage.clear).toHaveBeenCalled();
expect(localStorage.removeItem).toHaveBeenCalledWith('user');
expect(UserAgentApplication.prototype.logout).toHaveBeenCalled();
});

Expand Down Expand Up @@ -87,54 +84,56 @@ describe('AzureAdB2CService', () => {
});

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

service.isLogin().subscribe(isLogin => {
expect(isLogin).toEqual(true);
});
const isLogin = service.isLogin();

expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(cookieService.check).toHaveBeenCalled();
expect(isLogin).toEqual(true);
});

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

const isLogin = service.isLogin();

service.isLogin().subscribe(isLogin => {
expect(isLogin).toEqual(false);
});
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(cookieService.check).toHaveBeenCalled();
expect(isLogin).toEqual(false);
});

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

service.isLogin().subscribe(isLogin => {
expect(isLogin).toEqual(false);
});
const isLogin = service.isLogin();

expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(isLogin).toEqual(false);
});

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

service.isLogin().subscribe(isLogin => {
expect(isLogin).toEqual(true);
});

const isLogin = service.isLogin();
service.setTenantId();

expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(cookieService.check).toHaveBeenCalled();
expect(isLogin).toEqual(true);
expect(localStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
});

it('setTenantId should not save tenantId if login is false ', () => {
spyOn(service, 'isValidToken').and.returnValue(of(false));
spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);
spyOn(localStorage, 'setItem');

service.isLogin().subscribe(isLogin => {
expect(isLogin).toEqual(false);
});
const isLogin = service.isLogin();
expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
expect(isLogin).toEqual(false);
expect(localStorage.setItem).not.toHaveBeenCalled();
});

Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/shared/components/user/user.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('UserComponent', () => {

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

it('onInit does not get the name if isLogin false', () => {
component.isProduction = true;
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(false));
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');
spyOn(azureAdB2CService, 'getUserEmail').and.returnValue('Email');
spyOn(azureAdB2CService, 'setTenantId');
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/time-clock/pages/time-clock.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('TimeClockComponent', () => {

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

it('onInit does not get the name if isLogin false', () => {
component.isProduction = true;
spyOn(azureAdB2CService, 'isLogin').and.returnValue(of(false));
spyOn(azureAdB2CService, 'isLogin').and.returnValue(false);
spyOn(azureAdB2CService, 'getName').and.returnValue('Name');
component.ngOnInit();
expect(azureAdB2CService.isLogin).toHaveBeenCalled();
Expand Down