Skip to content

Add test coverage for readContext() on the server #14649

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

Merged
merged 2 commits into from
Jan 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ describe('ReactDOMServerIntegration', () => {
});

describe('context', function() {
let PurpleContext, RedContext, Consumer;
let Context, PurpleContextProvider, RedContextProvider, Consumer;
beforeEach(() => {
let Context = React.createContext('none');
Context = React.createContext('none');

class Parent extends React.Component {
render() {
Expand All @@ -51,8 +51,12 @@ describe('ReactDOMServerIntegration', () => {
}
}
Consumer = Context.Consumer;
PurpleContext = props => <Parent text="purple">{props.children}</Parent>;
RedContext = props => <Parent text="red">{props.children}</Parent>;
PurpleContextProvider = props => (
<Parent text="purple">{props.children}</Parent>
);
RedContextProvider = props => (
<Parent text="red">{props.children}</Parent>
);
});

itRenders('class child with context', async render => {
Expand All @@ -67,9 +71,9 @@ describe('ReactDOMServerIntegration', () => {
}

const e = await render(
<PurpleContext>
<PurpleContextProvider>
<ClassChildWithContext />
</PurpleContext>,
</PurpleContextProvider>,
);
expect(e.textContent).toBe('purple');
});
Expand All @@ -80,9 +84,9 @@ describe('ReactDOMServerIntegration', () => {
}

const e = await render(
<PurpleContext>
<PurpleContextProvider>
<FunctionChildWithContext />
</PurpleContext>,
</PurpleContextProvider>,
);
expect(e.textContent).toBe('purple');
});
Expand Down Expand Up @@ -127,9 +131,9 @@ describe('ReactDOMServerIntegration', () => {
const Child = props => <Grandchild />;

const e = await render(
<PurpleContext>
<PurpleContextProvider>
<Child />
</PurpleContext>,
</PurpleContextProvider>,
);
expect(e.textContent).toBe('purple');
});
Expand All @@ -144,15 +148,54 @@ describe('ReactDOMServerIntegration', () => {
};

const e = await render(
<PurpleContext>
<RedContext>
<PurpleContextProvider>
<RedContextProvider>
<Grandchild />
</RedContext>
</PurpleContext>,
</RedContextProvider>
</PurpleContextProvider>,
);
expect(e.textContent).toBe('red');
});

itRenders('readContext() in different components', async render => {
function readContext(Ctx, observedBits) {
const dispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher.current;
return dispatcher.readContext(Ctx, observedBits);
}

class Cls extends React.Component {
render() {
return readContext(Context);
}
}
function Fn() {
return readContext(Context);
}
const Memo = React.memo(() => {
return readContext(Context);
});
const FwdRef = React.forwardRef((props, ref) => {
return readContext(Context);
});

const e = await render(
<PurpleContextProvider>
<RedContextProvider>
<span>
<Fn />
<Cls />
<Memo />
<FwdRef />
<Consumer>{() => readContext(Context)}</Consumer>
</span>
</RedContextProvider>
</PurpleContextProvider>,
);
expect(e.textContent).toBe('redredredredred');
});

itRenders('multiple contexts', async render => {
const Theme = React.createContext('dark');
const Language = React.createContext('french');
Expand Down