Skip to content

Commit 315e617

Browse files
fix: TTA-90 Modify the date range selection module (#919)
* fix: TTA-90 Modify the date range selection module * fix: TTA-90 fix test login component Co-authored-by: Jimmy Jaramillo <[email protected]>
1 parent 4403b80 commit 315e617

File tree

7 files changed

+72
-29
lines changed

7 files changed

+72
-29
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('LoginComponent', () => {
101101
it('should sign up or login with google if is not logged-in into the app Locally', inject([Router], (router: Router) => {
102102
spyOn(loginService, 'isLogin').and.returnValue(of(false));
103103
spyOn(loginService, 'setLocalStorage').and.returnValue();
104-
spyOn(loginService, 'getUser').and.returnValue(of(() => {}));
104+
spyOn(loginService, 'getUser').and.returnValue(of({token: ''}));
105105
spyOn(loginService, 'setCookies').and.returnValue();
106106
spyOn(loginService, 'signIn').and.returnValue();
107107
spyOn(featureToggleCookiesService, 'setCookies').and.returnValue(featureToggleCookiesService.setCookies());

src/app/modules/reports/components/time-range-custom/time-range-custom.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<label class="col-form-label my-1">Select your Date Range:</label>
33
<mat-form-field appearance="fill">
44
<mat-label>Enter a date range</mat-label>
5-
<mat-date-range-input [formGroup]="range" [rangePicker]="picker">
6-
<input matStartDate formControlName="start" placeholder="Start date">
7-
<input matEndDate formControlName="end" placeholder="End date">
5+
<mat-date-range-input [formGroup]="range" [rangePicker]="picker" >
6+
<input matStartDate formControlName="start" placeholder="Start date" name="starDate" (dateChange)="dateRangeChange()">
7+
<input matEndDate formControlName="end" placeholder="End date" name="endDate" (dateChange)="dateRangeChange()">
88
</mat-date-range-input>
99
<mat-hint>MM/DD/YYYY – MM/DD/YYYY</mat-hint>
1010
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>

src/app/modules/reports/components/time-range-custom/time-range-custom.component.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,15 @@ describe('TimeRangeCustomComponent', () => {
136136

137137
expect(component.onSubmit).not.toHaveBeenCalled();
138138
});
139+
140+
it('should call range form and delete variable local storage ', () => {
141+
spyOn(localStorage, 'removeItem').withArgs('rangeDatePicker');
142+
component.range.setValue({start: null, end: null});
143+
jasmine.clock().install();
144+
component.dateRangeChange();
145+
jasmine.clock().tick(200);
146+
expect(localStorage.removeItem).toHaveBeenCalledWith('rangeDatePicker');
147+
jasmine.clock().uninstall();
148+
});
149+
139150
});

src/app/modules/reports/components/time-range-custom/time-range-custom.component.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class TimeRangeCustomComponent implements OnInit, OnChanges {
4949
start: formatDate(moment().startOf('isoWeek').format('l'), DATE_FORMAT, 'en'),
5050
end: formatDate(moment().format('l'), DATE_FORMAT, 'en')
5151
});
52+
localStorage.setItem('rangeDatePicker', 'custom');
5253
this.onSubmit();
5354
}
5455

@@ -65,4 +66,11 @@ export class TimeRangeCustomComponent implements OnInit, OnChanges {
6566
}
6667
}
6768

69+
dateRangeChange() {
70+
setTimeout(() => {
71+
if (this.range.get('start').value === null || this.range.get('end').value === null) {
72+
localStorage.removeItem('rangeDatePicker');
73+
}
74+
}, 200);
75+
}
6876
}
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
<div class="list-time-range">
2-
<mat-selection-list class="custom-items-time-range">
3-
<mat-list-option color="primary" class="custom-mat-list-option" (click)="resetTimeRange()">
4-
custom
5-
</mat-list-option>
6-
</mat-selection-list>
7-
<mat-selection-list class="custom-items-time-range" [multiple]="false">
8-
<mat-list-option class="custom-mat-list-option" *ngFor="let item of customPresets" [value]="item" (click)="selectRange(item)" >
9-
{{item}}
10-
</mat-list-option>
11-
</mat-selection-list>
2+
<mat-selection-list class="custom-items-time-range" [multiple]="false">
3+
<mat-list-option class="custom-mat-list-option" *ngFor="let item of customPresets" [value]="item" (click)="selectRange(item)" [selected]="item === rangeDateSelected">
4+
{{item}}
5+
</mat-list-option>
6+
</mat-selection-list>
127
</div>

src/app/modules/reports/components/time-range-custom/time-range-options/time-range-options.component.spec.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ describe('TimeRangeOptionsComponent', () => {
4040
expect(component).toBeTruthy();
4141
});
4242

