Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send story context to transform source #12327

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/docs/docs/recipes.md
Expand Up @@ -270,7 +270,7 @@ Alternatively, you can provide a function in the `docs.transformSource` paramete
const SOURCE_REGEX = /^\(\) => `(.*)`$/;
export const parameters = {
docs: {
transformSource: (src, storyId) => {
transformSource: (src, storyId, storyContext) => {
const match = SOURCE_REGEX.exec(src);
return match ? match[1] : src;
},
Expand Down
35 changes: 33 additions & 2 deletions addons/docs/src/blocks/enhanceSource.test.ts
Expand Up @@ -11,15 +11,14 @@ const emptyContext: StoryContext = {
parameters: {},
};

const transformSource = (src?: string) => (src ? `formatted: ${src}` : 'no src');

describe('addon-docs enhanceSource', () => {
describe('no source loaded', () => {
const baseContext = emptyContext;
it('no transformSource', () => {
expect(enhanceSource(baseContext)).toBeNull();
});
it('transformSource', () => {
const transformSource = (src?: string) => (src ? `formatted: ${src}` : 'no src');
const parameters = { ...baseContext.parameters, docs: { transformSource } };
expect(enhanceSource({ ...baseContext, parameters })).toBeNull();
});
Expand All @@ -35,11 +34,23 @@ describe('addon-docs enhanceSource', () => {
});
});
it('transformSource', () => {
const transformSource = (src?: string) => (src ? `formatted: ${src}` : 'no src');
const parameters = { ...baseContext.parameters, docs: { transformSource } };
expect(enhanceSource({ ...baseContext, parameters }).docs.source).toEqual({
code: 'formatted: storySource.source',
});
});
it('receives StoryContext as second argument', () => {
const transformSource = jest.fn();
const parameters = { ...baseContext.parameters, docs: { transformSource } };
const context = { ...baseContext, parameters };
enhanceSource(context);
expect(transformSource).toHaveBeenCalledWith(
baseContext.parameters.storySource.source,
baseContext.id,
context
);
});
});
describe('storysource source loaded w/ locationsMap', () => {
const baseContext = {
Expand All @@ -57,11 +68,19 @@ describe('addon-docs enhanceSource', () => {
expect(enhanceSource(baseContext)).toEqual({ docs: { source: { code: 'Source' } } });
});
it('transformSource', () => {
const transformSource = (src?: string) => (src ? `formatted: ${src}` : 'no src');
const parameters = { ...baseContext.parameters, docs: { transformSource } };
expect(enhanceSource({ ...baseContext, parameters }).docs.source).toEqual({
code: 'formatted: Source',
});
});
it('receives StoryContext as second argument', () => {
const transformSource = jest.fn();
const parameters = { ...baseContext.parameters, docs: { transformSource } };
const context = { ...baseContext, parameters };
enhanceSource(context);
expect(transformSource).toHaveBeenCalledWith('Source', context.id, context);
});
});
describe('custom docs.source provided', () => {
const baseContext = {
Expand All @@ -75,9 +94,21 @@ describe('addon-docs enhanceSource', () => {
expect(enhanceSource(baseContext)).toBeNull();
});
it('transformSource', () => {
const transformSource = (src?: string) => (src ? `formatted: ${src}` : 'no src');
const { source } = baseContext.parameters.docs;
const parameters = { ...baseContext.parameters, docs: { source, transformSource } };
expect(enhanceSource({ ...baseContext, parameters })).toBeNull();
});
it('receives StoryContext as second argument', () => {
Copy link
Member

@shilman shilman Aug 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we test this with a single test instead of three?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the extra tests.

const transformSource = jest.fn();
const parameters = { ...baseContext.parameters, docs: { transformSource } };
const context = { ...baseContext, parameters };
enhanceSource(context);
expect(transformSource).toHaveBeenCalledWith(
context.parameters.storySource.source,
context.id,
context
);
});
});
});
2 changes: 1 addition & 1 deletion addons/docs/src/blocks/enhanceSource.ts
Expand Up @@ -54,7 +54,7 @@ export const enhanceSource = (context: StoryContext): Parameters => {
}

const input = extract(id, storySource);
const code = transformSource ? transformSource(input, id) : input;
const code = transformSource ? transformSource(input, id, context) : input;

return { docs: combineParameters(docs, { source: { code } }) };
};
Expand Up @@ -2,7 +2,13 @@

exports[`Storyshots Demo Button 1`] = `
<button>
Hello Button
Click Me
</button>
`;

exports[`Storyshots Demo Button With Custom Source Code 1`] = `
<button>
Click Me
</button>
`;

Expand Down
35 changes: 29 additions & 6 deletions examples/html-kitchen-sink/stories/button.stories.js
Expand Up @@ -4,19 +4,42 @@ import { useEffect } from '@storybook/client-api';

export default {
title: 'Demo',
argTypes: {
buttonText: { control: 'text', defaultValue: 'Click Me' },
},
};

export const Heading = () => '<h1>Hello World</h1>';
export const Headings = () =>
'<h1>Hello World</h1><h2>Hello World</h2><h3>Hello World</h3><h4>Hello World</h4>';

export const Button = () => {
const Template = ({ buttonText }) => {
const btn = document.createElement('button');
btn.innerHTML = 'Hello Button';
btn.innerHTML = buttonText;
btn.addEventListener('click', action('Click'));
return btn;
};

export const ButtonWithCustomSourceCode = Template.bind({});

ButtonWithCustomSourceCode.args = {
buttonText: 'Click Me',
};

ButtonWithCustomSourceCode.parameters = {
docs: {
transformSource: (src, id, context) => {
return Template(context.args).outerHTML;
},
},
};

export const Button = Template.bind({});

Button.args = {
buttonText: 'Click Me',
};

export const Heading = () => '<h1>Hello World</h1>';
export const Headings = () =>
'<h1>Hello World</h1><h2>Hello World</h2><h3>Hello World</h3><h4>Hello World</h4>';

export const Effect = () => {
useEffect(() => {
document.getElementById('button').style.backgroundColor = 'yellow';
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/version.ts
@@ -1 +1 @@
export const version = '6.1.0-alpha.1';
export const version = '6.1.0-alpha.1';
2 changes: 1 addition & 1 deletion lib/cli/versions.json
Expand Up @@ -52,4 +52,4 @@
"@storybook/ui": "6.1.0-alpha.1",
"@storybook/vue": "6.1.0-alpha.1",
"@storybook/web-components": "6.1.0-alpha.1"
}
}