Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions src/csf/transformCsf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ export const prefixFunction = ({
testPrefixer,
title,
id,
name,
}: {
key: string;
title: string;
testPrefixer: TestPrefixer;
id?: string;
name?: string;
}) => {
const name = storyNameFromExport(key);
const storyName = name ?? storyNameFromExport(key);
const context: TestContext = {
storyExport: t.identifier(key),
name: t.stringLiteral(name), // FIXME .name annotation
name: t.stringLiteral(storyName),
title: t.stringLiteral(title),
id: t.stringLiteral(toId(id ?? title, name)),
};
Expand All @@ -59,19 +61,21 @@ const makePlayTest = ({
id,
testPrefix,
shouldSkip,
name,
}: {
key: string;
title: string;
id?: string;
metaOrStoryPlay?: boolean;
testPrefix: TestPrefixer;
shouldSkip?: boolean;
name?: string;
}): t.ExpressionStatement[] => {
return [
t.expressionStatement(
t.callExpression(shouldSkip ? t.identifier('it.skip') : t.identifier('it'), [
t.stringLiteral(metaOrStoryPlay ? 'play-test' : 'smoke-test'),
prefixFunction({ key, title, testPrefixer: testPrefix, id }),
prefixFunction({ key, title, testPrefixer: testPrefix, id, name }),
])
),
];
Expand Down Expand Up @@ -119,26 +123,29 @@ export const transformCsf = (
const storyExports = Object.keys(csf._stories);
const title = csf.meta?.title;

const storyAnnotations = storyExports.reduce<Record<string, { play?: t.Node; tags?: string[] }>>(
(acc, key) => {
const annotations = csf._storyAnnotations[key];
acc[key] = {};
if (annotations?.play) {
acc[key].play = annotations.play;
}
const storyAnnotations = storyExports.reduce<
Record<string, { play?: t.Node; tags?: string[]; name?: string }>
>((acc, key) => {
const annotations = csf._storyAnnotations[key];
acc[key] = {};
if (annotations?.play) {
acc[key].play = annotations.play;
}

acc[key].tags = combineTags(
'test',
'dev',
...previewAnnotations.tags,
...(csf.meta?.tags || []),
...(csf._stories[key].tags || [])
);

acc[key].tags = combineTags(
'test',
'dev',
...previewAnnotations.tags,
...(csf.meta?.tags || []),
...(csf._stories[key].tags || [])
);

return acc;
},
{}
);
if (annotations?.name) {
acc[key].name = csf._stories[key].name;
}

return acc;
}, {});

const allTests = storyExports
.filter((key) => {
Expand All @@ -165,6 +172,7 @@ export const transformCsf = (
metaOrStoryPlay: !!storyAnnotations[key]?.play,
testPrefix: testPrefixer,
shouldSkip,
name: storyAnnotations[key].name,
}),
];
}
Expand Down