mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-04 09:01:07 +08:00
76 lines
2.6 KiB
JavaScript
76 lines
2.6 KiB
JavaScript
const path = require('path');
|
|
const fs = require('fs-extra');
|
|
const mdx = require('@mdx-js/mdx');
|
|
const prettier = require('prettier');
|
|
const plugin = require('./mdx-compiler-plugin');
|
|
|
|
function format(code) {
|
|
return prettier.format(code, {
|
|
parser: 'babel',
|
|
printWidth: 100,
|
|
tabWidth: 2,
|
|
bracketSpacing: true,
|
|
trailingComma: 'es5',
|
|
singleQuote: true,
|
|
});
|
|
}
|
|
|
|
async function generate(filePath) {
|
|
const content = await fs.readFile(filePath, 'utf8');
|
|
|
|
const result = mdx.sync(content, {
|
|
filepath: filePath,
|
|
compilers: [plugin({})],
|
|
});
|
|
|
|
return format(result);
|
|
}
|
|
|
|
describe('docs-mdx-compiler-plugin', () => {
|
|
it('supports vanilla mdx', async () => {
|
|
const code = await generate(path.resolve(__dirname, './fixtures/vanilla.mdx'));
|
|
expect(code).toMatchSnapshot();
|
|
});
|
|
it('supports story definitions', async () => {
|
|
const code = await generate(path.resolve(__dirname, './fixtures/story-definitions.mdx'));
|
|
expect(code).toMatchSnapshot();
|
|
});
|
|
it('supports text-only story definitions', async () => {
|
|
const code = await generate(path.resolve(__dirname, './fixtures/story-def-text-only.mdx'));
|
|
expect(code).toMatchSnapshot();
|
|
});
|
|
it('supports object-style story definitions', async () => {
|
|
const code = await generate(path.resolve(__dirname, './fixtures/story-object.mdx'));
|
|
expect(code).toMatchSnapshot();
|
|
});
|
|
it('supports story references', async () => {
|
|
const code = await generate(path.resolve(__dirname, './fixtures/story-references.mdx'));
|
|
expect(code).toMatchSnapshot();
|
|
});
|
|
it('supports "smart" current story', async () => {
|
|
const code = await generate(path.resolve(__dirname, './fixtures/story-current.mdx'));
|
|
expect(code).toMatchSnapshot();
|
|
});
|
|
it('supports previews', async () => {
|
|
const code = await generate(path.resolve(__dirname, './fixtures/previews.mdx'));
|
|
expect(code).toMatchSnapshot();
|
|
});
|
|
it('supports decorators', async () => {
|
|
const code = await generate(path.resolve(__dirname, './fixtures/decorators.mdx'));
|
|
expect(code).toMatchSnapshot();
|
|
});
|
|
it('supports parameters', async () => {
|
|
const code = await generate(path.resolve(__dirname, './fixtures/parameters.mdx'));
|
|
expect(code).toMatchSnapshot();
|
|
});
|
|
it('supports non-story exports', async () => {
|
|
const code = await generate(path.resolve(__dirname, './fixtures/non-story-exports.mdx'));
|
|
expect(code).toMatchSnapshot();
|
|
});
|
|
it('errors on missing story props', async () => {
|
|
await expect(
|
|
generate(path.resolve(__dirname, './fixtures/story-missing-props.mdx'))
|
|
).rejects.toThrow('Expected a story name or ID attribute');
|
|
});
|
|
});
|