Skip to content

Commit

Permalink
Add stage-0 babel preset
Browse files Browse the repository at this point in the history
This allows to write static properties in a nicer way.
Also I've updated tests setup in order to have window object
specified from the very beginning. Without this components
were not able to use `window` before they were imported in tests.
  • Loading branch information
luchkonikita committed Jul 19, 2016
1 parent 2a924c0 commit ffe9b4d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"presets": ["react", "es2015"],
"presets": ["react", "es2015", "stage-0"],
"plugins": ["add-module-exports"],
"env": {
"development": {
Expand Down
42 changes: 21 additions & 21 deletions lib/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ const KEYCODES = {

export default class Portal extends React.Component {

static defaultProps = {
onOpen: () => {},
onClose: () => {},
onUpdate: () => {},
};

static propTypes = {
className: React.PropTypes.string,
style: React.PropTypes.object,
children: React.PropTypes.element.isRequired,
target: React.PropTypes.instanceOf(window.HTMLElement),
openByClickOn: React.PropTypes.element,
closeOnEsc: React.PropTypes.bool,
closeOnOutsideClick: React.PropTypes.bool,
isOpened: React.PropTypes.bool,
onOpen: React.PropTypes.func,
onClose: React.PropTypes.func,
beforeClose: React.PropTypes.func,
onUpdate: React.PropTypes.func,
};

constructor() {
super();
this.state = { active: false };
Expand Down Expand Up @@ -172,24 +193,3 @@ export default class Portal extends React.Component {
return null;
}
}

Portal.propTypes = {
className: React.PropTypes.string,
style: React.PropTypes.object,
children: React.PropTypes.element.isRequired,
target: React.PropTypes.instanceOf(window.HTMLElement),
openByClickOn: React.PropTypes.element,
closeOnEsc: React.PropTypes.bool,
closeOnOutsideClick: React.PropTypes.bool,
isOpened: React.PropTypes.bool,
onOpen: React.PropTypes.func,
onClose: React.PropTypes.func,
beforeClose: React.PropTypes.func,
onUpdate: React.PropTypes.func,
};

Portal.defaultProps = {
onOpen: () => {},
onClose: () => {},
onUpdate: () => {},
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"build:examples": "npm run clean && npm run build:examples:webpack",
"build:examples:webpack": "cross-env NODE_ENV=production webpack --config webpack.config.prod.babel.js",
"clean": "rimraf build",
"test": "mocha",
"test": "mocha ./test/portal_spec.js",
"lint": "mocha test/eslint_spec.js",
"prepublish": "cross-env NODE_ENV=production npm run build"
},
Expand All @@ -46,6 +46,7 @@
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.0.1",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.8.0",
"cross-env": "^1.0.7",
"enzyme": "^2.3.0",
Expand Down
5 changes: 5 additions & 0 deletions test/mocha.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
import setup from './setup';

process.env.NODE_ENV = 'test';
// Calling setup befere any components are imported in specs
// Otherwise window object can not be used in static methods
setup();
7 changes: 2 additions & 5 deletions test/portal_spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import jsdom from 'jsdom';
import Portal from '../lib/portal';
import assert from 'assert';
import { spy } from 'sinon';
import { render, unmountComponentAtNode } from 'react-dom';
import { mount } from 'enzyme';
import setup from './setup';

describe('react-portal', () => {
let React;
beforeEach(() => {
// Set up JSDOM
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = document.defaultView;
global.navigator = { userAgent: 'node.js' };
// Enzyme library uses React
setup();
/*eslint-disable */
React = require('react');
/*eslint-enable */
Expand Down
7 changes: 7 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import jsdom from 'jsdom';

export default function setup() {
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = document.defaultView;
global.navigator = { userAgent: 'node.js' };
}

0 comments on commit ffe9b4d

Please sign in to comment.