forked from ReactTooltip/react-tooltip
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using decorators for static method and tests
- Loading branch information
Showing
9 changed files
with
191 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"presets": ["es2015", "react"] | ||
"presets": ["es2015", "react", "stage-0"], | ||
"plugins": ["transform-decorators-legacy"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default { | ||
|
||
GLOBAL: { | ||
HIDE: '__react_tooltip_hide_event', | ||
REBUILD: '__react_tooltip_rebuild_event' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Static methods for react-tooltip | ||
*/ | ||
import CONSTANT from '../constant' | ||
|
||
const dispatchGlobalEvent = (eventName) => { | ||
// Compatibale with IE | ||
// @see http://stackoverflow.com/questions/26596123/internet-explorer-9-10-11-event-constructor-doesnt-work | ||
let event | ||
|
||
if (typeof window.Event === 'function') { | ||
event = new window.Event(eventName) | ||
} else { | ||
event = document.createEvent('Event') | ||
event.initEvent(eventName, false, true) | ||
} | ||
|
||
window.dispatchEvent(event) | ||
} | ||
|
||
export default function (target) { | ||
/** | ||
* Hide all tooltip | ||
* @trigger ReactTooltip.hide() | ||
*/ | ||
target.hide = () => { | ||
dispatchGlobalEvent(CONSTANT.GLOBAL.HIDE) | ||
} | ||
|
||
/** | ||
* Rebuild all tooltip | ||
* @trigger ReactTooltip.rebuild() | ||
*/ | ||
target.rebuild = () => { | ||
dispatchGlobalEvent(CONSTANT.GLOBAL.REBUILD) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Events that should be bound to the window | ||
*/ | ||
import CONSTANT from '../constant' | ||
|
||
export default function (target) { | ||
target.prototype.bindWindowEvents = function () { | ||
// ReactTooltip.hide | ||
window.removeEventListener(CONSTANT.GLOBAL.HIDE, this.globalHide) | ||
window.addEventListener(CONSTANT.GLOBAL.HIDE, this.globalHide, false) | ||
|
||
// ReactTooltip.rebuild | ||
window.removeEventListener(CONSTANT.GLOBAL.REBUILD, this.globalRebuild) | ||
window.addEventListener(CONSTANT.GLOBAL.REBUILD, this.globalRebuild, false) | ||
|
||
// Resize | ||
window.removeEventListener('resize', this.onWindowResize) | ||
window.addEventListener('resize', this.onWindowResize, false) | ||
} | ||
|
||
target.prototype.unbindWindowEvents = function () { | ||
window.removeEventListener(CONSTANT.GLOBAL.HIDE, this.globalHide) | ||
window.removeEventListener(CONSTANT.GLOBAL.REBUILD, this.globalRebuild) | ||
window.removeEventListener('resize', this.onWindowResize) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* For Standard.js lint checking */ | ||
/* eslint-env mocha */ | ||
|
||
import React from 'react' | ||
import { mount } from 'enzyme' | ||
import chai, { expect } from 'chai' | ||
import chaiEnzyme from 'chai-enzyme' | ||
import sinon from 'sinon' | ||
import ReactTooltip from '../src' | ||
|
||
/* Initial test tools */ | ||
chai.use(chaiEnzyme()) | ||
|
||
describe('Global methods', () => { | ||
it('should be hided by invoking ReactTooltip.hide', () => { | ||
const wrapper = mount(<ReactTooltip />) | ||
wrapper.setState({ show: true }) | ||
expect(wrapper).to.have.state('show', true) | ||
ReactTooltip.hide() | ||
setImmediate(() => { | ||
expect(wrapper).to.have.state('show', false) | ||
}) | ||
}) | ||
|
||
it('should be rebuild by invoking ReactTooltip.rebuild', () => { | ||
sinon.spy(ReactTooltip.prototype, 'globalRebuild') | ||
ReactTooltip.rebuild() | ||
setImmediate(() => { | ||
expect(ReactTooltip.prototype.globalRebuild.calledOnce).to.equal(true) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Setup jsdom for enzyme mount | ||
* @see https://github.com/airbnb/enzyme/blob/master/docs/guides/jsdom.md#using-enzyme-with-jsdom | ||
*/ | ||
import {jsdom} from 'jsdom' | ||
|
||
const exposedProperties = ['window', 'navigator', 'document'] | ||
|
||
global.document = jsdom('') | ||
global.window = document.defaultView | ||
Object.keys(document.defaultView).forEach((property) => { | ||
if (typeof global[property] === 'undefined') { | ||
exposedProperties.push(property) | ||
global[property] = document.defaultView[property] | ||
} | ||
}) | ||
|
||
global.navigator = { | ||
userAgent: 'node.js' | ||
} |