mirror of
https://github.com/storybookjs/storybook.git
synced 2025-03-28 05:10:17 +08:00
Added a test file for the start function
This commit is contained in:
parent
80c96a972f
commit
28f10cb715
101
lib/core/src/client/preview/start.test.js
Normal file
101
lib/core/src/client/preview/start.test.js
Normal file
@ -0,0 +1,101 @@
|
||||
import addons from '@storybook/addons';
|
||||
import Events from '@storybook/core-events';
|
||||
import { document } from 'global';
|
||||
|
||||
import start from './start';
|
||||
|
||||
jest.mock('@storybook/client-logger');
|
||||
jest.mock('@storybook/addons');
|
||||
jest.mock('global', () => ({
|
||||
navigator: { userAgent: 'browser' },
|
||||
window: {
|
||||
addEventListener: jest.fn(),
|
||||
location: { search: '' },
|
||||
history: { replaceState: jest.fn() },
|
||||
},
|
||||
document: {
|
||||
addEventListener: jest.fn(),
|
||||
getElementById: jest.fn().mockReturnValue({}),
|
||||
body: { classList: { add: jest.fn(), remove: jest.fn() } },
|
||||
documentElement: {},
|
||||
},
|
||||
}));
|
||||
|
||||
function mockEmit() {
|
||||
const emit = jest.fn();
|
||||
addons.getChannel.mockReturnValue({ emit });
|
||||
|
||||
return emit;
|
||||
}
|
||||
|
||||
it('renders nopreview when you have no stories', () => {
|
||||
const emit = mockEmit();
|
||||
|
||||
const render = jest.fn();
|
||||
|
||||
start(render);
|
||||
|
||||
expect(render).not.toHaveBeenCalled();
|
||||
expect(document.body.classList.add).toHaveBeenCalledWith('sb-show-nopreview');
|
||||
expect(emit).toHaveBeenCalledWith(Events.STORY_RENDERED);
|
||||
});
|
||||
|
||||
it('calls render when you add a story', () => {
|
||||
const emit = mockEmit();
|
||||
|
||||
const render = jest.fn();
|
||||
|
||||
const { clientApi, configApi } = start(render);
|
||||
|
||||
emit.mockReset();
|
||||
configApi.configure(() => {
|
||||
clientApi.storiesOf('kind', {}).add('story', () => {});
|
||||
}, {});
|
||||
|
||||
expect(render).toHaveBeenCalled();
|
||||
expect(emit).toHaveBeenCalledWith(Events.STORY_RENDERED);
|
||||
});
|
||||
|
||||
it('emits an exception and shows error when your story throws', () => {
|
||||
const emit = mockEmit();
|
||||
|
||||
const render = jest.fn().mockImplementation(() => {
|
||||
throw new Error('Some exception');
|
||||
});
|
||||
|
||||
const { clientApi, configApi } = start(render);
|
||||
|
||||
emit.mockReset();
|
||||
document.body.classList.add.mockReset();
|
||||
configApi.configure(() => {
|
||||
clientApi.storiesOf('kind', {}).add('story', () => {});
|
||||
}, {});
|
||||
|
||||
expect(render).toHaveBeenCalled();
|
||||
expect(document.body.classList.add).toHaveBeenCalledWith('sb-show-errordisplay');
|
||||
expect(emit).toHaveBeenCalledWith(Events.STORY_THREW_EXCEPTION, expect.any(Error));
|
||||
});
|
||||
|
||||
it('emits an error and shows error when your framework calls showError', () => {
|
||||
const emit = mockEmit();
|
||||
|
||||
const error = {
|
||||
title: 'Some error',
|
||||
description: 'description',
|
||||
};
|
||||
const render = jest.fn().mockImplementation(({ showError }) => {
|
||||
showError(error);
|
||||
});
|
||||
|
||||
const { clientApi, configApi } = start(render);
|
||||
|
||||
emit.mockReset();
|
||||
document.body.classList.add.mockReset();
|
||||
configApi.configure(() => {
|
||||
clientApi.storiesOf('kind', {}).add('story', () => {});
|
||||
}, {});
|
||||
|
||||
expect(render).toHaveBeenCalled();
|
||||
expect(document.body.classList.add).toHaveBeenCalledWith('sb-show-errordisplay');
|
||||
expect(emit).toHaveBeenCalledWith(Events.STORY_ERRORED, error);
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user