Skip to content

Commit 5ee06fb

Browse files
authored
fix(date-formatter): fixed tests for relative formats (#UIM-837) (#771)
* fix(date-formatter): fixed tests for relative formats (#UIM-837) * fixed linter errors
1 parent a25441f commit 5ee06fb

File tree

2 files changed

+76
-40
lines changed

2 files changed

+76
-40
lines changed

packages/mosaic/core/formatters/date/formatter.spec.ts

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@ import { inject, TestBed, waitForAsync } from '@angular/core/testing';
66
import { DateAdapter, MC_DATE_LOCALE } from '@ptsecurity/cdk/datetime';
77
import { LuxonDateAdapter, LuxonDateModule } from '@ptsecurity/mosaic-luxon-adapter/adapter';
88
import { DateFormatter, McFormattersModule } from '@ptsecurity/mosaic/core';
9-
import { DateTime } from 'luxon';
9+
import { DateTime, DurationUnit } from 'luxon';
1010

1111

1212
describe('Date formatter', () => {
1313
let adapter: LuxonDateAdapter;
1414
let formatter: DateFormatter<DateTime>;
15+
let currentDate: DateTime;
16+
17+
const mockAdapterAndFormatterForRelativeTests = () => {
18+
// @ts-ignore
19+
adapter.diffNow = (date: DateTime, unit: DurationUnit): number => {
20+
return date.diff(currentDate, unit)[unit];
21+
};
22+
23+
// @ts-ignore
24+
formatter.hasSame = (startDate: DateTime, endDate: DateTime, unit: DurationUnit): string => {
25+
return adapter.hasSame(startDate, currentDate, unit) ? 'yes' : 'no';
26+
};
27+
};
1528

1629
beforeEach(waitForAsync(() => {
1730
TestBed.configureTestingModule({
@@ -27,6 +40,8 @@ describe('Date formatter', () => {
2740
beforeEach(inject([DateAdapter, DateFormatter], (d: LuxonDateAdapter, f: DateFormatter<DateTime>) => {
2841
adapter = d;
2942
formatter = f;
43+
44+
currentDate = adapter.createDateTime(2000, 10, 10, 10, 0, 0, 0);
3045
}));
3146

3247
const YEAR = 'yyyy';
@@ -64,21 +79,23 @@ describe('Date formatter', () => {
6479
});
6580

6681
it('before yesterday, more than 2 days ago', () => {
67-
let date = adapter.today()
68-
.minus({ days: 3 });
82+
mockAdapterAndFormatterForRelativeTests();
83+
84+
let date = currentDate.minus({ days: 3 });
6985

7086
expect(formatter.relativeShortDate(date))
7187
.toBe(adapter.format(date, `${DAY}${NBSP}${SHORT_MONTH}, ${TIME}`));
7288

73-
date = adapter.today().minus({ days: 5 });
89+
date = currentDate.minus({ days: 5 });
7490

7591
expect(formatter.relativeShortDate(date))
7692
.toBe(adapter.format(date, `${DAY}${NBSP}${SHORT_MONTH}, ${TIME}`));
7793
});
7894

7995
it('yesterday', () => {
80-
const date = adapter.today()
81-
.minus({ days: 1 });
96+
mockAdapterAndFormatterForRelativeTests();
97+
98+
const date = currentDate.minus({ days: 1 });
8299

83100
expect(formatter.relativeShortDate(date))
84101
.toBe(`Вчера, ${date.toFormat(TIME)}`);
@@ -92,55 +109,60 @@ describe('Date formatter', () => {
92109
});
93110

94111
it('tomorrow', () => {
95-
const date = adapter.today().plus({ days: 1, hours: 1 });
112+
mockAdapterAndFormatterForRelativeTests();
113+
114+
const date = currentDate.plus({ days: 1, hours: 1 });
96115

97116
expect(formatter.relativeShortDate(date))
98117
.toBe(`Завтра, ${date.toFormat(TIME)}`);
99118
});
100119

101120
it('after tomorrow (current year)', () => {
102-
let date = adapter.today()
103-
.plus({ days: 3 });
121+
mockAdapterAndFormatterForRelativeTests();
122+
123+
let date = currentDate.plus({ days: 3 });
104124

105125
expect(formatter.relativeShortDate(date))
106126
.toBe(adapter.format(date, `${DAY}${NBSP}${SHORT_MONTH}, ${TIME}`));
107127

108-
date = adapter.today().plus({ days: 5 });
128+
date = currentDate.plus({ days: 5 });
109129

110130
expect(formatter.relativeShortDate(date))
111131
.toBe(adapter.format(date, `${DAY}${NBSP}${SHORT_MONTH}, ${TIME}`));
112132
});
113133

114134
it('after tomorrow (other year)', () => {
115-
const date = adapter.createDate(2015).plus({ hours: 49 });
135+
const date = currentDate.plus({ hours: 49 });
116136
expect(formatter.relativeShortDate(date))
117137
.toBe(adapter.format(date, `${DAY}${NBSP}${SHORT_MONTH} ${YEAR}`));
118138
});
119139
});
120140

121141
describe('Relative long (relativeLongDate method)', () => {
122142
it('before yesterday (other year)', () => {
123-
const date = adapter.createDate(2015).minus({ hours: 49 });
143+
const date = currentDate.minus({ hours: 49 });
124144
expect(formatter.relativeLongDate(date))
125145
.toBe(adapter.format(date, `${DAY_MONTH} ${YEAR}`));
126146
});
127147

128148
it('before yesterday, more than 2 days ago', () => {
129-
let date = adapter.today()
130-
.minus({ days: 3 });
149+
mockAdapterAndFormatterForRelativeTests();
150+
151+
let date = currentDate.minus({ days: 3 });
131152

132153
expect(formatter.relativeLongDate(date))
133154
.toBe(adapter.format(date, `${DAY_MONTH}, ${TIME}`));
134155

135-
date = adapter.today().minus({ days: 5 });
156+
date = currentDate.minus({ days: 5 });
136157

137158
expect(formatter.relativeLongDate(date))
138159
.toBe(adapter.format(date, `${DAY_MONTH}, ${TIME}`));
139160
});
140161

141162
it('yesterday', () => {
142-
const date = adapter.today()
143-
.minus({ days: 1 });
163+
mockAdapterAndFormatterForRelativeTests();
164+
165+
const date = currentDate.minus({ days: 1 });
144166

145167
expect(formatter.relativeLongDate(date))
146168
.toBe(`Вчера, ${date.toFormat(TIME)}`);
@@ -154,27 +176,30 @@ describe('Date formatter', () => {
154176
});
155177

156178
it('tomorrow', () => {
157-
const date = adapter.today().plus({ days: 1, hours: 1 });
179+
mockAdapterAndFormatterForRelativeTests();
180+
181+
const date = currentDate.plus({ days: 1, hours: 1 });
158182

159183
expect(formatter.relativeLongDate(date))
160184
.toBe(`Завтра, ${date.toFormat(TIME)}`);
161185
});
162186

163187
it('after tomorrow (current year)', () => {
164-
let date = adapter.today()
165-
.plus({ days: 3 });
188+
mockAdapterAndFormatterForRelativeTests();
189+
190+
let date = currentDate.plus({ days: 3 });
166191

167192
expect(formatter.relativeLongDate(date))
168193
.toBe(adapter.format(date, `${DAY_MONTH}, ${TIME}`));
169194

170-
date = adapter.today().plus({ days: 5 });
195+
date = currentDate.plus({ days: 5 });
171196

172197
expect(formatter.relativeLongDate(date))
173198
.toBe(adapter.format(date, `${DAY_MONTH}, ${TIME}`));
174199
});
175200

176201
it('after tomorrow (other year)', () => {
177-
const date = adapter.createDate(2015).plus({ hours: 49 });
202+
const date = currentDate.plus({ hours: 49 });
178203
expect(formatter.relativeLongDate(date))
179204
.toBe(adapter.format(date, `${DAY_MONTH} ${YEAR}`));
180205
});
@@ -792,20 +817,23 @@ describe('Date formatter', () => {
792817
});
793818

794819
it('before yesterday, more than 2 days ago', () => {
795-
let date = adapter.today()
796-
.minus({ days: 3 });
820+
mockAdapterAndFormatterForRelativeTests();
821+
822+
let date = currentDate.minus({ days: 3 });
797823

798824
expect(formatter.relativeShortDate(date))
799825
.toBe(adapter.format(date, `${SHORT_MONTH}${NBSP}${DAY}, ${TIME}`));
800826

801-
date = adapter.today().minus({ days: 5 });
827+
date = currentDate.minus({ days: 5 });
802828

803829
expect(formatter.relativeShortDate(date))
804830
.toBe(adapter.format(date, `${SHORT_MONTH}${NBSP}${DAY}, ${TIME}`));
805831
});
806832

807833
it('yesterday', () => {
808-
const date = adapter.today()
834+
mockAdapterAndFormatterForRelativeTests();
835+
836+
const date = currentDate
809837
.minus({ days: 1 });
810838

811839
expect(formatter.relativeShortDate(date))
@@ -820,20 +848,23 @@ describe('Date formatter', () => {
820848
});
821849

822850
it('tomorrow', () => {
823-
const date = adapter.today().plus({ days: 1, hours: 1 });
851+
mockAdapterAndFormatterForRelativeTests();
852+
853+
const date = currentDate.plus({ days: 1, hours: 1 });
824854

825855
expect(formatter.relativeShortDate(date))
826856
.toBe(`Tomorrow, ${date.toFormat(TIME)}`);
827857
});
828858

829859
it('after tomorrow (current year)', () => {
830-
let date = adapter.today()
831-
.plus({ days: 3 });
860+
mockAdapterAndFormatterForRelativeTests();
861+
862+
let date = currentDate.plus({ days: 3 });
832863

833864
expect(formatter.relativeShortDate(date))
834865
.toBe(adapter.format(date, `${SHORT_MONTH}${NBSP}${DAY}, ${TIME}`));
835866

836-
date = adapter.today().plus({ days: 5 });
867+
date = currentDate.plus({ days: 5 });
837868

838869
expect(formatter.relativeShortDate(date))
839870
.toBe(adapter.format(date, `${SHORT_MONTH}${NBSP}${DAY}, ${TIME}`));
@@ -855,21 +886,23 @@ describe('Date formatter', () => {
855886
});
856887

857888
it('before yesterday, more than 2 days ago', () => {
858-
let date = adapter.today()
859-
.minus({ days: 3 });
889+
mockAdapterAndFormatterForRelativeTests();
890+
891+
let date = currentDate.minus({ days: 3 });
860892

861893
expect(formatter.relativeLongDate(date))
862894
.toBe(adapter.format(date, `${DAY_MONTH}, ${TIME}`));
863895

864-
date = adapter.today().minus({ days: 5 });
896+
date = currentDate.minus({ days: 5 });
865897

866898
expect(formatter.relativeLongDate(date))
867899
.toBe(adapter.format(date, `${DAY_MONTH}, ${TIME}`));
868900
});
869901

870902
it('yesterday', () => {
871-
const date = adapter.today()
872-
.minus({ days: 1 });
903+
mockAdapterAndFormatterForRelativeTests();
904+
905+
const date = currentDate.minus({ days: 1 });
873906

874907
expect(formatter.relativeLongDate(date))
875908
.toBe(`Yesterday, ${date.toFormat(TIME)}`);
@@ -883,20 +916,23 @@ describe('Date formatter', () => {
883916
});
884917

885918
it('tomorrow', () => {
886-
const date = adapter.today().plus({ days: 1, hours: 1 });
919+
mockAdapterAndFormatterForRelativeTests();
920+
921+
const date = currentDate.plus({ days: 1, hours: 1 });
887922

888923
expect(formatter.relativeLongDate(date))
889924
.toBe(`Tomorrow, ${date.toFormat(TIME)}`);
890925
});
891926

892927
it('after tomorrow (current year)', () => {
893-
let date = adapter.today()
894-
.plus({ days: 3 });
928+
mockAdapterAndFormatterForRelativeTests();
929+
930+
let date = currentDate.plus({ days: 3 });
895931

896932
expect(formatter.relativeLongDate(date))
897933
.toBe(adapter.format(date, `${DAY_MONTH}, ${TIME}`));
898934

899-
date = adapter.today().plus({ days: 5 });
935+
date = currentDate.plus({ days: 5 });
900936

901937
expect(formatter.relativeLongDate(date))
902938
.toBe(adapter.format(date, `${DAY_MONTH}, ${TIME}`));

packages/mosaic/core/formatters/date/formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class DateFormatter<D> {
105105
relativeDate(date: D, template: FormatterRelativeTemplate): string {
106106
if (!this.adapter.isDateInstance(date)) { throw new Error(this.invalidDateErrorText); }
107107

108-
const isBeforeYesterday = this.adapter.diffNow(date, 'days') < -2;
108+
const isBeforeYesterday = this.adapter.diffNow(date, 'days') <= -2;
109109
const isYesterday = this.adapter.diffNow(date, 'days') <= -1 && this.adapter.diffNow(date, 'days') > -2;
110110
const isToday = this.adapter.hasSame(this.adapter.today(), date, 'days');
111111
const isTomorrow = this.adapter.diffNow(date, 'days') >= 1 && this.adapter.diffNow(date, 'days') < 2;

0 commit comments

Comments
 (0)