From c8a4e960019934d902bae4d5e6086c2ef01a1633 Mon Sep 17 00:00:00 2001 From: motao314 <663952074@qq.com> Date: Thu, 16 Jul 2020 13:01:52 +0800 Subject: [PATCH 1/4] Perfect shortcut key test --- .../__tests__/directives/vOn.spec.ts | 98 ++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/packages/runtime-dom/__tests__/directives/vOn.spec.ts b/packages/runtime-dom/__tests__/directives/vOn.spec.ts index a08c85af9ce..fcd26c38ac4 100644 --- a/packages/runtime-dom/__tests__/directives/vOn.spec.ts +++ b/packages/runtime-dom/__tests__/directives/vOn.spec.ts @@ -40,7 +40,7 @@ describe('runtime-dom: v-on directive', () => { expect(fn).not.toBeCalled() }) - test('it should support key modifiers and system modifiers', () => { + test('it should support key modifiers and system modifiers - ctrl', () => { const el = document.createElement('div') const fn = jest.fn() //
@@ -72,6 +72,102 @@ describe('runtime-dom: v-on directive', () => { expect(fn).toBeCalledTimes(2) }) + test('it should support key modifiers and system modifiers - shift', () => { + const el = document.createElement('div') + const fn = jest.fn() + // + const nextValue = withKeys(withModifiers(fn, ['shift']), [ + '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.shiftKey = false + e.key = 'esc' + }) + expect(fn).not.toBeCalled() + + triggerEvent(el, 'keyup', e => { + e.shiftKey = true + e.key = 'Escape' + }) + expect(fn).toBeCalledTimes(1) + + triggerEvent(el, 'keyup', e => { + e.shiftKey = true + e.key = 'ArrowLeft' + }) + expect(fn).toBeCalledTimes(2) + }) + + test('it should support key modifiers and system modifiers - alt', () => { + const el = document.createElement('div') + const fn = jest.fn() + // + const nextValue = withKeys(withModifiers(fn, ['alt']), [ + '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.altKey = false + e.key = 'esc' + }) + expect(fn).not.toBeCalled() + + triggerEvent(el, 'keyup', e => { + e.altKey = true + e.key = 'Escape' + }) + expect(fn).toBeCalledTimes(1) + + triggerEvent(el, 'keyup', e => { + e.altKey = true + e.key = 'ArrowLeft' + }) + expect(fn).toBeCalledTimes(2) + }) + + test('it should support key modifiers and system modifiers - meta', () => { + const el = document.createElement('div') + const fn = jest.fn() + // + const nextValue = withKeys(withModifiers(fn, ['meta']), [ + '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.metaKey = false + e.key = 'esc' + }) + expect(fn).not.toBeCalled() + + triggerEvent(el, 'keyup', e => { + e.metaKey = true + e.key = 'Escape' + }) + expect(fn).toBeCalledTimes(1) + + triggerEvent(el, 'keyup', e => { + e.metaKey = true + e.key = 'ArrowLeft' + }) + expect(fn).toBeCalledTimes(2) + }) + test('it should support "exact" modifier', () => { const el = document.createElement('div') // Case 1: From 807a6e4f71a421f98ff7176c1cf34dab1b538062 Mon Sep 17 00:00:00 2001 From: motao314 <663952074@qq.com> Date: Fri, 17 Jul 2020 11:21:21 +0800 Subject: [PATCH 2/4] Perfect shortcut key test --- .../__tests__/directives/vOn.spec.ts | 158 ++++-------------- 1 file changed, 32 insertions(+), 126 deletions(-) diff --git a/packages/runtime-dom/__tests__/directives/vOn.spec.ts b/packages/runtime-dom/__tests__/directives/vOn.spec.ts index fcd26c38ac4..63e0c92cc55 100644 --- a/packages/runtime-dom/__tests__/directives/vOn.spec.ts +++ b/packages/runtime-dom/__tests__/directives/vOn.spec.ts @@ -41,133 +41,39 @@ describe('runtime-dom: v-on directive', () => { }) test('it should support key modifiers and system modifiers - ctrl', () => { - const el = document.createElement('div') - const fn = jest.fn() - // - 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() - - 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) - }) - - test('it should support key modifiers and system modifiers - shift', () => { - const el = document.createElement('div') - const fn = jest.fn() - // - const nextValue = withKeys(withModifiers(fn, ['shift']), [ - '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.shiftKey = false - e.key = 'esc' - }) - expect(fn).not.toBeCalled() - - triggerEvent(el, 'keyup', e => { - e.shiftKey = true - e.key = 'Escape' - }) - expect(fn).toBeCalledTimes(1) - - triggerEvent(el, 'keyup', e => { - e.shiftKey = true - e.key = 'ArrowLeft' - }) - expect(fn).toBeCalledTimes(2) - }) - - test('it should support key modifiers and system modifiers - alt', () => { - const el = document.createElement('div') - const fn = jest.fn() - // - const nextValue = withKeys(withModifiers(fn, ['alt']), [ - '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.altKey = false - e.key = 'esc' - }) - expect(fn).not.toBeCalled() - - triggerEvent(el, 'keyup', e => { - e.altKey = true - e.key = 'Escape' - }) - expect(fn).toBeCalledTimes(1) - - triggerEvent(el, 'keyup', e => { - e.altKey = true - e.key = 'ArrowLeft' - }) - expect(fn).toBeCalledTimes(2) - }) - - test('it should support key modifiers and system modifiers - meta', () => { - const el = document.createElement('div') - const fn = jest.fn() - // - const nextValue = withKeys(withModifiers(fn, ['meta']), [ - '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.metaKey = false - e.key = 'esc' - }) - expect(fn).not.toBeCalled() - - triggerEvent(el, 'keyup', e => { - e.metaKey = true - e.key = 'Escape' - }) - expect(fn).toBeCalledTimes(1) - - triggerEvent(el, 'keyup', e => { - e.metaKey = true - e.key = 'ArrowLeft' - }) - expect(fn).toBeCalledTimes(2) + const keyNames = ["ctrl","shift","meta","alt"]; + keyNames.forEach(keyName=>{ + const el = document.createElement('div') + const fn = jest.fn() + // + 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', () => { const el = document.createElement('div') // Case 1: From 1fd5cce94f72463078964967eae5c4985adb31fc Mon Sep 17 00:00:00 2001 From: motao314 <663952074@qq.com> Date: Fri, 17 Jul 2020 11:22:21 +0800 Subject: [PATCH 3/4] Perfect shortcut key test --- packages/runtime-dom/__tests__/directives/vOn.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime-dom/__tests__/directives/vOn.spec.ts b/packages/runtime-dom/__tests__/directives/vOn.spec.ts index 63e0c92cc55..093ca57daf3 100644 --- a/packages/runtime-dom/__tests__/directives/vOn.spec.ts +++ b/packages/runtime-dom/__tests__/directives/vOn.spec.ts @@ -40,7 +40,7 @@ describe('runtime-dom: v-on directive', () => { expect(fn).not.toBeCalled() }) - test('it should support key modifiers and system modifiers - ctrl', () => { + test('it should support key modifiers and system modifiers', () => { const keyNames = ["ctrl","shift","meta","alt"]; keyNames.forEach(keyName=>{ const el = document.createElement('div') From 59cd84ac7d23905d02318c053189ebafd9369a4c Mon Sep 17 00:00:00 2001 From: Evan You