Skip to content

Commit

Permalink
feat: add ability to load store from bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 28, 2021
1 parent 2492e97 commit 9c7322e
Show file tree
Hide file tree
Showing 62 changed files with 6,457 additions and 145 deletions.
1 change: 1 addition & 0 deletions plugins/jest-snapshots/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"fix": "yarn lint --fix",
"lint": "yarn eslint . --ext mdx,ts,tsx",
"prepare": "yarn build",
"test:create-bundle": "ccc -c test/.config -d test/bundle",
"test": "yarn jest"
},
"homepage": "https://github.com/ccontrols/component-controls",
Expand Down
8 changes: 8 additions & 0 deletions plugins/jest-snapshots/src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export const jestCliArgs: ArgOptions = [
type: 'boolean',
},
},
{
name: 'bundle',
options: {
alias: 'b',
description: 'bundle path, if store loaded ffrom bundle',
type: 'string',
},
},
{
name: 'output',
options: {
Expand Down
2 changes: 2 additions & 0 deletions plugins/jest-snapshots/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const run = async (): Promise<void> => {
overwrite,
config,
test = 'stories.test.js',
bundle,
name,
} = parsedArgs;
const testFilePath = path.resolve(testFolder, test);
Expand All @@ -38,6 +39,7 @@ export const run = async (): Promise<void> => {
format,
configPath: config,
name,
bundle,
});
fs.writeFileSync(testFilePath, content, 'utf8');
};
29 changes: 28 additions & 1 deletion plugins/jest-snapshots/src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type TemplateOptions = {
renderer?: Renderers;
format?: TeplateFormats;
configPath?: string;
bundle?: string;
name?: string;
};
export const createTemplate = (options?: TemplateOptions): string => {
Expand All @@ -19,6 +20,7 @@ export const createTemplate = (options?: TemplateOptions): string => {
format = 'cjs',
configPath = path.resolve(process.cwd(), defaultConfigFolder),
name = 'component-controls generated',
bundle,
} = options || {};
const filePath = path.resolve(
__dirname,
Expand All @@ -29,15 +31,40 @@ export const createTemplate = (options?: TemplateOptions): string => {
__dirname,
`../templates/imports/${renderers[renderer]}.${format}.js`,
);
const store = bundle ? 'bundle' : 'imports';
const storeImportPath = path.resolve(
__dirname,
`../templates/store-import/${store}.${format}.js`,
);
const storeLoopPath = path.resolve(
__dirname,
`../templates/store-loop/${store}.${format}.js`,
);
const storeRenderPath = path.resolve(
__dirname,
`../templates/store-render/${store}.${format}.js`,
);
const storeRender = fs.readFileSync(storeRenderPath, 'utf8');
const renderPath = path.resolve(
__dirname,
`../templates/render/${renderers[renderer]}.${format}.js`,
);
const render = dot.template(fs.readFileSync(renderPath, 'utf8'))({
storeRender,
});

const vars = {
imports: fs.readFileSync(importsPath, 'utf8'),
render: fs.readFileSync(renderPath, 'utf8'),
render,
configPath,
storeRender,
storeImports: fs.readFileSync(storeImportPath, 'utf8'),
bundlePath: bundle,
storeLoop: dot.template(fs.readFileSync(storeLoopPath, 'utf8'))({
render,
bundlePath: bundle,
configPath,
}),
name,
};
const content = fs.readFileSync(filePath, 'utf8');
Expand Down
35 changes: 3 additions & 32 deletions plugins/jest-snapshots/templates/all-store.cjs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
const {
loadConfigurations,
extractDocuments,
} = require('@component-controls/config');
const { renderExample } = require('@component-controls/test-renderers');
const { render: reactRender } = require('@component-controls/render/react');
{{=it.storeImports}}

{{=it.imports}}
describe('{{=it.name}}', () => {
Expand Down Expand Up @@ -31,30 +26,6 @@ describe('{{=it.name}}', () => {
});
done();
});
const configPath = '{{=it.configPath}}';
const config = loadConfigurations(configPath);
if (!config.renderFn) {
config.renderFn = reactRender;
}
const documents = extractDocuments({ config, configPath });
if (documents) {
documents.forEach(file => {
const exports = require(file);
const doc = exports.default;
const examples = Object.keys(exports)
.filter(key => key !== 'default')
.map(key => exports[key]);

if (examples.length) {
describe(doc.title, () => {
examples.forEach(example => {
it(example.name, async () => {
{{=it.render}}
expect(serialize()).toMatchSnapshot();
});
});
});
}
});
}
{{=it.storeLoop}}

});
31 changes: 2 additions & 29 deletions plugins/jest-snapshots/templates/all-store.esm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { loadConfigurations, extractDocuments } from '@component-controls/config';
import { renderExample } from '@component-controls/test-renderers';
import { render as reactRender } from '@component-controls/render/react';
{{=it.storeImports}}

{{=it.imports}}
describe('{{=it.name}}', () => {
Expand Down Expand Up @@ -28,30 +26,5 @@ describe('{{=it.name}}', () => {
});
done();
});
const configPath = '{{=it.configPath}}';
const config = loadConfigurations(configPath);
if (!config.renderFn) {
config.renderFn = reactRender;
}
const documents = extractDocuments({ config, configPath });
if (documents) {
documents.forEach(file => {
const exports = require(file);
const doc = exports.default;
const examples = Object.keys(exports)
.filter(key => key !== 'default')
.map(key => exports[key]);

if (examples.length) {
describe(doc.title, () => {
examples.forEach(example => {
it(example.name, async () => {
{{=it.render}}
expect(serialize()).toMatchSnapshot();
});
});
});
}
});
}
{{=it.storeLoop}}
});
31 changes: 2 additions & 29 deletions plugins/jest-snapshots/templates/all-store.ts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { loadConfigurations, extractDocuments } from '@component-controls/config';
import { renderExample } from '@component-controls/test-renderers';
import { render as reactRender } from '@component-controls/render/react';
{{=it.storeImports}}

