Merge pull request #12265 from phated/transformSource-context

Addon-docs: Change 2nd argument of transformSource to the storyContext
This commit is contained in:
Michael Shilman 2020-09-03 21:09:27 +08:00 committed by GitHub
commit 910af1e743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 5 deletions

View File

@ -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, storyContext) => {
const match = SOURCE_REGEX.exec(src);
return match ? match[1] : src;
},

View File

@ -11,8 +11,6 @@ const emptyContext: StoryContext = {
parameters: {},
};
const transformSource = (src?: string) => (src ? `formatted: ${src}` : 'no src');
describe('addon-docs enhanceSource', () => {
describe('no source loaded', () => {
const baseContext = emptyContext;
@ -20,14 +18,16 @@ describe('addon-docs enhanceSource', () => {
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();
});
});
describe('custom/mdx source loaded', () => {
const source = 'storySource.source';
const baseContext = {
...emptyContext,
parameters: { storySource: { source: 'storySource.source' } },
parameters: { storySource: { source } },
};
it('no transformSource', () => {
expect(enhanceSource(baseContext)).toEqual({
@ -35,11 +35,19 @@ 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 context as 2nd argument', () => {
const transformSource = jest.fn();
const parameters = { ...baseContext.parameters, docs: { transformSource } };
const context = { ...baseContext, parameters };
enhanceSource(context);
expect(transformSource).toHaveBeenCalledWith(source, context);
});
});
describe('storysource source loaded w/ locationsMap', () => {
const baseContext = {
@ -57,11 +65,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 context as 2nd argument', () => {
const transformSource = jest.fn();
const parameters = { ...baseContext.parameters, docs: { transformSource } };
const context = { ...baseContext, parameters };
enhanceSource(context);
expect(transformSource).toHaveBeenCalledWith('Source', context);
});
});
describe('custom docs.source provided', () => {
const baseContext = {
@ -75,6 +91,7 @@ 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();

View File

@ -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, context) : input;
return { docs: combineParameters(docs, { source: { code } }) };
};