Skip to content

Commit

Permalink
feat: generate dummy loader when no stories found
Browse files Browse the repository at this point in the history
Closes: #57
  • Loading branch information
elderfo authored Mar 18, 2018
2 parents be67b34 + 00bd5b2 commit d300c57
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
18 changes: 6 additions & 12 deletions src/storyWriterProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@ const writeOutStoryLoader = pathConfig => {
);
});

if (storyFiles.length > 0) {
const sortedFiles = sortFiles(storyFiles);
writeFile(sortedFiles, outputFileConfig.outputFile);
logger.info(
`Compiled story loader for ${storyFiles.length} files:\n`,
` ${storyFiles.join('\n ')}`
);
} else {
logger.warn(
'No files were found matching the specified pattern. Story loader was not written.'
);
}
const sortedFiles = sortFiles(storyFiles);
writeFile(sortedFiles, outputFileConfig.outputFile);
logger.info(
`Compiled story loader for ${storyFiles.length} files:\n`,
` ${storyFiles.join('\n ')}`
);
});
};

Expand Down
6 changes: 3 additions & 3 deletions src/storyWriterProcess.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ test('writeOutStoryLoader should perform expected work', () => {
};
const firstFiles = generateArray(faker.system.fileName);
const secondFiles = generateArray(faker.system.fileName);
const thirdFiles = generateArray(faker.system.fileName);
const noFiles = [];

storyFinder.loadStories
.mockImplementationOnce(() => firstFiles)
.mockImplementationOnce(() => secondFiles)
.mockImplementationOnce(() => thirdFiles);
.mockImplementationOnce(() => noFiles);

writeOutStoryLoader(config);

Expand All @@ -48,7 +48,7 @@ test('writeOutStoryLoader should perform expected work', () => {
config.outputFiles[0].outputFile
);
expect(writer.writeFile).toHaveBeenCalledWith(
thirdFiles.concat().sort(),
noFiles.concat().sort(),
config.outputFiles[1].outputFile
);
});
24 changes: 23 additions & 1 deletion src/writer/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`writeFile should perform expected work 1`] = `
exports[`writeFile generate story loader 1`] = `
"
// Auto-generated file created by react-native-storybook-loader
// Do not edit.
Expand Down Expand Up @@ -31,3 +31,25 @@ module.exports = {
};
"
`;

exports[`writeFile should generate dummy story loader if no stories were found 1`] = `
"
// Auto-generated file created by react-native-storybook-loader
// Do not edit.
//
// https://github.com/elderfo/react-native-storybook-loader.git
function loadStories() {
}
const stories = [
];
module.exports = {
loadStories,
stories,
};
"
`;
11 changes: 10 additions & 1 deletion src/writer/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ afterEach(() => {
mock.restore();
});

test('writeFile should perform expected work', () => {
test('writeFile generate story loader', () => {
const files = [
path.resolve(__dirname, '../file1.js'),
path.resolve(__dirname, '../sub/file2.js'),
Expand All @@ -34,3 +34,12 @@ test('writeFile should perform expected work', () => {

expect(contents).toMatchSnapshot();
});

test('writeFile should generate dummy story loader if no stories were found', () => {
const files = [];
writeFile(files, outputPath, moduleDir);

const contents = fs.readFileSync(outputPath, encoding);

expect(contents).toMatchSnapshot();
});

0 comments on commit d300c57

Please sign in to comment.