Skip to content

Commit

Permalink
Use discrete semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Mar 12, 2021
1 parent 223cc2c commit c689d64
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
8 changes: 2 additions & 6 deletions packages/material-ui/src/SpeedDial/SpeedDial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createClientRender,
act,
fireEvent,
fireDiscreteEvent,
screen,
describeConformanceV5,
} from 'test/utils';
Expand Down Expand Up @@ -238,12 +239,7 @@ describe('<SpeedDial />', () => {
});
expect(queryByRole('tooltip')).not.to.equal(null);

// Manually fire an event to avoid calling act with:
// fireEvent.keyDown(actions[0], { key: 'Escape' });
const escapeEvent = new window.Event('KeyboardEvent');
escapeEvent.initEvent('keydown', true);
escapeEvent.key = 'Escape';
actions[0].dispatchEvent(escapeEvent);
fireDiscreteEvent.keyDown(actions[0], { key: 'Escape' });
act(() => {
clock.runAll();
});
Expand Down
33 changes: 30 additions & 3 deletions test/utils/fireDiscreteEvent.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { configure, fireEvent, getConfig } from '@testing-library/react';

const noWrapper = (callback) => callback();

/**
* @param {() => void} callback
* @returns {void}
*/
function withMissingActWarningsIgnored(callback) {
const originalConsoleError = console.error;
console.error = function silenceMissingActWarnings(message, ...args) {
Expand All @@ -6,9 +14,16 @@ function withMissingActWarningsIgnored(callback) {
originalConsoleError.call(console, message, ...args);
}
};

const originalConfig = getConfig();
configure({
eventWrapper: noWrapper,
});

try {
callback();
} finally {
configure(originalConfig);
console.error = originalConsoleError;
}
}
Expand All @@ -21,10 +36,22 @@ function withMissingActWarningsIgnored(callback) {
// Be aware that "discrete events" are an implementation detail of React.
// To test discrete events we cannot use `fireEvent` from `@testing-library/react` because they are all wrapped in `act`.
// `act` overrides the "discrete event" semantics with "batching" semantics: https://github.com/facebook/react/blob/3fbd47b86285b6b7bdeab66d29c85951a84d4525/packages/react-reconciler/src/ReactFiberWorkLoop.old.js#L1061-L1064
// Note that using `fireEvent` from `@testing-library/dom` would not work since /react configures both `fireEvent` to use `act` as a wrapper.
// -----------------------------------------

// eslint-disable-next-line import/prefer-default-export -- there are more than one discrete events.
export function click(element) {
// TODO: Why are there different semantics between `element.click` and `dtlFireEvent.click`
return withMissingActWarningsIgnored(() => element.click());
return withMissingActWarningsIgnored(() => {
fireEvent.click(element);
});
}

/**
* @param {Element} element
* @param {{}} [options]
* @returns {void}
*/
export function keyDown(element, options) {
return withMissingActWarningsIgnored(() => {
fireEvent.keyDown(element, options);
});
}

0 comments on commit c689d64

Please sign in to comment.