mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-04 19:21:07 +08:00
FIX unittests
This commit is contained in:
parent
ef83a816b9
commit
81420d244d
@ -17,25 +17,23 @@ const mockedApi = {
|
||||
getQueryParam: jest.fn(),
|
||||
setQueryParams: jest.fn(),
|
||||
};
|
||||
const channel = new EventEmitter();
|
||||
|
||||
describe('Background Panel', () => {
|
||||
it('should exist', () => {
|
||||
const SpiedChannel = new EventEmitter();
|
||||
const backgroundPanel = shallow(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
|
||||
const backgroundPanel = shallow(<BackgroundPanel channel={channel} api={mockedApi} />);
|
||||
|
||||
expect(backgroundPanel).toBeDefined();
|
||||
});
|
||||
|
||||
it('should have a default background value of transparent', () => {
|
||||
const SpiedChannel = new EventEmitter();
|
||||
const backgroundPanel = shallow(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
|
||||
const backgroundPanel = shallow(<BackgroundPanel channel={channel} api={mockedApi} />);
|
||||
|
||||
expect(backgroundPanel.state().backgrounds.length).toBe(0);
|
||||
});
|
||||
|
||||
it('should show setup instructions if no colors provided', () => {
|
||||
const SpiedChannel = new EventEmitter();
|
||||
const backgroundPanel = shallow(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
|
||||
const backgroundPanel = shallow(<BackgroundPanel channel={channel} api={mockedApi} />);
|
||||
|
||||
expect(backgroundPanel.html().match(/Setup Instructions/gim).length).toBeGreaterThan(0);
|
||||
});
|
||||
@ -51,7 +49,7 @@ describe('Background Panel', () => {
|
||||
it('should unset the query string', () => {
|
||||
const SpiedChannel = new EventEmitter();
|
||||
mount(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
|
||||
SpiedChannel.emit('background-unset', backgrounds);
|
||||
SpiedChannel.emit('background-unset', []);
|
||||
|
||||
expect(mockedApi.setQueryParams).toBeCalledWith({ background: null });
|
||||
});
|
||||
@ -61,47 +59,40 @@ describe('Background Panel', () => {
|
||||
const backgroundPanel = mount(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
|
||||
SpiedChannel.emit('background-set', backgrounds);
|
||||
|
||||
expect(backgroundPanel.state.backgrounds[0].name).toBe(backgrounds[0].name);
|
||||
expect(backgroundPanel.state.backgrounds[2].value).toBe(backgrounds[2].value);
|
||||
|
||||
// check to make sure the default bg was added
|
||||
const headings = backgroundPanel.find('h4');
|
||||
expect(headings.length).toBe(10);
|
||||
expect(backgroundPanel.state('backgrounds')).toEqual(backgrounds);
|
||||
});
|
||||
|
||||
it('should allow setting a default swatch', () => {
|
||||
const SpiedChannel = new EventEmitter();
|
||||
const backgroundPanel = mount(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
|
||||
const localBgs = [...backgrounds];
|
||||
localBgs[0].default = true;
|
||||
const [head, ...tail] = backgrounds;
|
||||
const localBgs = [{ ...head, default: true }, ...tail];
|
||||
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);
|
||||
expect(backgroundPanel.state('backgrounds')).toEqual(localBgs);
|
||||
backgroundPanel.setState({ backgrounds: localBgs }); // force re-render
|
||||
|
||||
// check to make sure the default bg was added
|
||||
const headings = backgroundPanel.find('h4');
|
||||
expect(headings.length).toBe(8);
|
||||
delete backgrounds[0].default;
|
||||
});
|
||||
|
||||
it('should allow the default swatch become the background color', () => {
|
||||
const SpiedChannel = new EventEmitter();
|
||||
const backgroundPanel = mount(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
|
||||
const localBgs = [...backgrounds];
|
||||
localBgs[1].default = true;
|
||||
const [head, second, ...tail] = backgrounds;
|
||||
const localBgs = [head, { ...second, default: true }, ...tail];
|
||||
SpiedChannel.on('background', bg => {
|
||||
expect(bg).toBe(localBgs[1].value);
|
||||
expect(bg).toBe(second.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);
|
||||
expect(backgroundPanel.state('backgrounds')).toEqual(localBgs);
|
||||
backgroundPanel.setState({ backgrounds: localBgs }); // force re-render
|
||||
|
||||
// check to make sure the default bg was added
|
||||
const headings = backgroundPanel.find('h4');
|
||||
expect(headings.length).toBe(8);
|
||||
delete backgrounds[1].default;
|
||||
});
|
||||
|
||||
it('should unset all swatches on receiving the background-unset message', () => {
|
||||
@ -109,22 +100,25 @@ describe('Background Panel', () => {
|
||||
const backgroundPanel = mount(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
|
||||
SpiedChannel.emit('background-set', backgrounds);
|
||||
|
||||
expect(backgroundPanel.state.backgrounds[0].name).toBe(backgrounds[0].name);
|
||||
expect(backgroundPanel.state.backgrounds[2].value).toBe(backgrounds[2].value);
|
||||
expect(backgroundPanel.state('backgrounds')).toEqual(backgrounds);
|
||||
backgroundPanel.setState({ backgrounds }); // force re-render
|
||||
|
||||
SpiedChannel.emit('background-unset');
|
||||
expect(backgroundPanel.state.backgrounds.length).toBe(0);
|
||||
expect(backgroundPanel.state('backgrounds')).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should pass the event from swatch clicks through the provided channel', () => {
|
||||
const SpiedChannel = new EventEmitter();
|
||||
const backgroundPanel = mount(<BackgroundPanel channel={SpiedChannel} api={mockedApi} />);
|
||||
SpiedChannel.emit('background-set', backgrounds);
|
||||
backgroundPanel.setState({ backgrounds }); // force re-render
|
||||
|
||||
const spy = jest.fn();
|
||||
SpiedChannel.on('background', spy);
|
||||
|
||||
backgroundPanel.find('h4').click();
|
||||
backgroundPanel
|
||||
.find('h4')
|
||||
.first()
|
||||
.simulate('click');
|
||||
|
||||
expect(spy).toBeCalledWith(backgrounds[0].value);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user