Skip to content

Commit

Permalink
fix(emit): force event handling even with fake timers
Browse files Browse the repository at this point in the history
Fixes #1854
  • Loading branch information
cexbrayat committed Nov 11, 2022
1 parent cc2c2b3 commit c442f76
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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
29 changes: 29 additions & 0 deletions tests/trigger.spec.ts
Expand Up @@ -364,4 +364,33 @@ 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()

wrapper.setProps({ label: 'foo' })

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

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

0 comments on commit c442f76

Please sign in to comment.