Skip to content

Commit

Permalink
set default background color (#23)
Browse files Browse the repository at this point in the history
* set default background color

* update changelog

* 0.0.7
  • Loading branch information
James Baxley authored and ndelangen committed Nov 2, 2017
1 parent b987342 commit bfce94f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
4 changes: 4 additions & 0 deletions addons/background/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

### vNext

### v0.0.7

- Bug: Default background will now auto set if nothing is set in the url [#23](https://github.com/NewSpring/react-storybook-addon-backgrounds/pull/23)

### v0.0.6

- Feature: Allow setting a background as a default [#21](https://github.com/NewSpring/react-storybook-addon-backgrounds/pull/21)
Expand Down
2 changes: 1 addition & 1 deletion addons/background/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-storybook-addon-backgrounds",
"version": "0.0.6",
"version": "0.0.7",
"description": "A react storybook addon to show different backgrounds for your preview",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
9 changes: 8 additions & 1 deletion addons/background/src/BackgroundPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ export default class BackgroundPanel extends React.Component<BackgroundPanelProp

this.props.channel.on("background-set", backgrounds => {
this.setState({ backgrounds });
this.setBackgroundInPreview(this.props.api.getQueryParam("background"));
const currentBackground = this.props.api.getQueryParam("background");

if (currentBackground) {
this.setBackgroundInPreview(currentBackground);
} else if (backgrounds.filter(x => x.default).length) {
const defaultBgs = backgrounds.filter(x => x.default);
this.setBackgroundInPreview(defaultBgs[0].value);
}
});

this.props.channel.on("background-unset", backgrounds => {
Expand Down
29 changes: 25 additions & 4 deletions addons/background/src/__tests__/BackgroundPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,36 @@ describe("Background Panel", () => {
it("should allow setting a default swatch", () => {
const SpiedChannel = new EventEmitter();
const backgroundPanel = TestUtils.renderIntoDocument(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
(backgrounds[0] as any).default = true;
SpiedChannel.emit("background-set", backgrounds);
const localBgs = [...backgrounds];
(localBgs[0] as any).default = true;
SpiedChannel.emit("background-set", localBgs);

expect(backgroundPanel.state.backgrounds[0].name).toBe(backgrounds[0].name);
expect(backgroundPanel.state.backgrounds[2].value).toBe(backgrounds[2].value);
expect(backgroundPanel.state.backgrounds[0].name).toBe(localBgs[0].name);
expect(backgroundPanel.state.backgrounds[2].value).toBe(localBgs[2].value);

//check to make sure the default bg was added
const headings = TestUtils.scryRenderedDOMComponentsWithTag(backgroundPanel, "h4");
expect(headings.length).toBe(8);
delete (backgrounds[0] as any).default;
});

it("should allow the default swatch become the background color", () => {
const SpiedChannel = new EventEmitter();
const backgroundPanel = TestUtils.renderIntoDocument(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
const localBgs = [...backgrounds];
(localBgs[1] as any).default = true;
SpiedChannel.on("background", bg => {
expect(bg).toBe(localBgs[1].value);
})
SpiedChannel.emit("background-set", localBgs);

expect(backgroundPanel.state.backgrounds[0].name).toBe(localBgs[0].name);
expect(backgroundPanel.state.backgrounds[2].value).toBe(localBgs[2].value);

//check to make sure the default bg was added
const headings = TestUtils.scryRenderedDOMComponentsWithTag(backgroundPanel, "h4");
expect(headings.length).toBe(8);
delete (backgrounds[1] as any).default;
});

it("should unset all swatches on receiving the backgroun-unset message", () => {
Expand Down
4 changes: 2 additions & 2 deletions addons/background/src/__tests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("Background Decorator", () => {
const backgroundDecorator = shallow(<BackgroundDecorator story={testStory} channel={SpiedChannel} />);

const spy = jest.fn();
SpiedChannel.on('background-unset', spy);
SpiedChannel.on("background-unset", spy);

backgroundDecorator.unmount();

Expand All @@ -50,7 +50,7 @@ describe("Background Decorator", () => {
it("should send background-set event when the component mounts", () => {
const SpiedChannel = new EventEmitter();
const spy = jest.fn();
SpiedChannel.on('background-set', spy);
SpiedChannel.on("background-set", spy);

const backgroundDecorator = shallow(<BackgroundDecorator story={testStory} channel={SpiedChannel} />);

Expand Down
2 changes: 1 addition & 1 deletion addons/background/src/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ storiesOf("Second Component", module)
.addDecorator(centered)
.addDecorator(backgrounds([
{ name: "twitter", value: "#00aced" },
{ name: "facebook", value: "#3b5998" },
{ name: "facebook", value: "#3b5998", default: true },
]))
.add("Second Button", () => <button>Click me</button>)
;

0 comments on commit bfce94f

Please sign in to comment.