-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
SSR support for class contextType #13889
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
266 changes: 266 additions & 0 deletions
266
packages/react-dom/src/__tests__/ReactDOMServerIntegrationClassContextType-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils'); | ||
|
||
let React; | ||
let ReactDOM; | ||
let ReactDOMServer; | ||
|
||
function initModules() { | ||
// Reset warning cache. | ||
jest.resetModuleRegistry(); | ||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
ReactDOMServer = require('react-dom/server'); | ||
|
||
// Make them available to the helpers. | ||
return { | ||
ReactDOM, | ||
ReactDOMServer, | ||
}; | ||
} | ||
|
||
const {resetModules, itRenders} = ReactDOMServerIntegrationUtils(initModules); | ||
|
||
describe('ReactDOMServerIntegration', () => { | ||
beforeEach(() => { | ||
resetModules(); | ||
}); | ||
|
||
describe('class contextType', function() { | ||
let PurpleContext, RedContext, Context; | ||
beforeEach(() => { | ||
Context = React.createContext('none'); | ||
|
||
class Parent extends React.Component { | ||
render() { | ||
return ( | ||
<Context.Provider value={this.props.text}> | ||
{this.props.children} | ||
</Context.Provider> | ||
); | ||
} | ||
} | ||
PurpleContext = props => <Parent text="purple">{props.children}</Parent>; | ||
RedContext = props => <Parent text="red">{props.children}</Parent>; | ||
}); | ||
|
||
itRenders('class child with context', async render => { | ||
class ClassChildWithContext extends React.Component { | ||
static contextType = Context; | ||
render() { | ||
const text = this.context; | ||
return <div>{text}</div>; | ||
} | ||
} | ||
|
||
const e = await render( | ||
<PurpleContext> | ||
<ClassChildWithContext /> | ||
</PurpleContext>, | ||
); | ||
expect(e.textContent).toBe('purple'); | ||
}); | ||
|
||
itRenders('class child without context', async render => { | ||
class ClassChildWithoutContext extends React.Component { | ||
render() { | ||
// this should render blank; context isn't passed to this component. | ||
return ( | ||
<div>{typeof this.context === 'string' ? this.context : ''}</div> | ||
); | ||
} | ||
} | ||
|
||
const e = await render( | ||
<PurpleContext> | ||
<ClassChildWithoutContext /> | ||
</PurpleContext>, | ||
); | ||
expect(e.textContent).toBe(''); | ||
}); | ||
|
||
itRenders('class child with wrong context', async render => { | ||
class ClassChildWithWrongContext extends React.Component { | ||
static contextType = Context; | ||
render() { | ||
// this should render blank; context.foo isn't passed to this component. | ||
return <div id="classWrongChild">{this.context.foo}</div>; | ||
} | ||
} | ||
|
||
const e = await render( | ||
<PurpleContext> | ||
<ClassChildWithWrongContext /> | ||
</PurpleContext>, | ||
); | ||
expect(e.textContent).toBe(''); | ||
}); | ||
|
||
itRenders('with context passed through to a grandchild', async render => { | ||
class Grandchild extends React.Component { | ||
static contextType = Context; | ||
render() { | ||
return <div>{this.context}</div>; | ||
} | ||
} | ||
|
||
const Child = props => <Grandchild />; | ||
|
||
const e = await render( | ||
<PurpleContext> | ||
<Child /> | ||
</PurpleContext>, | ||
); | ||
expect(e.textContent).toBe('purple'); | ||
}); | ||
|
||
itRenders('a child context overriding a parent context', async render => { | ||
class Grandchild extends React.Component { | ||
static contextType = Context; | ||
render() { | ||
return <div>{this.context}</div>; | ||
} | ||
} | ||
|
||
const e = await render( | ||
<PurpleContext> | ||
<RedContext> | ||
<Grandchild /> | ||
</RedContext> | ||
</PurpleContext>, | ||
); | ||
expect(e.textContent).toBe('red'); | ||
}); | ||
|
||
itRenders('multiple contexts', async render => { | ||
const Theme = React.createContext('dark'); | ||
const Language = React.createContext('french'); | ||
class Parent extends React.Component { | ||
render() { | ||
return ( | ||
<Theme.Provider value="light"> | ||
<Child /> | ||
</Theme.Provider> | ||
); | ||
} | ||
} | ||
|
||
function Child() { | ||
return ( | ||
<Language.Provider value="english"> | ||
<Grandchild /> | ||
</Language.Provider> | ||
); | ||
} | ||
|
||
class ThemeComponent extends React.Component { | ||
static contextType = Theme; | ||
render() { | ||
return <div id="theme">{this.context}</div>; | ||
} | ||
} | ||
|
||
class LanguageComponent extends React.Component { | ||
static contextType = Language; | ||
render() { | ||
return <div id="language">{this.context}</div>; | ||
} | ||
} | ||
|
||
const Grandchild = props => { | ||
return ( | ||
<div> | ||
<ThemeComponent /> | ||
<LanguageComponent /> | ||
</div> | ||
); | ||
}; | ||
|
||
const e = await render(<Parent />); | ||
expect(e.querySelector('#theme').textContent).toBe('light'); | ||
expect(e.querySelector('#language').textContent).toBe('english'); | ||
}); | ||
|
||
itRenders('nested context unwinding', async render => { | ||
const Theme = React.createContext('dark'); | ||
const Language = React.createContext('french'); | ||
|
||
class ThemeConsumer extends React.Component { | ||
static contextType = Theme; | ||
render() { | ||
return this.props.children(this.context); | ||
} | ||
} | ||
|
||
class LanguageConsumer extends React.Component { | ||
static contextType = Language; | ||
render() { | ||
return this.props.children(this.context); | ||
} | ||
} | ||
|
||
const App = () => ( | ||
<div> | ||
<Theme.Provider value="light"> | ||
<Language.Provider value="english"> | ||
<Theme.Provider value="dark"> | ||
<ThemeConsumer> | ||
{theme => <div id="theme1">{theme}</div>} | ||
</ThemeConsumer> | ||
</Theme.Provider> | ||
<ThemeConsumer> | ||
{theme => <div id="theme2">{theme}</div>} | ||
</ThemeConsumer> | ||
<Language.Provider value="sanskrit"> | ||
<Theme.Provider value="blue"> | ||
<Theme.Provider value="red"> | ||
<LanguageConsumer> | ||
{() => ( | ||
<Language.Provider value="chinese"> | ||
<Language.Provider value="hungarian" /> | ||
<LanguageConsumer> | ||
{language => <div id="language1">{language}</div>} | ||
</LanguageConsumer> | ||
</Language.Provider> | ||
)} | ||
</LanguageConsumer> | ||
</Theme.Provider> | ||
<LanguageConsumer> | ||
{language => ( | ||
<React.Fragment> | ||
<ThemeConsumer> | ||
{theme => <div id="theme3">{theme}</div>} | ||
</ThemeConsumer> | ||
<div id="language2">{language}</div> | ||
</React.Fragment> | ||
)} | ||
</LanguageConsumer> | ||
</Theme.Provider> | ||
</Language.Provider> | ||
</Language.Provider> | ||
</Theme.Provider> | ||
<LanguageConsumer> | ||
{language => <div id="language3">{language}</div>} | ||
</LanguageConsumer> | ||
</div> | ||
); | ||
let e = await render(<App />); | ||
expect(e.querySelector('#theme1').textContent).toBe('dark'); | ||
expect(e.querySelector('#theme2').textContent).toBe('light'); | ||
expect(e.querySelector('#theme3').textContent).toBe('blue'); | ||
expect(e.querySelector('#language1').textContent).toBe('chinese'); | ||
expect(e.querySelector('#language2').textContent).toBe('sanskrit'); | ||
expect(e.querySelector('#language3').textContent).toBe('french'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bonus fix. Better feature test.