-
Notifications
You must be signed in to change notification settings - Fork 244
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
fix: remove symbol props from stubs #1086
Conversation
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 makes me think that the fix should not be in VTU but in Vue itself (as it won't be in webidl-conversion).
The same will happen in Vue if someone binds a Symbol to an attribute right?
So instead of having this dirty hack, I thhink the issue should be opened upstream on vue-next, and fix it there.
If we ever end up merging this hack in VTU, maybe we should consider not removing the Symbol, but replacing its value with a string equivalent, so the stub will properly reflect that there was a Symbol.
I opened a PR to fix this in vue-next instead vuejs/core#4964 If that gets merged we won't need this PR, and a simple upgrade to the latest vue-next will solve the issue. |
Nice, I added a comment to your PR vuejs/core#4964 I like your fix, although Vue core has so many open PRs - if it won't be merged for a while, we could use this until that PR is merged, then change to that implementation. We should ping Evan for a review on that PR, as well as the |
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.
As the PR in vue-next was not merged, let's go with the hacky patch in VTU 😉
src/stubs.ts
Outdated
const $props = props as unknown as ComponentObjectPropsOptions | ||
return Object.keys($props).reduce((acc, key) => { | ||
if (typeof $props[key] === 'symbol') { | ||
return acc |
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.
WDYT of stringifying the Symbol instead of completely removing it? It would then show in the snapshot. (a simple $props[key].toString()
should do the trick)
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.
+1
given that this is only an issue when shallow mounting, I guess stringifying makes sense as people use shallow mounting to do things such as snapshotting or checking against a prop value. I never use it, but I guess stringifying the symbol aligns with the same mental model of "exploring" the rendered stuff
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.
Done!
src/stubs.ts
Outdated
@@ -62,7 +86,7 @@ export const createStub = ({ | |||
}) | |||
} | |||
|
|||
const createTransitionStub = ({ name }: StubOptions) => { | |||
const createTransitionStub = ({ name, propsDeclaration }: StubOptions) => { |
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.
Are the changes to createTransitionStub necessary? I fail to see the link. If it is necessary maybe add a test case with a transition?
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.
reverted
@cexbrayat @afontcu updated 👍 |
src/stubs.ts
Outdated
@@ -42,6 +43,17 @@ const shouldNotStub = (type: ConcreteComponent) => doNotStubComponents.has(type) | |||
export const addToDoNotStubComponents = (type: ConcreteComponent) => | |||
doNotStubComponents.add(type) | |||
|
|||
const removeSymbols = <T = ComponentPropsOptions>(props: T): T => { |
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.
nit: this could be renamed to stringifySymbols
as it is not removing them anymore?
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.
ooc: is the generic type T
necessary here? As it is only called once, can't we use ComponentPropsOptions
directly?
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.
Removed that
src/stubs.ts
Outdated
// something like `el.setAttribute('val', Symbol())` which is not valid and | ||
// causes an error. | ||
// Only a problem when shallow mounting. For this reason we iterate of the | ||
// props that will be passed and remove any that are symbols. |
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.
description is not accurate anymore: this should mention that they are stringified
src/stubs.ts
Outdated
// causes an error. | ||
// Only a problem when shallow mounting. For this reason we iterate of the | ||
// props that will be passed and remove any that are symbols. | ||
const propsWithoutSymbols = removeSymbols(ctx.$props) |
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.
nit: same here, the variable could be renamed
src/stubs.ts
Outdated
@@ -164,7 +188,8 @@ export function stubComponents( | |||
if (type === Transition && 'transition' in stubs && stubs['transition']) { | |||
return [ | |||
createTransitionStub({ | |||
name: 'transition-stub' | |||
name: 'transition-stub', | |||
propsDeclaration: {} |
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.
If the option is reverted to optional, this can be removed.
src/stubs.ts
Outdated
@@ -179,7 +204,8 @@ export function stubComponents( | |||
) { | |||
return [ | |||
createTransitionStub({ | |||
name: 'transition-group-stub' | |||
name: 'transition-group-stub', | |||
propsDeclaration: {} |
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.
same here
src/stubs.ts
Outdated
@@ -21,7 +22,7 @@ import { Stub, Stubs } from './types' | |||
|
|||
interface StubOptions { | |||
name: string | |||
propsDeclaration?: ComponentPropsOptions | |||
propsDeclaration: ComponentPropsOptions |
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.
is this necessary? Can't that be left optional, and have a default value in the stubs?
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.
Done, fixed all comments, thanks for the review!
resolves #1076
More context: ionic-team/ionic-framework#24181
Basically when you do a stub that receives a
Symbol
as a prop, it ends up doing<blah-stub sym="Symbol()">
which isn't allowed. Try it in your browser:document.body.setAttribute('sym', Symbol())
.I just filter out all the symbol props. Bit of a hack, but not a breaking change (passing a symbol as prop to a stub causes an error in both VTU v1 and v2).
Happy to solicit any better work arounds for this issue, best I could come up with for now to unblock the issue in Ionic team repo.