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

Commit

Permalink
Merge pull request #14 from Coobaha/patch-1
Browse files Browse the repository at this point in the history
Fixes HMR when used as a global decorator
  • Loading branch information
James Baxley committed Sep 11, 2016
2 parents 0fb84cf + 0dfeec1 commit dccb0c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/__tests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,22 @@ describe("Background Decorator", () => {

expect(spy).toBeCalled();
});

it("should update story on change", () => {
const SpiedChannel = new EventEmitter();
const nextStory = jest.fn(() => <p>I am next story!</p>);
const backgroundDecorator = shallow(<BackgroundDecorator story={testStory} channel={SpiedChannel} />);

backgroundDecorator.setProps({ story: nextStory });
expect(nextStory).toBeCalled();
});

it("should not update story on other props change", () => {
const SpiedChannel = new EventEmitter();
const story = jest.fn(() => <p>I am the only one!</p>);
const backgroundDecorator = shallow(<BackgroundDecorator story={story} channel={SpiedChannel} />);

backgroundDecorator.setProps({ randomProp: true });
expect(story.mock.calls.length).toBe(1);
});
});
6 changes: 6 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export class BackgroundDecorator extends React.Component<any, any> {
this.channel.emit("background-set", this.props.backgrounds);
}

componentWillReceiveProps(nextProps) {
if (nextProps.story !== this.props.story) {
this.story = nextProps.story();
}
}

componentWillUnmount() {
this.channel.removeListener("background", this.setBackground);
this.channel.emit("background-unset");
Expand Down

0 comments on commit dccb0c9

Please sign in to comment.