-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Further cleanup and adjustments to fix portable stories
- Loading branch information
1 parent
7135a94
commit 8e30709
Showing
19 changed files
with
136 additions
and
129 deletions.
There are no files selected for viewing
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
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
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
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
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
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 |
---|---|---|
@@ -1,11 +1,5 @@ | ||
import { definePreview } from 'storybook/internal/preview-api'; | ||
|
||
import * as addonAnnotations from 'storybook/actions/preview'; | ||
|
||
export * from './constants'; | ||
export * from './models'; | ||
export * from './runtime'; | ||
|
||
export default () => definePreview(addonAnnotations); | ||
|
||
export type { ActionsParameters } from './types'; |
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
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 |
---|---|---|
@@ -1,2 +1,12 @@ | ||
export * from './addArgs'; | ||
import { definePreview } from 'storybook/internal/preview-api'; | ||
|
||
import * as addArgs from './addArgs'; | ||
import * as loaders from './loaders'; | ||
|
||
export * from './loaders'; | ||
|
||
export default () => | ||
definePreview({ | ||
...addArgs, | ||
...loaders, | ||
}); |
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
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
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
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
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
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 |
---|---|---|
@@ -1,9 +1,85 @@ | ||
import { | ||
enhanceContext, | ||
nameSpiesAndWrapActionsInSpies, | ||
resetAllMocksLoader, | ||
} from 'storybook/test'; | ||
import { within } from '@testing-library/dom'; | ||
import { userEvent } from '@testing-library/user-event'; | ||
|
||
export default { | ||
loaders: [resetAllMocksLoader, nameSpiesAndWrapActionsInSpies, enhanceContext], | ||
import type { LoaderFunction } from 'storybook/internal/csf'; | ||
import { definePreview } from 'storybook/internal/preview-api'; | ||
|
||
import { clearAllMocks, fn, isMockFunction, resetAllMocks, restoreAllMocks } from './spy'; | ||
|
||
const resetAllMocksLoader: LoaderFunction = ({ parameters }) => { | ||
if (parameters?.test?.mockReset === true) { | ||
resetAllMocks(); | ||
} else if (parameters?.test?.clearMocks === true) { | ||
clearAllMocks(); | ||
} else if (parameters?.test?.restoreMocks !== false) { | ||
restoreAllMocks(); | ||
} | ||
}; | ||
|
||
export const traverseArgs = (value: unknown, depth = 0, key?: string): unknown => { | ||
// Make sure to not get in infinite loops with self referencing args | ||
if (depth > 5) { | ||
return value; | ||
} | ||
|
||
if (value == null) { | ||
return value; | ||
} | ||
if (isMockFunction(value)) { | ||
// Makes sure we get the arg name in the interactions panel | ||
if (key) { | ||
value.mockName(key); | ||
} | ||
return value; | ||
} | ||
|
||
// wrap explicit actions in a spy | ||
if ( | ||
typeof value === 'function' && | ||
'isAction' in value && | ||
value.isAction && | ||
!('implicit' in value && value.implicit) | ||
) { | ||
const mock = fn(value as any); | ||
|
||
if (key) { | ||
mock.mockName(key); | ||
} | ||
return mock; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
depth++; | ||
return value.map((item) => traverseArgs(item, depth)); | ||
} | ||
|
||
if (typeof value === 'object' && value.constructor === Object) { | ||
depth++; | ||
for (const [k, v] of Object.entries(value)) { | ||
if (Object.getOwnPropertyDescriptor(value, k)?.writable) { | ||
// We have to mutate the original object for this to survive HMR. | ||
(value as Record<string, unknown>)[k] = traverseArgs(v, depth, k); | ||
} | ||
} | ||
return value; | ||
} | ||
return value; | ||
}; | ||
|
||
const nameSpiesAndWrapActionsInSpies: LoaderFunction = ({ initialArgs }) => { | ||
traverseArgs(initialArgs); | ||
}; | ||
|
||
const enhanceContext: LoaderFunction = (context) => { | ||
if (globalThis.HTMLElement && context.canvasElement instanceof globalThis.HTMLElement) { | ||
context.canvas = within(context.canvasElement); | ||
} | ||
if (globalThis.window) { | ||
context.userEvent = userEvent.setup(); | ||
} | ||
}; | ||
|
||
export default () => | ||
definePreview({ | ||
loaders: [resetAllMocksLoader, nameSpiesAndWrapActionsInSpies, enhanceContext], | ||
}); |
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
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
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
Oops, something went wrong.