]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test(runtime-dom): improve v-on system key modifiers test (#1597)
authormotao <motao314@foxmail.com>
Sun, 19 Jul 2020 02:55:39 +0000 (10:55 +0800)
committerGitHub <noreply@github.com>
Sun, 19 Jul 2020 02:55:39 +0000 (22:55 -0400)
packages/runtime-dom/__tests__/directives/vOn.spec.ts

index a08c85af9ce21eeea4d4d106a2e7ff578a880f55..e2417d9567761c1f3c16d6a73527d941c49bdc3d 100644 (file)
@@ -41,35 +41,39 @@ describe('runtime-dom: v-on directive', () => {
   })
 
   test('it should support key modifiers and system modifiers', () => {
-    const el = document.createElement('div')
-    const fn = jest.fn()
-    // <div @keyup.ctrl.esc="test"/>
-    const nextValue = withKeys(withModifiers(fn, ['ctrl']), [
-      'esc',
-      'arrow-left'
-    ])
-    patchEvent(el, 'onKeyup', null, nextValue, null)
-
-    triggerEvent(el, 'keyup', e => (e.key = 'a'))
-    expect(fn).not.toBeCalled()
-
-    triggerEvent(el, 'keyup', e => {
-      e.ctrlKey = false
-      e.key = 'esc'
-    })
-    expect(fn).not.toBeCalled()
+    const keyNames = ["ctrl","shift","meta","alt"]
 
-    triggerEvent(el, 'keyup', e => {
-      e.ctrlKey = true
-      e.key = 'Escape'
-    })
-    expect(fn).toBeCalledTimes(1)
-
-    triggerEvent(el, 'keyup', e => {
-      e.ctrlKey = true
-      e.key = 'ArrowLeft'
-    })
-    expect(fn).toBeCalledTimes(2)
+    keyNames.forEach(keyName=>{
+      const el = document.createElement('div')
+      const fn = jest.fn()
+      // <div @keyup[keyName].esc="test"/>
+      const nextValue = withKeys(withModifiers(fn, [keyName]), [
+        'esc',
+        'arrow-left'
+      ])
+      patchEvent(el, 'onKeyup', null, nextValue, null)
+  
+      triggerEvent(el, 'keyup', e => (e.key = 'a'))
+      expect(fn).not.toBeCalled()
+  
+      triggerEvent(el, 'keyup', e => {
+        e[`${keyName}Key`] = false
+        e.key = 'esc'
+      })
+      expect(fn).not.toBeCalled()
+  
+      triggerEvent(el, 'keyup', e => {
+        e[`${keyName}Key`] = true
+        e.key = 'Escape'
+      })
+      expect(fn).toBeCalledTimes(1)
+  
+      triggerEvent(el, 'keyup', e => {
+        e[`${keyName}Key`] = true
+        e.key = 'ArrowLeft'
+      })
+      expect(fn).toBeCalledTimes(2)
+    });
   })
 
   test('it should support "exact" modifier', () => {