-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename portable stories files, refactor tests
- Loading branch information
Showing
10 changed files
with
207 additions
and
327 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
code/lib/preview-api/src/modules/store/csf/portable-stories.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { describe, expect, vi, it } from 'vitest'; | ||
import { composeStory, composeStories } from './portable-stories'; | ||
|
||
// Most integration tests for this functionality are located under renderers/react | ||
describe('composeStory', () => { | ||
const meta = { | ||
title: 'Button', | ||
parameters: { | ||
firstAddon: true, | ||
}, | ||
args: { | ||
label: 'Hello World', | ||
primary: true, | ||
}, | ||
}; | ||
|
||
it('should return story with composed args and parameters', () => { | ||
const Story = () => {}; | ||
Story.args = { primary: true }; | ||
Story.parameters = { | ||
parameters: { | ||
secondAddon: true, | ||
}, | ||
}; | ||
|
||
const composedStory = composeStory(Story, meta); | ||
expect(composedStory.args).toEqual({ ...Story.args, ...meta.args }); | ||
expect(composedStory.parameters).toEqual( | ||
expect.objectContaining({ ...Story.parameters, ...meta.parameters }) | ||
); | ||
}); | ||
|
||
it('should throw an error if Story is undefined', () => { | ||
expect(() => { | ||
// @ts-expect-error (invalid input) | ||
composeStory(undefined, meta); | ||
}).toThrow(); | ||
}); | ||
|
||
describe('Id of the story', () => { | ||
it('is exposed correctly when composeStories is used', () => { | ||
const module = { | ||
default: { | ||
title: 'Example/Button', | ||
}, | ||
CSF3Primary: () => {}, | ||
}; | ||
const Primary = composeStory(module.CSF3Primary, module.default, {}); | ||
expect(Primary.id).toBe('example-button--csf-3-primary'); | ||
}); | ||
it('is exposed correctly when composeStory is used and exportsName is passed', () => { | ||
const module = { | ||
default: { | ||
title: 'Example/Button', | ||
}, | ||
CSF3Primary: () => {}, | ||
}; | ||
const Primary = composeStory(module.CSF3Primary, module.default, {}, {}, 'overwritten'); | ||
expect(Primary.id).toBe('example-button--overwritten'); | ||
}); | ||
it("is not unique when composeStory is used and exportsName isn't passed", () => { | ||
const Primary = composeStory({ render: () => {} }, {}); | ||
expect(Primary.id).toContain('unknown'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('composeStories', () => { | ||
const composeStoryFn = vi.fn((v) => v); | ||
const defaultAnnotations = { render: () => '' }; | ||
it('should call composeStoryFn with stories', () => { | ||
const composeStorySpy = vi.fn((v) => v); | ||
const module = { | ||
default: { | ||
title: 'Button', | ||
}, | ||
StoryOne: () => {}, | ||
StoryTwo: () => {}, | ||
}; | ||
const globalConfig = {}; | ||
composeStories(module, globalConfig, composeStorySpy); | ||
expect(composeStorySpy).toHaveBeenCalledWith( | ||
module.StoryOne, | ||
module.default, | ||
globalConfig, | ||
'StoryOne' | ||
); | ||
expect(composeStorySpy).toHaveBeenCalledWith( | ||
module.StoryTwo, | ||
module.default, | ||
globalConfig, | ||
'StoryTwo' | ||
); | ||
}); | ||
|
||
it('should not call composeStoryFn for non-story exports', () => { | ||
const composeStorySpy = vi.fn((v) => v); | ||
const module = { | ||
default: { | ||
title: 'Button', | ||
excludeStories: /Data/, | ||
}, | ||
mockData: {}, | ||
}; | ||
composeStories(module, defaultAnnotations, composeStoryFn); | ||
expect(composeStorySpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
describe('non-story exports', () => { | ||
it('should filter non-story exports with excludeStories', () => { | ||
const StoryModuleWithNonStoryExports = { | ||
default: { | ||
title: 'Some/Component', | ||
excludeStories: /.*Data/, | ||
}, | ||
LegitimateStory: () => 'hello world', | ||
mockData: {}, | ||
}; | ||
|
||
const result = composeStories( | ||
StoryModuleWithNonStoryExports, | ||
defaultAnnotations, | ||
composeStoryFn | ||
); | ||
expect(Object.keys(result)).not.toContain('mockData'); | ||
}); | ||
|
||
it('should filter non-story exports with includeStories', () => { | ||
const StoryModuleWithNonStoryExports = { | ||
default: { | ||
title: 'Some/Component', | ||
includeStories: /.*Story/, | ||
}, | ||
LegitimateStory: () => 'hello world', | ||
mockData: {}, | ||
}; | ||
|
||
const result = composeStories( | ||
StoryModuleWithNonStoryExports, | ||
defaultAnnotations, | ||
composeStoryFn | ||
); | ||
expect(Object.keys(result)).not.toContain('mockData'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 0 additions & 79 deletions
79
code/lib/preview-api/src/modules/store/csf/portable-stories/index.test.ts
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.