43-
it('should call resetTimeRange method and clean time range input ', () => {
44-
component.resetTimeRange();
45-
expect(component.picker.startAt).toEqual(undefined);
46-
});
47-
4843
it('should click selectRange button and call calculateDateRange method', () => {
4944
spyOn(component, 'calculateDateRange').and.returnValues(['', '']);
5045
component.selectRange('today');
@@ -65,16 +60,18 @@ describe('TimeRangeOptionsComponent', () => {
6560
expect(new Date(end).toDateString()).toEqual(new Date().toDateString());
6661
});
6762

68-
it('should call calculateMonth and calculateWeek method when is called calculateDateRange method', () => {
63+
it('should call getMondayCurrent, calculateMonth and calculateWeek method when is called calculateDateRange method', () => {
6964

7065
const dataAll = [
71-
{method: 'calculateWeek', options: ['this week', 'last week']},
72-
{method: 'calculateMonth', options: ['this month', 'last month']}];
66+
{method: 'getMondayCurrent', ranges: ['custom']},
67+
{method: 'calculateWeek', ranges: ['this week', 'last week']},
68+
{method: 'calculateMonth', ranges: ['this month', 'last month']}
69+
];
7370

7471
dataAll.forEach((val: any) => {
7572
spyOn(component, val.method);
76-
val.options.forEach((option: any) => {
77-
component.calculateDateRange(option);
73+
val.ranges.forEach((range: any) => {
74+
component.calculateDateRange(range);
7875
expect(component[val.method]).toHaveBeenCalled();
7976
});
8077
});
@@ -130,4 +127,12 @@ describe('TimeRangeOptionsComponent', () => {
130127
expect(toastrServiceStub.error).toHaveBeenCalled();
131128
});
132129

130+
it('should call to method an error when the date created is null from date adapter', () => {
131+
spyOn(component.dateAdapter, 'getYear').and.returnValues(2022);
132+
spyOn(component.dateAdapter, 'getMonth').and.returnValues(7);
133+
component.getMondayCurrent();
134+
expect(component.dateAdapter.getYear).toHaveBeenCalled();
135+
expect(component.dateAdapter.getMonth).toHaveBeenCalled();
136+
});
137+
133138
});

src/app/modules/reports/components/time-range-custom/time-range-options/time-range-options.component.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Component, HostBinding, ChangeDetectionStrategy } from '@angular/core';
1+
import { Component, HostBinding, ChangeDetectionStrategy, OnInit } from '@angular/core';
22
import { DateAdapter } from '@angular/material/core';
33
import { MatDateRangePicker } from '@angular/material/datepicker';
44
import { ToastrService } from 'ngx-toastr';
55

66

77
const customPresets = [
8+
'custom',
89
'today',
910
'last 7 days',
1011
'this week',
@@ -22,9 +23,10 @@ type CustomPreset = typeof customPresets[number];
2223
styleUrls: ['./time-range-options.component.scss'],
2324
changeDetection: ChangeDetectionStrategy.OnPush,
2425
})
25-
export class TimeRangeOptionsComponent<Date> {
26+
export class TimeRangeOptionsComponent<Date> implements OnInit{
2627

2728
customPresets = customPresets;
29+
rangeDateSelected = '';
2830
@HostBinding('class.touch-ui')
2931
readonly isTouchUi = this.picker.touchUi;
3032
constructor(
@@ -35,8 +37,22 @@ export class TimeRangeOptionsComponent<Date> {
3537
this.dateAdapter.getFirstDayOfWeek = () => 1;
3638
}
3739

40+
ngOnInit() {
41+
this.rangeDateSelected = this.getLocalStorageRange();
42+
}
43+
44+
getLocalStorageRange(): string {
45+
return localStorage.getItem('rangeDatePicker');
46+
}
47+
48+
setLocalStorageRange(range: string): void {
49+
localStorage.setItem('rangeDatePicker', range);
50+
}
51+
3852
selectRange(rangeName: CustomPreset): void {
3953
const [start, end] = this.calculateDateRange(rangeName);
54+
this.setLocalStorageRange(rangeName);
55+
4056
this.picker.select(start);
4157
this.picker.select(end);
4258
this.picker.close();
@@ -47,6 +63,9 @@ export class TimeRangeOptionsComponent<Date> {
4763
const year = this.dateAdapter.getYear(today);
4864

4965
switch (rangeName) {
66+
case 'custom':
67+
const mondayWeek = this.getMondayCurrent();
68+
return [mondayWeek, today];
5069
case 'today':
5170
return [today, today];
5271
case 'last 7 days': {
@@ -108,9 +127,14 @@ export class TimeRangeOptionsComponent<Date> {
108127
return today;
109128
}
110129

111-
resetTimeRange() {
112-
this.picker.select(undefined);
113-
this.picker.select(undefined);
130+
getMondayCurrent(): Date {
131+
const yearCurrent = this.dateAdapter.getYear(this.dateAdapter.today());
132+
const monthCurrent = this.dateAdapter.getMonth(this.dateAdapter.today());
133+
const today = new Date();
134+
const first = today.getDate() - today.getDay() + 1;
135+
const monday = new Date(today.setDate(first));
136+
const mondayDayCurrent = monday.getDate();
137+
return this.dateAdapter.createDate(yearCurrent, monthCurrent, mondayDayCurrent);
114138
}
115139

116140
}

0 commit comments

Comments
 (0)