Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestRenderer warns if flushThrough is passed the wrong params #12909

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/react-test-renderer/src/ReactTestRendererScheduling.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,22 @@ export function flushThrough(expectedValues: Array<mixed>): Array<mixed> {
}
if (yieldedValues === null) {
// Always return an array.
return [];
yieldedValues = [];
}
for (let i = 0; i < expectedValues.length; i++) {
const expectedValue = `"${(expectedValues[i]: any)}"`;
const yieldedValue =
i < yieldedValues.length ? `"${(yieldedValues[i]: any)}"` : 'nothing';
if (yieldedValue !== expectedValue) {
const error = new Error(
`flushThrough expected to yield ${(expectedValue: any)}, but ${(yieldedValue: any)} was yielded`,
);
// Attach expected and yielded arrays,
// So the caller could pretty print the diff (if desired).
(error: any).expectedValues = expectedValues;
(error: any).actualValues = yieldedValues;
throw error;
}
}
return yieldedValues;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@

'use strict';

const React = require('react');
const ReactTestRenderer = require('react-test-renderer');
let React;
let ReactTestRenderer;

describe('ReactTestRendererAsync', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactTestRenderer = require('react-test-renderer');
});

it('flushAll flushes all work', () => {
function Foo(props) {
return props.children;
Expand Down Expand Up @@ -134,4 +140,56 @@ describe('ReactTestRendererAsync', () => {
// Only the higher priority properties have been committed
expect(renderer.toJSON()).toEqual(['A:2', 'B:2']);
});

it('should error if flushThrough params dont match yielded values', () => {
const Yield = ({id}) => {
renderer.unstable_yield(id);
return id;
};

const renderer = ReactTestRenderer.create(
<div>
<Yield id="foo" />
<Yield id="bar" />
<Yield id="baz" />
</div>,
{
unstable_isAsync: true,
},
);

expect(() => renderer.unstable_flushThrough(['foo', 'baz'])).toThrow(
'flushThrough expected to yield "baz", but "bar" was yielded',
);
});

it('should error if flushThrough yields the wrong number of values', () => {
const Yield = ({id}) => {
renderer.unstable_yield(id);
return id;
};

const renderer = ReactTestRenderer.create(
<div>
<Yield id="foo" />
</div>,
{
unstable_isAsync: true,
},
);

expect(() => renderer.unstable_flushThrough(['foo', 'bar'])).toThrow(
'flushThrough expected to yield "bar", but nothing was yielded',
);
});

it('should error if flushThrough yields no values', () => {
const renderer = ReactTestRenderer.create(null, {
unstable_isAsync: true,
});

expect(() => renderer.unstable_flushThrough(['foo'])).toThrow(
'flushThrough expected to yield "foo", but nothing was yielded',
);
});
});
4 changes: 3 additions & 1 deletion packages/react/src/__tests__/ReactProfiler-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,9 @@ describe('Profiler', () => {
</React.unstable_Profiler>,
{unstable_isAsync: true},
);
expect(renderer.unstable_flushThrough(['first'])).toEqual(['Yield:10']);
expect(renderer.unstable_flushThrough(['Yield:10'])).toEqual([
Copy link
Contributor Author

@bvaughn bvaughn May 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR caught this (actual) mistake from master.

'Yield:10',
]);
expect(callback).toHaveBeenCalledTimes(0);

// Simulate time moving forward while frame is paused.
Expand Down