wix-storybook-utils
Version:
Utilities for automated component documentation within Storybook
109 lines • 5.28 kB
JavaScript
import { mount } from 'enzyme';
import path from 'path';
import kebabCase from 'lodash/kebabCase';
import * as builders from './index';
import { SectionType } from '../typings/story-section';
import { api } from './views/api';
var storyConfigEmpty = {
metadata: { displayName: '', props: {} },
config: { importFormat: '', moduleName: '', repoBaseURL: '' },
component: null,
};
var cwd = path.resolve(__dirname, 'views');
var methodToFileName = function (f) { return kebabCase(path.parse(f).name); };
var sectionTypes = Object.keys(SectionType).map(function (t) { return SectionType[t]; });
describe('Sections', function () {
sectionTypes.map(function (type) {
it("should export view for ".concat(type, " section"), function () {
try {
var view = require(path.resolve(cwd, methodToFileName(type)));
expect(typeof view[type]).toBe('function');
}
catch (e) {
throw new Error("Missing view function for \"".concat(type, "\" section type. Make sure one exists in src/Sections/views. ERROR: ").concat(e));
}
});
it("should have builder for \"".concat(type, "\" section type"), function () {
return expect(typeof builders[type]).toBe('function');
});
});
it('should use parsedSource from api section', function () {
var parsedSource = { props: {} };
var renderedProps = mount(api({ type: SectionType.Api, parsedSource: parsedSource }, storyConfigEmpty)).props();
expect(renderedProps.metadata).toEqual(parsedSource);
});
});
describe('title section', function () {
it('should work with string or config object', function () {
expect(builders.title({ title: 'hello' })).toEqual(expect.objectContaining({
title: 'hello',
}));
expect(builders.title('hello')).toEqual(expect.objectContaining({
title: 'hello',
}));
});
});
describe('columns section', function () {
it('should work with array or config object', function () {
var items = ['a', 'b', 'c'].map(function (c) { return builders.title(c); });
expect(builders.columns({ items: items })).toEqual(expect.objectContaining({ items: items }));
expect(builders.columns(items)).toEqual(expect.objectContaining({ items: items }));
});
});
describe('tabs section', function () {
it('should work with array or config object', function () {
var tabs = [1, 2, 3].map(function (c) { return builders.tab({ sections: [] }); });
expect(builders.tabs({ tabs: tabs })).toEqual(expect.objectContaining({ tabs: tabs }));
expect(builders.tabs(tabs)).toEqual(expect.objectContaining({ tabs: tabs }));
});
});
describe('description section', function () {
it('should work with string or config object', function () {
var text = 'hello text';
var expectation = expect.objectContaining({ text: text });
expect(builders.description({ text: text })).toEqual(expectation);
expect(builders.description(text)).toEqual(expectation);
});
});
describe('table section', function () {
it('should work with array or config object', function () {
expect(builders.table({ rows: [] })).toEqual(expect.objectContaining({ rows: [] }));
expect(builders.table([])).toEqual(expect.objectContaining({ rows: [] }));
});
});
describe('importExample section', function () {
it('should work with string or config object', function () {
var source = 'hello world';
expect(builders.importExample({ source: source })).toEqual(expect.objectContaining({ source: source }));
expect(builders.importExample(source)).toEqual(expect.objectContaining({ source: source }));
});
});
describe('code section', function () {
it('should work with string or config object', function () {
var source = 'hello world';
expect(builders.code({ source: source })).toEqual(expect.objectContaining({ source: source }));
expect(builders.code(source)).toEqual(expect.objectContaining({ source: source }));
});
});
describe('plugin section', function () {
it('should delegate section generation to handler and pass context', function () {
var mockStoryConfig = { test: 'test' };
var pluginView = require(path.resolve(cwd, methodToFileName(SectionType.Plugin)))[SectionType.Plugin];
var handler = function (section, storyConfig) { return ({
section: section,
storyConfig: storyConfig,
}); };
var output = pluginView(builders.plugin({ handler: handler }), mockStoryConfig);
expect(output.section).toBeDefined();
expect(output.section.handler).toBe(handler);
expect(output.storyConfig).toBe(mockStoryConfig);
});
});
describe('example section', function () {
it('should set `compact` to true by default', function () {
var source = 'hello world';
expect(builders.example({ source: source })).toEqual(expect.objectContaining({ source: source, compact: true }));
expect(builders.example({ source: source, compact: false })).toEqual(expect.objectContaining({ source: source, compact: false }));
});
});
//# sourceMappingURL=index.test.js.map