-
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
Svelte extend not triggered when using render #419
Comments
I don't work with compiling Svelte to custom elements, but I wouldn't expect any custom element code (like Instead, you probably want to compile your components and use the DOM directly: import { test } from 'vitest'
import { getByRole } from '@testing-library/dom'
import 'my-custom-component-library'
test('my custom component', () => {
const subject = document.createElement('atp-select')
// ...
}) Let me know if you get this working, since it might be useful to throw into the docs somewhere |
Yes, you are correct, loading the element with document.createElement does call the extend and makes the element available to test. import { expect, test, vi } from 'vitest';
import './Select.svelte'; // my custom element, this needs to only load
// Svelte throws an animate error when running in vitest, this catches this error
Element.prototype.animate = vi
.fn()
.mockImplementation(() => ({ cancel: vi.fn(), finished: Promise.resolve() }));
// create a delay so the component can render
const waitForRender = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
test('Click on option, multiple false', async () => {
const subject = document.createElement('atp-select');
subject.setAttribute('options', JSON.stringify(["v1", "v2", "v3"]));
document.body.appendChild(subject);
await waitForRender(0);
const select = subject.querySelector('.select') as HTMLDivElement;
await select.click();
const options = subject.querySelectorAll('.options button') as unknown as HTMLButtonElement[];
await options[1].click();
await expect(options[1].outerHTML).include('checkmark');
}); It would be great is this could be added in the documentation somewhere, since this is probally not the most obvious to use when testing components. |
@EricVanEldik great to hear you got it working! I'm going to close this issue since it doesn't involve the A couple quick recommendations:
|
I'm having an issue when trying to test custom elements (using jsdom) in Svelte.
When I use
const result = render(MyComponent, { props: { options: '["v1","v2","v3"]', value: '', multiple: false } });
the component get rendered and all functions work as it should.However the extend in this part of the code is never executed:
How can I make sure the code int the extend is executed when testing?
The text was updated successfully, but these errors were encountered: