Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
[New] Use hydrate when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Wing Leung authored and ljharb committed Oct 9, 2017
1 parent 4939509 commit 82b92ba
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"homepage": "https://github.com/airbnb/hypernova-react#readme",
"peerDependencies": {
"hypernova": "^2.0.0",
"react": "0.14.x || >= 15.x",
"react-dom": "0.14.x || >= 15.x"
"react": "^0.14 || ^15 || ^16",
"react-dom": "^0.14 || ^15 || ^16"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand All @@ -62,8 +62,8 @@
"jsdom": "^9.12.0",
"mocha": "^4.0.1",
"prop-types": "^15.6.0",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react": "^0.14 || ^15 || ^16",
"react-dom": "^0.14 || ^15 || ^16",
"rimraf": "^2.6.2",
"sinon": "^4.1.2",
"sinon-sandbox": "^1.0.2"
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ export const renderReact = (name, component) => hypernova({
payloads.forEach((payload) => {
const { node, data } = payload;
const element = React.createElement(component, data);
ReactDOM.render(element, node);

if (ReactDOM.hydrate) {
ReactDOM.hydrate(element, node);
} else {
ReactDOM.render(element, node);
}
});
}

Expand Down
39 changes: 38 additions & 1 deletion test/renderReact-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import jsdom from 'jsdom';
import { assert } from 'chai';
import sinon from 'sinon';
import ReactDOM from 'react-dom';

import ExampleReactComponent from './components/ExampleReactComponent';
import { renderReact } from '..';
Expand All @@ -20,7 +22,7 @@ describe('renderReact', () => {
assert.match(result, /Hello Desmond/);
});

it('calls hypernova.client', (done) => {
it('calls hypernova.client (hydrate method)', (done) => {
jsdom.env(result, (err, window) => {
if (err) {
done(err);
Expand All @@ -30,9 +32,44 @@ describe('renderReact', () => {
global.window = window;
global.document = window.document;

const hydrateMethod = sinon.spy(ReactDOM, 'hydrate');

// Calling it again for the client.
renderReact('ExampleReactComponent', ExampleReactComponent);

assert(hydrateMethod.calledOnce);

delete global.window;
delete global.document;

hydrateMethod.restore();

done();
});
});

it('calls hypernova.client (render method)', (done) => {
jsdom.env(result, (err, window) => {
if (err) {
done(err);
return;
}

const sandbox = sinon.createSandbox();
sandbox.stub(ReactDOM, 'hydrate').value(undefined);

const renderMethod = sinon.spy(ReactDOM, 'render');

global.window = window;
global.document = window.document;

// Calling it again for the client.
renderReact('ExampleReactComponent', ExampleReactComponent);

assert(renderMethod.calledOnce);

sandbox.restore();

delete global.window;
delete global.document;

Expand Down

0 comments on commit 82b92ba

Please sign in to comment.