Skip to content

Commit

Permalink
chore(date-picker): move keyboard-related disabled date tests to keyb…
Browse files Browse the repository at this point in the history
…oard nav suite
  • Loading branch information
dethell committed Nov 14, 2023
1 parent 3b2446d commit 07e8687
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 68 deletions.
69 changes: 69 additions & 0 deletions packages/date-picker/test/keyboard-navigation.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,5 +532,74 @@ describe('keyboard navigation', () => {
await overlay.focusDateElement();
expect(spy.calledOnce).to.be.true;
});

describe('disabled dates', () => {
let focusSpy;
let datePicker;

beforeEach(async () => {
datePicker = fixtureSync(`<vaadin-date-picker min="2010-01-01" max="2010-01-31"></vaadin-date-picker>`);
await nextRender();
await open(datePicker);
focusSpy = sinon.spy(datePicker._overlayContent, 'focusDate');
datePicker.isDateDisabled = (date) => {
if (!date) {
return false;
}
return date.year === 2010 && date.month === 0 && date.day === 29;
};
});

it('should not allow navigation prior to min', async () => {
datePicker.value = '2010-01-02';
datePicker.inputElement.focus();
// Move focus inside the dropdown to the typed date.
await sendKeys({ press: 'ArrowDown' });
await waitForScrollToFinish(datePicker._overlayContent);
// Move focus to the previous week and it should instead move to the min date
await sendKeys({ press: 'ArrowUp' });
await waitForScrollToFinish(datePicker._overlayContent);
expect(focusSpy.called).to.be.true;
let calledDate = focusSpy.firstCall.args[0];
expect(calledDate.toISOString().split('T')[0]).to.be.eql('2010-01-01');
// Attempt to move focus to the previous week and it should stay on the min date
await sendKeys({ press: 'ArrowUp' });
await waitForScrollToFinish(datePicker._overlayContent);
calledDate = focusSpy.secondCall.args[0];
expect(calledDate.toISOString().split('T')[0]).to.be.eql('2010-01-01');
});

it('should not allow navigation beyond max', async () => {
datePicker.value = '2010-01-30';
datePicker.inputElement.focus();
// Move focus inside the dropdown to the typed date.
await sendKeys({ press: 'ArrowDown' });
await waitForScrollToFinish(datePicker._overlayContent);
// Move focus to the previous week and it should instead move to the min date
await sendKeys({ press: 'ArrowDown' });
await waitForScrollToFinish(datePicker._overlayContent);
expect(focusSpy.called).to.be.true;
let calledDate = focusSpy.firstCall.args[0];
expect(calledDate.toISOString().split('T')[0]).to.be.eql('2010-01-31');
// Attempt to move focus to the previous week and it should stay on the min date
await sendKeys({ press: 'ArrowDown' });
await waitForScrollToFinish(datePicker._overlayContent);
calledDate = focusSpy.secondCall.args[0];
expect(calledDate.toISOString().split('T')[0]).to.be.eql('2010-01-31');
});

it('should allow navigation on a disabled date', async () => {
datePicker.value = '2010-01-28';
// Move focus inside the dropdown to the typed date.
await sendKeys({ press: 'ArrowDown' });
await waitForScrollToFinish(datePicker._overlayContent);
// Attempt to move focus to a disabled date
await sendKeys({ press: 'ArrowRight' });
await waitForScrollToFinish(datePicker._overlayContent);
expect(focusSpy.called).to.be.true;
const calledDate = focusSpy.firstCall.args[0];
expect(calledDate.toISOString().split('T')[0]).to.be.eql('2010-01-29');
});
});
});
});
68 changes: 0 additions & 68 deletions packages/date-picker/test/validation.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,72 +358,4 @@ describe('validation', () => {
expect(datePicker.invalid).to.be.false;
});
});

describe('keyboard navigation', () => {
let focusSpy;

beforeEach(async () => {
datePicker = fixtureSync(`<vaadin-date-picker min="2010-01-01" max="2010-01-31"></vaadin-date-picker>`);
await nextRender();
await open(datePicker);
focusSpy = sinon.spy(datePicker._overlayContent, 'focusDate');
datePicker.isDateDisabled = (date) => {
if (!date) {
return false;
}
return date.year === 2010 && date.month === 0 && date.day === 29;
};
});

it('should not allow navigation prior to min', async () => {
datePicker.value = '2010-01-02';
datePicker.inputElement.focus();
// Move focus inside the dropdown to the typed date.
await sendKeys({ press: 'ArrowDown' });
await waitForScrollToFinish(datePicker._overlayContent);
// Move focus to the previous week and it should instead move to the min date
await sendKeys({ press: 'ArrowUp' });
await waitForScrollToFinish(datePicker._overlayContent);
expect(focusSpy.called).to.be.true;
let calledDate = focusSpy.firstCall.args[0];
expect(calledDate.toISOString().split('T')[0]).to.be.eql('2010-01-01');
// Attempt to move focus to the previous week and it should stay on the min date
await sendKeys({ press: 'ArrowUp' });
await waitForScrollToFinish(datePicker._overlayContent);
calledDate = focusSpy.secondCall.args[0];
expect(calledDate.toISOString().split('T')[0]).to.be.eql('2010-01-01');
});

it('should not allow navigation beyond max', async () => {
datePicker.value = '2010-01-30';
datePicker.inputElement.focus();
// Move focus inside the dropdown to the typed date.
await sendKeys({ press: 'ArrowDown' });
await waitForScrollToFinish(datePicker._overlayContent);
// Move focus to the previous week and it should instead move to the min date
await sendKeys({ press: 'ArrowDown' });
await waitForScrollToFinish(datePicker._overlayContent);
expect(focusSpy.called).to.be.true;
let calledDate = focusSpy.firstCall.args[0];
expect(calledDate.toISOString().split('T')[0]).to.be.eql('2010-01-31');
// Attempt to move focus to the previous week and it should stay on the min date
await sendKeys({ press: 'ArrowDown' });
await waitForScrollToFinish(datePicker._overlayContent);
calledDate = focusSpy.secondCall.args[0];
expect(calledDate.toISOString().split('T')[0]).to.be.eql('2010-01-31');
});

it('should allow navigation on a disabled date', async () => {
datePicker.value = '2010-01-28';
// Move focus inside the dropdown to the typed date.
await sendKeys({ press: 'ArrowDown' });
await waitForScrollToFinish(datePicker._overlayContent);
// Attempt to move focus to a disabled date
await sendKeys({ press: 'ArrowRight' });
await waitForScrollToFinish(datePicker._overlayContent);
expect(focusSpy.called).to.be.true;
const calledDate = focusSpy.firstCall.args[0];
expect(calledDate.toISOString().split('T')[0]).to.be.eql('2010-01-29');
});
});
});

0 comments on commit 07e8687

Please sign in to comment.