Skip to content

Commit

Permalink
test: adapt for async ripple
Browse files Browse the repository at this point in the history
  • Loading branch information
romgrk committed Jun 6, 2024
1 parent f1f23d7 commit 39e2bc7
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 134 deletions.
4 changes: 4 additions & 0 deletions packages-internal/test-utils/src/focusVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function simulatePointerDevice() {
fireEvent.pointerDown(document.body);
}

export function simulateKeyboardDevice() {
fireEvent.keyDown(document.body, { key: 'TAB' });
}

/**
* See https://bugs.chromium.org/p/chromium/issues/detail?id=1127875 for more details.
*/
Expand Down
30 changes: 14 additions & 16 deletions packages/mui-material/src/Button/Button.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as React from 'react';
import { expect } from 'chai';
import { act, createRenderer, fireEvent, screen } from '@mui/internal-test-utils';
import { act, createRenderer, fireEvent, screen, simulateKeyboardDevice } from '@mui/internal-test-utils';
import { ClassNames } from '@emotion/react';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import Button, { buttonClasses as classes } from '@mui/material/Button';
import ButtonBase, { touchRippleClasses } from '@mui/material/ButtonBase';
import describeConformance from '../../test/describeConformance';
import * as ripple from '../../test/ripple';

describe('<Button />', () => {
const { render, renderToString } = createRenderer();
Expand Down Expand Up @@ -555,23 +556,23 @@ describe('<Button />', () => {
expect(endIcon).not.to.have.class(classes.startIcon);
});

it('should have a ripple by default', () => {
it('should have a ripple', async () => {
const { getByRole } = render(
<Button TouchRippleProps={{ className: 'touch-ripple' }}>Hello World</Button>,
);
const button = getByRole('button');

await ripple.startTouch(button);
expect(button.querySelector('.touch-ripple')).not.to.equal(null);
});

it('can disable the ripple', () => {
it('can disable the ripple', async () => {
const { getByRole } = render(
<Button disableRipple TouchRippleProps={{ className: 'touch-ripple' }}>
Hello World
</Button>,
);
const button = getByRole('button');

await ripple.startTouch(button);
expect(button.querySelector('.touch-ripple')).to.equal(null);
});

Expand All @@ -582,23 +583,21 @@ describe('<Button />', () => {
expect(button).to.have.class(classes.disableElevation);
});

it('should have a focusRipple by default', () => {
it('should have a focusRipple', async () => {
const { getByRole } = render(
<Button TouchRippleProps={{ classes: { ripplePulsate: 'pulsate-focus-visible' } }}>
Hello World
</Button>,
);
const button = getByRole('button');

fireEvent.keyDown(document.body, { key: 'TAB' });
act(() => {
button.focus();
});
simulateKeyboardDevice();
await ripple.startFocus(button);

expect(button.querySelector('.pulsate-focus-visible')).not.to.equal(null);
});

it('can disable the focusRipple', () => {
it('can disable the focusRipple', async () => {
const { getByRole } = render(
<Button
disableFocusRipple
Expand All @@ -609,10 +608,8 @@ describe('<Button />', () => {
);
const button = getByRole('button');

act(() => {
fireEvent.keyDown(document.body, { key: 'TAB' });
button.focus();
});
simulateKeyboardDevice();
await ripple.startFocus(button);

expect(button.querySelector('.pulsate-focus-visible')).to.equal(null);
});
Expand Down Expand Up @@ -666,7 +663,7 @@ describe('<Button />', () => {
expect(container.firstChild.querySelector(`.${touchRippleClasses.root}`)).to.equal(null);
});

it("should disable ripple when MuiButtonBase has disableRipple in theme's defaultProps but override on the individual Buttons if provided", () => {
it("should disable ripple when MuiButtonBase has disableRipple in theme's defaultProps but override on the individual Buttons if provided", async () => {
const theme = createTheme({
components: {
MuiButtonBase: {
Expand All @@ -683,6 +680,7 @@ describe('<Button />', () => {
<Button>Disabled ripple 2</Button>
</ThemeProvider>,
);
await ripple.startTouch(container.querySelector('button'))
expect(container.querySelectorAll(`.${touchRippleClasses.root}`)).to.have.length(1);
});

Expand Down
Loading

0 comments on commit 39e2bc7

Please sign in to comment.