Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbf committed Sep 2, 2024
1 parent a476108 commit 57af7c0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
43 changes: 42 additions & 1 deletion code/lib/cli-storybook/src/add.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { describe, expect, test, vi } from 'vitest';
import { beforeEach, describe, expect, test, vi } from 'vitest';

import { add, getVersionSpecifier } from './add';

const MockedConfig = vi.hoisted(() => {
return {
appendValueToArray: vi.fn(),
getFieldNode: vi.fn(),
valueToNode: vi.fn(),
appendNodeToArray: vi.fn(),
};
});
const MockedPackageManager = vi.hoisted(() => {
Expand All @@ -20,6 +23,12 @@ const MockedPostInstall = vi.hoisted(() => {
postinstallAddon: vi.fn(),
};
});
const MockWrapRequireUtils = vi.hoisted(() => {
return {
getRequireWrapperName: vi.fn(),
wrapValueWithRequireWrapper: vi.fn(),
};
});
const MockedConsole = {
log: vi.fn(),
warn: vi.fn(),
Expand All @@ -35,6 +44,9 @@ vi.mock('storybook/internal/csf-tools', () => {
vi.mock('./postinstallAddon', () => {
return MockedPostInstall;
});
vi.mock('./automigrate/fixes/wrap-require-utils', () => {
return MockWrapRequireUtils;
});
vi.mock('storybook/internal/common', () => {
return {
getStorybookInfo: vi.fn(() => ({ mainConfig: {}, configDir: '' })),
Expand Down Expand Up @@ -103,6 +115,35 @@ describe('add', () => {
});

describe('add (extra)', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('should not add a "wrap require" to the addon when not needed', async () => {
MockedConfig.getFieldNode.mockReturnValue({});
MockWrapRequireUtils.getRequireWrapperName.mockReturnValue(null);
await add(
'@storybook/addon-docs',
{ packageManager: 'npm', skipPostinstall: true },
MockedConsole
);

expect(MockWrapRequireUtils.wrapValueWithRequireWrapper).not.toHaveBeenCalled();
expect(MockedConfig.appendValueToArray).toHaveBeenCalled();
expect(MockedConfig.appendNodeToArray).not.toHaveBeenCalled();
});
test('should add a "wrap require" to the addon when applicable', async () => {
MockedConfig.getFieldNode.mockReturnValue({});
MockWrapRequireUtils.getRequireWrapperName.mockReturnValue('require');
await add(
'@storybook/addon-docs',
{ packageManager: 'npm', skipPostinstall: true },
MockedConsole
);

expect(MockWrapRequireUtils.wrapValueWithRequireWrapper).toHaveBeenCalled();
expect(MockedConfig.appendValueToArray).not.toHaveBeenCalled();
expect(MockedConfig.appendNodeToArray).toHaveBeenCalled();
});
test('not warning when installing the correct version of storybook', async () => {
await add(
'@storybook/addon-docs',
Expand Down
2 changes: 0 additions & 2 deletions code/lib/cli-storybook/src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { dedent } from 'ts-dedent';

import {
getRequireWrapperName,
isRequireWrapperNecessary,
wrapValueWithRequireWrapper,
} from './automigrate/fixes/wrap-require-utils';
import { postinstallAddon } from './postinstallAddon';
Expand Down Expand Up @@ -143,7 +142,6 @@ export async function add(
logger.log(`Adding '${addon}' to main.js addons field.`);

const mainConfigAddons = main.getFieldNode(['addons']);

if (mainConfigAddons && getRequireWrapperName(main) !== null) {
const addonNode = main.valueToNode(addonName);
main.appendNodeToArray(['addons'], addonNode as any);
Expand Down

0 comments on commit 57af7c0

Please sign in to comment.