Skip to content

Commit 30240dc

Browse files
authored
test: improve date focus tests to reduce flakiness (#4307)
1 parent 5720427 commit 30240dc

4 files changed

Lines changed: 72 additions & 90 deletions

File tree

‎packages/date-picker/test/basic.test.js‎

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import {
55
fixtureSync,
66
keyboardEventFor,
77
makeSoloTouchEvent,
8-
nextRender,
98
oneEvent,
109
tap,
1110
} from '@vaadin/testing-helpers';
1211
import { sendKeys } from '@web/test-runner-commands';
1312
import sinon from 'sinon';
1413
import '../src/vaadin-date-picker.js';
1514
import * as settings from '@polymer/polymer/lib/utils/settings.js';
16-
import { close, getOverlayContent, monthsEqual, open } from './common.js';
15+
import { close, getFocusedCell, getOverlayContent, idleCallback, monthsEqual, open } from './common.js';
1716

1817
settings.setCancelSyntheticClickEvents(false);
1918

@@ -275,13 +274,11 @@ describe('basic features', () => {
275274
it('should set focused attribute when focused', async () => {
276275
datepicker.focus();
277276
await open(datepicker);
278-
await nextRender();
279277
expect(datepicker.hasAttribute('focused')).to.be.true;
280278
});
281279

282280
it('should close the dropdown on Today button Esc', async () => {
283281
await open(datepicker);
284-
await nextRender();
285282

286283
getOverlayContent(datepicker).$.todayButton.focus();
287284
await sendKeys({ press: 'Escape' });
@@ -291,7 +288,6 @@ describe('basic features', () => {
291288

292289
it('should close the dropdown on Cancel button Esc', async () => {
293290
await open(datepicker);
294-
await nextRender();
295291

296292
getOverlayContent(datepicker).$.cancelButton.focus();
297293
await sendKeys({ press: 'Escape' });
@@ -301,7 +297,6 @@ describe('basic features', () => {
301297

302298
it('should remove focused attribute when closed and not focused', async () => {
303299
await open(datepicker);
304-
await nextRender();
305300

306301
getOverlayContent(datepicker).$.todayButton.focus();
307302
await sendKeys({ press: 'Escape' });
@@ -315,25 +310,13 @@ describe('basic features', () => {
315310
expect(spy.called).to.be.true;
316311
});
317312

318-
describe('focus date', () => {
319-
let spy;
320-
321-
beforeEach(() => {
322-
// Do not spy on the DOM element because it can be reused
323-
// by infinite scroller. Instead, spy on the native focus.
324-
spy = sinon.spy(HTMLElement.prototype, 'focus');
325-
});
326-
327-
afterEach(() => {
328-
spy.restore();
329-
});
330-
331-
it('should focus date element when opened', async () => {
332-
await open(datepicker);
333-
await nextRender();
334-
const cell = spy.lastCall.thisValue;
335-
expect(cell.hasAttribute('today')).to.be.true;
336-
});
313+
it('should focus date element when opened', async () => {
314+
await open(datepicker);
315+
await idleCallback();
316+
const content = getOverlayContent(datepicker);
317+
const cell = getFocusedCell(content);
318+
expect(cell).to.be.instanceOf(HTMLTableCellElement);
319+
expect(cell.hasAttribute('today')).to.be.true;
337320
});
338321
});
339322

‎packages/date-picker/test/common.js‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { fire, listenOnce, nextRender } from '@vaadin/testing-helpers';
1+
import { aTimeout, fire, listenOnce, nextRender } from '@vaadin/testing-helpers';
2+
import { flush } from '@polymer/polymer/lib/utils/flush.js';
23
import { afterNextRender } from '@polymer/polymer/lib/utils/render-status.js';
34

45
export function activateScroller(scroller) {
@@ -49,18 +50,38 @@ export function getDefaultI18n() {
4950

5051
export function open(datepicker) {
5152
return new Promise((resolve) => {
52-
listenOnce(datepicker.$.overlay, 'vaadin-overlay-open', resolve);
53+
listenOnce(datepicker.$.overlay, 'vaadin-overlay-open', async () => {
54+
// Wait until infinite scrollers are rendered
55+
await waitForOverlayRender();
56+
57+
resolve();
58+
});
5359
datepicker.open();
5460
});
5561
}
5662

63+
export async function waitForOverlayRender() {
64+
// Wait until infinite scrollers are rendered
65+
await aTimeout(1);
66+
await nextRender();
67+
68+
// Force dom-repeat to render table elements
69+
flush();
70+
}
71+
5772
export function close(datepicker) {
5873
return new Promise((resolve) => {
5974
listenOnce(datepicker.$.overlay, 'vaadin-overlay-close', resolve);
6075
datepicker.close();
6176
});
6277
}
6378

79+
export function idleCallback() {
80+
return new Promise((resolve) => {
81+
window.requestIdleCallback ? window.requestIdleCallback(resolve) : setTimeout(resolve, 16);
82+
});
83+
}
84+
6485
/**
6586
* Emulates clicking outside the dropdown overlay
6687
*/

‎packages/date-picker/test/keyboard-input.test.js‎

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { sendKeys } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55
import './not-animated-styles.js';
66
import '../vaadin-date-picker.js';
7-
import { close, getFocusedCell, getOverlayContent, open, waitForScrollToFinish } from './common.js';
7+
import { close, getFocusedCell, getOverlayContent, idleCallback, open, waitForScrollToFinish } from './common.js';
88

99
describe('keyboard', () => {
1010
let datepicker;
@@ -75,7 +75,6 @@ describe('keyboard', () => {
7575
it('should reflect focused date to input', async () => {
7676
datepicker.value = '2000-01-01';
7777
await open(datepicker);
78-
await nextRender(datepicker);
7978

8079
// Move focus to the calendar
8180
await sendKeys({ press: 'Tab' });
@@ -170,8 +169,8 @@ describe('keyboard', () => {
170169
beforeEach(async () => {
171170
// Open the overlay
172171
await open(datepicker);
173-
await nextRender(datepicker);
174172
overlayContent = getOverlayContent(datepicker);
173+
await idleCallback();
175174
});
176175

177176
it('should keep focused attribute when the focus moves to the overlay', async () => {
@@ -257,70 +256,43 @@ describe('keyboard', () => {
257256
});
258257

259258
describe('focus date not in the viewport', () => {
260-
let spy;
261-
262-
afterEach(() => {
263-
spy.restore();
264-
});
265-
266-
it('should focus date scrolled out of the view on input Tab', async () => {
267-
// Move focus to the calendar
268-
await sendKeys({ press: 'Tab' });
269-
await nextRender(datepicker);
259+
beforeEach(async () => {
260+
// Disable scrolling animation
261+
const stub = sinon.stub(overlayContent, 'revealDate').callsFake((date) => {
262+
stub.wrappedMethod.call(overlayContent, date, false);
263+
});
270264

271265
// Scroll to date outside viewport
272266
const date = new Date();
273267
date.setFullYear(date.getFullYear() - 1);
274268
overlayContent.revealDate(date);
275-
await waitForScrollToFinish(overlayContent);
269+
await idleCallback();
270+
});
276271

277-
// Move focus to the input
278-
await sendKeys({ down: 'Shift' });
272+
it('should focus date scrolled out of the view on input Tab', async () => {
273+
// Move focus to the calendar
279274
await sendKeys({ press: 'Tab' });
280-
await sendKeys({ up: 'Shift' });
281-
282-
// Do not spy on the DOM element because it can be reused
283-
// by infinite scroller. Instead, spy on the native focus.
284-
spy = sinon.spy(HTMLElement.prototype, 'focus');
285275

286-
// Move focus back to the date
287-
await sendKeys({ press: 'Tab' });
288276
await waitForScrollToFinish(overlayContent);
289277

290-
expect(spy.calledOnce).to.be.true;
291-
292-
const cell = spy.firstCall.thisValue;
278+
const cell = getFocusedCell(overlayContent);
279+
expect(cell).to.be.instanceOf(HTMLTableCellElement);
293280
expect(cell.hasAttribute('today')).to.be.true;
294281
});
295282

296283
it('should focus date scrolled out of the view on Today button Shift Tab', async () => {
297-
// Move focus to the calendar
298-
await sendKeys({ press: 'Tab' });
299-
await nextRender(datepicker);
300-
301-
// Scroll to date outside viewport
302-
const date = new Date();
303-
date.setFullYear(date.getFullYear() - 1);
304-
overlayContent.revealDate(date);
305-
await waitForScrollToFinish(overlayContent);
306-
307284
// Move focus to the Today button
308-
await sendKeys({ press: 'Tab' });
309-
310-
// Do not spy on the DOM element because it can be reused
311-
// by infinite scroller. Instead, spy on the native focus.
312-
spy = sinon.spy(HTMLElement.prototype, 'focus');
285+
overlayContent.$.todayButton.focus();
313286

314-
// Move focus back to the date
287+
// Move focus to the calendar
315288
await sendKeys({ down: 'Shift' });
316289
await sendKeys({ press: 'Tab' });
317290
await sendKeys({ up: 'Shift' });
318291

319292
await waitForScrollToFinish(overlayContent);
320293

321-
expect(spy.calledOnce).to.be.true;
322-
323-
const cell = spy.firstCall.thisValue;
294+
const cell = getFocusedCell(overlayContent);
295+
expect(cell).to.be.instanceOf(HTMLTableCellElement);
324296
expect(cell.hasAttribute('today')).to.be.true;
325297
});
326298
});
@@ -364,7 +336,6 @@ describe('keyboard', () => {
364336
beforeEach(async () => {
365337
// Open the overlay
366338
await open(datepicker);
367-
await nextRender(datepicker);
368339
});
369340

370341
it('should close the overlay when input is focused', async () => {
@@ -436,7 +407,6 @@ describe('keyboard', () => {
436407
beforeEach(async () => {
437408
datepicker.value = '2000-01-01';
438409
await open(datepicker);
439-
await nextRender(datepicker);
440410
input.select();
441411
});
442412

@@ -579,8 +549,7 @@ describe('keyboard', () => {
579549

580550
it('should fire change on selecting date with Enter', async () => {
581551
// Open the overlay
582-
await sendKeys({ press: 'ArrowDown' });
583-
await nextRender(datepicker);
552+
await open(datepicker);
584553

585554
// Move focus to the calendar
586555
await sendKeys({ press: 'ArrowDown' });
@@ -592,8 +561,7 @@ describe('keyboard', () => {
592561

593562
it('should fire change on selecting date with Space', async () => {
594563
// Open the overlay
595-
await sendKeys({ press: 'ArrowDown' });
596-
await nextRender(datepicker);
564+
await open(datepicker);
597565

598566
// Move focus to the calendar
599567
await sendKeys({ press: 'ArrowDown' });

‎packages/date-picker/test/keyboard-navigation.test.js‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import { sendKeys } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55
import './not-animated-styles.js';
66
import '../vaadin-date-picker.js';
7-
import { getDefaultI18n, getFocusedCell, getOverlayContent, open, waitForScrollToFinish } from './common.js';
7+
import {
8+
getDefaultI18n,
9+
getFocusedCell,
10+
getOverlayContent,
11+
idleCallback,
12+
open,
13+
waitForOverlayRender,
14+
waitForScrollToFinish,
15+
} from './common.js';
816

917
(isIOS ? describe.skip : describe)('keyboard navigation', () => {
1018
describe('date-picker', () => {
@@ -21,7 +29,6 @@ import { getDefaultI18n, getFocusedCell, getOverlayContent, open, waitForScrollT
2129
it('should be focused on today if no value / initial position is set', async () => {
2230
const today = new Date();
2331
await open(datepicker);
24-
await nextRender(datepicker);
2532

2633
// Move focus to the calendar
2734
await sendKeys({ press: 'Tab' });
@@ -35,7 +42,7 @@ import { getDefaultI18n, getFocusedCell, getOverlayContent, open, waitForScrollT
3542

3643
input.click();
3744
await oneEvent(datepicker.$.overlay, 'vaadin-overlay-open');
38-
await nextRender(datepicker);
45+
await waitForOverlayRender();
3946

4047
// Reset overlay focused date
4148
input.click();
@@ -57,7 +64,6 @@ import { getDefaultI18n, getFocusedCell, getOverlayContent, open, waitForScrollT
5764

5865
it('should be focused on selected value when overlay is opened', async () => {
5966
await open(datepicker);
60-
await nextRender(datepicker);
6167

6268
// Move focus to the calendar
6369
await sendKeys({ press: 'Tab' });
@@ -68,7 +74,6 @@ import { getDefaultI18n, getFocusedCell, getOverlayContent, open, waitForScrollT
6874

6975
it('should not lose focused date after deselecting', async () => {
7076
await open(datepicker);
71-
await nextRender(datepicker);
7277

7378
const content = getOverlayContent(datepicker);
7479
const focused = content.focusedDate;
@@ -93,7 +98,6 @@ import { getDefaultI18n, getFocusedCell, getOverlayContent, open, waitForScrollT
9398

9499
it('should be focused on initial position when opened', async () => {
95100
await open(datepicker);
96-
await nextRender(datepicker);
97101

98102
// Move focus to the calendar
99103
await sendKeys({ press: 'Tab' });
@@ -105,7 +109,7 @@ import { getDefaultI18n, getFocusedCell, getOverlayContent, open, waitForScrollT
105109
it('should be focused on initial position when focused date is empty', async () => {
106110
input.click();
107111
await oneEvent(datepicker.$.overlay, 'vaadin-overlay-open');
108-
await nextRender(datepicker);
112+
await waitForOverlayRender();
109113

110114
// Reset overlay focused date
111115
input.click();
@@ -124,16 +128,20 @@ import { getDefaultI18n, getFocusedCell, getOverlayContent, open, waitForScrollT
124128

125129
beforeEach(async () => {
126130
overlay = fixtureSync(`
127-
<vaadin-date-picker-overlay-content
128-
style="position: absolute; top: 0"
129-
scroll-duration="0"
130-
></vaadin-date-picker-overlay-content>`);
131+
<vaadin-date-picker-overlay-content
132+
style="position: absolute; top: 0; width: 400px"
133+
scroll-duration="0"
134+
></vaadin-date-picker-overlay-content>
135+
`);
131136
overlay.i18n = getDefaultI18n();
132137

138+
// Set initialPosition to activate scrollers
133139
const initialDate = new Date(2000, 0, 1);
134140
overlay.initialPosition = initialDate;
135-
await nextRender(overlay);
141+
142+
await waitForOverlayRender();
136143
await overlay.focusDate(initialDate);
144+
await idleCallback();
137145
});
138146

139147
it('should focus one week forward with arrow down', async () => {
@@ -324,12 +332,14 @@ import { getDefaultI18n, getFocusedCell, getOverlayContent, open, waitForScrollT
324332
it('should move to max date when targeted date is disabled', async () => {
325333
overlay.maxDate = new Date(2000, 0, 7);
326334
await sendKeys({ down: 'ArrowDown' });
335+
await waitForScrollToFinish(overlay);
327336
expect(overlay.focusedDate).to.eql(new Date(2000, 0, 7));
328337
});
329338

330339
it('should move to min date when targeted date is disabled', async () => {
331340
overlay.minDate = new Date(1999, 11, 26);
332341
await sendKeys({ down: 'ArrowUp' });
342+
await waitForScrollToFinish(overlay);
333343
expect(overlay.focusedDate).to.eql(new Date(1999, 11, 26));
334344
});
335345

0 commit comments

Comments
 (0)