From 357a45e3a7a6b0f5388277c4eb5cdc5defa08562 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Mon, 6 Jul 2020 13:23:24 +0200 Subject: [PATCH] FIX bug && FOX unit tests --- lib/core/src/server/presets.js | 6 +- lib/core/src/server/presets.test.js | 109 ++++++++++++++++------------ 2 files changed, 66 insertions(+), 49 deletions(-) diff --git a/lib/core/src/server/presets.js b/lib/core/src/server/presets.js index 3b23041bc102..5333b6890dc0 100644 --- a/lib/core/src/server/presets.js +++ b/lib/core/src/server/presets.js @@ -40,7 +40,7 @@ export const resolveAddonName = (configDir, name) => { path = resolveFrom(configDir, name); } else if (name.startsWith('/')) { path = name; - } else if (name.match(/\/(preset|register(-panel)?)\.(js|ts|tsx|jsx)$/)) { + } else if (name.match(/\/(preset|register(-panel)?)(\.(js|ts|tsx|jsx))?$/)) { path = name; } @@ -49,7 +49,7 @@ export const resolveAddonName = (configDir, name) => { return { name: path, // Accept `register`, `register.js`, `require.resolve('foo/register'), `register-panel` - type: path.match(/register(-panel)?(.(js|ts|tsx|jsx))?$/) ? 'managerEntries' : 'presets', + type: path.match(/register(-panel)?(\.(js|ts|tsx|jsx))?$/) ? 'managerEntries' : 'presets', }; } @@ -223,7 +223,7 @@ function applyPresets(presets, extension, config, args, storybookOptions) { }, presetResult); } -function getPresets(presets, storybookOptions) { +function getPresets(presets, storybookOptions = {}) { const loadedPresets = loadPresets(presets, 0, storybookOptions); return { diff --git a/lib/core/src/server/presets.test.js b/lib/core/src/server/presets.test.js index 33e1ff0f89f4..233a2b7df4b1 100644 --- a/lib/core/src/server/presets.test.js +++ b/lib/core/src/server/presets.test.js @@ -17,25 +17,27 @@ jest.mock('@storybook/node-logger', () => ({ }, })); -jest.mock('./utils/resolve-file', () => ({ - resolveFile: (name) => { - const KNOWN_FILES = [ - '@storybook/addon-actions/register', - '@storybook/addon-docs', - '@storybook/addon-docs/preset', - '@storybook/addon-knobs', - '@storybook/addon-notes/register-panel', - '@storybook/preset-typescript', - 'addon-bar/preset.js', - 'addon-baz/register.js', - 'addon-foo/register.js', - ]; - if (KNOWN_FILES.includes(name)) { - return name; - } - throw new Error(`Cannot find module '${name}'`); - }, -})); +jest.mock('resolve-from', () => (l, name) => { + const KNOWN_FILES = [ + '@storybook/addon-actions/register', + './local/preset', + './local/addons', + '/absolute/preset', + '/absolute/addons', + '@storybook/addon-docs/preset', + '@storybook/addon-knobs/register', + '@storybook/addon-notes/register-panel', + '@storybook/preset-typescript', + 'addon-bar/preset.js', + 'addon-baz/register.js', + 'addon-foo/register.js', + ]; + + if (KNOWN_FILES.includes(name)) { + return name; + } + throw new Error(`cannot resolve ${name}`); +}); describe('presets', () => { it('does not throw when there is no preset file', async () => { @@ -120,7 +122,7 @@ describe('presets', () => { }); const getPresets = jest.requireActual('./presets').default; - const presets = wrapPreset(getPresets(['preset-foo', 'preset-bar'])); + const presets = wrapPreset(getPresets(['preset-foo', 'preset-bar'], {})); async function testPresets() { await presets.webpack(); @@ -341,83 +343,98 @@ describe('resolveAddonName', () => { const { resolveAddonName } = jest.requireActual('./presets'); it('should resolve packages with metadata (relative path)', () => { - expect(resolveAddonName('@storybook/addon-docs')).toEqual({ - name: '@storybook/addon-docs/preset', + mockPreset('./local/preset', { + presets: [], + }); + expect(resolveAddonName({}, './local/preset')).toEqual({ + name: './local/preset', type: 'presets', }); }); it('should resolve packages with metadata (absolute path)', () => { - expect(resolveAddonName('@storybook/addon-knobs')).toEqual({ - name: '@storybook/addon-knobs', + mockPreset('/absolute/preset', { + presets: [], + }); + expect(resolveAddonName({}, '/absolute/preset')).toEqual({ + name: '/absolute/preset', type: 'presets', }); }); it('should resolve packages without metadata', () => { - expect(resolveAddonName('@storybook/preset-create-react-app')).toEqual({ + expect(resolveAddonName({}, '@storybook/preset-create-react-app')).toEqual({ name: '@storybook/preset-create-react-app', type: 'presets', }); }); it('should resolve managerEntries', () => { - expect(resolveAddonName('@storybook/addon-actions/register')).toEqual({ + expect(resolveAddonName({}, '@storybook/addon-actions/register')).toEqual({ name: '@storybook/addon-actions/register', type: 'managerEntries', }); }); it('should resolve presets', () => { - expect(resolveAddonName('@storybook/addon-docs')).toEqual({ + expect(resolveAddonName({}, '@storybook/addon-docs')).toEqual({ name: '@storybook/addon-docs/preset', type: 'presets', }); }); it('should resolve preset packages', () => { - expect(resolveAddonName('@storybook/addon-essentials')).toEqual({ + expect(resolveAddonName({}, '@storybook/addon-essentials')).toEqual({ name: '@storybook/addon-essentials', type: 'presets', }); }); it('should error on invalid inputs', () => { - expect(() => resolveAddonName(null)).toThrow(); + expect(() => resolveAddonName({}, null)).toThrow(); }); }); describe('loadPreset', () => { - const { loadPreset } = jest.requireActual('./presets'); - mockPreset('@storybook/preset-typescript', {}); - mockPreset('@storybook/addon-docs', {}); + mockPreset('@storybook/addon-docs/preset', {}); mockPreset('@storybook/addon-actions/register', {}); mockPreset('addon-foo/register.js', {}); - mockPreset('addon-bar/preset.js', {}); + mockPreset('addon-bar/preset', {}); mockPreset('addon-baz/register.js', {}); mockPreset('@storybook/addon-notes/register-panel', {}); + const { loadPreset } = jest.requireActual('./presets'); + it('should resolve all addons & presets in correct order', () => { - const loaded = loadPreset({ - type: 'managerEntries', - name: '', - presets: ['@storybook/preset-typescript'], - addons: [ - '@storybook/addon-docs', - '@storybook/addon-actions/register', - 'addon-foo/register.js', - 'addon-bar', - 'addon-baz/register.tsx', - '@storybook/addon-notes/register-panel', - ], - }); + const loaded = loadPreset( + { + name: '', + type: 'managerEntries', + presets: ['@storybook/preset-typescript'], + addons: [ + '@storybook/addon-docs', + '@storybook/addon-actions/register', + 'addon-foo/register.js', + 'addon-bar', + 'addon-baz/register.tsx', + '@storybook/addon-notes/register-panel', + ], + }, + 0, + {} + ); expect(loaded).toEqual([ { name: '@storybook/preset-typescript', options: {}, preset: {}, }, + { + name: '@storybook/addon-docs/preset', + options: {}, + preset: {}, + }, { name: '@storybook/addon-actions/register_additionalManagerEntries', options: {},