Skip to content

Commit 70abc5b

Browse files
fix: TTA-141 Fix broken tests and test coverage in the UI (#926)
Co-authored-by: Jimmy Jaramillo <[email protected]>
1 parent 6486c51 commit 70abc5b

File tree

8 files changed

+85
-17
lines changed

8 files changed

+85
-17
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import { FeatureToggleCookiesService } from '../shared/feature-toggles/feature-t
88
import { LoginService } from './services/login.service';
99
import { HttpClientTestingModule } from '@angular/common/http/testing';
1010
import { SocialAuthService } from 'angularx-social-login';
11+
import { UserService } from '../user/services/user.service';
12+
1113

1214
describe('LoginComponent', () => {
1315
let component: LoginComponent;
1416
let fixture: ComponentFixture<LoginComponent>;
1517
let azureAdB2CService: AzureAdB2CService;
1618
let loginService: LoginService;
19+
let userService: UserService;
1720
let featureToggleCookiesService: FeatureToggleCookiesService;
1821

1922
const azureAdB2CServiceStub = {
@@ -38,6 +41,16 @@ describe('LoginComponent', () => {
3841
}
3942
};
4043

44+
const userTest = {
45+
name: 'user',
46+
47+
roles: ['admin'],
48+
groups: ['admin'],
49+
id: 'user_id',
50+
tenant_id: 'tenant_test',
51+
deleted: 'no',
52+
};
53+
4154
const featureToggleCookiesServiceStub = {
4255
setCookies() {
4356
return null;
@@ -66,6 +79,7 @@ describe('LoginComponent', () => {
6679
fixture.detectChanges();
6780
azureAdB2CService = TestBed.inject(AzureAdB2CService);
6881
loginService = TestBed.inject(LoginService);
82+
userService = TestBed.inject(UserService);
6983
featureToggleCookiesService = TestBed.inject(FeatureToggleCookiesService);
7084
});
7185

@@ -90,6 +104,7 @@ describe('LoginComponent', () => {
90104
spyOn(azureAdB2CService, 'setCookies').and.returnValue();
91105
spyOn(azureAdB2CService, 'signIn').and.returnValue(of(() => {}));
92106
spyOn(azureAdB2CService, 'getUserId').and.returnValue('userId_123');
107+
spyOn(userService, 'loadUser').withArgs('userId_123').and.returnValue(of(userTest));
93108
spyOn(featureToggleCookiesService, 'setCookies').and.returnValue(featureToggleCookiesService.setCookies());
94109

95110
component.login();
@@ -98,6 +113,7 @@ describe('LoginComponent', () => {
98113
expect(azureAdB2CService.setCookies).toHaveBeenCalled();
99114
expect(azureAdB2CService.getUserId).toHaveBeenCalled();
100115
expect(featureToggleCookiesService.setCookies).toHaveBeenCalled();
116+
expect(userService.loadUser).toHaveBeenCalledWith('userId_123');
101117
}));
102118

103119
it('should not sign-up or login with google if is already logged-in into the app on Production', inject([Router], (router: Router) => {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ export class LoginComponent implements OnInit {
8989
this.azureAdB2CService.signIn().subscribe(() => {
9090
this.featureToggleCookiesService.setCookies();
9191
this.azureAdB2CService.setCookies();
92-
const userId = this.azureAdB2CService.getUserId()
92+
const userId = this.azureAdB2CService.getUserId();
9393
this.userService.loadUser(userId).subscribe((user) => {
94-
const user_groups = {
94+
const userGroups = {
9595
groups: user.groups
96-
}
97-
this.loginService.setLocalStorage('user', JSON.stringify(user_groups));
96+
};
97+
this.loginService.setLocalStorage('user', JSON.stringify(userGroups));
9898
this.router.navigate(['']);
99-
})
99+
});
100100
});
101101
}
102102
}

src/app/modules/login/services/login.service.spec.ts

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
1+
import { HttpClient } from '@angular/common/http';
2+
import { HttpClientTestingModule } from '@angular/common/http/testing';
23
import { TestBed } from '@angular/core/testing';
34
import { JwtHelperService } from '@auth0/angular-jwt';
45
import { SocialAuthService } from 'angularx-social-login';
@@ -9,11 +10,11 @@ import { LoginService } from './login.service';
910

1011
describe('LoginService', () => {
1112
let service: LoginService;
12-
let httpMock: HttpTestingController;
1313
let cookieService: CookieService;
1414
let socialAuthService: SocialAuthService;
1515
let account;
1616
const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['signOut', 'signIn']);
17+
const httpClientSpy = jasmine.createSpyObj('HttpClient', ['post', 'get']);
1718
const cookieStoreStub = {};
1819
const helper = new JwtHelperService();
1920
const getAccountInfo = () => {
@@ -26,11 +27,11 @@ describe('LoginService', () => {
2627
providers: [
2728
{ providers: CookieService, useValue: cookieStoreStub },
2829
{ provide: SocialAuthService, useValue: socialAuthServiceStub },
30+
{ provide: HttpClient, useValue: httpClientSpy }
2931
],
3032
});
3133
service = TestBed.inject(LoginService);
3234
cookieService = TestBed.inject(CookieService);
33-
httpMock = TestBed.inject(HttpTestingController);
3435
socialAuthService = TestBed.inject(SocialAuthService);
3536
account = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6ImFiYyIsIm5hbWUiOiJhYmMiLCJlbWFpbCI6ImFiYyIsImdyb3VwcyI6WyJhYmMiXX0.UNxyDT8XzXJhI1F3LySBU7TJlpENPUPHj8my7Obw2ZM';
3637
let store = {};
@@ -49,6 +50,7 @@ describe('LoginService', () => {
4950
spyOn(localStorage, 'setItem').and.callFake(mockLocalStorage.setItem);
5051
spyOn(localStorage, 'clear').and.callFake(mockLocalStorage.clear);
5152
localStorage.setItem('user', account);
53+
localStorage.setItem('user2', '"test_token_123"');
5254
});
5355

5456
it('should be created', () => {
@@ -90,12 +92,16 @@ describe('LoginService', () => {
9092
});
9193

9294
it('load a user by sending a token using POST', () => {
95+
const token = 'test_123';
9396
service.baseUrl = '/users';
94-
service.getUser('token').subscribe();
95-
96-
const loadUserRequest = httpMock.expectOne(`${service.baseUrl}/login`);
97-
expect(loadUserRequest.request.method).toBe('POST');
98-
});
97+
const mockSuccessDataPost = {
98+
SUCCESS: true,
99+
data: {}
100+
};
101+
httpClientSpy.post.and.returnValue(of(mockSuccessDataPost));
102+
service.getUser(token).subscribe();
103+
expect(httpClientSpy.post).toHaveBeenCalled();
104+
});
99105

100106
it('should return true when user is Login', () => {
101107
spyOn(cookieService, 'check').and.returnValue(true);
@@ -122,4 +128,40 @@ describe('LoginService', () => {
122128
expect(localStorage.clear).toHaveBeenCalled();
123129
expect(cookieService.deleteAll).toHaveBeenCalled();
124130
});
131+
132+
it('should call cookieService when app is isLegacyProd', () => {
133+
service.isLegacyProd = true;
134+
service.localStorageKey = 'user2';
135+
spyOn(cookieService, 'check').and.returnValue(true);
136+
spyOn(service, 'isValidToken').and.returnValue(of(true));
137+
service.isLogin().subscribe(isLogin => {
138+
expect(cookieService.check).toHaveBeenCalled();
139+
});
140+
});
141+
142+
it('should call JSON parse when app is isLegacyProd', () => {
143+
spyOn(JSON, 'parse').and.returnValue('test_user_123');
144+
service.isLegacyProd = true;
145+
service.localStorageKey = 'user2';
146+
service.getUserId();
147+
service.getName();
148+
service.getUserEmail();
149+
service.getUserGroup();
150+
expect(JSON.parse).toHaveBeenCalled();
151+
});
152+
153+
it('should call setLocalStorage when there is a new_token ', () => {
154+
spyOn(cookieService, 'check').and.returnValue(true);
155+
spyOn(service, 'setLocalStorage');
156+
const token = 'test123';
157+
service.baseUrl = '/users';
158+
const mockSuccessDataPost = {
159+
SUCCESS: true,
160+
new_token: 'test_token'
161+
};
162+
httpClientSpy.post.and.returnValue(of(mockSuccessDataPost));
163+
service.isValidToken(token).subscribe();
164+
expect(service.setLocalStorage).toHaveBeenCalled();
165+
expect(cookieService.check).toHaveBeenCalled();
166+
});
125167
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class TimeClockComponent implements OnInit, OnDestroy {
4343
}else{
4444
this.loginService.isLogin().subscribe(isLogin => {
4545
this.username = isLogin ? this.loginService.getName() : '';
46-
})
46+
});
4747
}
4848
this.storeSubscription = this.store.pipe(select(getActiveTimeEntry)).subscribe((activeTimeEntry) => {
4949
this.activeTimeEntry = activeTimeEntry;

src/app/modules/time-clock/services/entry.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export class EntryService {
4646
}
4747

4848
stopEntryRunning(idEntry: string): Observable<any> {
49-
return (this.urlInProductionLegacy ? this.http.post(`${this.baseUrl}/${idEntry}/stop`, null) : this.http.put(`${this.baseUrl}/stop`, null) );
49+
return (this.urlInProductionLegacy ?
50+
this.http.post(`${this.baseUrl}/${idEntry}/stop`, null) : this.http.put(`${this.baseUrl}/stop`, null) );
5051
}
5152

5253
restartEntry(idEntry: string): Observable<Entry> {

src/app/modules/time-clock/store/entry.reducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export const entryReducer = (state: EntryState = initialState, action: EntryActi
266266
return {
267267
...state,
268268
isLoading: true,
269-
resultSumEntriesSelected:{hours:0, minutes:0, seconds:0},
269+
resultSumEntriesSelected: {hours: 0, minutes: 0, seconds: 0},
270270
reportDataSource: {
271271
data: [],
272272
isLoading: true

src/app/modules/time-clock/store/entry.selectors.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('Entry selectors', () => {
5555
});
5656

5757
it('should select resultSumEntriesSelected', () => {
58-
const resultSumEntriesSelected:TotalHours = { hours:0, minutes:0, seconds:0 };
58+
const resultSumEntriesSelected: TotalHours = { hours: 0, minutes: 0, seconds: 0 };
5959
const entryState = { resultSumEntriesSelected };
6060

6161
expect(selectors.getResultSumEntriesSelected.projector(entryState)).toEqual(resultSumEntriesSelected);

src/app/modules/user/services/user-info.service.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,13 @@ describe('UserInfoService', () => {
7575
});
7676
});
7777

78+
it('should return true if is Admin and isLegacyProduction', () => {
79+
const groupsTT = {groups: ['fake-admin', 'fake-admin-tt']};
80+
spyOn(mockLoginService, 'getLocalStorage').and.returnValue(JSON.stringify(groupsTT));
81+
service.isLegacyProduction = true;
82+
service.isMemberOf('fake-admin').subscribe((value) => {
83+
expect(value).toEqual(true);
84+
});
85+
});
86+
7887
});

0 commit comments

Comments
 (0)