Skip to content

Commit

Permalink
Add test for json indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
floodfx committed Apr 28, 2023
1 parent 6a8fed1 commit dc5a9b8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
58 changes: 53 additions & 5 deletions code/lib/csf-tools/src/CsfFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/// <reference types="@types/jest" />;

/* eslint-disable no-underscore-dangle */
import { dedent } from 'ts-dedent';
import yaml from 'js-yaml';
import { loadCsf } from './CsfFile';
import { dedent } from 'ts-dedent';
import { loadCsf, loadCsfFromJson } from './CsfFile';

expect.addSnapshotSerializer({
print: (val: any) => yaml.dump(val).trimEnd(),
Expand Down Expand Up @@ -529,9 +529,57 @@ describe('CsfFile', () => {
}
`)
).toMatchInlineSnapshot(`
meta:
title: Chip
stories: []
meta:
title: Chip
stories: []
`);
});
});

describe('json parsing', () => {
it('json', () => {
expect(
loadCsfFromJson(
JSON.stringify({
title: 'Foo/Bar/Baz',
parameters: {
server: {
params: {
ParamA: 'ParamA',
},
},
},
args: {
ArgsA: 'ArgsA',
},
stories: [
{
name: 'StoryA',
args: {
StoryArgsA: 'StoryArgsA',
},
},
{
name: 'StoryB',
args: {
StoryArgsB: 'StoryArgsB',
},
},
],
}),
{
fileName: 'foo.json',
makeTitle: (path) => path,
}
)
).toMatchInlineSnapshot(`
meta:
title: Foo/Bar/Baz
stories:
- id: foo-bar-baz--storya
name: StoryA
- id: foo-bar-baz--storyb
name: StoryB
`);
});
});
Expand Down
2 changes: 1 addition & 1 deletion code/lib/csf-tools/src/CsfFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ export const loadCsfFromJson = (jsonString: string, options: CsfOptions): Indexe
// TODO: meta tags?
};
// TODO: title format helpers?
const metaTitle = meta.title + ''.replace(/\/|\\/g, '-').toLowerCase();
const metaTitle = meta.title.replace(/\/|\\/g, '-').toLowerCase();
const stories: IndexedStory[] = json.stories.map((story: any) => {
const id = `${metaTitle}--${story.name.toLowerCase().replace(/ /g, '-')}`;
const { name } = story;
Expand Down

0 comments on commit dc5a9b8

Please sign in to comment.