Dan Knights
Dan Knights

Reputation: 8378

How to listen to custom Svelte event in Jest

I'm dispatching a custom event from a Svelte component:

const dispatch = createEventDispatcher();
...
dispatch('navigation', result);

How can I listen for this event using Jest?

I can't find anything in the Jest docs, Testing Library docs or Svelte Jester which demonstrates how to do this.

Upvotes: 1

Views: 778

Answers (1)

Dan Knights
Dan Knights

Reputation: 8378

Turns out you can do so this way:

const { getByTestId, component } = render(SLink, {
    props: { id: 'khjb23' },
});

const link = getByTestId('khjb23');

component.$on('navigation', e => {
    console.log(e.detail); // Dispatched value
});

Upvotes: 3

Related Questions