From ffe9b4dee79b87c651edd9c6cee52db75d5b4bfe Mon Sep 17 00:00:00 2001 From: Nikita Luchko Date: Tue, 19 Jul 2016 14:44:44 +0200 Subject: [PATCH] Add `stage-0` babel preset 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. --- .babelrc | 2 +- lib/portal.js | 42 +++++++++++++++++++++--------------------- package.json | 3 ++- test/mocha.js | 5 +++++ test/portal_spec.js | 7 ++----- test/setup.js | 7 +++++++ 6 files changed, 38 insertions(+), 28 deletions(-) create mode 100644 test/setup.js diff --git a/.babelrc b/.babelrc index 671a63b..b4a673c 100644 --- a/.babelrc +++ b/.babelrc @@ -1,5 +1,5 @@ { - "presets": ["react", "es2015"], + "presets": ["react", "es2015", "stage-0"], "plugins": ["add-module-exports"], "env": { "development": { diff --git a/lib/portal.js b/lib/portal.js index 9f44981..c633f4e 100644 --- a/lib/portal.js +++ b/lib/portal.js @@ -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 }; @@ -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: () => {}, -}; diff --git a/package.json b/package.json index ee65ab1..cb5a3cb 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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", diff --git a/test/mocha.js b/test/mocha.js index f7534b3..3da7742 100644 --- a/test/mocha.js +++ b/test/mocha.js @@ -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(); diff --git a/test/portal_spec.js b/test/portal_spec.js index 2237438..df5e66e 100644 --- a/test/portal_spec.js +++ b/test/portal_spec.js @@ -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(''); - global.window = document.defaultView; - global.navigator = { userAgent: 'node.js' }; - // Enzyme library uses React + setup(); /*eslint-disable */ React = require('react'); /*eslint-enable */ diff --git a/test/setup.js b/test/setup.js new file mode 100644 index 0000000..0600f26 --- /dev/null +++ b/test/setup.js @@ -0,0 +1,7 @@ +import jsdom from 'jsdom'; + +export default function setup() { + global.document = jsdom.jsdom(''); + global.window = document.defaultView; + global.navigator = { userAgent: 'node.js' }; +}