-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
ResizeGroup SCSS to MergeStyles Part 2: Style Conversion #4072
Changes from 9 commits
6766f11
2320f81
1c3a3a8
62109bf
2438eb4
4944214
e7825b0
d3cfa7b
6559ee6
1e8dd14
125adcd
a0e687d
0779d03
912cf74
5b6268f
186ed05
b7a8756
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "office-ui-fabric-react", | ||
"comment": "Converting ResizeGroup SCSS to MergeStyles step 2 - style converstion", | ||
"type": "minor" | ||
} | ||
], | ||
"packageName": "office-ui-fabric-react", | ||
"email": "[email protected]" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { shallow, ShallowWrapper } from 'enzyme'; | ||
|
||
/** | ||
* Duplicated enzyme's ShallowRendererProps | ||
* | ||
* @internal | ||
*/ | ||
export interface IShallowRendererProps { | ||
lifecycleExperimental?: boolean; | ||
disableLifecycleMethods?: boolean; | ||
} | ||
|
||
/** | ||
* ShallowUntilTarget Interface | ||
* | ||
* @internal | ||
*/ | ||
export interface IShallowUntilTarget { | ||
maxTries: number; | ||
shallowOptions: IShallowRendererProps; | ||
} | ||
|
||
/** | ||
* An extention of enzyme's shallow function which will fail to work | ||
* with decorated components and/or components using the styled() function. | ||
* This function allows you to pass a 'target' component (e.g. ComponentBase) | ||
* and keep running shallow on each child component till a match is found. | ||
* | ||
* @public | ||
*/ | ||
export function shallowUntilTarget<P, S>( | ||
componentInstance: React.ReactElement<P>, | ||
TargetComponent: string, | ||
options: IShallowUntilTarget = { | ||
maxTries: 10, | ||
shallowOptions: {} | ||
} | ||
): ShallowWrapper { | ||
const { maxTries, shallowOptions } = options; | ||
|
||
let root = shallow<P, S>(componentInstance, shallowOptions); | ||
|
||
if (typeof root.type() === 'string') { | ||
// If type() is a string then it's a DOM Node. | ||
// If it were wrapped, it would be a React component. | ||
throw new Error('Cannot unwrap this component because it is not wrapped'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that we shouldn't throw in this case. I should be able to use this function anywhere that |
||
} | ||
|
||
for (let tries = 1; tries <= maxTries; tries++) { | ||
// Check for target as a string to avoid conflicts | ||
// with decoratored components name | ||
if (root.type().toString().includes(TargetComponent)) { | ||
// Now that we found the target component, render it. | ||
return root.first().shallow(shallowOptions); | ||
} | ||
// Unwrap the next component in the hierarchy. | ||
root = root.first().shallow(shallowOptions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I don't think that any decorators we currently have render multiple siblings, it is possible that this will miss a component that isn't the first child. |
||
} | ||
|
||
throw new Error( | ||
`Could not find ${TargetComponent} in React instance: ${componentInstance}; | ||
gave up after ${maxTries} tries` | ||
); | ||
} |
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.
You could have target component be type of
Function
, and then users could pass in the actual class that they care about. You could then access the string by callingTargetComponent.name
and it would work out fine. That way it keeps things a bit more type safe since they would need to import the actual class they care about. This is just a suggestion, if you feel like trying it out that would be cool, otherwise no worries.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.
This was the original way that I had implemented this, but specifically for decorated functions like ResizeGroupBase, the name becomes 'ComponentWithInjectedProps' so it returns before it should. Passing a string name into the function was a way to avoid this.