Skip to content

Commit

Permalink
Test for pollution of later renders
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jan 27, 2019
1 parent 466fe06 commit 8088074
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,46 @@ describe('ReactDOMServerIntegration', () => {
}
});

it('does not pollute later renders when stream destroyed', () => {
const LoggedInUser = React.createContext('default');

const AppWithUser = user => (
<LoggedInUser.Provider value={user}>
<header>
<LoggedInUser.Consumer>{whoAmI => whoAmI}</LoggedInUser.Consumer>
</header>
</LoggedInUser.Provider>
);

const stream = ReactDOMServer.renderToNodeStream(
AppWithUser('Amy'),
).setEncoding('utf8');

const {threadID} = stream.partialRenderer;

// Read enough to render Provider but not enough for it to be exited
stream._read(10);
expect(LoggedInUser[threadID]).toBe('Amy');

stream.destroy();

const AppWithUserNoProvider = () => (
<LoggedInUser.Consumer>{whoAmI => whoAmI}</LoggedInUser.Consumer>
);

const stream2 = ReactDOMServer.renderToNodeStream(
AppWithUserNoProvider(),
).setEncoding('utf8');

// Sanity check to ensure 2nd render has same threadID as 1st render,
// otherwise this test is not testing what it's meant to
expect(stream2.partialRenderer.threadID).toBe(threadID);

const markup = stream2.read(Infinity);

expect(markup).toBe('default');
});

it('frees context value reference when stream destroyed', () => {
const LoggedInUser = React.createContext('default');

Expand Down

0 comments on commit 8088074

Please sign in to comment.