-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add additional type checks to adapters #1701
Add additional type checks to adapters #1701
Conversation
…ValidElementType`
…fNode`, if present
…entType`, if present
packages/enzyme/src/Utils.js
Outdated
return (typeof node.type === 'function' ? | ||
functionName(node.type) === type : node.type.name === type) || node.type.displayName === type; | ||
|
||
return typeof node.type === 'function' ? |
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.
left this since displayNameOfNode returns the display over the ctor name
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.
I'm concerned about how to make this not be a breaking change - specifically, with "old enzyme + new adapter" ("new enzyme + old adapter" and "new enzyme + new adapter" should work fine)
packages/enzyme/src/ReactWrapper.js
Outdated
@@ -728,7 +727,8 @@ class ReactWrapper { | |||
* @returns {String} | |||
*/ | |||
name() { | |||
return this.single('name', n => displayNameOfNode(n)); | |||
const adapter = getAdapter(this[OPTIONS]); | |||
return this.single('name', n => adapter.displayNameOfNode(n)); |
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.
i think this needs adapter.displayNameOfNode ? adapter.displayNameOfNode(n) : displayNameOfNode(n)
@@ -896,7 +895,8 @@ class ShallowWrapper { | |||
* @returns {String} | |||
*/ | |||
name() { | |||
return this.single('name', displayNameOfNode); | |||
const adapter = getAdapter(this[OPTIONS]); | |||
return this.single('name', n => adapter.displayNameOfNode(n)); |
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.
i think this needs adapter.displayNameOfNode ? adapter.displayNameOfNode(n) : displayNameOfNode
packages/enzyme/src/Utils.js
Outdated
@@ -42,10 +42,14 @@ export function typeOfNode(node) { | |||
|
|||
export function nodeHasType(node, type) { | |||
if (!type || !node) return false; | |||
|
|||
const displayName = getAdapter().displayNameOfNode(node); |
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.
and i think this needs the same treatment
packages/enzyme/src/Debug.js
Outdated
@@ -66,6 +64,7 @@ function indentChildren(childrenStrs, indentLength) { | |||
|
|||
export function debugNode(node, indentLength = 2, options = {}) { | |||
if (typeof node === 'string' || typeof node === 'number') return escape(node); | |||
if (typeof node === 'function') return '[function]'; |
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.
a) this should probably be [function Foo]
where Foo is the functionName
b) could this small change be peeled out into a separate PR with its own tests?
packages/enzyme/src/Debug.js
Outdated
|
||
const booleanValue = Function.bind.call(Function.call, Boolean.prototype.valueOf); | ||
|
||
export function typeName(node) { | ||
return typeof node.type === 'function' |
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.
i think all this logic needs to stay as a fallback for when the adapter lacks .displayNameOfNode
packages/enzyme/src/Utils.js
Outdated
@@ -214,15 +218,6 @@ export function AND(fns) { | |||
return x => fnsReversed.every(fn => fn(x)); | |||
} | |||
|
|||
export function displayNameOfNode(node) { |
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.
and i think we may need to keep this as well
// If the selector is a function, check if the node's constructor matches | ||
if (typeof selector === 'function') { | ||
// If the selector is a string, parse it as a simple CSS selector | ||
if (typeof selector === 'string') { |
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.
these changes also seem like they could be peeled out into a separate PR?
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.
They could yeah, I wanted to validate that the apis I was adding were sufficient
packages/enzyme/src/selectors.js
Outdated
return buildPredicateFromToken(tokens[0]); | ||
} | ||
// If the selector is an element type, check if the node's type matches | ||
if (getAdapter().isValidElementType(selector)) { |
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 needs a fallback for when the adapter lacks .isValidElementType
091fc3e
to
d79da18
Compare
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.
ok, i rebased this a bit and split up some commits. what this needs now is tests :-)
d79da18
to
8480406
Compare
@ljharb I'm hoping to have a moment, i'm pretty swamped with life and work at the moment but may have a second to finish this up in the next few days |
@jquense I certainly understand :-) thanks! |
8480406
to
fee9fa5
Compare
fee9fa5
to
e9e63a2
Compare
Sorry, this has tests now |
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.
Thanks! There’s a few more tweaks needed; if you don’t have a chance to update the PR, I’ll be happy to do it for you.
@@ -801,4 +801,69 @@ describe('Adapter', () => { | |||
}, | |||
})); | |||
}); | |||
|
|||
it.only('should determine valid element types', () => { |
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.
oops, the only needs to be removed
class Component extends React.Component { | ||
render() { return null } | ||
} | ||
const ArrowFunction = () => null |
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.
the arrow function test needs it’s own it, so it can be skipped on React 13
|
||
expect(adapter.isValidElementType('div')).to.equal(true); | ||
expect(adapter.isValidElementType(Component)).to.equal(true); | ||
expect(adapter.isValidElementType(ArrowFunction )).to.equal(true); |
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.
extra space
expect(adapter.isValidElementType(Component)).to.equal(true); | ||
expect(adapter.isValidElementType(ArrowFunction )).to.equal(true); | ||
|
||
if (createPortal) { |
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.
instead of using if checks, these should be checking the React versions and running conditionslly, as their own it’s
e9e63a2
to
1a6b47b
Compare
I've done some rebasing; this PR now has 6 commits, the first 2 of which are (thoroughly) tested. I can pull those into master now, but the last 4 commits still need test coverage. |
b2743d8
to
6824acf
Compare
i've added some more tests; now of the 6 commits, |
ee6ce05
to
bdec3d4
Compare
bdec3d4
to
6a47051
Compare
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.
k, that should do it. I'll merge in the morning once tests pass.
🙇 thanks for the help @ljharb |
6a47051
to
67b9dc0
Compare
- [New] Add Portal support (#1760, #1761, #1772, #1774, @jgzuke) - [New] Add pointer events support (#1753, @ljharb) - [New] Add `displayNameOfNode`, `isValidElementType` (#1701, @jquense) - [New] pass the adapter into `createMountWrapper` (#1592, @jquense) - [Fix] `shallow`: skip updates when nextState is `null` or `undefined` (#1785, @koba04) - update deps - [meta] ensure a license and readme is present in all packages when published
- [New] Add forwardRef support (#1592, @jquense) - [New] Add Portal support (#1760, #1761, #1772, #1774, @jgzuke) - [New] Add pointer events support (#1753, @ljharb) - [New] Add `displayNameOfNode`, `isValidElementType` (#1701, @jquense) - [New] pass the adapter into `createMountWrapper` (#1592, @jquense) - [Fix] preemptively fix compat with React v16.4.3 (#1790, #1778, @gaearon, @aweary) - [Fix] `shallow`: skip updates when nextState is `null` or `undefined` (#1785, @koba04) - update deps - [meta] ensure a license and readme is present in all packages when published
Required for #1592 to work properly, still in progress but i want to make sure the proposed API changes to adapters are ok before i polish up all the tests and what not
cc @ljharb