-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: add type annotations in javascript main config files
- Loading branch information
Showing
2 changed files
with
172 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import fse from 'fs-extra'; | ||
import dedent from 'ts-dedent'; | ||
import { SupportedLanguage } from '../project_types'; | ||
import { configureMain, configurePreview } from './configure'; | ||
|
||
jest.mock('fs-extra'); | ||
|
||
describe('configureMain', () => { | ||
beforeAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('should generate main.js', async () => { | ||
await configureMain({ | ||
language: SupportedLanguage.JAVASCRIPT, | ||
addons: [], | ||
storybookConfigFolder: '.storybook', | ||
framework: { | ||
name: '@storybook/react-vite', | ||
}, | ||
}); | ||
|
||
const { calls } = (fse.writeFile as unknown as jest.Mock).mock; | ||
const [mainConfigPath, mainConfigContent] = calls[0]; | ||
|
||
expect(mainConfigPath).toEqual('./.storybook/main.js'); | ||
expect(mainConfigContent).toMatchInlineSnapshot(` | ||
"/** @type { import('@storybook/react-vite').StorybookConfig } */ | ||
const config = { | ||
\\"stories\\": [ | ||
\\"../stories/**/*.mdx\\", | ||
\\"../stories/**/*.stories.@(js|jsx|ts|tsx)\\" | ||
], | ||
\\"addons\\": [], | ||
\\"framework\\": { | ||
\\"name\\": \\"@storybook/react-vite\\" | ||
} | ||
}; | ||
export default config;" | ||
`); | ||
}); | ||
|
||
test('should generate main.ts', async () => { | ||
await configureMain({ | ||
language: SupportedLanguage.TYPESCRIPT, | ||
addons: [], | ||
storybookConfigFolder: '.storybook', | ||
framework: { | ||
name: '@storybook/react-vite', | ||
}, | ||
}); | ||
|
||
const { calls } = (fse.writeFile as unknown as jest.Mock).mock; | ||
const [mainConfigPath, mainConfigContent] = calls[0]; | ||
|
||
expect(mainConfigPath).toEqual('./.storybook/main.ts'); | ||
expect(mainConfigContent).toMatchInlineSnapshot(` | ||
"import { StorybookConfig } from '@storybook/react-vite'; | ||
const config: StorybookConfig = { | ||
\\"stories\\": [ | ||
\\"../stories/**/*.mdx\\", | ||
\\"../stories/**/*.stories.@(js|jsx|ts|tsx)\\" | ||
], | ||
\\"addons\\": [], | ||
\\"framework\\": { | ||
\\"name\\": \\"@storybook/react-vite\\" | ||
} | ||
}; | ||
export default config;" | ||
`); | ||
}); | ||
}); | ||
|
||
describe('configurePreview', () => { | ||
test('should generate preview.js', async () => { | ||
await configurePreview({ | ||
language: SupportedLanguage.JAVASCRIPT, | ||
storybookConfigFolder: '.storybook', | ||
}); | ||
|
||
const { calls } = (fse.writeFile as unknown as jest.Mock).mock; | ||
const [previewConfigPath, previewConfigContent] = calls[0]; | ||
|
||
expect(previewConfigPath).toEqual('./.storybook/preview.js'); | ||
expect(previewConfigContent).toMatchInlineSnapshot(` | ||
"export const parameters = { | ||
actions: { argTypesRegex: \\"^on[A-Z].*\\" }, | ||
controls: { | ||
matchers: { | ||
color: /(background|color)$/i, | ||
date: /Date$/, | ||
}, | ||
}, | ||
}" | ||
`); | ||
}); | ||
|
||
test('should generate preview.ts', async () => { | ||
await configurePreview({ | ||
language: SupportedLanguage.TYPESCRIPT, | ||
storybookConfigFolder: '.storybook', | ||
}); | ||
|
||
const { calls } = (fse.writeFile as unknown as jest.Mock).mock; | ||
const [previewConfigPath, previewConfigContent] = calls[0]; | ||
|
||
expect(previewConfigPath).toEqual('./.storybook/preview.ts'); | ||
expect(previewConfigContent).toMatchInlineSnapshot(` | ||
"export const parameters = { | ||
actions: { argTypesRegex: \\"^on[A-Z].*\\" }, | ||
controls: { | ||
matchers: { | ||
color: /(background|color)$/i, | ||
date: /Date$/, | ||
}, | ||
}, | ||
}" | ||
`); | ||
}); | ||
|
||
test('should not do anything if the framework template already included a preview', async () => { | ||
(fse.pathExists as unknown as jest.Mock).mockReturnValueOnce(true); | ||
await configurePreview({ | ||
language: SupportedLanguage.TYPESCRIPT, | ||
storybookConfigFolder: '.storybook', | ||
}); | ||
expect(fse.writeFile).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('should add prefix if frameworkParts are passed', async () => { | ||
await configurePreview({ | ||
language: SupportedLanguage.TYPESCRIPT, | ||
storybookConfigFolder: '.storybook', | ||
frameworkPreviewParts: { | ||
prefix: dedent` | ||
import { setCompodocJson } from "@storybook/addon-docs/angular"; | ||
import docJson from "../documentation.json"; | ||
setCompodocJson(docJson); | ||
`, | ||
}, | ||
}); | ||
|
||
const { calls } = (fse.writeFile as unknown as jest.Mock).mock; | ||
const [previewConfigPath, previewConfigContent] = calls[0]; | ||
|
||
expect(previewConfigPath).toEqual('./.storybook/preview.ts'); | ||
expect(previewConfigContent).toMatchInlineSnapshot(` | ||
"import { setCompodocJson } from \\"@storybook/addon-docs/angular\\"; | ||
import docJson from \\"../documentation.json\\"; | ||
setCompodocJson(docJson); | ||
export const parameters = { | ||
actions: { argTypesRegex: \\"^on[A-Z].*\\" }, | ||
controls: { | ||
matchers: { | ||
color: /(background|color)$/i, | ||
date: /Date$/, | ||
}, | ||
}, | ||
}" | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters