Skip to content

Commit

Permalink
Add extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed Jun 26, 2024
1 parent f24a39b commit 054f661
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ import {
STORY_THREW_EXCEPTION,
} from '@storybook/core-events';

import type { ModuleImportFn, StoryIndex, TeardownRenderToCanvas } from '@storybook/types';
import type {
ModuleImportFn,
ProjectAnnotations,
Renderer,
StoryIndex,
TeardownRenderToCanvas,
} from '@storybook/types';
import type { RenderPhase } from './render/StoryRender';
import { composeConfigs } from '../store';

export const componentOneExports = {
default: {
Expand Down Expand Up @@ -65,14 +72,18 @@ export const docsRenderer = {
unmount: vi.fn(),
};
export const teardownrenderToCanvas: Mock<[TeardownRenderToCanvas]> = vi.fn();
export const projectAnnotations = {
const rawProjectAnnotations = {
initialGlobals: { a: 'b' },
globalTypes: {},
decorators: [vi.fn((s) => s())],
render: vi.fn(),
renderToCanvas: vi.fn().mockReturnValue(teardownrenderToCanvas),
parameters: { docs: { renderer: () => docsRenderer } },
};
export const projectAnnotations = composeConfigs([
rawProjectAnnotations,
]) as ProjectAnnotations<Renderer> & typeof rawProjectAnnotations;

export const getProjectAnnotations = vi.fn(() => projectAnnotations as any);

export const storyIndex: StoryIndex = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ describe('PreviewWeb', () => {
forceRemount: true,
storyContext: expect.objectContaining({
loaded: { l: 8 }, // This is the value returned by the *first* loader call
args: { foo: 'a', new: 'arg', one: 'mapped-1' },
args: { foo: 'a', one: 'mapped-1' },
}),
}),
'story-element'
Expand Down
18 changes: 12 additions & 6 deletions code/lib/preview-api/src/modules/store/StoryStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { prepareStory } from './csf/prepareStory';
import { processCSFFile } from './csf/processCSFFile';
import { StoryStore } from './StoryStore';
import type { HooksContext } from './hooks';
import { composeConfigs } from './csf/composeConfigs';

// Spy on prepareStory/processCSFFile
vi.mock('./csf/prepareStory', async (importOriginal) => {
Expand Down Expand Up @@ -41,12 +42,14 @@ const importFn = vi.fn(async (path) => {
return path === './src/ComponentOne.stories.js' ? componentOneExports : componentTwoExports;
});

const projectAnnotations: ProjectAnnotations<any> = {
globals: { a: 'b' },
globalTypes: { a: { type: 'string' } },
argTypes: { a: { type: 'string' } },
render: vi.fn(),
};
const projectAnnotations: ProjectAnnotations<any> = composeConfigs([
{
globals: { a: 'b' },
globalTypes: { a: { type: 'string' } },
argTypes: { a: { type: 'string' } },
render: vi.fn(),
},
]);

const storyIndex: StoryIndex = {
v: 5,
Expand Down Expand Up @@ -660,6 +663,7 @@ describe('StoryStore', () => {
"fileName": "./src/ComponentOne.stories.js",
},
"playFunction": undefined,
"runStep": [Function],
"story": "A",
"storyFn": [Function],
"subcomponents": undefined,
Expand Down Expand Up @@ -707,6 +711,7 @@ describe('StoryStore', () => {
"fileName": "./src/ComponentOne.stories.js",
},
"playFunction": undefined,
"runStep": [Function],
"story": "B",
"storyFn": [Function],
"subcomponents": undefined,
Expand Down Expand Up @@ -754,6 +759,7 @@ describe('StoryStore', () => {
"fileName": "./src/ComponentTwo.stories.js",
},
"playFunction": undefined,
"runStep": [Function],
"story": "C",
"storyFn": [Function],
"subcomponents": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ describe('prepareMeta', () => {
...preparedStory
} = prepareStory({ id, name, moduleExport }, meta, { render });

const { ...preparedMeta } = prepareMeta(
const preparedMeta = prepareMeta(
meta,
normalizeProjectAnnotations(composeConfigs([{ render }])),
{}
Expand Down
21 changes: 21 additions & 0 deletions code/lib/test/template/stories/before-each.stories.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/naming-convention,storybook/prefer-pascal-case */
import { expect, mocked, getByRole, spyOn, userEvent } from '@storybook/test';

const meta = {
Expand Down Expand Up @@ -42,3 +43,23 @@ export const BeforeEachOrder = {
]);
},
};

export const before_each_and_loaders_can_extend_context = {
parameters: { chromatic: { disable: true } },
loaders(context) {
context.foo = ['bar'];
},
beforeEach(context) {
context.foo = [...context.foo, 'baz'];
},
async play({ foo }) {
await expect(foo).toEqual(['bar', 'baz']);
},
};

export const context_prop_is_available = {
parameters: { chromatic: { disable: true } },
async play({ context, canvasElement }) {
await expect(context.canvasElement).toEqual(canvasElement);
},
};

0 comments on commit 054f661

Please sign in to comment.