-
Notifications
You must be signed in to change notification settings - Fork 51k
Add async/await to unit tests; modify server rendering tests to use async/await. #9089
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
sophiebits
merged 12 commits into
facebook:master
from
aickin:server-render-unit-tests-async-await
Mar 3, 2017
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
437d029
Added a handful of SSR unit tests, ported from a previous pull request.
aickin 7c85408
Fixing linting errors
aickin c1d9787
Fixed a test helper function to properly report errors.
aickin 5531bf3
Merge branch 'master' into server-render-unit-tests
aickin 9257660
Un-nested the new rendering tests. Updated the fiber test passing/not…
aickin 08b4936
Edited to comply with the react/jsx-space-before-closing eslint rule,…
aickin a91c836
Response to code review from @spicyj. Moved tests to separate file, r…
aickin 1d680be
Converted the unit tests to use async-await style.
aickin 66fe606
Merge branch 'master' into server-render-unit-tests
aickin 0cb238e
Merge branch 'server-render-unit-tests' into server-render-unit-tests…
aickin d9ac8bb
Moved async-await babel transform for tests from .babelrc to preproce…
aickin ec62549
Response to code review in PR #9089. Thanks, @spicyj!
aickin 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
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -19,70 +19,70 @@ let ReactDOMServer; | |
| // Helper functions for rendering tests | ||
| // ==================================== | ||
|
|
||
| // promisified version of ReactDOM.render() | ||
| function asyncReactDOMRender(reactElement, domElement) { | ||
| return new Promise(resolve => | ||
| ReactDOM.render(reactElement, domElement, resolve)); | ||
| } | ||
| // performs fn asynchronously and expects count errors logged to console.error. | ||
| // will fail the test if the count of errors logged is not equal to count. | ||
| function expectErrors(fn, count) { | ||
| async function expectErrors(fn, count) { | ||
| if (console.error.calls && console.error.calls.reset) { | ||
| console.error.calls.reset(); | ||
| } else { | ||
| spyOn(console, 'error'); | ||
| } | ||
|
|
||
| return fn().then((result) => { | ||
| if (console.error.calls.count() !== count) { | ||
| console.log(`We expected ${count} warning(s), but saw ${console.error.calls.count()} warning(s).`); | ||
| if (console.error.calls.count() > 0) { | ||
| console.log(`We saw these warnings:`); | ||
| for (var i = 0; i < console.error.calls.count(); i++) { | ||
| console.log(console.error.calls.argsFor(i)[0]); | ||
| } | ||
| const result = await fn(); | ||
| if (console.error.calls.count() !== count && console.error.calls.count() !== 0) { | ||
| console.log(`We expected ${count} warning(s), but saw ${console.error.calls.count()} warning(s).`); | ||
| if (console.error.calls.count() > 0) { | ||
| console.log(`We saw these warnings:`); | ||
| for (var i = 0; i < console.error.calls.count(); i++) { | ||
| console.log(console.error.calls.argsFor(i)[0]); | ||
| } | ||
| } | ||
| expectDev(console.error.calls.count()).toBe(count); | ||
| return result; | ||
| }); | ||
| } | ||
| expectDev(console.error.calls.count()).toBe(count); | ||
| return result; | ||
| } | ||
|
|
||
| // renders the reactElement into domElement, and expects a certain number of errors. | ||
| // returns a Promise that resolves when the render is complete. | ||
| function renderIntoDom(reactElement, domElement, errorCount = 0) { | ||
| return expectErrors( | ||
| () => new Promise((resolve) => { | ||
| async () => { | ||
| ExecutionEnvironment.canUseDOM = true; | ||
| ReactDOM.render(reactElement, domElement, () => { | ||
| ExecutionEnvironment.canUseDOM = false; | ||
| resolve(domElement.firstChild); | ||
| }); | ||
| }), | ||
| await asyncReactDOMRender(reactElement, domElement); | ||
| ExecutionEnvironment.canUseDOM = false; | ||
| return domElement.firstChild; | ||
| }, | ||
| errorCount | ||
| ); | ||
| } | ||
|
|
||
| // Renders text using SSR and then stuffs it into a DOM node; returns the DOM | ||
| // element that corresponds with the reactElement. | ||
| // Does not render on client or perform client-side revival. | ||
| function serverRender(reactElement, errorCount = 0) { | ||
| return expectErrors( | ||
| async function serverRender(reactElement, errorCount = 0) { | ||
| const markup = await expectErrors( | ||
| () => Promise.resolve(ReactDOMServer.renderToString(reactElement)), | ||
| errorCount) | ||
| .then((markup) => { | ||
| var domElement = document.createElement('div'); | ||
| domElement.innerHTML = markup; | ||
| return domElement.firstChild; | ||
| }); | ||
| errorCount); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: fb style is to have this closing ); on the next line, unindented |
||
| var domElement = document.createElement('div'); | ||
| domElement.innerHTML = markup; | ||
| return domElement.firstChild; | ||
| } | ||
|
|
||
| const clientCleanRender = (element, errorCount = 0) => { | ||
| const div = document.createElement('div'); | ||
| return renderIntoDom(element, div, errorCount); | ||
| }; | ||
|
|
||
| const clientRenderOnServerString = (element, errorCount = 0) => { | ||
| return serverRender(element, errorCount).then((markup) => { | ||
| var domElement = document.createElement('div'); | ||
| domElement.innerHTML = markup; | ||
| return renderIntoDom(element, domElement, errorCount); | ||
| }); | ||
| const clientRenderOnServerString = async (element, errorCount = 0) => { | ||
| const markup = await serverRender(element, errorCount); | ||
| var domElement = document.createElement('div'); | ||
| domElement.innerHTML = markup; | ||
| return renderIntoDom(element, domElement, errorCount); | ||
| }; | ||
|
|
||
| const clientRenderOnBadMarkup = (element, errorCount = 0) => { | ||
|
|
@@ -143,24 +143,26 @@ describe('ReactDOMServerIntegration', () => { | |
| }); | ||
|
|
||
| describe('basic rendering', function() { | ||
| itRenders('a blank div', render => | ||
| render(<div />).then(e => expect(e.tagName).toBe('DIV'))); | ||
|
|
||
| itRenders('a div with inline styles', render => | ||
| render(<div style={{color:'red', width:'30px'}} />).then(e => { | ||
| expect(e.style.color).toBe('red'); | ||
| expect(e.style.width).toBe('30px'); | ||
| }) | ||
| ); | ||
|
|
||
| itRenders('a self-closing tag', render => | ||
| render(<br />).then(e => expect(e.tagName).toBe('BR'))); | ||
|
|
||
| itRenders('a self-closing tag as a child', render => | ||
| render(<div><br /></div>).then(e => { | ||
| expect(e.childNodes.length).toBe(1); | ||
| expect(e.firstChild.tagName).toBe('BR'); | ||
| }) | ||
| ); | ||
| itRenders('a blank div', async render => { | ||
| const e = await render(<div />); | ||
| expect(e.tagName).toBe('DIV'); | ||
| }); | ||
|
|
||
| itRenders('a div with inline styles', async render => { | ||
| const e = await render(<div style={{color:'red', width:'30px'}} />); | ||
| expect(e.style.color).toBe('red'); | ||
| expect(e.style.width).toBe('30px'); | ||
| }); | ||
|
|
||
| itRenders('a self-closing tag', async render => { | ||
| const e = await render(<br />); | ||
| expect(e.tagName).toBe('BR'); | ||
| }); | ||
|
|
||
| itRenders('a self-closing tag as a child', async render => { | ||
| const e = await render(<div><br /></div>); | ||
| expect(e.childNodes.length).toBe(1); | ||
| expect(e.firstChild.tagName).toBe('BR'); | ||
| }); | ||
| }); | ||
| }); | ||
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.
Can we do
.match(/\/__tests__\//)?