Skip to content

Commit 5a61dfb

Browse files
Spin Button Date Picker: Fix unreliable date math test (pull #1639)
* Fix unreliable date math bug * fix: Down arrow test When you get towards the end of the month and try to add a month, Javascript Date objects start to do unexpected things. This is causing failures in the CI for all regression tests run on November 30. This is the issue: ```javascript let date = new Date('10/31/2020'); date.setMonth(date.getMonth + 1); ``` The second line increases the date by one month, resulting in date being December 1st, 2020, because there is no November 31st. The fix is to put the day of the month to the 1st using `date.setDate(1)` before doing any math, because all months have the 1st of the month. Co-authored-by: Nick Schonning <[email protected]>
1 parent 360afb8 commit 5a61dfb

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

test/tests/spinbutton_datepicker.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,17 +370,23 @@ ariaTest(
370370
exampleFile,
371371
'spinbutton-down-arrow',
372372
async (t) => {
373-
let control = parseInt(ex.dayNow);
374-
let daysInMonth = parseInt(ex.dayMax);
373+
let control = 31;
374+
375+
// Set to December for a 31 day month
376+
let monthSpinner = await t.context.session.findElement(
377+
By.css(ex.monthSelector)
378+
);
379+
await monthSpinner.sendKeys(Key.END);
375380

376381
// Send down arrow to day date spinner
377382
let daySpinner = await t.context.session.findElement(
378383
By.css(ex.daySelector)
379384
);
380-
await daySpinner.sendKeys(Key.ARROW_DOWN);
381385

382-
// Subtract a day to the control
383-
control = (control - 1) % daysInMonth;
386+
// Set to first of month
387+
await daySpinner.sendKeys(Key.HOME);
388+
389+
await daySpinner.sendKeys(Key.ARROW_DOWN);
384390

385391
t.is(
386392
parseInt(await daySpinner.getText()),
@@ -395,7 +401,7 @@ ariaTest(
395401
}
396402

397403
// Subtract 30 days to the control
398-
control = daysInMonth + ((control - 30) % daysInMonth);
404+
control -= 30;
399405

400406
t.is(
401407
parseInt(await daySpinner.getText()),
@@ -408,6 +414,7 @@ ariaTest(
408414

409415
ariaTest('up arrow on month', exampleFile, 'spinbutton-up-arrow', async (t) => {
410416
let date = new Date();
417+
date.setDate(1); // This is necessary to do the correct date math for months.
411418

412419
let monthSpinner = await t.context.session.findElement(
413420
By.css(ex.monthSelector)
@@ -417,7 +424,6 @@ ariaTest('up arrow on month', exampleFile, 'spinbutton-up-arrow', async (t) => {
417424
for (let i = 1; i <= 12; i++) {
418425
await monthSpinner.sendKeys(Key.ARROW_UP);
419426
const index = new Date(date.setMonth(date.getMonth() + 1)).getMonth();
420-
console.log(index);
421427
t.is(
422428
await monthSpinner.getText(),
423429
valuesMonth[index],

0 commit comments

Comments
 (0)