From d89481e188a0668ef6e800d85befbb4b55b72b32 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Fri, 7 Jun 2024 23:43:44 +0800 Subject: [PATCH] Fixing tests WIP --- .../src/modules/store/StoryStore.ts | 6 ++-- .../src/modules/store/csf/composeConfigs.ts | 4 ++- .../csf/normalizeProjectAnnotations.test.ts | 33 +++++++++++++++++++ .../store/csf/normalizeProjectAnnotations.ts | 4 +-- 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 code/lib/preview-api/src/modules/store/csf/normalizeProjectAnnotations.test.ts diff --git a/code/lib/preview-api/src/modules/store/StoryStore.ts b/code/lib/preview-api/src/modules/store/StoryStore.ts index 59674aa4f0f8..8e2c6e444f80 100644 --- a/code/lib/preview-api/src/modules/store/StoryStore.ts +++ b/code/lib/preview-api/src/modules/store/StoryStore.ts @@ -77,7 +77,7 @@ export class StoryStore { this.storyIndex = new StoryIndexStore(storyIndex); this.projectAnnotations = normalizeProjectAnnotations(projectAnnotations); - const { initialGlobals, globalTypes } = projectAnnotations; + const { initialGlobals, globalTypes } = this.projectAnnotations; this.args = new ArgsStore(); this.globals = new GlobalsStore({ globals: initialGlobals, globalTypes }); @@ -95,8 +95,8 @@ export class StoryStore { setProjectAnnotations(projectAnnotations: ProjectAnnotations) { // By changing `this.projectAnnotations, we implicitly invalidate the `prepareStoryWithCache` this.projectAnnotations = normalizeProjectAnnotations(projectAnnotations); - const { globals, globalTypes } = projectAnnotations; - this.globals.set({ globals, globalTypes }); + const { initialGlobals, globalTypes } = projectAnnotations; + this.globals.set({ globals: initialGlobals, globalTypes }); } // This means that one of the CSF files has changed. diff --git a/code/lib/preview-api/src/modules/store/csf/composeConfigs.ts b/code/lib/preview-api/src/modules/store/csf/composeConfigs.ts index 9c943e14f1c1..924ad10a5522 100644 --- a/code/lib/preview-api/src/modules/store/csf/composeConfigs.ts +++ b/code/lib/preview-api/src/modules/store/csf/composeConfigs.ts @@ -55,7 +55,9 @@ export function composeConfigs( ...allArgTypeEnhancers.filter((e) => !e.secondPass), ...allArgTypeEnhancers.filter((e) => e.secondPass), ], - globals: getObjectField(moduleExportList, 'globals'), + initialGlobals: + getObjectField(moduleExportList, 'initialGlobals') ?? + getObjectField(moduleExportList, 'globals'), globalTypes: getObjectField(moduleExportList, 'globalTypes'), loaders: getArrayField(moduleExportList, 'loaders'), beforeEach: getArrayField(moduleExportList, 'beforeEach'), diff --git a/code/lib/preview-api/src/modules/store/csf/normalizeProjectAnnotations.test.ts b/code/lib/preview-api/src/modules/store/csf/normalizeProjectAnnotations.test.ts new file mode 100644 index 000000000000..a6aa1a2f7a83 --- /dev/null +++ b/code/lib/preview-api/src/modules/store/csf/normalizeProjectAnnotations.test.ts @@ -0,0 +1,33 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; + +import { normalizeProjectAnnotations } from './normalizeProjectAnnotations'; + +describe('normalizeProjectAnnotations', () => { + describe('blah', () => { + beforeEach(() => { + const warnThatThrows = vi.mocked(console.warn).getMockImplementation(); + vi.mocked(console.warn).mockImplementation(() => {}); + return () => { + vi.mocked(console.warn).mockImplementation(warnThatThrows!); + }; + }); + it('normalizes globals to initialGlobals', () => { + expect( + normalizeProjectAnnotations({ + globals: { a: 'b' }, + }) + ).toMatchObject({ + initialGlobals: { a: 'b' }, + }); + }); + }); + it('passes through initialGlobals', () => { + expect( + normalizeProjectAnnotations({ + initialGlobals: { a: 'b' }, + }) + ).toMatchObject({ + initialGlobals: { a: 'b' }, + }); + }); +}); diff --git a/code/lib/preview-api/src/modules/store/csf/normalizeProjectAnnotations.ts b/code/lib/preview-api/src/modules/store/csf/normalizeProjectAnnotations.ts index ae0dabd589e2..581fa8579a7a 100644 --- a/code/lib/preview-api/src/modules/store/csf/normalizeProjectAnnotations.ts +++ b/code/lib/preview-api/src/modules/store/csf/normalizeProjectAnnotations.ts @@ -4,7 +4,7 @@ import type { ProjectAnnotations, NormalizedProjectAnnotations, } from '@storybook/types'; -import { once } from '@storybook/client-logger'; +import { deprecate } from '@storybook/client-logger'; import { dedent } from 'ts-dedent'; import { inferArgTypes } from '../inferArgTypes'; @@ -24,7 +24,7 @@ export function normalizeProjectAnnotations({ ...annotations }: ProjectAnnotations): NormalizedProjectAnnotations { if (globals) { - once.warn(dedent` + deprecate(dedent` The preview.js 'globals' field is deprecated and will be removed in Storybook 9.0. Please use 'initialGlobals' instead. Learn more: