diff --git a/addons/background/src/__tests__/index.tsx b/addons/background/src/__tests__/index.tsx new file mode 100644 index 00000000000..6927e1c18c3 --- /dev/null +++ b/addons/background/src/__tests__/index.tsx @@ -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 = () => () =>

Hello World!

; + +describe("Background Decorator", () => { + it("should exist", () => { + const SpiedChannel = new EventEmitter(); + const backgroundDecorator = shallow(); + expect(backgroundDecorator).toBeDefined(); + }); + + it("should initially have a transparent background state", () => { + const SpiedChannel = new EventEmitter(); + const backgroundDecorator = shallow(); + + expect(backgroundDecorator.state().background).toBe("transparent"); + }); + + it("should have a background matching its state", () => { + const SpiedChannel = new EventEmitter(); + const backgroundDecorator = shallow(); + + 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(); + + 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(); + + 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(); + + expect(spy).toBeCalled(); + }); +}); diff --git a/addons/background/src/index.tsx b/addons/background/src/index.tsx index 3cde354e0d1..ba0b37089e4 100644 --- a/addons/background/src/index.tsx +++ b/addons/background/src/index.tsx @@ -26,7 +26,13 @@ export class BackgroundDecorator extends React.Component { constructor(props) { super(props); - this.channel = addons.getChannel(); + // A channel is explicitly passed in for testing + if (this.props.channel) { + this.channel = this.props.channel; + } else { + this.channel = addons.getChannel(); + } + this.story = this.props.story(); }