]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test: make events test more stable
authorEvan You <yyx990803@gmail.com>
Thu, 2 Apr 2020 01:59:42 +0000 (21:59 -0400)
committerEvan You <yyx990803@gmail.com>
Thu, 2 Apr 2020 01:59:42 +0000 (21:59 -0400)
packages/runtime-dom/__tests__/modules/events.spec.ts

index 8d6cb9de3a9997b9292ce5202422bb51788b3fea..2021a1547c57844a24c3ff3360b227a77e3346f4 100644 (file)
@@ -1,5 +1,6 @@
 import { patchEvent } from '../../src/modules/events'
-import { nextTick } from '@vue/runtime-dom'
+
+const timeout = () => new Promise(r => setTimeout(r))
 
 describe(`events`, () => {
   it('should assign event handler', async () => {
@@ -8,11 +9,11 @@ describe(`events`, () => {
     const fn = jest.fn()
     patchEvent(el, 'click', null, fn, null)
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     expect(fn).toHaveBeenCalledTimes(3)
   })
 
@@ -24,11 +25,11 @@ describe(`events`, () => {
     patchEvent(el, 'click', null, prevFn, null)
     el.dispatchEvent(event)
     patchEvent(el, 'click', prevFn, nextFn, null)
-    await nextTick()
+    await timeout()
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     expect(prevFn).toHaveBeenCalledTimes(1)
     expect(nextFn).toHaveBeenCalledTimes(2)
   })
@@ -40,7 +41,7 @@ describe(`events`, () => {
     const fn2 = jest.fn()
     patchEvent(el, 'click', null, [fn1, fn2], null)
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     expect(fn1).toHaveBeenCalledTimes(1)
     expect(fn2).toHaveBeenCalledTimes(1)
   })
@@ -52,7 +53,7 @@ describe(`events`, () => {
     patchEvent(el, 'click', null, fn, null)
     patchEvent(el, 'click', fn, null, null)
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     expect(fn).not.toHaveBeenCalled()
   })
 
@@ -68,9 +69,9 @@ describe(`events`, () => {
     }
     patchEvent(el, 'click', null, nextValue, null)
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     expect(fn).toHaveBeenCalledTimes(1)
   })
 
@@ -88,9 +89,9 @@ describe(`events`, () => {
     patchEvent(el, 'click', null, prevFn, null)
     patchEvent(el, 'click', prevFn, nextValue, null)
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     expect(prevFn).not.toHaveBeenCalled()
     expect(nextFn).toHaveBeenCalledTimes(1)
   })
@@ -108,9 +109,9 @@ describe(`events`, () => {
     patchEvent(el, 'click', null, nextValue, null)
     patchEvent(el, 'click', nextValue, null, null)
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     el.dispatchEvent(event)
-    await nextTick()
+    await timeout()
     expect(fn).not.toHaveBeenCalled()
   })
 })