I have a snackbar component which I am writing tests for. It contains a child component (A button, when clicked closes the snackbar).
This button emits a @click event which the parent listens to and runs a method to either close the snackbar or open another.
Is there a way of mocking the $emit? I can't seem to find something anywhere related to this.
import { render, fireEvent, waitFor } from '@testing-library/vue';
import Snackbar from '../Snackbar.vue';
import EventBus from '@/services/EventBus';
describe('Snackbar', () => {
const role = 'alert';
it('should close snackbar on button click', async () => {
const type = 'success';
const { getByRole } = render(Snackbar, {
// with this the button is inaccessible
stubs: ['OBButton'],
});
await EventBus.$emit('addSnack', {
type,
});
const snackbar = getByRole('alert');
// this is wrong...
await fireEvent.click(button);
// this expect should be appropriate.
expect(snackbar).not.toBeInTheDocument;
});
});
This is the template of the component:
<template>
<div class="snackbar-wrapper elevated">
<transition name="snack-slide" v-on:after-leave="checkForMoreSnacks">
<div
role="alert"
class="snack-data"
v-if="currentSnack != null"
>
<span class="snack-text">{{ currentSnack.text }}</span>
<OBButton
:text="currentSnack.buttonText"
@click="closeCurrentSnack"
:type="buttonType"
></OBButton>
</div>
</transition>
</div>
</template>
Additional Info:
- This component is never unmounted. Closing it means setting the
currentSnackvalue tonullwhich hides it.