wix-storybook-utils
Version:
Utilities for automated component documentation within Storybook
92 lines • 7.66 kB
JavaScript
"use strict";
/* global describe it expect */
var prepareStory = require('./prepareStory');
var path = require('path');
describe('prepareStory', function () {
describe('given erroneous input', function () {
it('should reject promise with message', function () {
return expect(prepareStory()()).rejects.toEqual('ERROR: unable to prepare story, both `storyConfig` and `source` must be provided');
});
});
describe('with 2 curried calls', function () {
it('should return promise', function () {
expect(prepareStory({})('test').then).toBeDefined();
});
it('should reject with error when exported config is not an object', function () {
var source = "const something = \"hello\";\nexport default something;";
return expect(prepareStory({})(source)).rejects.toMatch('ERROR');
});
it('should wrap exported object with `story()`', function () {
var source = 'export default { a: 1 };';
var expectation = "import story from \"wix-storybook-utils/Story\";\nimport { storiesOf } from \"@storybook/react\";\nstory({\n a: 1,\n _config: {\n storiesOf: storiesOf\n }\n})";
return expect(prepareStory({})(source)).resolves.toEqual(expectation);
});
it('should add _config to exported object', function () {
var source = 'export default { a: 1 };';
var config = { a: 1 };
var expectation = "import story from \"wix-storybook-utils/Story\";\nimport { storiesOf } from \"@storybook/react\";\nstory({\n a: 1,\n _config: {\n \"a\": 1,\n storiesOf: storiesOf\n }\n})";
return expect(prepareStory(config)(source)).resolves.toEqual(expectation);
});
it('should work with referenced story config', function () {
var source = "\n const config = { a: 1, b: { c: 'hey' } };\n export default config;\n ";
var config = {
hello: 'config!',
time: { to: { say: { good: 'buy' } } },
};
var expectation = "import story from \"wix-storybook-utils/Story\";\nimport { storiesOf } from \"@storybook/react\";\nconst config = {\n a: 1,\n b: {\n c: 'hey'\n },\n _config: {\n \"hello\": \"config!\",\n \"time\": {\n \"to\": {\n \"say\": {\n \"good\": \"buy\"\n }\n }\n },\n storiesOf: storiesOf\n }\n};\nstory(config)";
return expect(prepareStory(config)(source)).resolves.toEqual(expectation);
});
it('should work with spread properties', function () {
var source = "\n const stuff = { thing: { moreThings: ['hello'] } };\n export default {\n a: 1,\n b: {\n ...stuff,\n c: ['d']\n }\n };\n ";
var config = { 'i-am-config': 'yes' };
var expectation = "import story from \"wix-storybook-utils/Story\";\nimport { storiesOf } from \"@storybook/react\";\nconst stuff = {\n thing: {\n moreThings: ['hello']\n }\n};\nstory({\n a: 1,\n b: {\n ...stuff,\n c: ['d']\n },\n _config: {\n \"i-am-config\": \"yes\",\n storiesOf: storiesOf\n }\n})";
return expect(prepareStory(config)(source)).resolves.toEqual(expectation);
});
it('should work with existing nested properties', function () {
var source = "\n const stuff = { thing: { moreThings: ['hello'] } };\n const callMe = () => true;\n export default {\n a: 1,\n b: {\n ...stuff,\n c: ['d']\n },\n d: {\n e: {\n f: {\n hello: callMe('maybe')\n }\n }\n }\n };\n ";
var config = {
'i-am-config': 'yes',
nested: {
oh: {
boy: {
thing: 'hey there!',
},
},
},
};
var expectation = "import story from \"wix-storybook-utils/Story\";\nimport { storiesOf } from \"@storybook/react\";\nconst stuff = {\n thing: {\n moreThings: ['hello']\n }\n};\nconst callMe = () => true;\nstory({\n a: 1,\n b: {\n ...stuff,\n c: ['d']\n },\n d: {\n e: {\n f: {\n hello: callMe('maybe')\n }\n }\n },\n _config: {\n \"i-am-config\": \"yes\",\n \"nested\": {\n \"oh\": {\n \"boy\": {\n \"thing\": \"hey there!\"\n }\n }\n },\n storiesOf: storiesOf\n }\n})";
return expect(prepareStory(config)(source)).resolves.toEqual(expectation);
});
it('should work with module.exports', function () {
var source = 'module.exports = { a: 1 };';
var config = { a: 1 };
var expectation = "const story = require(\"wix-storybook-utils/Story\").default;\nconst {\n storiesOf\n} = require(\"@storybook/react\");\nstory({\n a: 1,\n _config: {\n \"a\": 1,\n storiesOf: storiesOf\n }\n})";
return expect(prepareStory(config)(source)).resolves.toEqual(expectation);
});
it('should work with referenced module.exports', function () {
var source = "\n const stuff = { thing: { moreThings: ['hello'] } };\n const callMe = () => true;\n const reference = {\n a: 1,\n b: {\n ...stuff,\n c: ['d']\n },\n d: {\n e: {\n f: {\n hello: callMe('maybe')\n }\n }\n }\n };\n\n module.exports = reference;\n ";
var config = {
'i-am-config': 'yes',
nested: {
oh: {
boy: {
thing: 'hey there!',
},
},
},
};
var expectation = "const story = require(\"wix-storybook-utils/Story\").default;\nconst {\n storiesOf\n} = require(\"@storybook/react\");\nconst stuff = {\n thing: {\n moreThings: ['hello']\n }\n};\nconst callMe = () => true;\nconst reference = {\n a: 1,\n b: {\n ...stuff,\n c: ['d']\n },\n d: {\n e: {\n f: {\n hello: callMe('maybe')\n }\n }\n },\n _config: {\n \"i-am-config\": \"yes\",\n \"nested\": {\n \"oh\": {\n \"boy\": {\n \"thing\": \"hey there!\"\n }\n }\n },\n storiesOf: storiesOf\n }\n};\nstory(reference)";
return expect(prepareStory(config)(source)).resolves.toEqual(expectation);
});
it('should inject playgroundComponents require when given playgroundComponentsPath through storyConfig', function () {
var componentContextPath = '/src/component';
var source = "\n export default {};\n ";
var config = {
playgroundComponentsPath: '/.storybook/playground.scope',
};
var expectation = "import story from \"wix-storybook-utils/Story\";\nimport { storiesOf } from \"@storybook/react\";\nstory({\n _config: {\n \"playgroundComponentsPath\": \"/.storybook/playground.scope\",\n playgroundComponents: require('../../.storybook/playground.scope').default,\n storiesOf: storiesOf\n }\n})";
return expect(prepareStory(config, componentContextPath)(source)).resolves.toEqual(expectation);
});
});
});
//# sourceMappingURL=prepareStory.spec.js.map