Skip to content

Commit

Permalink
Codemod tests to waitFor pattern (4/?) (#26302)
Browse files Browse the repository at this point in the history
This converts some of our test suite to use the `waitFor` test pattern,
instead of the `expect(Scheduler).toFlushAndYield` pattern. Most of
these changes are automated with jscodeshift, with some slight manual
cleanup in certain cases.

See #26285 for full context.
  • Loading branch information
acdlite committed Mar 4, 2023
1 parent 06460b6 commit faacefb
Show file tree
Hide file tree
Showing 13 changed files with 1,095 additions and 1,274 deletions.
Expand Up @@ -18,6 +18,8 @@ describe('SimpleEventPlugin', function () {

let onClick;
let container;
let assertLog;
let waitForAll;

function expectClickThru(element) {
element.click();
Expand All @@ -43,6 +45,10 @@ describe('SimpleEventPlugin', function () {
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');

const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
waitForAll = InternalTestUtils.waitForAll;

onClick = jest.fn();
});

Expand Down Expand Up @@ -222,12 +228,12 @@ describe('SimpleEventPlugin', function () {

ReactDOM.render(<Button />, container);
expect(button.textContent).toEqual('Count: 0');
expect(Scheduler).toHaveYielded([]);
assertLog([]);

click();

// There should be exactly one update.
expect(Scheduler).toHaveYielded(['didUpdate - Count: 3']);
assertLog(['didUpdate - Count: 3']);
expect(button.textContent).toEqual('Count: 3');
});

Expand All @@ -240,6 +246,10 @@ describe('SimpleEventPlugin', function () {
ReactDOMClient = require('react-dom/client');
Scheduler = require('scheduler');

const InternalTestUtils = require('internal-test-utils');
assertLog = InternalTestUtils.assertLog;
waitForAll = InternalTestUtils.waitForAll;

act = require('jest-react').act;
});

Expand Down Expand Up @@ -274,10 +284,10 @@ describe('SimpleEventPlugin', function () {
// Initial mount
root.render(<Button />);
// Should not have flushed yet because it's async
expect(Scheduler).toHaveYielded([]);
assertLog([]);
expect(button).toBe(undefined);
// Flush async work
expect(Scheduler).toFlushAndYield(['render button: enabled']);
await waitForAll(['render button: enabled']);

function click() {
const event = new MouseEvent('click', {
Expand All @@ -292,7 +302,7 @@ describe('SimpleEventPlugin', function () {

// Click the button to trigger the side-effect
await act(async () => click());
expect(Scheduler).toHaveYielded([
assertLog([
// The handler fired
'Side-effect',
// The component re-rendered synchronously, even in concurrent mode.
Expand All @@ -301,7 +311,7 @@ describe('SimpleEventPlugin', function () {

// Click the button again
click();
expect(Scheduler).toHaveYielded([
assertLog([
// The event handler was removed from the button, so there's no effect.
]);

Expand All @@ -312,7 +322,7 @@ describe('SimpleEventPlugin', function () {
click();
click();
click();
expect(Scheduler).toFlushAndYield([]);
await waitForAll([]);
});

// NOTE: This test was written for the old behavior of discrete updates,
Expand Down Expand Up @@ -345,7 +355,7 @@ describe('SimpleEventPlugin', function () {
// Should not have flushed yet because it's async
expect(button).toBe(undefined);
// Flush async work
Scheduler.unstable_flushAll();
await waitForAll([]);
expect(button.textContent).toEqual('Count: 0');

function click() {
Expand Down Expand Up @@ -373,7 +383,7 @@ describe('SimpleEventPlugin', function () {
await act(async () => click());

// Flush the remaining work
Scheduler.unstable_flushAll();
await waitForAll([]);
// The counter should equal the total number of clicks
expect(button.textContent).toEqual('Count: 7');
});
Expand Down

0 comments on commit faacefb

Please sign in to comment.