Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix using of new Date() #2315

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/core/datetime/date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ export interface DateAdapter<T = any> {

/** Get the given string as Date.
* @param value The date in the format DD.MM.YYYY.
* @param now The current date as Date.
*/
parseDate: (value: string) => T;
parseDate: (value: string, now: Date) => T;

/** Format the given Date as string.
* @param value The date to format.
Expand Down
17 changes: 10 additions & 7 deletions src/components/core/datetime/native-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ describe('NativeDateAdapter', () => {
});

it('hasSameMonthAndYear should return the correct value', () => {
expect(nativeDateAdapter.hasSameMonthAndYear(new Date(), new Date(0))).to.be.equal(false);
expect(
nativeDateAdapter.hasSameMonthAndYear(new Date(2023, 8, 15, 0, 0, 0, 0), new Date(0)),
).to.be.equal(false);
expect(
nativeDateAdapter.hasSameMonthAndYear(new Date(2023, 0, 1), new Date(2023, 1, 4)),
).to.be.equal(false);
Expand Down Expand Up @@ -166,7 +168,7 @@ describe('NativeDateAdapter', () => {
it('compareDate should return the correct value', () => {
const dateZero: Date = new Date(0);
const dateMillennium: Date = new Date(946684800000);
const dateNow: Date = new Date();
const dateNow: Date = new Date(2023, 8, 15, 0, 0, 0, 0);
expect(nativeDateAdapter.compareDate(dateZero, dateZero)).to.be.equal(0);
expect(nativeDateAdapter.compareDate(dateZero, dateMillennium)).to.be.lessThan(0);
expect(nativeDateAdapter.compareDate(dateMillennium, dateNow)).to.be.lessThan(0);
Expand Down Expand Up @@ -207,15 +209,16 @@ describe('NativeDateAdapter', () => {
});

it('parseDate should return the correct value', function () {
expect(nativeDateAdapter.parseDate(null)).to.be.undefined;
expect(nativeDateAdapter.parseDate('Test')).to.be.undefined;
expect(nativeDateAdapter.parseDate('1.1')).to.be.undefined;
let formattedDate: Date = nativeDateAdapter.parseDate('1/1/2000');
const now = new Date(2023, 8, 15, 0, 0, 0, 0);
expect(nativeDateAdapter.parseDate(null, now)).to.be.undefined;
expect(nativeDateAdapter.parseDate('Test', now)).to.be.undefined;
expect(nativeDateAdapter.parseDate('1.1', now)).to.be.undefined;
let formattedDate: Date = nativeDateAdapter.parseDate('1/1/2000', now);
expect(formattedDate.getFullYear()).to.be.equal(2000);
expect(formattedDate.getMonth()).to.be.equal(0);
expect(formattedDate.getDate()).to.be.equal(1);

formattedDate = nativeDateAdapter.parseDate('1.1.2000');
formattedDate = nativeDateAdapter.parseDate('1.1.2000', now);
expect(formattedDate.getFullYear()).to.be.equal(2000);
expect(formattedDate.getMonth()).to.be.equal(0);
expect(formattedDate.getDate()).to.be.equal(1);
Expand Down
4 changes: 2 additions & 2 deletions src/components/core/datetime/native-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export class NativeDateAdapter implements DateAdapter<Date> {
}

/** Returns the right format for the `valueAsDate` property. */
public parseDate(value: string): Date {
public parseDate(value: string, now: Date): Date {
if (!value) {
return undefined;
}
Expand All @@ -263,7 +263,7 @@ export class NativeDateAdapter implements DateAdapter<Date> {
let year = +match[3];

if (typeof year === 'number' && year < 100 && year >= 0) {
const shift = new Date().getFullYear() - 2000 + this._cutoffYearOffset;
const shift = now.getFullYear() - 2000 + this._cutoffYearOffset;
year = year <= shift ? 2000 + year : 1900 + year;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export class SbbDatepickerNextDayElement extends LitElement implements ButtonPro
if (!this._datePickerElement || isValidAttribute(this, 'data-disabled')) {
return;
}
const startingDate: Date = this._datePickerElement.getValueAsDate() ?? this._now();
const startingDate: Date =
this._datePickerElement.getValueAsDate() ?? this._datePickerElement.now();
const date: Date = findNextAvailableDate(
startingDate,
this._datePickerElement.dateFilter,
Expand Down Expand Up @@ -178,23 +179,6 @@ export class SbbDatepickerNextDayElement extends LitElement implements ButtonPro
this._disabled = this._dateAdapter.compareDate(nextDate, pickerValueAsDate) === 0;
}

private _hasDataNow(): boolean {
if (!this._datePickerElement) {
return false;
}
const dataNow = +this._datePickerElement.dataset?.now;
return !isNaN(dataNow);
}

private _now(): Date {
if (this._hasDataNow()) {
const today = new Date(+this._datePickerElement.dataset?.now);
today.setHours(0, 0, 0, 0);
return today;
}
return this._dateAdapter.today();
}

private _setAriaLabel(): void {
const currentDate = this._datePickerElement?.getValueAsDate?.();

Expand All @@ -204,7 +188,7 @@ export class SbbDatepickerNextDayElement extends LitElement implements ButtonPro
}

const currentDateString =
this._dateAdapter.today().toDateString() === currentDate.toDateString()
this._datePickerElement.now().toDateString() === currentDate.toDateString()
? i18nToday[this._language.current].toLowerCase()
: this._dateAdapter.getAccessibilityFormatDate(currentDate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export class SbbDatepickerPreviousDayElement extends LitElement implements Butto
if (!this._datePickerElement || isValidAttribute(this, 'data-disabled')) {
return;
}
const startingDate: Date = this._datePickerElement.getValueAsDate() ?? this._now();
const startingDate: Date =
this._datePickerElement.getValueAsDate() ?? this._datePickerElement.now();
const date: Date = findPreviousAvailableDate(
startingDate,
this._datePickerElement.dateFilter,
Expand Down Expand Up @@ -178,23 +179,6 @@ export class SbbDatepickerPreviousDayElement extends LitElement implements Butto
this._disabled = this._dateAdapter.compareDate(previousDate, pickerValueAsDate) === 0;
}

private _hasDataNow(): boolean {
if (!this._datePickerElement) {
return false;
}
const dataNow = +this._datePickerElement.dataset?.now;
return !isNaN(dataNow);
}

private _now(): Date {
if (this._hasDataNow()) {
const today = new Date(+this._datePickerElement.dataset?.now);
today.setHours(0, 0, 0, 0);
return today;
}
return this._dateAdapter.today();
}

private _setAriaLabel(): void {
const currentDate = this._datePickerElement?.getValueAsDate?.();

Expand All @@ -204,7 +188,7 @@ export class SbbDatepickerPreviousDayElement extends LitElement implements Butto
}

const currentDateString =
this._dateAdapter.today().toDateString() === currentDate.toDateString()
this._datePickerElement.now().toDateString() === currentDate.toDateString()
? i18nToday[this._language.current].toLowerCase()
: this._dateAdapter.getAccessibilityFormatDate(currentDate);

Expand Down
3 changes: 2 additions & 1 deletion src/components/datepicker/datepicker/datepicker.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ describe('sbb-datepicker', () => {
element.dateParser = (s) => {
s = s.replace(/\D/g, ' ').trim();
const date = s.split(' ');
return new Date(new Date().getFullYear(), +date[1] - 1, +date[0]);
const now = new Date(2023, 8, 15, 0, 0, 0, 0);
return new Date(now.getFullYear(), +date[1] - 1, +date[0]);
};
element.format = (d) => {
//Intl.DateTimeFormat API is not available in test environment.
Expand Down
22 changes: 21 additions & 1 deletion src/components/datepicker/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,28 @@ export class SbbDatepickerElement extends LitElement {
return value;
}

/**
* @internal
* Returns current date or configured date.
*/
public now(): Date {
if (this._hasDataNow()) {
const today = new Date(+this.dataset?.now);
today.setHours(0, 0, 0, 0);
return today;
}
return this._dateAdapter.today();
}

private _hasDataNow(): boolean {
const dataNow = +this.dataset?.now;
return !isNaN(dataNow);
}

private _parse(value: string): Date | undefined {
return this.dateParser ? this.dateParser(value) : this._dateAdapter.parseDate(value);
return this.dateParser
? this.dateParser(value)
: this._dateAdapter.parseDate(value, this.now());
}

private _format(date: Date): string {
Expand Down