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

[ButtonBase] Change tests to use faketimers #6559

Merged
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
2 changes: 2 additions & 0 deletions src/internal/ButtonBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export default class ButtonBase extends Component {
keyDown = false; // Used to help track keyboard activation keyDown
button = null;
keyboardFocusTimeout = undefined;
keyboardFocusCheckTime = 40;
keyboardFocusMaxCheckTimes = 5;

focus = () => this.button.focus();

Expand Down
36 changes: 24 additions & 12 deletions src/internal/ButtonBase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import keycode from 'keycode';
import { assert } from 'chai';
import { spy } from 'sinon';
import { spy, useFakeTimers } from 'sinon';
import { createShallow, createMount } from 'src/test-utils';
import ButtonBase, { styleSheet } from './ButtonBase';

Expand Down Expand Up @@ -287,12 +287,20 @@ describe('<ButtonBase />', () => {
});

describe('mounted tab press listener', () => {
it('should listen for tab presses and set keyboard focus', (done) => {
const wrapper = mount(
let wrapper;
let instance;
let button;
let clock;

before(() => {
clock = useFakeTimers();
wrapper = mount(
<ButtonBase id="test-button">Hello</ButtonBase>,
);
instance = wrapper.instance();

button = document.getElementById('test-button');

const button = document.getElementById('test-button');
if (!button) {
throw new Error('missing button');
}
Expand All @@ -301,15 +309,19 @@ describe('<ButtonBase />', () => {
const event = new window.Event('keyup');
event.which = keycode('tab');
window.dispatchEvent(event);
});

after(() => {
clock.restore();
});

it('should not set keyboard focus before time has passed', () => {
assert.strictEqual(wrapper.state('keyboardFocused'), false, 'should not be keyboardFocused');
});

setTimeout(() => {
assert.strictEqual(
wrapper.state('keyboardFocused'),
true,
'should be keyboardFocused',
);
done();
}, 200);
it('should listen for tab presses and set keyboard focus', () => {
clock.tick(instance.keyboardFocusCheckTime * instance.keyboardFocusMaxCheckTimes);
assert.strictEqual(wrapper.state('keyboardFocused'), true, 'should be keyboardFocused');
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/utils/keyboardFocus.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export function detectKeyboardFocus(instance, element, cb, attempt = 1) {
(document.activeElement === element || contains(element, document.activeElement))
) {
cb();
} else if (attempt < 5) {
} else if (attempt < instance.keyboardFocusMaxCheckTimes) {
detectKeyboardFocus(instance, element, cb, attempt + 1);
}
}, 40);
}, instance.keyboardFocusCheckTime);
}

export function listenForFocusKeys() {
Expand Down