-
Notifications
You must be signed in to change notification settings - Fork 264
Context of 'click' event #456
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
Comments
Isn't that meant to be mounted as opposed to beforeMount? I'm facing a similar/same issue by the way, except my directive is in the component: jest.useFakeTimers();
const mockFn = jest.fn();
const wrapper = mount({
template: '<div id="outside"><div id="inside" v-click-away="mockFn"><span id="content" /></div></div>',
directives: { clickAway },
setup: () => ({ mockFn })
});
jest.runAllTimers();
await wrapper.get('#content').trigger('click');
expect(mockFn).not.toHaveBeenCalled();
await wrapper.get('#inside').trigger('click');
expect(mockFn).not.toHaveBeenCalled();
// this doesn't seem to work either
// const event = new MouseEvent('click', { relatedTarget: wrapper.element });
// wrapper.element.dispatchEvent(event);
// await nextTick();
await wrapper.get('#outside').trigger('click');
expect(mockFn).toHaveBeenCalled();
expect(mockFn).toHaveBeenCalledWith(MouseEvent);
jest.useRealTimers(); my directive seems to work flawless when testing manually |
I don't see any obvious problem with your code. I can't see why this would work any differently in test utils, though. You could try running test utils in the browser and see if it's a jsdom problem, or a test-utils specific problem. Other than that, someone (maybe me) will need to debug it. |
Will try to run that in the Browser on monday. |
This is not passing a browser for me, but as you said, it does work when testing manually. Hm. |
Ohhhh, I found the problem. At least, I got it to pass in a browser (using test utils). The problem is the elements are not actually mounted in the document by default. They are just hanging out in memory. You need to create an element and use the attachTo method. I guess it makes sense, you cannot really click outside something that isn't mounted in the document to begin with. Give that a try. I saw it pass, at least in a browser. If you can't get it working, please ping me again, but for now I'll close this since I think we have a solution. |
Nice catch! It worked with the following: const elem = document.createElement('div');
if (document.body) {
document.body.appendChild(elem);
}
const wrapper = mount(App, {
global: {
directives: {
'click-outside': clickOutside,
},
},
attachTo: elem,
});
expect(wrapper).toBeDefined();
const inside = wrapper.get('[data-test=inside]');
const outside = wrapper.get('[data-test=outside]');
await inside.trigger('click');
expect(wrapper.get('[data-test=inside]').text()).toContain('initial');
await outside.trigger('click');
expect(wrapper.get('[data-test=inside]').text()).toContain('clickedOutside'); Thanks you so much! And yes, if you think about it it makes sense, but it still is not obvious ;) |
@lmiller1990 , How to using test-utils in browser,I can't find in Doc. |
Do you mean from a cdn (using |
i'm facing same issue and attachTo is not working for me, here is my test code :
am using
|
@ziaadini can you provide a minimal reproduction we can run? You could upload a repo, or use https://stackblitz.com/edit/vitest-dev-vitest-qoiwy3?file=package.json&initialPath=__vitest. |
@ziaadini @lmiller1990 It looks onClickOutside from @vueuse/core attaches events to |
@unematiii it loooks nice thanks, for your solution |
this worked for me as well. thanks! |
When I test a custom
v-click-outside
directive with vue-test-utils the directive does not get triggered.Does the
mount
method not recognize the dom events attached vith the directive?My test code:
My directive:
My problem is, that the
v-click-outside
method does not get called in tests, but works fine in the browser.The text was updated successfully, but these errors were encountered: