Skip to content
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

fix(emit): force event handling even with fake timers #1856

Merged
merged 1 commit into from Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/baseWrapper.ts
Expand Up @@ -369,6 +369,13 @@ export default abstract class BaseWrapper<ElementType extends Node>

if (this.element && !this.isDisabled()) {
const event = createDOMEvent(eventString, options)
// see https://github.com/vuejs/test-utils/issues/1854
// fakeTimers provoke an issue as Date.now() always return the same value
// and Vue relies on it to determine if the handler should be invoked
// see https://github.com/vuejs/core/blob/5ee40532a63e0b792e0c1eccf3cf68546a4e23e9/packages/runtime-dom/src/modules/events.ts#L100-L104
// we workaround this issue by manually setting _vts to Date.now() + 1
// thus making sure the event handler is invoked
event._vts = Date.now() + 1
this.element.dispatchEvent(event)
}

Expand Down
27 changes: 27 additions & 0 deletions tests/trigger.spec.ts
Expand Up @@ -364,4 +364,31 @@ describe('trigger', () => {

expect(wrapper.emitted().enter).toHaveLength(1)
})

// https://github.com/vuejs/test-utils/issues/1854
it('dispatches events even with fakeTimers', async () => {
const handlerSpy = vi.fn()

const Component = defineComponent({
setup() {
return { handlerSpy }
},
template: `
<div>
<a @click="handlerSpy">
<span @click="() => {}">Ok</span>
</a>
</div>
`
})

vi.useFakeTimers()
const wrapper = mount(Component)

expect(handlerSpy).not.toHaveBeenCalled()

await wrapper.get('span').trigger('click')

expect(handlerSpy).toHaveBeenCalled()
})
})