Browse Source

Flesh out basic functionality

pull/5/head
Luke Childs 7 years ago
parent
commit
6081727e49
  1. 6
      package.json
  2. 28
      src/index.js
  3. 13
      test/react-jsdom.js

6
package.json

@ -26,12 +26,16 @@
"url": "https://github.com/lukechilds/react-jsdom/issues"
},
"homepage": "https://github.com/lukechilds/react-jsdom",
"dependencies": {},
"dependencies": {
"react-dom": "^15.6.1",
"window": "^4.1.0"
},
"devDependencies": {
"ava": "^0.20.0",
"coveralls": "^2.13.1",
"eslint-config-xo-lukechilds": "^1.0.0",
"nyc": "^11.0.3",
"react": "^15.6.1",
"this": "^1.0.2",
"xo": "^0.19.0"
}

28
src/index.js

@ -1,5 +1,31 @@
'use strict';
const ReactJSDOM = () => {};
const Window = require('window');
const ReactDOM = require('react-dom');
const ReactJSDOM = {
version: ReactDOM.version,
render: component => {
const window = new Window();
const document = window.document;
const origGlobals = {
window: global.window,
document: global.document
};
global.window = window;
global.document = document;
const container = document.createElement('div');
ReactDOM.render(component, container);
Object.keys(origGlobals).forEach(prop => {
global[prop] = origGlobals[prop];
});
return container.children.length > 1 ? container.children : container.children[0];
}
};
module.exports = ReactJSDOM;

13
test/react-jsdom.js

@ -1,6 +1,15 @@
import test from 'ava';
import React from 'react';
import ReactJSDOM from 'this';
test('ReactJSDOM is a function', t => {
t.is(typeof ReactJSDOM, 'function');
test('ReactJSDOM is a object', t => {
t.is(typeof ReactJSDOM, 'object');
});
test('ReactJSDOM renders a React Component', t => {
const component = ReactJSDOM.render(
React.createElement('div', {}, 'hi')
);
t.is(component.nodeName, 'DIV');
t.is(component.textContent, 'hi');
});

Loading…
Cancel
Save