Skip to content

Commit a7fed56

Browse files
build: TT-106 Perform unit test on the TimeClock, ProjectListHover and EntryFields components
1 parent 65ea62e commit a7fed56

File tree

6 files changed

+98
-43
lines changed

6 files changed

+98
-43
lines changed

src/app/modules/time-clock/components/entry-fields/entry-fields.component.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ describe('EntryFieldsComponent', () => {
155155
expect(component.selectedTechnologies).toEqual([]);
156156
});
157157

158+
159+
160+
const exponentialGrowth = [true, false];
161+
exponentialGrowth.map((toggleValue) => {
162+
it(`when FeatureToggle is ${toggleValue} should return true`, () => {
163+
console.log(toggleValue);
164+
spyOn(featureManagerService, 'isToggleEnabled').and.returnValue(of(toggleValue));
165+
const isFeatureToggleActivated: Promise<boolean> = component.isFeatureToggleActivated();
166+
expect(featureManagerService.isToggleEnabled).toHaveBeenCalled();
167+
isFeatureToggleActivated.then((value) => expect(value).toEqual(toggleValue));
168+
});
169+
});
170+
171+
172+
158173
it('displays error message when the date selected is in the future', () => {
159174
const mockEntry = { ...entry,
160175
start_date : moment().format(DATE_FORMAT_YEAR),
@@ -424,5 +439,10 @@ describe('EntryFieldsComponent', () => {
424439
// expect(component.isFeatureToggleActivated).toHaveBeenCalled();
425440
// expect(component.isUserRoleToggleOn).toBe(true);
426441
// });
442+
});
443+
444+
describe('Describe de la primera prueba', () => {
427445

446+
447+
428448
});

src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
6262

6363
async ngOnInit(): Promise<void> {
6464

65-
this.exponentialGrowth = await this.isFeatureToggleActivated();
66-
67-
console.log(this.exponentialGrowth);
68-
65+
this.exponentialGrowth = await this.isFeatureToggleActivated();
6966

7067
this.store.dispatch(new LoadActivities());
7168
this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1, new Date().getFullYear()));

src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
55
import { provideMockStore, MockStore } from '@ngrx/store/testing';
66
import { HttpClientTestingModule } from '@angular/common/http/testing';
77
import { AutocompleteLibModule } from 'angular-ng-autocomplete';
8-
import { Subscription } from 'rxjs';
8+
import { Subscription, of } from 'rxjs';
99
import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer';
1010
import { getCustomerProjects } from '../../../customer-management/components/projects/components/store/project.selectors';
1111
import { FilterProjectPipe } from '../../../shared/pipes';
1212
import { UpdateEntryRunning } from '../../store/entry.actions';
1313
import { ProjectListHoverComponent } from './project-list-hover.component';
14+
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
1415

1516
describe('ProjectListHoverComponent', () => {
1617
let component: ProjectListHoverComponent;
1718
let fixture: ComponentFixture<ProjectListHoverComponent>;
1819
let store: MockStore<ProjectState>;
1920
let mockProjectsSelector;
21+
let featureManagerService : FeatureManagerService;
2022
const toastrServiceStub = {
2123
error: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { }
2224
};
@@ -56,6 +58,7 @@ describe('ProjectListHoverComponent', () => {
5658
fixture = TestBed.createComponent(ProjectListHoverComponent);
5759
component = fixture.componentInstance;
5860
fixture.detectChanges();
61+
featureManagerService = TestBed.inject(FeatureManagerService);
5962
});
6063

6164
it('should create', () => {
@@ -118,6 +121,17 @@ describe('ProjectListHoverComponent', () => {
118121
.toHaveBeenCalledWith({ project_id: 'customer - xyz'});
119122
});
120123

124+
const exponentialGrowth = [true, false];
125+
exponentialGrowth.map((toggleValue) => {
126+
it(`when FeatureToggle is ${toggleValue} should return true`, () => {
127+
console.log(toggleValue);
128+
spyOn(featureManagerService, 'isToggleEnabled').and.returnValue(of(toggleValue));
129+
const isFeatureToggleActivated: Promise<boolean> = component.isFeatureToggleActivated();
130+
expect(featureManagerService.isToggleEnabled).toHaveBeenCalled();
131+
isFeatureToggleActivated.then((value) => expect(value).toEqual(toggleValue));
132+
});
133+
});
134+
121135

122136
// TODO Fix this test since it is throwing this error
123137
// Expected spy dispatch to have been called with:

src/app/modules/time-clock/components/project-list-hover/project-list-hover.component.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { FormBuilder, FormGroup } from '@angular/forms';
33
import { ActionsSubject, select, Store } from '@ngrx/store';
44
import { ToastrService } from 'ngx-toastr';
55
import { Observable, Subscription } from 'rxjs';
6-
import { delay, filter } from 'rxjs/operators';
6+
import { delay, filter, map } from 'rxjs/operators';
77
import { Project } from 'src/app/modules/shared/models';
88
import * as actions from '../../../customer-management/components/projects/components/store/project.actions';
99
import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer';
1010
import * as entryActions from '../../store/entry.actions';
1111
import { getIsLoading, getProjects } from './../../../customer-management/components/projects/components/store/project.selectors';
1212
import { EntryActionTypes } from './../../store/entry.actions';
1313
import { getActiveTimeEntry } from './../../store/entry.selectors';
14+
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
1415
@Component({
1516
selector: 'app-project-list-hover',
1617
templateUrl: './project-list-hover.component.html',
@@ -25,24 +26,26 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
2526
showClockIn: boolean;
2627
updateEntrySubscription: Subscription;
2728
isLoading$: Observable<boolean>;
28-
29-
30-
/**
31-
* subscription variables
32-
*/
3329
projectsSubscribe : Subscription;
3430
activeEntrySubscribe : Subscription;
31+
projectsSubject;
32+
activeEntrySubject;
33+
exponentialGrowth;
3534

36-
constructor(private formBuilder: FormBuilder, private store: Store<ProjectState>,
35+
constructor(private featureManagerService: FeatureManagerService,
36+
private formBuilder: FormBuilder, private store: Store<ProjectState>,
3737
private actionsSubject$: ActionsSubject, private toastrService: ToastrService) {
3838
this.projectsForm = this.formBuilder.group({ project_id: null, });
3939
this.isLoading$ = this.store.pipe(delay(0), select(getIsLoading));
4040
}
4141

42-
ngOnInit(): void {
42+
async ngOnInit(): Promise<void> {
43+
44+
this.exponentialGrowth = await this.isFeatureToggleActivated();
45+
4346
this.store.dispatch(new actions.LoadProjects());
4447
const projects$ = this.store.pipe(select(getProjects));
45-
this.projectsSubscribe = projects$.subscribe((projects) => {
48+
this.projectsSubject = projects$.subscribe((projects) => {
4649
this.listProjects = [];
4750
projects.forEach((project) => {
4851
const projectWithSearchField = {...project};
@@ -53,6 +56,8 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
5356
this.loadActiveTimeEntry();
5457
});
5558

59+
this.exponentialGrowth ? this.projectsSubscribe = this.projectsSubject : this.projectsSubject;
60+
5661
this.updateEntrySubscription = this.actionsSubject$.pipe(
5762
filter((action: any) => (
5863
action.type === EntryActionTypes.UPDATE_ENTRY_SUCCESS
@@ -68,7 +73,7 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
6873
loadActiveTimeEntry() {
6974
this.store.dispatch(new entryActions.LoadActiveEntry());
7075
const activeEntry$ = this.store.pipe(select(getActiveTimeEntry));
71-
this.activeEntrySubscribe = activeEntry$.subscribe((activeEntry) => {
76+
this.activeEntrySubject = activeEntry$.subscribe((activeEntry) => {
7277
this.activeEntry = activeEntry;
7378
if (activeEntry) {
7479
this.showClockIn = false;
@@ -78,6 +83,7 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
7883
this.projectsForm.setValue({ project_id: null });
7984
}
8085
});
86+
this.exponentialGrowth ? this.activeEntrySubscribe = this.activeEntrySubject : this.activeEntrySubject;
8187
}
8288

8389
setSelectedProject() {
@@ -117,8 +123,18 @@ export class ProjectListHoverComponent implements OnInit, OnDestroy {
117123
}
118124

119125
ngOnDestroy(): void {
120-
this.updateEntrySubscription.unsubscribe();
121-
this.projectsSubscribe.unsubscribe();
126+
if(this.exponentialGrowth){
127+
this.projectsSubscribe.unsubscribe();
122128
this.activeEntrySubscribe.unsubscribe();
129+
}
130+
this.updateEntrySubscription.unsubscribe();
123131
}
132+
133+
isFeatureToggleActivated() {
134+
return this.featureManagerService.isToggleEnabledForUser('exponential-growth').pipe(
135+
map((enabled) => {
136+
return enabled === true ? true : false;
137+
})
138+
).toPromise();
139+
}
124140
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { of } from 'rxjs';
12
import { FormBuilder } from '@angular/forms';
23
import { StopTimeEntryRunning, EntryActionTypes, LoadEntriesSummary } from './../store/entry.actions';
34
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
@@ -11,12 +12,14 @@ import { AzureAdB2CService } from '../../login/services/azure.ad.b2c.service';
1112
import { ActionsSubject } from '@ngrx/store';
1213
import { EntryFieldsComponent } from '../components/entry-fields/entry-fields.component';
1314
import { ToastrService } from 'ngx-toastr';
15+
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
1416

1517
describe('TimeClockComponent', () => {
1618
let component: TimeClockComponent;
1719
let fixture: ComponentFixture<TimeClockComponent>;
1820
let store: MockStore<ProjectState>;
1921
let azureAdB2CService: AzureAdB2CService;
22+
let featureManagerService : FeatureManagerService;
2023
const actionSub: ActionsSubject = new ActionsSubject();
2124

2225
let injectedToastrService;
@@ -69,6 +72,7 @@ describe('TimeClockComponent', () => {
6972
fixture.detectChanges();
7073
azureAdB2CService = TestBed.inject(AzureAdB2CService);
7174
injectedToastrService = TestBed.inject(ToastrService);
75+
featureManagerService = TestBed.inject(FeatureManagerService);
7276
});
7377

7478
it('should be created', () => {
@@ -142,4 +146,15 @@ describe('TimeClockComponent', () => {
142146

143147
expect(injectedToastrService.error).toHaveBeenCalled();
144148
});
149+
150+
const exponentialGrowth = [true, false];
151+
exponentialGrowth.map((toggleValue) => {
152+
it(`when FeatureToggle is ${toggleValue} should return true`, () => {
153+
console.log(toggleValue);
154+
spyOn(featureManagerService, 'isToggleEnabled').and.returnValue(of(toggleValue));
155+
const isFeatureToggleActivated: Promise<boolean> = component.isFeatureToggleActivated();
156+
expect(featureManagerService.isToggleEnabled).toHaveBeenCalled();
157+
isFeatureToggleActivated.then((value) => expect(value).toEqual(toggleValue));
158+
});
159+
});
145160
});

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

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ export class TimeClockComponent implements OnInit, OnDestroy {
2323
areFieldsVisible = false;
2424
activeTimeEntry: Entry;
2525
clockOutSubscription: Subscription;
26-
27-
28-
/**
29-
* subscription variables
30-
*/
3126
storeSubscribe : Subscription;
32-
3327
storeSubject;
3428
exponentialGrowth;
3529

@@ -43,21 +37,13 @@ export class TimeClockComponent implements OnInit, OnDestroy {
4337
private actionsSubject$: ActionsSubject
4438
) {}
4539

46-
ngOnDestroy(): void {
47-
// if(this.exponentialGrowth){
48-
// this.storeSubscribe.unsubscribe();
49-
// }
50-
this.clockOutSubscription.unsubscribe();
51-
this.storeSubscribe.unsubscribe();
52-
}
40+
5341

54-
ngOnInit() {
42+
async ngOnInit(): Promise<void> {
5543

56-
this.isFeatureToggleActivated().subscribe((flag) => {
57-
this.exponentialGrowth = flag;
58-
// console.log('Time-clock', this.exponentialGrowth);
59-
});
60-
// console.log('Time-clock', this.exponentialGrowth);
44+
this.exponentialGrowth = await this.isFeatureToggleActivated();
45+
46+
console.log('This is',this.exponentialGrowth);
6147

6248
this.username = this.azureAdB2CService.isLogin() ? this.azureAdB2CService.getName() : '';
6349

@@ -101,13 +87,20 @@ export class TimeClockComponent implements OnInit, OnDestroy {
10187
}
10288
}
10389

104-
isFeatureToggleActivated() {
105-
return this.featureManagerService.isToggleEnabledForUser('exponential-growth').pipe(
106-
map((enabled) => {
107-
return enabled === true ? true : false;
108-
})
109-
);
110-
}
90+
ngOnDestroy(): void {
91+
this.exponentialGrowth && this.storeSubscribe.unsubscribe();
92+
this.clockOutSubscription.unsubscribe();
93+
this.storeSubscribe.unsubscribe();
94+
}
95+
96+
isFeatureToggleActivated() {
97+
return this.featureManagerService.isToggleEnabledForUser('exponential-growth').pipe(
98+
map((enabled) => {
99+
return enabled === true ? true : false;
100+
})
101+
).toPromise();
102+
}
103+
111104
}
112105

113106

0 commit comments

Comments
 (0)