UNPKG

wix-storybook-utils

Version:

Utilities for automated component documentation within Storybook

112 lines 5.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var enzyme_1 = require("enzyme"); var path_1 = tslib_1.__importDefault(require("path")); var kebabCase_1 = tslib_1.__importDefault(require("lodash/kebabCase")); var builders = tslib_1.__importStar(require("./index")); var story_section_1 = require("../typings/story-section"); var api_1 = require("./views/api"); var storyConfigEmpty = { metadata: { displayName: '', props: {} }, config: { importFormat: '', moduleName: '', repoBaseURL: '' }, component: null, }; var cwd = path_1.default.resolve(__dirname, 'views'); var methodToFileName = function (f) { return (0, kebabCase_1.default)(path_1.default.parse(f).name); }; var sectionTypes = Object.keys(story_section_1.SectionType).map(function (t) { return story_section_1.SectionType[t]; }); describe('Sections', function () { sectionTypes.map(function (type) { it("should export view for ".concat(type, " section"), function () { try { var view = require(path_1.default.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 = (0, enzyme_1.mount)((0, api_1.api)({ type: story_section_1.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_1.default.resolve(cwd, methodToFileName(story_section_1.SectionType.Plugin)))[story_section_1.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