Skip to content

Commit

Permalink
frint-react: add hydrate function to frint-react (#427)
Browse files Browse the repository at this point in the history
* Add `hydrate` function to `frint-react`

* Update README
  • Loading branch information
rbardini authored Jul 10, 2018
1 parent 4e0df0e commit b605222
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/frint-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Multi-instance Apps](#multi-instance-apps)
- [API](#api)
- [render](#render)
- [hydrate](#hydrate)
- [observe](#observe)
- [Region](#region)
- [RegionService](#regionservice)
Expand Down Expand Up @@ -477,6 +478,12 @@ Renders a Root App in target DOM node.
1. `app` (`App`): The Root App instance.
1. `node` (`Element`): The DOM Element where you want your App to render.

## hydrate

> hydrate(app, node)
Hydrates a Root App in target DOM node.

## observe

> observe(fn)(MyComponent)
Expand Down
11 changes: 11 additions & 0 deletions packages/frint-react/src/hydrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable import/no-extraneous-dependencies, global-require */
import React from 'react';

import getMountableComponent from './components/getMountableComponent';

export default function hydrate(app, node) {
const MountableComponent = getMountableComponent(app);
const ReactDOM = require('react-dom');

return ReactDOM.hydrate(<MountableComponent />, node);
}
36 changes: 36 additions & 0 deletions packages/frint-react/src/hydrate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable import/no-extraneous-dependencies, func-names */
/* globals describe, document, it, beforeEach, resetDOM */
import { expect } from 'chai';
import React from 'react';
import { createApp } from 'frint';

import hydrate from './hydrate';

describe('frint-react › hydrate', function () {
function TestComponent() {
return <p>Hello World from TestApp!</p>;
}

const TestApp = createApp({
name: 'TestApp',
providers: [
{
name: 'component',
useValue: TestComponent,
},
],
});

beforeEach(function () {
resetDOM();
});

it('hydrates app to DOM', function () {
const app = new TestApp();
const rootEl = document.getElementById('root');
hydrate(app, rootEl);

expect(rootEl.innerHTML)
.to.contain('Hello World from TestApp!</p>');
});
});
2 changes: 2 additions & 0 deletions packages/frint-react/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import render from './render';
import hydrate from './hydrate';
import streamProps from './streamProps';
import isObservable from './isObservable';

Expand All @@ -13,6 +14,7 @@ import ReactHandler from './handlers/ReactHandler';

export default {
render,
hydrate,
streamProps,
isObservable,

Expand Down
23 changes: 23 additions & 0 deletions packages/frint-react/src/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable import/no-extraneous-dependencies, func-names */
/* global describe, it */
import { expect } from 'chai';

import index from './index';

describe('frint-react › index', function () {
it('exports an object with the required keys', function () {
expect(index).to.be.an('object');
expect(index).to.have.all.keys(
'render',
'hydrate',
'streamProps',
'isObservable',
'getMountableComponent',
'observe',
'Region',
'Provider',
'RegionService',
'ReactHandler',
);
});
});

0 comments on commit b605222

Please sign in to comment.