Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added callbacks #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ render() {
<EasterEgg
keys={/* Array of keys to type to trigger the easter egg */}
simultaneous={/* Add this prop if keys should be pressed all together */}
timeout={/* Duration to show your easter egg, easter egg is displayed forever if prop is not set */}>
timeout={/* Duration to show your easter egg, easter egg is displayed forever if prop is not set */}
onKeysCoincide={/* Callback to call when the code is correctly inserted*/}
onTimeout={/* Callback to call when the timer runs out*/}>
{/* Your easter egg JSX goes here */}
</EasterEgg>
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"dependencies": {
"bluebird": "3.4.7",
"lodash": "^4.17.4",
"react": "15.4.2",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-shortcut": "1.0.6"
},
Expand Down
15 changes: 8 additions & 7 deletions src/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ var React = require('react');

var HotKey = require('react-shortcut');


module.exports = React.createClass({
propTypes: {
keys: React.PropTypes.array,
simultaneous: React.PropTypes.bool,
timeout: React.PropTypes.number,
onKeysCoincide: React.PropTypes.func,
onTimeout: React.PropTypes.func,
children: React.PropTypes.oneOfType([
React.PropTypes.array,
React.PropTypes.element
Expand Down Expand Up @@ -43,17 +44,17 @@ module.exports = React.createClass({
onKeysCoincide: function keysCoincide() {
var props = this.props || {};
var timeout = props.timeout || null;
var onKeysCoincide = props.onKeysCoincide || null;
var onTimeout = props.onTimeout || null;

if (onKeysCoincide) onKeysCoincide();
if (timeout) {
this.setState({
timer: setTimeout((function resolveTimeout() {
var state = this.state || {};

clearTimeout(state.timer);
clearTimeout(this.state.timer);
if (onTimeout) onTimeout();

this.setState({
timer: null
});
this.setState({ timer: null });
}).bind(this), timeout)
});
} else {
Expand Down
71 changes: 69 additions & 2 deletions test/Component.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
var React = require('react');
var enzyme = require('enzyme');
var sinon = require('sinon');
var Promise = require('bluebird');

var Component = require('../src/Component');


var mount = enzyme.mount;


function Page(wrapper) {
this.wrapper = function() {
return wrapper;
Expand Down Expand Up @@ -128,4 +127,72 @@ describe('<EasterEgg />', function() {
expect(page.wrapper().contains(child)).toEqual(false);
});
});

it('Should fire callback when keys are correct', function() {
var handleKeysCoincide = sinon.stub();

var child = createChild();
var created = createComponent({
keys: ['t', 'e', 's', 't'],
onKeysCoincide: handleKeysCoincide
}, child);

var wrapper = created.wrapper;
var page = created.page;

return new Promise(function(resolveAll) {
return new Promise(function(resolve) {
expect(page.wrapper().contains(child)).toEqual(false);

document.dispatchEvent(new KeyboardEvent('keydown', {'key': 't'}));
document.dispatchEvent(new KeyboardEvent('keydown', {'key': 'e'}));
document.dispatchEvent(new KeyboardEvent('keydown', {'key': 's'}));
document.dispatchEvent(new KeyboardEvent('keydown', {'key': 't'}));

setTimeout(function() {
resolve();
}, 500);
}).then(function() {
expect(page.wrapper().contains(child)).toEqual(true);
expect(handleKeysCoincide.callCount).toEqual(1);
setTimeout(function() {
resolveAll();
}, 500);
});
}).then(function() {
expect(page.wrapper().contains(child)).toEqual(true);
});
});

it('Should fire callback when timer runs out', function() {
var handleTimeout = sinon.stub();

var child = createChild();
var created = createComponent({
keys: ['t', 'e', 's', 't'],
timeout: 300,
onTimeout: handleTimeout
}, child);

var wrapper = created.wrapper;
var page = created.page;

return new Promise(function(resolve) {
expect(page.wrapper().contains(child)).toEqual(false);

document.dispatchEvent(new KeyboardEvent('keydown', {'key': 't'}));
document.dispatchEvent(new KeyboardEvent('keydown', {'key': 'e'}));
document.dispatchEvent(new KeyboardEvent('keydown', {'key': 's'}));
document.dispatchEvent(new KeyboardEvent('keydown', {'key': 't'}));

expect(page.wrapper().contains(child)).toEqual(true);

setTimeout(function() {
resolve();
}, 1000);
}).then(function() {
expect(handleTimeout.callCount).toEqual(1);
expect(page.wrapper().contains(child)).toEqual(false);
});
});
});