Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
Make sure the exact same references are subscribed/unsubscribed
Browse files Browse the repository at this point in the history
Issue #1283
  • Loading branch information
pablosichert committed Nov 16, 2017
1 parent 4a4b727 commit 0d6434f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/components/Shortcuts/Shortcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ export default class Shortcut extends Component {
const { subscribe } = this.context.shortcuts;
const { name, handler } = this.props;

this.name = name;
this.handler = handler;

subscribe(name, handler);
}

componentWillUnmount() {
const { unsubscribe } = this.context.shortcuts;
const { name, handler } = this.props;
const { name, handler } = this;

unsubscribe(name, handler);
}
Expand Down
41 changes: 35 additions & 6 deletions src/components/Shortcuts/Shortcut.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ describe('Shortcut', () => {
});

it('should return null from render()', () => {
const shortcut = new Shortcut;
const shortcut = new Shortcut();

expect(shortcut.render()).to.equal(null);
});

it('should subscribe when mounting', () => {
const shortcut = new Shortcut;
const shortcut = new Shortcut();

const subscribe = spy();

Expand All @@ -34,19 +34,48 @@ describe('Shortcut', () => {
expect(subscribe).to.have.been.calledWith(name, handler);
});

it('should unsubscribe when mounting', () => {
const shortcut = new Shortcut;
it('should unsubscribe when unmounting', () => {
const shortcut = new Shortcut();

const unsubscribe = spy();

shortcut.context = { shortcuts: { unsubscribe } };

const name = 'Foo';
const handler = () => {};

shortcut.props = { name, handler };
shortcut.context = { shortcuts: { unsubscribe } };
shortcut.name = name;
shortcut.handler = handler;

shortcut.componentWillUnmount();

expect(unsubscribe).to.have.been.calledWith(name, handler);
});

it('should unsubscribe the same attributes which were subscribed', () => {
const shortcut = new Shortcut();

const subscribe = spy();
const unsubscribe = spy();

shortcut.context = { shortcuts: { subscribe, unsubscribe } };

const name1 = 'Foo';
const handler1 = () => {};

shortcut.props = { name: name1, handler: handler1 };

shortcut.componentWillMount();

expect(subscribe).to.have.been.calledWith(name1, handler1);

const name2 = 'Bar';
const handler2 = () => {};

shortcut.props = { name: name2, handler: handler2 };

shortcut.componentWillUnmount();

expect(unsubscribe).to.have.been.calledWith(name1, handler1);
});
});

0 comments on commit 0d6434f

Please sign in to comment.