I am having an issue with the close()
method in my Test
component. It seems to only work when clicking outside of the div
that the directive is applied to. How can I ensure that the method is triggered appropriately in my test? The component utilizes the v-click-outside
npm package.
Component
<script>
import vClickOutside from 'v-click-outside';
export default {
name: 'Test',
directives: {
vClickOutside,
},
data: () => ({
isOpen: false,
}),
methods: {
close() {
this.isOpen = false;
},
};
</script>
<template>
<div
v-click-outside="close"
class="test-class"
>
<OtherComponent />
</div>
</template>
Test File
const clickOutsidelDirective = jest.fn();
describe('Test.vue', () => {
const wrapper = shallowMount(Component, {
directives: {
clickOutside: clickOutsidelDirective,
},
});
// Not sure how to mock the close() function
wrapper.find('.test-class').trigger('click');
// This assertion does not pass as expected
expect(clickOutsidelDirective).toHaveBeenCalled();
}