-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor withChildrenMock > withPropMock for simpler and more reusabl…
…e usage
- Loading branch information
1 parent
241102b
commit 623d2d0
Showing
14 changed files
with
107 additions
and
95 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
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
This file was deleted.
Oops, something went wrong.
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,81 @@ | ||
import { ArgTypes } from '@storybook/addons/dist/types'; | ||
|
||
/** | ||
* HOC options. | ||
*/ | ||
type Options = { | ||
/** | ||
* Name of the property to mock. | ||
* | ||
* Disables `children` by default, because it's the main reason why we use this HOC. | ||
* Also, can often crash the app if not proper `children` is passed down, bad UX. | ||
* | ||
* @default children | ||
*/ | ||
propName: string; | ||
|
||
/** | ||
* Whether to mock the `propName`. | ||
* | ||
* @default true | ||
*/ | ||
shouldMockProp?: boolean; | ||
|
||
/** | ||
* Name of the property that mocks the `propName`. | ||
* | ||
* Replaces `children` prop by `text` by default, because that's our most common use-case. | ||
* | ||
* @default text | ||
*/ | ||
propMockName?: string; | ||
}; | ||
|
||
/** | ||
* Options applied by default. | ||
*/ | ||
const defaultOptions: Partial<Options> = { | ||
propName: 'children', | ||
shouldMockProp: true, | ||
propMockName: 'text', | ||
}; | ||
|
||
/** | ||
* Mocks the "argTypes" to automatically disable `children` control and add a mock field (default: `text`) with default documentation. | ||
* Helps avoid code duplication | ||
*/ | ||
const withPropMock = (argTypes: ArgTypes, options?: Options): ArgTypes => { | ||
const { | ||
propName, | ||
shouldMockProp, | ||
propMockName, | ||
} = { ...defaultOptions, ...options || {} }; | ||
let computedArgTypes: ArgTypes; | ||
|
||
if (shouldMockProp) { | ||
computedArgTypes = { | ||
...argTypes, | ||
|
||
/** | ||
* Disables `propName` control. | ||
*/ | ||
[propName]: { | ||
control: { | ||
disable: true, | ||
}, | ||
}, | ||
|
||
/** | ||
* `propName` mock field, meant to replace the `propName` prop by providing interactivity (controls enabled). | ||
* Must be added to the Story `args` with a default value to be interactive. | ||
*/ | ||
[propMockName]: { | ||
description: `<code>${propName}</code> mock property.<br /><br /><span className="tip">Mock</span><i>This property doesn't really exist in the component.<br />It is made available to help manipulate the <code>${propName}</code> from Storybook</i>.<br /><br />You must use <code>${propName}</code> instead during actual code implementation.`, | ||
}, | ||
}; | ||
} | ||
|
||
return computedArgTypes; | ||
}; | ||
|
||
export default withPropMock; |
623d2d0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not what you expected? Are your scores flaky? Run Lighthouse on Foo
If scores continue to be inconsistent consider running all audits on Foo