This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add <Shortcut> that subscribes to <ShortcutProvider>
Issue #1283
- Loading branch information
1 parent
1e77a05
commit ceaba65
Showing
2 changed files
with
87 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import keymap from './keymap'; | ||
|
||
export default class Shortcut extends Component { | ||
static contextTypes = { | ||
shortcuts: PropTypes.shape({ | ||
subscribe: PropTypes.func.isRequired, | ||
unsubscribe: PropTypes.func.isRequired | ||
}).isRequired | ||
}; | ||
|
||
static propTypes = { | ||
name: PropTypes.oneOf(Object.keys(keymap)).isRequired, | ||
handler: PropTypes.func.isRequired | ||
}; | ||
|
||
componentWillMount() { | ||
const { subscribe } = this.context.shortcuts; | ||
const { name, handler } = this.props; | ||
|
||
subscribe(name, handler); | ||
} | ||
|
||
componentWillUnmount() { | ||
const { unsubscribe } = this.context.shortcuts; | ||
const { name, handler } = this.props; | ||
|
||
unsubscribe(name, handler); | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} |
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,52 @@ | ||
/* eslint-env mocha */ | ||
import chai, { expect } from 'chai'; | ||
import { spy } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import { Component } from 'react'; | ||
import Shortcut from './Shortcut'; | ||
|
||
chai.use(sinonChai); | ||
|
||
describe('Shortcut', () => { | ||
it('should be a React component', () => { | ||
expect(Shortcut).to.be.an.instanceOf(Component.constructor); | ||
}); | ||
|
||
it('should return null from render()', () => { | ||
const shortcut = new Shortcut; | ||
|
||
expect(shortcut.render()).to.equal(null); | ||
}); | ||
|
||
it('should subscribe when mounting', () => { | ||
const shortcut = new Shortcut; | ||
|
||
const subscribe = spy(); | ||
|
||
const name = 'Foo'; | ||
const handler = () => {}; | ||
|
||
shortcut.props = { name, handler }; | ||
shortcut.context = { shortcuts: { subscribe } }; | ||
|
||
shortcut.componentWillMount(); | ||
|
||
expect(subscribe).to.have.been.calledWith(name, handler); | ||
}); | ||
|
||
it('should unsubscribe when mounting', () => { | ||
const shortcut = new Shortcut; | ||
|
||
const unsubscribe = spy(); | ||
|
||
const name = 'Foo'; | ||
const handler = () => {}; | ||
|
||
shortcut.props = { name, handler }; | ||
shortcut.context = { shortcuts: { unsubscribe } }; | ||
|
||
shortcut.componentWillUnmount(); | ||
|
||
expect(unsubscribe).to.have.been.calledWith(name, handler); | ||
}); | ||
}); |