From b8df411f88abefb49607284431c21ab4760cf2cf Mon Sep 17 00:00:00 2001 From: Artem Sapegin Date: Sat, 20 Feb 2021 08:29:44 +0100 Subject: [PATCH] Don't prepend the current component import if it's the only import --- .../rehype/__tests__/exportStories.spec.ts | 62 ++++++++++++++++++- src/loaders/rehype/exportStories.ts | 27 +++++--- 2 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/loaders/rehype/__tests__/exportStories.spec.ts b/src/loaders/rehype/__tests__/exportStories.spec.ts index 965f8f708..45dfa1abf 100644 --- a/src/loaders/rehype/__tests__/exportStories.spec.ts +++ b/src/loaders/rehype/__tests__/exportStories.spec.ts @@ -153,6 +153,64 @@ export const basic = () => `); }); + test('skips current component', async () => { + const result = await compile( + `Henlo`, + ` +import Pizza from './Pizza'; +import Container from './Container'; +export const basic = () => +` + ); + expect(result).toMatchInlineSnapshot(` + " + import * as __story_import_0 from './Pizza' + import * as __story_import_1 from './Container' + export const __namedExamples = { + 'basic': 'import Container from \\\\'./Container\\\\';\\\\n\\\\n' + }; + export const __storiesScope = { + './Pizza': __story_import_0, + './Container': __story_import_1 + }; + + const layoutProps = { + __namedExamples, + __storiesScope + }; + " + `); + }); + + test('includes the current component when it is not the only import', async () => { + const result = await compile( + `Henlo`, + ` +import Pizza, { cheese } from './Pizza'; +import Container from './Container'; +export const basic = () => +` + ); + expect(result).toMatchInlineSnapshot(` + " + import * as __story_import_0 from './Pizza' + import * as __story_import_1 from './Container' + export const __namedExamples = { + 'basic': 'import Pizza, { cheese } from \\\\'./Pizza\\\\';\\\\nimport Container from \\\\'./Container\\\\';\\\\n\\\\n' + }; + export const __storiesScope = { + './Pizza': __story_import_0, + './Container': __story_import_1 + }; + + const layoutProps = { + __namedExamples, + __storiesScope + }; + " + `); + }); + test('unused imports', async () => { const result = await compile( `Henlo`, @@ -182,7 +240,7 @@ export const basic = () =>