Skip to content

Commit

Permalink
fix(kit): InputDate incorrect works with date less than 1900 (#9719)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlufy authored Nov 22, 2024
2 parents f191380 + 3154d57 commit 5a8d751
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
7 changes: 6 additions & 1 deletion projects/cdk/date-time/day.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,12 @@ export class TuiDay extends TuiMonth {
* Returns native {@link Date} based on local time zone
*/
override toLocalNativeDate(): Date {
return new Date(this.year, this.month, this.day);
const date = new Date(this.year, this.month, this.day);

// needed for years less than 1900
date.setFullYear(Number(this.year ?? '0'));

return date;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('InputDate', () => {

readOnly = false;

min = new TuiDay(1900, 0, 1);
min: TuiDay | null = new TuiDay(1500, 0, 1);

labelOutside = false;

Expand Down Expand Up @@ -125,6 +125,15 @@ describe('InputDate', () => {
expect(inputPO.value).toBe('14.03.2017');
});

it('correct shows value less than 01.01.1900', async () => {
testComponent.control.patchValue(new TuiDay(1000, 0, 1));
fixture.detectChanges();

await fixture.whenStable();

expect(inputPO.value).toBe('01.01.1000');
});

describe('Keyboard input', () => {
it('The passed date is inserted into the field', () => {
inputPO.sendText('01.03.2017');
Expand Down Expand Up @@ -206,15 +215,17 @@ describe('InputDate', () => {
expect(typedDay.year).toBe(2021);
});

it('does not accept mm.dd.yyyy (and set min day if it is less min day)', () => {
it('does not accept mm.dd.yyyy (and set min day if it is less min day)', async () => {
inputPO.sendText('12.23.2021');

await fixture.whenStable();

const typedDay = testComponent.control.value;

expect(inputPO.value).toBe('1900.01.01');
expect(inputPO.value).toBe('1500.01.01');
expect(typedDay.day).toBe(1);
expect(typedDay.month).toBe(0);
expect(typedDay.year).toBe(1900);
expect(typedDay.year).toBe(1500);
});

it('sets valid day if date selected via calendar', async () => {
Expand Down Expand Up @@ -253,15 +264,17 @@ describe('InputDate', () => {
expect(typedDay.year).toBe(2021);
});

it('does not accept yyyy.mm.dd (and set min day if it is less min day)', () => {
it('does not accept yyyy.mm.dd (and set min day if it is less min day)', async () => {
inputPO.sendText('2021.12.23');

await fixture.whenStable();

const typedDay = testComponent.control.value;

expect(inputPO.value).toBe('01.01.1900');
expect(inputPO.value).toBe('01.01.1500');
expect(typedDay.day).toBe(1);
expect(typedDay.month).toBe(0);
expect(typedDay.year).toBe(1900);
expect(typedDay.year).toBe(1500);
});

it('sets valid day if date selected via calendar', async () => {
Expand Down Expand Up @@ -360,11 +373,13 @@ describe('InputDate', () => {
expect(testComponent.control.value).toEqual(new Date(1905, 0, 9));
});

it('transforms min day as output (if typed day is less than min day)', () => {
inputPO.sendText('19.02.1861');
it('transforms min day as output (if typed day is less than min day)', async () => {
inputPO.sendText('19.02.1300');

await fixture.whenStable();

expect(inputPO.value).toBe('01.01.1900');
expect(testComponent.control.value).toEqual(new Date(1900, 0, 1));
expect(inputPO.value).toBe('01.01.1500');
expect(testComponent.control.value).toEqual(new Date(1500, 0, 1));
});

it('transforms value which was selected via calendar', async () => {
Expand Down

0 comments on commit 5a8d751

Please sign in to comment.