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

Fix getExampleFilename customization if file is not exist #1362

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"jest-serializer-html": "^6.0.0",
"keymirror": "^0.1.1",
"lint-staged": "^8.1.5",
"memfs": "^2.15.2",
"prettier": "1.16.4",
"raf": "^3.4.1",
"react": "^16.8.5",
Expand Down
16 changes: 16 additions & 0 deletions src/loaders/utils/__tests__/getExamples.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import deabsDeep from 'deabsdeep';
import { vol } from 'memfs';
import getExamples from '../getExamples';

jest.mock('fs', () => {
return require('memfs').fs;
});

const file = '../pizza.js';
const displayName = 'Pizza';
const examplesFile = './Pizza.md';
const defaultExample = './Default.md';

afterEach(vol.reset.bind(vol));

test('require an example file if component has example file', () => {
vol.fromJSON({ [examplesFile]: 'pizza' });

const result = getExamples(file, displayName, examplesFile);
expect(deabsDeep(result).require).toMatchInlineSnapshot(
`"!!~/src/loaders/examples-loader.js?displayName=Pizza&file=.%2F..%2Fpizza.js&shouldShowDefaultExample=false!./Pizza.md"`
);
});

test('require default example file if component has no example in the file system', () => {
const result = getExamples(file, displayName, examplesFile, defaultExample);
expect(deabsDeep(result).require).toMatchInlineSnapshot(
`"!!~/src/loaders/examples-loader.js?displayName=Pizza&file=.%2F..%2Fpizza.js&shouldShowDefaultExample=false!./Default.md"`
);
});

test('require default example has no example file', () => {
const result = getExamples(file, displayName, false, defaultExample);
expect(deabsDeep(result).require).toMatchInlineSnapshot(
Expand Down
3 changes: 2 additions & 1 deletion src/loaders/utils/getExamples.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import path from 'path';
import fs from 'fs';
import { encode } from 'qss';
import requireIt from './requireIt';

Expand All @@ -14,7 +15,7 @@ module.exports = function getExamples(
examplesFile: string,
defaultExample: string
): {} | null {
const examplesFileToLoad = examplesFile || defaultExample;
const examplesFileToLoad = (fs.existsSync(examplesFile) && examplesFile) || defaultExample;
if (!examplesFileToLoad) {
return null;
}
Expand Down