-
Notifications
You must be signed in to change notification settings - Fork 258
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
Slotting behaviour #482
Comments
Just to confirm what you'd like to do...
So something like:
<template>
<slot />
</template> Usage: <template>
<blah>
<span /><foo /?>
</blah>
</template> And the problem is you cannot pass in This does seem like a problem. We might want to support passing an array to mount(Blah, {
slots: {
default: [Foo, Bar] // or [h(Foo), h(Bar)]
}
}) It looks like you should be able to accomplish this already using a template, see this test. This might also solve your props problem? |
In the linked test, it only tests with a single html element
This would work if to the mount the first argument is a wrapping component in which would have the const wrapper = mount({
template: `
<ParentComp prop="something">
<ChildComp prop="foo" />
<ChildComp prop="bar" />
</ParentComp>`,
components: { ParentComp, ChildComp }
}); The The array to slots is probably a good idea. |
We should include Another alternative might be supporting mount(Foo {
slots: {
default: h(Bar, { someProp: 'val' })
}
}) That solves the props problem too. 🤔 |
Running into this same issue being unable to pass an array into a slot, would be happy to try to implement that in VTU if that work hasn't been started and you can point me in the right direction. Not sure how to work around it currently without wrapping the array contents in a div. |
Sure @cowills. Looks like the slot stuff is here, I guess you can just check if it's an array an iterate over it. I'd recommend figuring out the exact API first. How do you think it will look? mount(Foo, {
slots: {
default: [Foo]
}
}) Do you need to pass props to the slots? How would that look? Crazy idea might be: mount(Foo, {
slots: {
default: [
{
component: Foo, props: { msg: 'Hello' }
}
]
}
}) Not sure how useful this would be. Maybe there is a nicer API. We should figure this out before we write too much code. |
I think both of those proposed should be accepted. Without looking at the code, would it be possible to give it a wrapper or instantiated vm? Like: const childWraper = mount(Bar)
mount(Foo, {
slots: {
default: [
childWrapper,
// or
childWrapper.vm
]
}
}) That could be cool if I don't have to reach for |
couldn't it just support whatever component styles a non-array slot does? im not sure i understand the need for an additional API beyond supporting an array of components like VTU for vue2 currently supports
an example from vue2 VTU from the codebase i work on: |
I didn't realize we supported an array in VTU v1. We should support whatever we support there. Ideally these libraries will have the same API so people can easily migrate from Vue 2 to Vue 3. I say we start with the above post (matching Vue 2/VTU v1 API). We can discuss additional improves separately. Would you like to work on this @cowills? I think this should not be too difficult, since it's basically the current behaviour, but you need to loop over the content if it's an array. |
Not sure I understand typescript or vue internals well enough to make the change sadly, here's what I came up with: slots.spec.ts
mount.ts
|
Seems legit to me. Do you want to make a PR adding the test and code? It's fine if other tests break, I can help out. Alternatively, if you don't have the time/bandwidth, I can pick this up using your code above. Either way, let me know. :) |
Sure! i just pushed up my branch and made a PR. If anyone reading this needs a quick workaround, you can wrap the component you are testing in an
|
Thanks, I'll continue on from your PR this week. |
This feature has been implemented anad will be released soon. |
Hi,
I would like to do the two following things:
mount(Comp)
)This isn't necessarily a code smell that the testing scope is too wide. My use case is I have a component that has a default slot into which you pass multiple components of one type.
<AvatarGroup> <Avatar> <AvatarGroup />
The second would also be very useful when the user trying to test some unexpected patterns such as accessing children or parent.
Are these already possible I just missed something and perhaps the documentation can use some improvement?
Without delving into the source is this possible to implement? Is it desired?
The text was updated successfully, but these errors were encountered: