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

Storyshots: Fix support for jsx/tsx config files #9834

Merged
merged 4 commits into from Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions __mocks__/fs.js
Expand Up @@ -14,10 +14,14 @@ function __setMockFiles(newMockFiles) {
// file list set via __setMockFiles
const readFileSync = (filePath = '') => mockFiles[filePath];
const existsSync = filePath => !!mockFiles[filePath];
const lstatSync = filePath => ({
isFile: () => !!mockFiles[filePath],
});

// eslint-disable-next-line no-underscore-dangle
fs.__setMockFiles = __setMockFiles;
fs.readFileSync = readFileSync;
fs.existsSync = existsSync;
fs.lstatSync = lstatSync;

module.exports = fs;
26 changes: 26 additions & 0 deletions addons/storyshots/storyshots-core/src/frameworks/configure.test.ts
@@ -0,0 +1,26 @@
import { getPreviewFile } from './configure';

// eslint-disable-next-line global-require, jest/no-mocks-import
jest.mock('fs', () => require('../../../../../__mocks__/fs'));
const setupFiles = (files: Record<string, string>) => {
// eslint-disable-next-line no-underscore-dangle, global-require
require('fs').__setMockFiles(files);
};

it.each`
filepath
${'preview.ts'}
${'preview.tsx'}
${'preview.js'}
${'preview.jsx'}
`('resolves a valid preview file from $filepath', ({ filepath }) => {
setupFiles({ [`test/${filepath}`]: 'true' });

expect(getPreviewFile('test/')).toEqual(`test/${filepath}`);
});

it('returns false when none of the paths exist', () => {
setupFiles(Object.create(null));

expect(getPreviewFile('test/')).toEqual(false);
});
27 changes: 8 additions & 19 deletions addons/storyshots/storyshots-core/src/frameworks/configure.ts
Expand Up @@ -21,26 +21,15 @@ interface Output {
files: string[];
}

const getPreviewFile = (configDir: string): string | false => {
const preview = path.join(configDir, 'preview.js');
const previewTS = path.join(configDir, 'preview.ts');
const config = path.join(configDir, 'config.js');
const configTS = path.join(configDir, 'config.ts');

if (isFile(previewTS)) {
return previewTS;
}
if (isFile(preview)) {
return preview;
}
if (isFile(configTS)) {
return configTS;
}
if (isFile(config)) {
return config;
}
const supportedExtensions = ['ts', 'tsx', 'js', 'jsx'];
const supportedFilenames = ['preview', 'config'];

return false;
export const getPreviewFile = (configDir: string): string | false => {
const allFilenames = supportedFilenames
.flatMap(filename => supportedExtensions.map(ext => `${filename}.${ext}`))
.map(filename => path.join(configDir, filename));

return allFilenames.find(isFile) || false;
};

const getMainFile = (configDir: string): string | false => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

question: should the same file extension support be implemented for main.js as well?

Copy link
Member

Choose a reason for hiding this comment

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

Yes please! 🙏

Expand Down