mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-07 21:51:48 +08:00
added tests for BackgroundPanel
This commit is contained in:
parent
7364bbb968
commit
50ef16556f
59
addons/background/src/__tests__/index.tsx
Normal file
59
addons/background/src/__tests__/index.tsx
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import * as React from "react"; // tslint:disable-line
|
||||||
|
const EventEmitter = require("events"); // tslint:disable-line
|
||||||
|
import { shallow } from "enzyme";
|
||||||
|
import { BackgroundDecorator } from "../index";
|
||||||
|
const TestUtils = require("react-addons-test-utils"); // tslint:disable-line
|
||||||
|
|
||||||
|
const testStory = () => () => <p>Hello World!</p>;
|
||||||
|
|
||||||
|
describe("Background Decorator", () => {
|
||||||
|
it("should exist", () => {
|
||||||
|
const SpiedChannel = new EventEmitter();
|
||||||
|
const backgroundDecorator = shallow(<BackgroundDecorator story={testStory} channel={SpiedChannel} />);
|
||||||
|
expect(backgroundDecorator).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should initially have a transparent background state", () => {
|
||||||
|
const SpiedChannel = new EventEmitter();
|
||||||
|
const backgroundDecorator = shallow(<BackgroundDecorator story={testStory} channel={SpiedChannel} />);
|
||||||
|
|
||||||
|
expect(backgroundDecorator.state().background).toBe("transparent");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should have a background matching its state", () => {
|
||||||
|
const SpiedChannel = new EventEmitter();
|
||||||
|
const backgroundDecorator = shallow(<BackgroundDecorator story={testStory} channel={SpiedChannel} />);
|
||||||
|
|
||||||
|
expect(backgroundDecorator.html().match(/background-color:transparent/gmi).length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should set internal state when background event called", () => {
|
||||||
|
const SpiedChannel = new EventEmitter();
|
||||||
|
const backgroundDecorator = shallow(<BackgroundDecorator story={testStory} channel={SpiedChannel} />);
|
||||||
|
|
||||||
|
SpiedChannel.emit("background", "#123456");
|
||||||
|
expect(backgroundDecorator.state().background).toBe("#123456");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should send background-unset event when the component unmounts", () => {
|
||||||
|
const SpiedChannel = new EventEmitter();
|
||||||
|
const backgroundDecorator = shallow(<BackgroundDecorator story={testStory} channel={SpiedChannel} />);
|
||||||
|
|
||||||
|
const spy = jest.fn();
|
||||||
|
SpiedChannel.on('background-unset', spy);
|
||||||
|
|
||||||
|
backgroundDecorator.unmount();
|
||||||
|
|
||||||
|
expect(spy).toBeCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should send background-set event when the component mounts", () => {
|
||||||
|
const SpiedChannel = new EventEmitter();
|
||||||
|
const spy = jest.fn();
|
||||||
|
SpiedChannel.on('background-set', spy);
|
||||||
|
|
||||||
|
const backgroundDecorator = shallow(<BackgroundDecorator story={testStory} channel={SpiedChannel} />);
|
||||||
|
|
||||||
|
expect(spy).toBeCalled();
|
||||||
|
});
|
||||||
|
});
|
@ -26,7 +26,13 @@ export class BackgroundDecorator extends React.Component<any, any> {
|
|||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
|
// A channel is explicitly passed in for testing
|
||||||
|
if (this.props.channel) {
|
||||||
|
this.channel = this.props.channel;
|
||||||
|
} else {
|
||||||
this.channel = addons.getChannel();
|
this.channel = addons.getChannel();
|
||||||
|
}
|
||||||
|
|
||||||
this.story = this.props.story();
|
this.story = this.props.story();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user