-
Notifications
You must be signed in to change notification settings - Fork 33
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
Testing slots? #48
Testing slots? #48
Comments
Yes it's more a svelte API question that should be redirected to their repository. Anyway: import Button from './Button.svelte';
const { container } = render(Badge, { props: { color: 'primary' }, slots: { default: Button } }); |
afaik is still wip, there's no way to declare slots from the Component API, see:
we could patch the workaround until it is merged to the main api, what do you think? something like: import Badge from './Badge.svelte';
import Button from './Button.svelte';
const { container } = render(
Badge,
{ props: { color: 'primary' } }, // component props
{ slots: { default: Button } } // component ctx
); ping @benmonro @EmilTholin |
2684 looks like it would work and I'm of the opinion that we should wait for that to be merged but I'll defer to @EmilTholin |
I agree with the rest of you that it's a Svelte API issue and that 2684 will resolve it. In the meantime you could do the less than ideal solution of creating a test-specific component that renders <!-- TestBadgeSlot.svelte -->
<Badge>
<Button />
</Badge> // Badge.test.js
import TestBadgeSlot from './TestBadgeSlot.svelte';
const { container } = render(
TestBadgeSlot,
{ props: { color: 'primary' } }
); |
Thanks for the info, I'll wait until sveltejs/svelte#2684 is merged. If by chance it's not merged for some reason a patch in this lib would be very useful 🙏 |
Closing for now not feel free to reopen if that doesn't get merged in svelte |
The mentioned issue (sveltejs/svelte#268) has been closed in favor of another refactoring in the svelte repo (which is still open!). Are there any news on how slots can be tested? |
the new pr to svelte api is still open: sveltejs/svelte#4296 this suggestion still stands: #48 (comment) |
If it helps, I worked around this by adding an optional https://github.com/bestguy/sveltestrap/blob/master/src/Alert.svelte#L37 Allows testing like: Not perfect 🤷♂️ but helps until there is a better option |
wait for the pr merged. |
Still actual. |
Should this be reopened until sveltejs/svelte#4296 is merged? |
It has been a year with no movement on that svelte PR. Can we open this again? |
Sure |
FWIW I discovered this library recently: https://github.com/DockYard/svelte-inline-compile which I am now using in svelte-headlessui. I think something like this library seems to me to be the nicest way of dealing with this, though it's not perfect. |
@rgossiaux @Akolyte01 I am one of the developers of https://github.com/DockYard/svelte-inline-compile. This week we plan to make it work for Vitest, a new trending test runner better suited for sveltekit apps than jest. We are working with the core team to make the testing experience nicer out of the box. |
For whoever is interested, we've released two packages that make testing components much nicer than the current state of things in Jest. It is similar to https://github.com/DockYard/svelte-inline-compile but less complicated internally and with less drawbacks. Right now the experience of testing components that take slots, actions or that must interact with other components was pretty bad. Essentially the only way was to create a on-off component in the test folder that used your component in the way you wanted and render that component instead. Our package https://github.com/dockyard/svelte-inline-component allows to define components inline in the test itself. import { render } from '@testing-library/svelte'
import { svelte } from 'svelte-inline-component';
describe('DefinitionEntry.svelte', () => {
it('renders a link with the given href', async () => {
const { getByTestId } = render(await svelte`
<script>import DefinitionEntry from '$lib/DefinitionEntry.svelte';</script>
<DefinitionEntry background="gray">
<svelte:fragment slot="dt">I'm the description term</svelte:fragment>
<svelte:fragment slot="dd">I'm the description definition</svelte:fragment>
</DefinitionEntry>
`);
expect(getByTestId('definition-entry')).to.have.class('bg-gray-50');
expect(getByTestId('dt')).to.have.text("I'm the description term");
expect(getByTestId('dd')).to.have.text("I'm the description definition");
});
}); I'm biased but IMO it is SO. MUCH. NICER to invoke components in tests with the same syntax you use in regular app code that I can't go back. There are some downsides that we're working on to polish, like publishing VSCode plugin that enables svelte syntax and intellisense inside the strings for better developer experience. But it's very functional. |
@cibernox I agree that is nice. feel free to update the testing-library.com docs to list this as an alternative, that will help raise awareness of your library too |
@benmonro I'll take a look. I'm not sure how to frame it within the docs of testing-library, because right now there's a lot of "if"s. Namely, that your app must use vitest, which is, as their own documentation takes great care of explaining, not production ready (although in my opinion, neither is jest and yet everyone uses it). |
How are we currently supposed to test something that is like this currently? It just doesn't work. The Layout component takes a slot ands obviously this library doesn't support that, right? Is the workaround to use something like svelte-inline-compile? ParentComponent.svelte
Layout.svelte
|
Any news on this? |
This is great, thank you for your efforts with the library @cibernox - it would be amazing if the render(<Button>Click me</Button>) |
I'm currently using svelte-htm and it works well 👍 |
@ivanhofer Can you share how you use render(html`<${MyComponent} //>`)
|
@davipon it worked fine until we upgraded our dependencies a few weeks back. |
@davipon that looks like an issue in https://github.com/kenoxa/svelte-htm. I filed an issue there to see if the author can resolve it: kenoxa/svelte-htm#234 |
@benmccann Thank you for your input. I'll dive into the issue and see how I can help to resolve that. |
Changes almost all of the remaining pages to FormPages, using the existing icons that are used in the actions to open the form and the form button: - Build image - Pull image - Play Kube YAML - Create Pod from containers Adds a new 'actions' slot for the PullImage page's Manage registries button. Adds 'missing' pull image action to the button to match other pages. Makes Kube play icon resizable so that a larger form can be used in the header. Svelte has no API to test slots directly (see below), so a FormPageSpec component is added solely to be able to test that the icon/actions/content slots work correctly. The actions section is also tested by confirming the pull image registry button. testing-library/svelte-testing-library#48 https://stackoverflow.com/questions/60771586/testing-svelte-components-that-use-slots Signed-off-by: Tim deBoer <git@tdeboer.ca>
Changes almost all of the remaining pages to FormPages, using the existing icons that are used in the actions to open the form and the form button: - Build image - Pull image - Play Kube YAML - Create Pod from containers Adds a new 'actions' slot for the PullImage page's Manage registries button. Adds 'missing' pull image action to the button to match other pages. Makes Kube play icon resizable so that a larger form can be used in the header. Svelte has no API to test slots directly (see below), so a FormPageSpec component is added solely to be able to test that the icon/actions/content slots work correctly. The actions section is also tested by confirming the pull image registry button. testing-library/svelte-testing-library#48 https://stackoverflow.com/questions/60771586/testing-svelte-components-that-use-slots Signed-off-by: Tim deBoer <git@tdeboer.ca>
Any news on this? How do you test |
same as looking for new solution for sveltev4 |
Perhaps this is more svelte API question, but it's not clear how to test component slots, for example:
Badge.svelte:
Badge.spec.js:
Looking for default slots primarily, and do not want to add props for this.
The text was updated successfully, but these errors were encountered: