From 2fe47b63fa9fc271ddda13a8d1fd1b6cb4a117e0 Mon Sep 17 00:00:00 2001 From: David Golightly Date: Wed, 12 Feb 2020 13:41:28 -0800 Subject: [PATCH 1/4] implement support for jsx/tsx preview files --- __mocks__/fs.js | 4 +++ .../src/frameworks/configure.test.ts | 26 ++++++++++++++++++ .../src/frameworks/configure.ts | 27 ++++++------------- 3 files changed, 38 insertions(+), 19 deletions(-) create mode 100644 addons/storyshots/storyshots-core/src/frameworks/configure.test.ts diff --git a/__mocks__/fs.js b/__mocks__/fs.js index 261aedd7a6a7..c0de20b9c307 100644 --- a/__mocks__/fs.js +++ b/__mocks__/fs.js @@ -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; diff --git a/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts b/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts new file mode 100644 index 000000000000..0b9095e457a4 --- /dev/null +++ b/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) => { + // 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); +}); diff --git a/addons/storyshots/storyshots-core/src/frameworks/configure.ts b/addons/storyshots/storyshots-core/src/frameworks/configure.ts index fd2689eb27d2..4d8303c6c6bc 100644 --- a/addons/storyshots/storyshots-core/src/frameworks/configure.ts +++ b/addons/storyshots/storyshots-core/src/frameworks/configure.ts @@ -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 => { From 5dcd677d997510ac309501aec460e7d8fcaa3728 Mon Sep 17 00:00:00 2001 From: David Golightly Date: Wed, 12 Feb 2020 16:00:25 -0800 Subject: [PATCH 2/4] implement the same check for main.js --- .../src/frameworks/configure.test.ts | 48 ++++++++++++++----- .../src/frameworks/configure.ts | 15 +++--- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts b/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts index 0b9095e457a4..61325ecbe51a 100644 --- a/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts +++ b/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts @@ -1,4 +1,4 @@ -import { getPreviewFile } from './configure'; +import { getPreviewFile, getMainFile } from './configure'; // eslint-disable-next-line global-require, jest/no-mocks-import jest.mock('fs', () => require('../../../../../__mocks__/fs')); @@ -7,20 +7,42 @@ const setupFiles = (files: Record) => { 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' }); +describe('preview 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}`); + expect(getPreviewFile('test/')).toEqual(`test/${filepath}`); + }); + + it('returns false when none of the paths exist', () => { + setupFiles(Object.create(null)); + + expect(getPreviewFile('test/')).toEqual(false); + }); }); -it('returns false when none of the paths exist', () => { - setupFiles(Object.create(null)); +describe('main files', () => { + it.each` + filepath + ${'main.ts'} + ${'main.tsx'} + ${'main.js'} + ${'main.jsx'} + `('resolves a valid main file path from $filepath', ({ filepath }) => { + setupFiles({ [`test/${filepath}`]: 'true' }); + + expect(getMainFile('test/')).toEqual(`test/${filepath}`); + }); + + it('returns false when none of the paths exist', () => { + setupFiles(Object.create(null)); - expect(getPreviewFile('test/')).toEqual(false); + expect(getPreviewFile('test/')).toEqual(false); + }); }); diff --git a/addons/storyshots/storyshots-core/src/frameworks/configure.ts b/addons/storyshots/storyshots-core/src/frameworks/configure.ts index 4d8303c6c6bc..6b07a8714f76 100644 --- a/addons/storyshots/storyshots-core/src/frameworks/configure.ts +++ b/addons/storyshots/storyshots-core/src/frameworks/configure.ts @@ -22,9 +22,9 @@ interface Output { } const supportedExtensions = ['ts', 'tsx', 'js', 'jsx']; -const supportedFilenames = ['preview', 'config']; export const getPreviewFile = (configDir: string): string | false => { + const supportedFilenames = ['preview', 'config']; const allFilenames = supportedFilenames .flatMap(filename => supportedExtensions.map(ext => `${filename}.${ext}`)) .map(filename => path.join(configDir, filename)); @@ -32,14 +32,13 @@ export const getPreviewFile = (configDir: string): string | false => { return allFilenames.find(isFile) || false; }; -const getMainFile = (configDir: string): string | false => { - const main = path.join(configDir, 'main.js'); - - if (isFile(main)) { - return main; - } +export const getMainFile = (configDir: string): string | false => { + const supportedFilenames = ['main']; + const allFilenames = supportedFilenames + .flatMap(filename => supportedExtensions.map(ext => `${filename}.${ext}`)) + .map(filename => path.join(configDir, filename)); - return false; + return allFilenames.find(isFile) || false; }; function getConfigPathParts(input: string): Output { From a44c34fae596c8e182449b8bbaf5b83dfad3bee1 Mon Sep 17 00:00:00 2001 From: David Golightly Date: Wed, 12 Feb 2020 16:06:04 -0800 Subject: [PATCH 3/4] refactor --- .../src/frameworks/configure.test.ts | 4 ++++ .../src/frameworks/configure.ts | 21 +++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts b/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts index 61325ecbe51a..76c88463876a 100644 --- a/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts +++ b/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts @@ -14,6 +14,10 @@ describe('preview files', () => { ${'preview.tsx'} ${'preview.js'} ${'preview.jsx'} + ${'config.ts'} + ${'config.tsx'} + ${'config.js'} + ${'config.jsx'} `('resolves a valid preview file from $filepath', ({ filepath }) => { setupFiles({ [`test/${filepath}`]: 'true' }); diff --git a/addons/storyshots/storyshots-core/src/frameworks/configure.ts b/addons/storyshots/storyshots-core/src/frameworks/configure.ts index 6b07a8714f76..f6fe644fc7c8 100644 --- a/addons/storyshots/storyshots-core/src/frameworks/configure.ts +++ b/addons/storyshots/storyshots-core/src/frameworks/configure.ts @@ -23,23 +23,16 @@ interface Output { const supportedExtensions = ['ts', 'tsx', 'js', 'jsx']; -export const getPreviewFile = (configDir: string): string | false => { - const supportedFilenames = ['preview', 'config']; - const allFilenames = supportedFilenames +const resolveFile = (configDir: string, supportedFilenames: string[]) => + supportedFilenames .flatMap(filename => supportedExtensions.map(ext => `${filename}.${ext}`)) - .map(filename => path.join(configDir, filename)); + .map(filename => path.join(configDir, filename)) + .find(isFile) || false; - return allFilenames.find(isFile) || false; -}; - -export const getMainFile = (configDir: string): string | false => { - const supportedFilenames = ['main']; - const allFilenames = supportedFilenames - .flatMap(filename => supportedExtensions.map(ext => `${filename}.${ext}`)) - .map(filename => path.join(configDir, filename)); +export const getPreviewFile = (configDir: string): string | false => + resolveFile(configDir, ['preview', 'config']); - return allFilenames.find(isFile) || false; -}; +export const getMainFile = (configDir: string): string | false => resolveFile(configDir, ['main']); function getConfigPathParts(input: string): Output { const configDir = path.resolve(input); From 97dceceb535127e88c036cdbd506a44ae4949433 Mon Sep 17 00:00:00 2001 From: David Golightly Date: Wed, 12 Feb 2020 16:12:52 -0800 Subject: [PATCH 4/4] simplify a bit --- addons/storyshots/storyshots-core/src/frameworks/configure.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/addons/storyshots/storyshots-core/src/frameworks/configure.ts b/addons/storyshots/storyshots-core/src/frameworks/configure.ts index f6fe644fc7c8..93a2a0b9b6f3 100644 --- a/addons/storyshots/storyshots-core/src/frameworks/configure.ts +++ b/addons/storyshots/storyshots-core/src/frameworks/configure.ts @@ -25,8 +25,7 @@ const supportedExtensions = ['ts', 'tsx', 'js', 'jsx']; const resolveFile = (configDir: string, supportedFilenames: string[]) => supportedFilenames - .flatMap(filename => supportedExtensions.map(ext => `${filename}.${ext}`)) - .map(filename => path.join(configDir, filename)) + .flatMap(filename => supportedExtensions.map(ext => path.join(configDir, `${filename}.${ext}`))) .find(isFile) || false; export const getPreviewFile = (configDir: string): string | false =>