Skip to content

Feature Request: built in function to check if a snippet is rendered #429

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

Open
AntonioVentilii opened this issue Apr 15, 2025 · 1 comment

Comments

@AntonioVentilii
Copy link

AntonioVentilii commented Apr 15, 2025

Hello,

Is there a way to render a component, set the test conditions and then call something like expect(RenderedComponent).toRenderSnippet(...) or .not.toRenderSnippet(...)?

For example

it('should render the snippet if conditions are met', async () => {
                // this would be a new functions that auto-fills the children/props that are Snippet for example
		const {renderedComponent} = renderWithMockedSnippets(MyComponent);
		
                 // here the consumer sets the conditions to render the snippet (if it is conditional)
                ........

		expect(renderedComponent).toHaveSnippet(...)
})
@AntonioVentilii AntonioVentilii changed the title Feature Request: built in function to check if a slot is rendered Feature Request: built in function to check if a snippet is rendered Apr 15, 2025
@mcous
Copy link
Collaborator

mcous commented Apr 15, 2025

I recommend the approach outlined in the docs for testing slots

  1. Create a wrapper component around your component under test, passing all non-snippet props along
  2. In the wrapper component, set the snippet to a DOM element with a data-testid attribute
  3. Render the wrapper component, setting or updating the props as necessary to render (or hide) the snippet
  4. Check if getByTestId('your-snippet-test-id') is in the document or not

Snippets are hard to create generic fixtures for, due to fundamental limitations with how Svelte works, so (for the time being) the best approach is "one wrapper component per test," unfortunately.

In terms of adding new methods to expect - that's outside the scope of the @testing-library/svelte library. You can add your own - see the vitest docs - or check out testing-library/jest-dom if you're curious to see how they do it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants