Skip to content

Commit

Permalink
Swap expect(ReactNoop) for expect(Scheduler) (#14971)
Browse files Browse the repository at this point in the history
* Swap expect(ReactNoop) for expect(Scheduler)

In the previous commits, I upgraded our custom Jest matchers for the
noop and test renderers to use Scheduler under the hood.

Now that all these matchers are using Scheduler, we can drop
support for passing ReactNoop and test roots and always pass
Scheduler directly.

* Externalize Scheduler in noop and test bundles

I also noticed we don't need to regenerator runtime in noop anymore.
  • Loading branch information
acdlite authored Feb 28, 2019
1 parent fb64460 commit c016cae
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 38 deletions.
3 changes: 2 additions & 1 deletion src/__tests__/ReactTestRenderer-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const prettyFormat = require('pretty-format');
// Isolate noop renderer
jest.resetModules();
const ReactNoop = require('react-noop-renderer');
const Scheduler = require('scheduler');

// Kind of hacky, but we nullify all the instances to test the tree structure
// with jasmine's deep equality function, and test the instances separate. We
Expand Down Expand Up @@ -1018,7 +1019,7 @@ describe('ReactTestRenderer', () => {
</Context.Provider>
);
ReactNoop.render(<App />);
expect(ReactNoop).toFlushWithoutYielding();
expect(Scheduler).toFlushWithoutYielding();
ReactTestRenderer.create(<App />);
});

Expand Down
56 changes: 19 additions & 37 deletions src/__tests__/ReactTestRendererAsync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ describe('ReactTestRendererAsync', () => {
expect(renderer.toJSON()).toEqual(null);

// Flush initial mount.
expect(renderer).toFlushWithoutYielding();
expect(Scheduler).toFlushWithoutYielding();
expect(renderer.toJSON()).toEqual('Hi');

// Update
renderer.update(<Foo>Bye</Foo>);
// Not yet updated.
expect(renderer.toJSON()).toEqual('Hi');
// Flush update.
expect(renderer).toFlushWithoutYielding();
expect(Scheduler).toFlushWithoutYielding();
expect(renderer.toJSON()).toEqual('Bye');
});

Expand All @@ -64,11 +64,11 @@ describe('ReactTestRendererAsync', () => {
unstable_isConcurrent: true,
});

expect(renderer).toFlushAndYield(['A:1', 'B:1', 'C:1']);
expect(Scheduler).toFlushAndYield(['A:1', 'B:1', 'C:1']);
expect(renderer.toJSON()).toEqual(['A:1', 'B:1', 'C:1']);

renderer.update(<Parent step={2} />);
expect(renderer).toFlushAndYield(['A:2', 'B:2', 'C:2']);
expect(Scheduler).toFlushAndYield(['A:2', 'B:2', 'C:2']);
expect(renderer.toJSON()).toEqual(['A:2', 'B:2', 'C:2']);
});

Expand All @@ -91,12 +91,12 @@ describe('ReactTestRendererAsync', () => {
});

// Flush the first two siblings
expect(renderer).toFlushAndYieldThrough(['A:1', 'B:1']);
expect(Scheduler).toFlushAndYieldThrough(['A:1', 'B:1']);
// Did not commit yet.
expect(renderer.toJSON()).toEqual(null);

// Flush the remaining work
expect(renderer).toFlushAndYield(['C:1']);
expect(Scheduler).toFlushAndYield(['C:1']);
expect(renderer.toJSON()).toEqual(['A:1', 'B:1', 'C:1']);
});

Expand Down Expand Up @@ -128,7 +128,7 @@ describe('ReactTestRendererAsync', () => {
});

// Flush the some of the changes, but don't commit
expect(renderer).toFlushAndYieldThrough(['A:1']);
expect(Scheduler).toFlushAndYieldThrough(['A:1']);
expect(renderer.toJSON()).toEqual(null);

// Interrupt with higher priority properties
Expand All @@ -147,7 +147,7 @@ describe('ReactTestRendererAsync', () => {
return id;
};

const renderer = ReactTestRenderer.create(
ReactTestRenderer.create(
<div>
<Yield id="foo" />
<Yield id="bar" />
Expand All @@ -159,7 +159,7 @@ describe('ReactTestRendererAsync', () => {
);

expect(() =>
expect(renderer).toFlushAndYieldThrough(['foo', 'baz']),
expect(Scheduler).toFlushAndYieldThrough(['foo', 'baz']),
).toThrow('Expected value to equal:');
});

Expand All @@ -180,7 +180,7 @@ describe('ReactTestRendererAsync', () => {
},
);

expect(() => expect(renderer).toFlushWithoutYielding()).toThrowError(
expect(() => expect(Scheduler).toFlushWithoutYielding()).toThrowError(
'Expected value to equal:',
);

Expand All @@ -192,7 +192,7 @@ describe('ReactTestRendererAsync', () => {
</div>,
);

expect(() => expect(renderer).toFlushAndYield(['foo', 'baz'])).toThrow(
expect(() => expect(Scheduler).toFlushAndYield(['foo', 'baz'])).toThrow(
'Expected value to equal:',
);
});
Expand Down Expand Up @@ -223,34 +223,16 @@ describe('ReactTestRendererAsync', () => {
unstable_isConcurrent: true,
});

expect(renderer).toFlushAndThrow('Oh no!');
expect(ReactTestRenderer).toHaveYielded([
'A',
'B',
'C',
'D',
'A',
'B',
'C',
'D',
]);
expect(Scheduler).toFlushAndThrow('Oh no!');
expect(Scheduler).toHaveYielded(['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D']);

renderer.update(<App />);

expect(renderer).toFlushAndThrow('Oh no!');
expect(ReactTestRenderer).toHaveYielded([
'A',
'B',
'C',
'D',
'A',
'B',
'C',
'D',
]);
expect(Scheduler).toFlushAndThrow('Oh no!');
expect(Scheduler).toHaveYielded(['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D']);

renderer.update(<App />);
expect(renderer).toFlushAndThrow('Oh no!');
expect(Scheduler).toFlushAndThrow('Oh no!');
});
});

Expand All @@ -271,17 +253,17 @@ describe('ReactTestRendererAsync', () => {
}

ReactTestRenderer.create(<App />);
expect(() => expect(ReactTestRenderer).toHaveYielded(['A', 'B'])).toThrow(
expect(() => expect(Scheduler).toHaveYielded(['A', 'B'])).toThrow(
'Expected value to equal:',
);
});

it('flush methods throw if log is not empty', () => {
const renderer = ReactTestRenderer.create(<div />, {
ReactTestRenderer.create(<div />, {
unstable_isConcurrent: true,
});
Scheduler.yieldValue('Something');
expect(() => expect(renderer).toFlushWithoutYielding()).toThrow(
expect(() => expect(Scheduler).toFlushWithoutYielding()).toThrow(
'Log of yielded values is not empty.',
);
});
Expand Down

0 comments on commit c016cae

Please sign in to comment.