Skip to content

Commit d2753ad

Browse files
fix: TTA-141 Fix broken tests and test coverage in the UI
1 parent 4c478b3 commit d2753ad

File tree

10 files changed

+90
-22
lines changed

10 files changed

+90
-22
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 sign up or login with google if is not logged-in into the app Locally', 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
@@ -50,14 +50,14 @@ export class LoginComponent implements OnInit {
5050
this.azureAdB2CService.signIn().subscribe(() => {
5151
this.featureToggleCookiesService.setCookies();
5252
this.azureAdB2CService.setCookies();
53-
const userId = this.azureAdB2CService.getUserId()
53+
const userId = this.azureAdB2CService.getUserId();
5454
this.userService.loadUser(userId).subscribe((user) => {
55-
const user_groups = {
55+
const userGroups = {
5656
groups: user.groups
57-
}
58-
this.loginService.setLocalStorage('user', JSON.stringify(user_groups));
57+
};
58+
this.loginService.setLocalStorage('user', JSON.stringify(userGroups));
5959
this.router.navigate(['']);
60-
})
60+
});
6161
});
6262
}
6363
}

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);
@@ -128,4 +134,40 @@ describe('LoginService', () => {
128134
expect(localStorage.clear).toHaveBeenCalled();
129135
expect(cookieService.deleteAll).toHaveBeenCalled();
130136
});
137+
138+
it('should call cookieService when app is isLegacyProd', () => {
139+
service.isLegacyProd = true;
140+
service.localStorageKey = 'user2';
141+
spyOn(cookieService, 'check').and.returnValue(true);
142+
spyOn(service, 'isValidToken').and.returnValue(of(true));
143+
service.isLogin().subscribe(isLogin => {
144+
expect(cookieService.check).toHaveBeenCalled();
145+
});
146+
});
147+
148+
it('should call JSON parse when app is isLegacyProd', () => {
149+
spyOn(JSON, 'parse').and.returnValue('test_user_123');
150+
service.isLegacyProd = true;
151+
service.localStorageKey = 'user2';
152+
service.getUserId();
153+
service.getName();
154+
service.getUserEmail();
155+
service.getUserGroup();
156+
expect(JSON.parse).toHaveBeenCalled();
157+
});
158+
159+
it('should call setLocalStorage when there is a new_token ', () => {
160+
spyOn(cookieService, 'check').and.returnValue(true);
161+
spyOn(service, 'setLocalStorage');
162+
const token = 'test123';
163+
service.baseUrl = '/users';
164+
const mockSuccessDataPost = {
165+
SUCCESS: true,
166+
new_token: 'test_token'
167+
};
168+
httpClientSpy.post.and.returnValue(of(mockSuccessDataPost));
169+
service.isValidToken(token).subscribe();
170+
expect(service.setLocalStorage).toHaveBeenCalled();
171+
expect(cookieService.check).toHaveBeenCalled();
172+
});
131173
});

src/app/modules/reports/components/time-entries-table/time-entries-table.component.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ describe('Reports Page', () => {
212212

213213
it('the sume of hours of entries selected is equal to {hours:0, minutes:0, seconds:0}', () => {
214214
let checked = true;
215-
let {hours, minutes, seconds}:TotalHours = component.sumHoursEntriesSelected(timeEntryList[0], checked);
215+
let {hours, minutes, seconds}: TotalHours = component.sumHoursEntriesSelected(timeEntryList[0], checked);
216216
checked = false;
217-
({hours, minutes,seconds} = component.sumHoursEntriesSelected(timeEntryList[0], checked));
218-
expect({hours, minutes, seconds}).toEqual({hours:0, minutes:0, seconds:0});
217+
({hours, minutes, seconds} = component.sumHoursEntriesSelected(timeEntryList[0], checked));
218+
expect({hours, minutes, seconds}).toEqual({hours: 0, minutes: 0, seconds: 0});
219219
});
220220

221221
it('should export data with the correct format', () => {

src/app/modules/reports/components/time-entries-table/time-entries-table.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class TimeEntriesTableComponent implements OnInit, OnDestroy, AfterViewIn
6969
},
7070
],
7171
columnDefs: [{ type: 'date', targets: 2}, {orderable: false, targets: [0]}],
72-
order: [[1,'asc'],[2,'desc'],[4,'desc']]
72+
order: [[1, 'asc'], [2, 'desc'], [4, 'desc']]
7373
};
7474
dtTrigger: Subject<any> = new Subject();
7575
@ViewChild(DataTableDirective, { static: false })
@@ -79,7 +79,7 @@ export class TimeEntriesTableComponent implements OnInit, OnDestroy, AfterViewIn
7979
rerenderTableSubscription: Subscription;
8080
resultSum: TotalHours;
8181
resultSumEntriesSelected: TotalHours;
82-
resultSumEntriesSelected$:Observable<TotalHours>;
82+
resultSumEntriesSelected$: Observable<TotalHours>;
8383
totalHoursSubscription: Subscription;
8484
dateTimeOffset: ParseDateTimeOffset;
8585

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)