Skip to content

Commit

Permalink
fix code and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Jan 16, 2023
1 parent 884b926 commit f5c5ad9
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 5 deletions.
15 changes: 10 additions & 5 deletions code/lib/core-server/src/presets/common-preset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs, { pathExists } from 'fs-extra';
import { pathExists, readFile } from 'fs-extra';
import { deprecate, logger } from '@storybook/node-logger';
import {
getDirectoryFromWorkingDir,
Expand All @@ -21,7 +21,10 @@ import { parseStaticDir } from '../utils/server-statics';

const defaultFavicon = require.resolve('@storybook/core-server/public/favicon.svg');

export const favicon = async (value: string, options: Options) => {
export const favicon = async (
value: string,
options: Pick<Options, 'presets' | 'configDir' | 'staticDir'>
) => {
if (value) {
return value;
}
Expand All @@ -42,6 +45,7 @@ export const favicon = async (value: string, options: Options) => {
directory: dir,
})
: dir;

const { staticPath, targetEndpoint } = await parseStaticDir(relativeDir);

if (targetEndpoint === '/') {
Expand All @@ -58,16 +62,17 @@ export const favicon = async (value: string, options: Options) => {
results.push(path);
}
}

return results;
})
);

const flatlist = lists.reduce((l1, l2) => l1.concat(l2), []);

if (flatlist.length > 1) {
logger.warn(dedent`
Looks like multiple favicons were detected. Using the first one.
${flatlist.map((f) => f.path).join(', ')}
${flatlist.join(', ')}
`);
}

Expand Down Expand Up @@ -170,7 +175,7 @@ export const features = async (

export const storyIndexers = async (indexers?: StoryIndexer[]) => {
const csfIndexer = async (fileName: string, opts: IndexerOptions) => {
const code = (await fs.readFile(fileName, 'utf-8')).toString();
const code = (await readFile(fileName, 'utf-8')).toString();
return loadCsf(code, { ...opts, fileName }).parse();
};
return [
Expand Down
127 changes: 127 additions & 0 deletions code/lib/core-server/src/presets/favicon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/// <reference types="@types/jest" />;

import { join } from 'path';
import * as fs from 'fs-extra';
import { logger } from '@storybook/node-logger';
import * as m from './common-preset';

const defaultFavicon = require.resolve('@storybook/core-server/public/favicon.svg');

const createPath = (...p: string[]) => join(process.cwd(), ...p);
const createOptions = (locations: string[]): Parameters<typeof m.favicon>[1] => ({
configDir: '',
presets: {
apply: async (extension: string, config: any) => {
switch (extension) {
case 'staticDirs': {
return locations.map((location) => ({ from: location, to: '/' }));
}
default: {
return config as any;
}
}
},
},
});

jest.mock('fs-extra', () => {
return {
pathExists: jest.fn((p: string) => {
return false;
}),
};
});

jest.mock('@storybook/node-logger', () => {
return {
logger: {
warn: jest.fn(() => {}),
},
};
});

const pathExists = fs.pathExists as jest.Mock;

test('with no staticDirs favicon should return default', async () => {
const options = createOptions([]);

expect(await m.favicon(undefined, options)).toBe(defaultFavicon);
});

test('with staticDirs containing a single favicon.ico should return the found favicon', async () => {
const location = 'static';
pathExists.mockImplementation((p: string) => {
if (p === createPath(location)) {
return true;
}
if (p === createPath(location, 'favicon.ico')) {
return true;
}
return false;
});
const options = createOptions([location]);

expect(await m.favicon(undefined, options)).toBe(createPath(location, 'favicon.ico'));
});

test('with staticDirs containing a single favicon.svg should return the found favicon', async () => {
const location = 'static';
pathExists.mockImplementation((p: string) => {
if (p === createPath(location)) {
return true;
}
if (p === createPath(location, 'favicon.svg')) {
return true;
}
return false;
});
const options = createOptions([location]);

expect(await m.favicon(undefined, options)).toBe(createPath(location, 'favicon.svg'));
});

test('with staticDirs containing a multiple favicons should return the first favicon and warn', async () => {
const location = 'static';
pathExists.mockImplementation((p: string) => {
if (p === createPath(location)) {
return true;
}
if (p === createPath(location, 'favicon.ico')) {
return true;
}
if (p === createPath(location, 'favicon.svg')) {
return true;
}
return false;
});
const options = createOptions([location]);

expect(await m.favicon(undefined, options)).toBe(createPath(location, 'favicon.svg'));

expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('multiple favicons'));
});

test('with multiple staticDirs containing a multiple favicons should return the first favicon and warn', async () => {
const locationA = 'static-a';
const locationB = 'static-b';
pathExists.mockImplementation((p: string) => {
if (p === createPath(locationA)) {
return true;
}
if (p === createPath(locationB)) {
return true;
}
if (p === createPath(locationA, 'favicon.ico')) {
return true;
}
if (p === createPath(locationB, 'favicon.svg')) {
return true;
}
return false;
});
const options = createOptions([locationA, locationB]);

expect(await m.favicon(undefined, options)).toBe(createPath(locationA, 'favicon.ico'));

expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('multiple favicons'));
});

0 comments on commit f5c5ad9

Please sign in to comment.