Repository navigation
Addon-Docs: Prevent source transform from being applied twice on docs pages #36061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
valentinpalkovic
merged 5 commits into
storybookjs:next
from
webdevsamran:fix/issue-35889-source-transform-applied-twice
Oct 20, 2026
+349
−4
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5bb5f44
fix(docs): prevent source transform from being applied twice on docs …
webdevsamran b6fe1fe
fix(docs): complete vi.fn mock bodies and guard service-snippet trans…
webdevsamran e747065
test(docs): rewrite Source tests using renderHook and polish sourceKi…
webdevsamran 8d56697
fix(docs): preserve hook count and avoid early fallback on dynamic So…
webdevsamran 25c8224
Merge branch 'next' into fix/issue-35889-source-transform-applied-twice
valentinpalkovic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,325 @@ | ||
| // @vitest-environment happy-dom | ||
| import { renderHook } from '@testing-library/react'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { SourceType } from 'storybook/internal/docs-tools'; | ||
| import type { PreparedStory } from 'storybook/internal/types'; | ||
|
|
||
| import type { DocsContextProps } from './DocsContext'; | ||
| import type { SourceProps } from './Source'; | ||
| import { useSourceProps } from './Source'; | ||
| import type { SourceContextProps } from './SourceContainer'; | ||
| import { UNKNOWN_ARGS_HASH } from './SourceContainer'; | ||
|
|
||
| type ReducedStoryContext = ReturnType<DocsContextProps['getStoryContext']>; | ||
|
|
||
| const createMockDocsContext = ( | ||
| story: PreparedStory, | ||
| storyContext: ReducedStoryContext | ||
| ): DocsContextProps => | ||
| ({ | ||
| storyById: () => story, | ||
| getStoryContext: () => storyContext, | ||
| }) as unknown as DocsContextProps; | ||
|
|
||
| describe('useSourceProps — source transform handling (issue #35889)', () => { | ||
| it('does not apply parameter transform twice when snippet arrives on later render for auto args story', () => { | ||
| const storyContext: ReducedStoryContext = { | ||
| id: 'story--primary', | ||
| parameters: { | ||
| __isArgsStory: true, | ||
| docs: { | ||
| source: { | ||
| type: SourceType.AUTO, | ||
| originalSource: '<Button />', | ||
| transform: (code: string) => `T:${code}`, | ||
| }, | ||
| }, | ||
| }, | ||
| args: {}, | ||
| initialArgs: {}, | ||
| unmappedArgs: {}, | ||
| } as unknown as ReducedStoryContext; | ||
| const story: PreparedStory = { | ||
| id: 'story--primary', | ||
| parameters: storyContext.parameters, | ||
| } as unknown as PreparedStory; | ||
| const docsContext = createMockDocsContext(story, storyContext); | ||
|
|
||
| const { result, rerender } = renderHook( | ||
| ({ sourceContext, serviceSnippet, props }) => | ||
| useSourceProps(props, docsContext, sourceContext, serviceSnippet), | ||
| { | ||
| initialProps: { | ||
| sourceContext: { sources: {} } as SourceContextProps, | ||
| serviceSnippet: '', | ||
| props: {} as SourceProps, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| // Initial render before snippet arrives falls back to originalSource transformed | ||
| expect(result.current.code).toBe('T:<Button />'); | ||
|
|
||
| // Snippet arrives after initial render (already transformed by emitTransformCode) | ||
| rerender({ | ||
| sourceContext: { | ||
| sources: { | ||
| 'story--primary': { | ||
| [UNKNOWN_ARGS_HASH]: { code: 'T:<Button />' }, | ||
| }, | ||
| }, | ||
| }, | ||
| serviceSnippet: '', | ||
| props: {}, | ||
| }); | ||
|
|
||
| // Should not apply transform a second time (would be T:T:<Button />) | ||
| expect(result.current.code).toBe('T:<Button />'); | ||
| }); | ||
|
|
||
| it('does not apply parameter transform twice when snippet arrives on later render for dynamic story', () => { | ||
| const storyContext: ReducedStoryContext = { | ||
| id: 'story--primary', | ||
| parameters: { | ||
| __isArgsStory: true, | ||
| docs: { | ||
| source: { | ||
| type: SourceType.DYNAMIC, | ||
| originalSource: '<Button />', | ||
| transform: (code: string) => `T:${code}`, | ||
| }, | ||
| }, | ||
| }, | ||
| args: {}, | ||
| initialArgs: {}, | ||
| unmappedArgs: {}, | ||
| } as unknown as ReducedStoryContext; | ||
| const story: PreparedStory = { | ||
| id: 'story--primary', | ||
| parameters: storyContext.parameters, | ||
| } as unknown as PreparedStory; | ||
| const docsContext = createMockDocsContext(story, storyContext); | ||
|
|
||
| const { result, rerender } = renderHook( | ||
| ({ sourceContext, serviceSnippet, props }) => | ||
| useSourceProps(props, docsContext, sourceContext, serviceSnippet), | ||
| { | ||
| initialProps: { | ||
| sourceContext: { sources: {} } as SourceContextProps, | ||
| serviceSnippet: '', | ||
| props: {} as SourceProps, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| expect(result.current.code).toBe(''); | ||
|
|
||
| // Snippet arrives after initial render | ||
| rerender({ | ||
| sourceContext: { | ||
| sources: { | ||
| 'story--primary': { | ||
| [UNKNOWN_ARGS_HASH]: { code: 'T:<Button />' }, | ||
| }, | ||
| }, | ||
| }, | ||
| serviceSnippet: '', | ||
| props: {}, | ||
| }); | ||
|
|
||
| expect(result.current.code).toBe('T:<Button />'); | ||
| }); | ||
|
|
||
| it('applies transform when raw service snippet arrives even if emitted snippet is present', () => { | ||
| const storyContext: ReducedStoryContext = { | ||
| id: 'story--primary', | ||
| parameters: { | ||
| __isArgsStory: true, | ||
| docs: { | ||
| source: { | ||
| type: SourceType.AUTO, | ||
| originalSource: '<Button />', | ||
| transform: (code: string) => `T:${code}`, | ||
| }, | ||
| }, | ||
| }, | ||
| args: {}, | ||
| initialArgs: {}, | ||
| unmappedArgs: {}, | ||
| } as unknown as ReducedStoryContext; | ||
| const story: PreparedStory = { | ||
| id: 'story--primary', | ||
| parameters: storyContext.parameters, | ||
| } as unknown as PreparedStory; | ||
| const docsContext = createMockDocsContext(story, storyContext); | ||
|
|
||
| const { result, rerender } = renderHook( | ||
| ({ sourceContext, serviceSnippet, props }) => | ||
| useSourceProps(props, docsContext, sourceContext, serviceSnippet), | ||
| { | ||
| initialProps: { | ||
| sourceContext: { | ||
| sources: { | ||
| 'story--primary': { | ||
| [UNKNOWN_ARGS_HASH]: { code: 'T:<Button />' }, | ||
| }, | ||
| }, | ||
| } as SourceContextProps, | ||
| serviceSnippet: '', | ||
| props: {} as SourceProps, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| expect(result.current.code).toBe('T:<Button />'); | ||
|
|
||
| // Raw service snippet arrives: not pre-transformed, so transform must be applied | ||
| rerender({ | ||
| sourceContext: { | ||
| sources: { | ||
| 'story--primary': { | ||
| [UNKNOWN_ARGS_HASH]: { code: 'T:<Button />' }, | ||
| }, | ||
| }, | ||
| }, | ||
| serviceSnippet: '<Svc />', | ||
| props: {}, | ||
| }); | ||
|
|
||
| expect(result.current.code).toBe('T:<Svc />'); | ||
| }); | ||
|
|
||
| it('transforms originalSource once for type code even when snippet is present', () => { | ||
| const storyContext: ReducedStoryContext = { | ||
| id: 'story--primary', | ||
| parameters: { | ||
| __isArgsStory: true, | ||
| docs: { | ||
| source: { | ||
| type: SourceType.CODE, | ||
| originalSource: '<Button />', | ||
| transform: (code: string) => `T:${code}`, | ||
| }, | ||
| }, | ||
| }, | ||
| args: {}, | ||
| initialArgs: {}, | ||
| unmappedArgs: {}, | ||
| } as unknown as ReducedStoryContext; | ||
| const story: PreparedStory = { | ||
| id: 'story--primary', | ||
| parameters: storyContext.parameters, | ||
| } as unknown as PreparedStory; | ||
| const docsContext = createMockDocsContext(story, storyContext); | ||
|
|
||
| const { result } = renderHook( | ||
| ({ sourceContext, serviceSnippet, props }) => | ||
| useSourceProps(props, docsContext, sourceContext, serviceSnippet), | ||
| { | ||
| initialProps: { | ||
| sourceContext: { | ||
| sources: { | ||
| 'story--primary': { | ||
| [UNKNOWN_ARGS_HASH]: { code: 'T:<Snippet />' }, | ||
| }, | ||
| }, | ||
| } as SourceContextProps, | ||
| serviceSnippet: '', | ||
| props: {} as SourceProps, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| expect(result.current.code).toBe('T:<Button />'); | ||
| }); | ||
|
|
||
| it('transforms originalSource once for auto non-args story even when snippet is present', () => { | ||
| const storyContext: ReducedStoryContext = { | ||
| id: 'story--primary', | ||
| parameters: { | ||
| __isArgsStory: false, | ||
| docs: { | ||
| source: { | ||
| type: SourceType.AUTO, | ||
| originalSource: '<Button />', | ||
| transform: (code: string) => `T:${code}`, | ||
| }, | ||
| }, | ||
| }, | ||
| args: {}, | ||
| initialArgs: {}, | ||
| unmappedArgs: {}, | ||
| } as unknown as ReducedStoryContext; | ||
| const story: PreparedStory = { | ||
| id: 'story--primary', | ||
| parameters: storyContext.parameters, | ||
| } as unknown as PreparedStory; | ||
| const docsContext = createMockDocsContext(story, storyContext); | ||
|
|
||
| const { result } = renderHook( | ||
| ({ sourceContext, serviceSnippet, props }) => | ||
| useSourceProps(props, docsContext, sourceContext, serviceSnippet), | ||
| { | ||
| initialProps: { | ||
| sourceContext: { | ||
| sources: { | ||
| 'story--primary': { | ||
| [UNKNOWN_ARGS_HASH]: { code: 'T:<Snippet />' }, | ||
| }, | ||
| }, | ||
| } as SourceContextProps, | ||
| serviceSnippet: '', | ||
| props: {} as SourceProps, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| expect(result.current.code).toBe('T:<Button />'); | ||
| }); | ||
|
|
||
| it('applies transformFromProps to emitted snippet when explicitly provided on props', () => { | ||
| const storyContext: ReducedStoryContext = { | ||
| id: 'story--primary', | ||
| parameters: { | ||
| __isArgsStory: true, | ||
| docs: { | ||
| source: { | ||
| type: SourceType.DYNAMIC, | ||
| transform: (code: string) => `T:${code}`, | ||
| }, | ||
| }, | ||
| }, | ||
| args: {}, | ||
| initialArgs: {}, | ||
| unmappedArgs: {}, | ||
| } as unknown as ReducedStoryContext; | ||
| const story: PreparedStory = { | ||
| id: 'story--primary', | ||
| parameters: storyContext.parameters, | ||
| } as unknown as PreparedStory; | ||
| const docsContext = createMockDocsContext(story, storyContext); | ||
|
|
||
| const { result } = renderHook( | ||
| ({ sourceContext, serviceSnippet, props }) => | ||
| useSourceProps(props, docsContext, sourceContext, serviceSnippet), | ||
| { | ||
| initialProps: { | ||
| sourceContext: { | ||
| sources: { | ||
| 'story--primary': { | ||
| [UNKNOWN_ARGS_HASH]: { code: 'T:<Button />' }, | ||
| }, | ||
| }, | ||
| } as SourceContextProps, | ||
| serviceSnippet: '', | ||
| props: { | ||
| transform: (code: string) => `/*prop*/ ${code}`, | ||
| } as SourceProps, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| expect(result.current.code).toBe('/*prop*/ T:<Button />'); | ||
| }); | ||
| }); | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.