Skip to content

Commit

Permalink
[Chip] Added :focus-visible test case
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmotoc authored and oliviertassinari committed Aug 31, 2020
1 parent 367550a commit 9ed548c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
19 changes: 2 additions & 17 deletions packages/material-ui/src/ButtonBase/ButtonBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,14 @@ import {
act,
createClientRender,
fireEvent,
focusVisible,
screen,
simulatePointerDevice,
} from 'test/utils';
import * as PropTypes from 'prop-types';
import TouchRipple from './TouchRipple';
import ButtonBase from './ButtonBase';

/**
* @param {HTMLElement} element
*/
function focusVisible(element) {
act(() => {
element.blur();
fireEvent.keyDown(document.body, { key: 'Tab' });
element.focus();
});
}

function simulatePointerDevice() {
// first focus on a page triggers focus visible until a pointer event
// has been dispatched
fireEvent.pointerDown(document.body);
}

describe('<ButtonBase />', () => {
const render = createClientRender();
/**
Expand Down
19 changes: 18 additions & 1 deletion packages/material-ui/src/Chip/Chip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
act,
createClientRender,
fireEvent,
focusVisible,
simulatePointerDevice,
} from 'test/utils';
import CheckBox from '../internal/svg-icons/CheckBox';
import Avatar from '../Avatar';
Expand Down Expand Up @@ -398,7 +400,7 @@ describe('<Chip />', () => {
const input = container.querySelector('input');
act(() => {
input.focus();
})
});
fireEvent.keyDown(input, { key: 'Backspace' });

// defaultPrevented?
Expand Down Expand Up @@ -538,4 +540,19 @@ describe('<Chip />', () => {
expect(icon).to.have.class(classes.deleteIconSmall);
});
});

describe('event: focus', () => {
it('has a focus-visible polyfill', () => {
const { container } = render(<Chip label="Test Chip" onClick={() => {}} />);
const chip = container.querySelector(`.${classes.root}`);
simulatePointerDevice();

expect(chip).not.to.have.class(classes.focusVisible);
chip.focus();
expect(chip).not.to.have.class(classes.focusVisible);
focusVisible(chip);

expect(chip).to.have.class(classes.focusVisible);
});
});
});
18 changes: 18 additions & 0 deletions test/utils/focusVisible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { act, fireEvent } from './createClientRender';

/**
* @param {HTMLElement} element
*/
export function focusVisible(element) {
act(() => {
element.blur();
fireEvent.keyDown(document.body, { key: 'Tab' });
element.focus();
});
}

export function simulatePointerDevice() {
// first focus on a page triggers focus visible until a pointer event
// has been dispatched
fireEvent.pointerDown(document.body);
}
1 change: 1 addition & 0 deletions test/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export { default as createMount } from './createMount';
export { default as createServerRender } from './createServerRender';
export { default as createShallow } from './createShallow';
export { default as findOutermostIntrinsic, wrapsIntrinsicElement } from './findOutermostIntrinsic';
export * from './focusVisible';
export { default as getClasses } from './getClasses';
export {} from './initMatchers';

0 comments on commit 9ed548c

Please sign in to comment.