-
Notifications
You must be signed in to change notification settings - Fork 47.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
957 additions
and
55 deletions.
There are no files selected for viewing
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,134 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
let JSDOM; | ||
let React; | ||
let ReactDOM; | ||
let clientAct; | ||
let ReactDOMFizzServer; | ||
let Stream; | ||
let useId; | ||
let useRef; | ||
let useEffect; | ||
let document; | ||
let writable; | ||
let container; | ||
let buffer = ''; | ||
let hasErrored = false; | ||
let fatalError = undefined; | ||
|
||
describe('useId', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
JSDOM = require('jsdom').JSDOM; | ||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
clientAct = require('jest-react').act; | ||
ReactDOMFizzServer = require('react-dom/server'); | ||
Stream = require('stream'); | ||
// TODO: Rename API | ||
useId = React.unstable_useId; | ||
useRef = React.useRef; | ||
useEffect = React.useEffect; | ||
|
||
// Test Environment | ||
const jsdom = new JSDOM( | ||
'<!DOCTYPE html><html><head></head><body><div id="container">', | ||
{ | ||
runScripts: 'dangerously', | ||
}, | ||
); | ||
document = jsdom.window.document; | ||
container = document.getElementById('container'); | ||
|
||
buffer = ''; | ||
hasErrored = false; | ||
|
||
writable = new Stream.PassThrough(); | ||
writable.setEncoding('utf8'); | ||
writable.on('data', chunk => { | ||
buffer += chunk; | ||
}); | ||
writable.on('error', error => { | ||
hasErrored = true; | ||
fatalError = error; | ||
}); | ||
}); | ||
|
||
async function serverAct(callback) { | ||
await callback(); | ||
// Await one turn around the event loop. | ||
// This assumes that we'll flush everything we have so far. | ||
await new Promise(resolve => { | ||
setImmediate(resolve); | ||
}); | ||
if (hasErrored) { | ||
throw fatalError; | ||
} | ||
// JSDOM doesn't support stream HTML parser so we need to give it a proper fragment. | ||
// We also want to execute any scripts that are embedded. | ||
// We assume that we have now received a proper fragment of HTML. | ||
const bufferedContent = buffer; | ||
buffer = ''; | ||
const fakeBody = document.createElement('body'); | ||
fakeBody.innerHTML = bufferedContent; | ||
while (fakeBody.firstChild) { | ||
const node = fakeBody.firstChild; | ||
if (node.nodeName === 'SCRIPT') { | ||
const script = document.createElement('script'); | ||
script.textContent = node.textContent; | ||
fakeBody.removeChild(node); | ||
container.appendChild(script); | ||
} else { | ||
container.appendChild(node); | ||
} | ||
} | ||
} | ||
|
||
function DivWithId({label, children}) { | ||
const id = useId(); | ||
const ref = useRef(null); | ||
useEffect(() => { | ||
const div = ref.current; | ||
if (div !== null) { | ||
if (div.id !== id) { | ||
throw new Error('Server and client ids do not match'); | ||
} | ||
} | ||
}, [id]); | ||
return ( | ||
<div ref={ref} id={id}> | ||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
test('basic usage', async () => { | ||
function App() { | ||
return ( | ||
<div> | ||
<div> | ||
<DivWithId label="A" /> | ||
<DivWithId label="B" /> | ||
</div> | ||
<DivWithId label="C" /> | ||
</div> | ||
); | ||
} | ||
|
||
await serverAct(async () => { | ||
const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />); | ||
pipe(writable); | ||
}); | ||
|
||
await clientAct(async () => { | ||
ReactDOM.hydrateRoot(container, <App />); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.