{{=it.imports}}
describe('{{=it.name}}', () => {
Expand Down Expand Up @@ -28,30 +26,5 @@ describe('{{=it.name}}', () => {
});
done();
});
const configPath = '{{=it.configPath}}';
const config = loadConfigurations(configPath);
if (!config.renderFn) {
config.renderFn = reactRender;
}
const documents = extractDocuments({ config, configPath });
if (documents) {
documents.forEach((file: string) => {
const exports = require(file);
const doc = exports.default;
const examples = Object.keys(exports)
.filter(key => key !== 'default')
.map(key => exports[key]);

if (examples.length) {
describe(doc.title, () => {
examples.forEach(example => {
it(example.name, async () => {
{{=it.render}}
expect(serialize()).toMatchSnapshot();
});
});
});
}
});
}
{{=it.storeLoop}}
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const rendered = renderExample({
example,
doc,
config,
});
let rendered;
{{=it.storeRender}}
const component = mount(rendered);
const serialize = () => toJson(component, { mode: 'deep' });
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const rendered = renderExample({
example,
doc,
config,
});
let rendered;
{{=it.storeRender}}
const component = mount(rendered);
const serialize = () => toJson(component, { mode: 'deep' });
7 changes: 2 additions & 5 deletions plugins/jest-snapshots/templates/render/enzyme-react-17.ts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const rendered = renderExample({
example,
doc,
config,
});
let rendered;
{{=it.storeRender}}
const component = mount(rendered);
const serialize = () => toJson(component, { mode: 'deep' });
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
let rendered;
renderer.act(() => {
rendered = renderExample({
example,
doc,
config,
});
});
{{=it.storeRender}}
})
const component = renderer.create(rendered);
const serialize = component ? component.toJSON : () => undefined;
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
let rendered;
act(() => {
rendered = renderExample({
example,
doc,
config,
});
{{=it.storeRender}}
});
const component = rendered ? renderer.create(rendered) : undefined;
const serialize = component ? component.toJSON : () => undefined;
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
let rendered;
act(() => {
rendered = renderExample({
example,
doc,
config,
});
});
{{=it.storeRender}}
})
const component = rendered ? renderer.create(rendered) : undefined;
const serialize = component ? component.toJSON : () => undefined;
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
let rendered;
act(() => {
rendered = renderExample({
example,
doc,
config,
});
{{=it.storeRender}}
});
const { asFragment: serialize } = render(rendered);
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
let rendered;
act(() => {
rendered = renderExample({
example,
doc,
config,
});
{{=it.storeRender}}
});
let serialize;
if (rendered) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
let rendered;
act(() => {
rendered = renderExample({
example,
doc,
config,
});
{{=it.storeRender}}
});
let serialize;
if (rendered) {
Expand Down
2 changes: 2 additions & 0 deletions plugins/jest-snapshots/templates/store-import/bundle.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const { loadStore } = require('@component-controls/store');
const { render: reactRender } = require('@component-controls/render/react');
2 changes: 2 additions & 0 deletions plugins/jest-snapshots/templates/store-import/bundle.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { loadStore } from '@component-controls/store';
const { render: reactRender } = require('@component-controls/render/react');
2 changes: 2 additions & 0 deletions plugins/jest-snapshots/templates/store-import/bundle.ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { loadStore } from '@component-controls/store';
const { render: reactRender } = require('@component-controls/render/react');
6 changes: 6 additions & 0 deletions plugins/jest-snapshots/templates/store-import/imports.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const {
loadConfigurations,
extractDocuments,
} = require('@component-controls/config');
const { renderExample } = require('@component-controls/test-renderers');
const { render: reactRender } = require('@component-controls/render/react');
3 changes: 3 additions & 0 deletions plugins/jest-snapshots/templates/store-import/imports.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { loadConfigurations, extractDocuments } from '@component-controls/config';
import { renderExample } from '@component-controls/test-renderers';
import { render as reactRender } from '@component-controls/render/react';
3 changes: 3 additions & 0 deletions plugins/jest-snapshots/templates/store-import/imports.ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { loadConfigurations, extractDocuments } from '@component-controls/config';
import { renderExample } from '@component-controls/test-renderers';
import { render as reactRender } from '@component-controls/render/react';
17 changes: 17 additions & 0 deletions plugins/jest-snapshots/templates/store-loop/bundle.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const store = loadStore(require('{{=it.bundlePath}}'));
const renderFn = store.config.renderFn || reactRender;
Object.keys(store.docs).forEach(docId => {
const doc = store.docs[docId];
if (doc.stories && doc.stories.length) {
const stories = doc.stories;
describe(doc.title, () => {
stories.forEach(storyId => {
const story = store.stories[storyId];
it(story.name, async () => {
{{=it.render}}
expect(serialize()).toMatchSnapshot();
});
});
});
}
});
17 changes: 17 additions & 0 deletions plugins/jest-snapshots/templates/store-loop/bundle.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const store = loadStore(require('{{=it.bundlePath}}'));
const renderFn = store.config.renderFn || reactRender;
Object.keys(store.docs).forEach(docId => {
const doc = store.docs[docId];
if (doc.stories && doc.stories.length) {
const stories = doc.stories;
describe(doc.title, () => {
stories.forEach(storyId => {
const story = store.stories[storyId];
it(story.name, async () => {
{{=it.render}}
expect(serialize()).toMatchSnapshot();
});
});
});
}
});
Loading

0 comments on commit 9c7322e

Please sign in to comment.