From 8a9925215ea7f745eff360292a90ae275d29382f Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Sat, 7 Apr 2018 13:20:04 +0800 Subject: [PATCH] Format test case code. --- test/run.test.js | 285 ++++++++++++++++++++++++----------------------- 1 file changed, 144 insertions(+), 141 deletions(-) diff --git a/test/run.test.js b/test/run.test.js index 8582d62b..f932ab30 100644 --- a/test/run.test.js +++ b/test/run.test.js @@ -1,6 +1,7 @@ const puppeteer = require('puppeteer'); const path = require('path'); const hotkeys = require('../dist/hotkeys'); + let browser; let page; @@ -8,52 +9,56 @@ let page; // expect().toBeFalsy() :判断结果是否为假。 // expect().toBeTruthy() :判断结果是否为真。 -var isff = navigator.userAgent.toLowerCase().indexOf('firefox') > 0; +const isff = navigator.userAgent.toLowerCase().indexOf('firefox') > 0; // 模拟键盘摁键 // http://output.jsbin.com/awenaq/3 function __triggerKeyboardEvent(el, keyCode, opt) { - var eventObj = document.createEventObject ? - document.createEventObject() : document.createEvent("Events"); + const eventObj = document.createEventObject ? + document.createEventObject() : document.createEvent('Events'); if (eventObj.initEvent) { - eventObj.initEvent("keydown", true, true); + eventObj.initEvent('keydown', true, true); } if (keyCode) { eventObj.keyCode = keyCode; eventObj.which = keyCode; } if (opt) { - for (var a in opt) { - eventObj[a] = opt[a]; + for (const a in opt) { + if (Object.prototype.hasOwnProperty.call(opt, a)) { + eventObj[a] = opt[a]; + } } } - el.dispatchEvent ? el.dispatchEvent(eventObj) : el.fireEvent("onkeydown", eventObj); + el.dispatchEvent ? el.dispatchEvent(eventObj) : el.fireEvent('onkeydown', eventObj); } function __triggerKeyboardUp(el, keyCode, opt) { - var eventObj = document.createEventObject ? - document.createEventObject() : document.createEvent("Events"); + const eventObj = document.createEventObject ? + document.createEventObject() : document.createEvent('Events'); if (eventObj.initEvent) { - eventObj.initEvent("keyup", true, true); + eventObj.initEvent('keyup', true, true); } if (keyCode) { eventObj.keyCode = keyCode; eventObj.which = keyCode; } if (opt) { - for (var a in opt) { - eventObj[a] = opt[a]; + for (const a in opt) { + if (Object.prototype.hasOwnProperty.call(opt, a)) { + eventObj[a] = opt[a]; + } } } - el.dispatchEvent ? el.dispatchEvent(eventObj) : el.fireEvent("onkeyup", eventObj); + el.dispatchEvent ? el.dispatchEvent(eventObj) : el.fireEvent('onkeyup', eventObj); } beforeAll(async () => { - browser = await puppeteer.launch({ args: ['--no-sandbox']}); + browser = await puppeteer.launch({ args: ['--no-sandbox'] }); page = await browser.newPage(); -}) +}); describe('\n Hotkeys.js Test Case.\n', () => { test('HTML loader', async () => { - await page.goto('file://' + path.resolve('./test/index.html'), { waitUntil: 'networkidle2' }); + await page.goto(`file://${path.resolve('./test/index.html')}`, { waitUntil: 'networkidle2' }); }, 10000); test('Test HTML load', async () => { @@ -62,244 +67,244 @@ describe('\n Hotkeys.js Test Case.\n', () => { const text = await page.$eval('#root', el => el.textContent); expect(text).toBe('hotkeys'); expect(window.hotkeys).toBeTruthy(); - }) + }); test('HotKeys getPressedKeyCodes Test Case', async () => { - await hotkeys('command+ctrl+shift+a', function (e) { + await hotkeys('command+ctrl+shift+a', (e) => { expect(e.metaKey).toBeTruthy(); expect(e.ctrlKey).toBeTruthy(); expect(e.shiftKey).toBeTruthy(); expect(hotkeys.getPressedKeyCodes()[0]).toBe(65); - }) + }); await __triggerKeyboardEvent(document.body, 65, { metaKey: true, ctrlKey: true, shiftKey: true, }); - await hotkeys.unbind("command+ctrl+shift+a"); - }) + await hotkeys.unbind('command+ctrl+shift+a'); + }); test('HotKeys unbind Test Case', async () => { - hotkeys('enter', function (e) { + hotkeys('enter', (e) => { expect(e.keyCode).toBe(13); }); expect(hotkeys.unbind()).toBe(undefined); expect(hotkeys.unbind('enter')).toBe(undefined); expect(hotkeys.unbind('enter12')).toBe(undefined); + }); - }) test('HotKeys Special keys Test Case', async () => { - hotkeys('enter', function (e) { + hotkeys('enter', (e) => { expect(e.keyCode).toBe(13); }); - hotkeys('return', function (e) { + hotkeys('return', (e) => { expect(e.keyCode).toBe(13); }); __triggerKeyboardEvent(document.body, 13); - __triggerKeyboardUp(document.body, 13) - hotkeys.unbind("return"); - hotkeys.unbind("enter"); + __triggerKeyboardUp(document.body, 13); + hotkeys.unbind('return'); + hotkeys.unbind('enter'); - hotkeys('space', function (e) { + hotkeys('space', (e) => { expect(e.keyCode).toBe(32); }); __triggerKeyboardEvent(document.body, 32); __triggerKeyboardUp(document.body, 32); - hotkeys.unbind("space"); + hotkeys.unbind('space'); - hotkeys('insert,ins', function (e) { + hotkeys('insert,ins', (e) => { expect(e.keyCode).toBe(45); }); __triggerKeyboardEvent(document.body, 45); __triggerKeyboardUp(document.body, 45); - hotkeys.unbind("insert"); - hotkeys.unbind("ins"); + hotkeys.unbind('insert'); + hotkeys.unbind('ins'); - hotkeys('backspace', function (e) { + hotkeys('backspace', (e) => { expect(e.keyCode).toBe(8); }); __triggerKeyboardEvent(document.body, 8); __triggerKeyboardUp(document.body, 8); - hotkeys.unbind("backspace"); + hotkeys.unbind('backspace'); - hotkeys('tab', function (e) { + hotkeys('tab', (e) => { expect(e.keyCode).toBe(9); }); __triggerKeyboardEvent(document.body, 9); __triggerKeyboardUp(document.body, 9); - hotkeys.unbind("tab"); + hotkeys.unbind('tab'); - hotkeys('clear', function (e) { + hotkeys('clear', (e) => { expect(e.keyCode).toBe(12); }); __triggerKeyboardEvent(document.body, 12); __triggerKeyboardUp(document.body, 12); - hotkeys.unbind("clear"); + hotkeys.unbind('clear'); - hotkeys(',', function (e) { + hotkeys(',', (e) => { expect(e.keyCode).toBe(188); }); __triggerKeyboardEvent(document.body, 188); __triggerKeyboardUp(document.body, 188); - hotkeys.unbind(","); + hotkeys.unbind(','); - hotkeys('.', function (e) { + hotkeys('.', (e) => { expect(e.keyCode).toBe(190); }); __triggerKeyboardEvent(document.body, 190); __triggerKeyboardUp(document.body, 190); - hotkeys.unbind("."); + hotkeys.unbind('.'); - hotkeys('/', function (e) { + hotkeys('/', (e) => { expect(e.keyCode).toBe(191); }); __triggerKeyboardEvent(document.body, 191); __triggerKeyboardUp(document.body, 191); - hotkeys.unbind("/"); + hotkeys.unbind('/'); - hotkeys('`', function (e) { + hotkeys('`', (e) => { expect(e.keyCode).toBe(192); }); __triggerKeyboardEvent(document.body, 192); __triggerKeyboardUp(document.body, 192); - hotkeys.unbind("`"); + hotkeys.unbind('`'); - hotkeys('-', function (e) { + hotkeys('-', (e) => { expect(e.keyCode).toBe(isff ? 173 : 189); }); __triggerKeyboardEvent(document.body, isff ? 173 : 189); __triggerKeyboardUp(document.body, isff ? 173 : 189); - hotkeys.unbind("-"); + hotkeys.unbind('-'); - hotkeys('=', function (e) { + hotkeys('=', (e) => { expect(e.keyCode).toBe(isff ? 61 : 187); }); __triggerKeyboardEvent(document.body, isff ? 61 : 187); - hotkeys.unbind("="); + hotkeys.unbind('='); - hotkeys(';', function (e) { + hotkeys(';', (e) => { expect(e.keyCode).toBe(isff ? 59 : 186); }); __triggerKeyboardEvent(document.body, isff ? 59 : 186); - hotkeys.unbind(";"); + hotkeys.unbind(';'); - hotkeys("\'".toString(), function (e) { + hotkeys('\''.toString(), (e) => { expect(e.keyCode).toBe(222); }); __triggerKeyboardEvent(document.body, 222); - hotkeys.unbind("\'"); + hotkeys.unbind('\''); - hotkeys("\\".toString(), function (e) { + hotkeys('\\'.toString(), (e) => { expect(e.keyCode).toBe(220); }); __triggerKeyboardEvent(document.body, 220); - hotkeys.unbind("\\"); + hotkeys.unbind('\\'); - hotkeys("[".toString(), function (e) { + hotkeys('['.toString(), (e) => { expect(e.keyCode).toBe(219); }); __triggerKeyboardEvent(document.body, 219); - hotkeys.unbind("["); + hotkeys.unbind('['); - hotkeys("]".toString(), function (e) { + hotkeys(']'.toString(), (e) => { expect(e.keyCode).toBe(221); }); __triggerKeyboardEvent(document.body, 221); - hotkeys.unbind("]"); + hotkeys.unbind(']'); - hotkeys('left', function (e) { + hotkeys('left', (e) => { expect(e.keyCode).toBe(37); }); __triggerKeyboardEvent(document.body, 37); - hotkeys.unbind("left"); + hotkeys.unbind('left'); - hotkeys('up', function (e) { + hotkeys('up', (e) => { expect(e.keyCode).toBe(38); }); __triggerKeyboardEvent(document.body, 38); - hotkeys.unbind("up"); + hotkeys.unbind('up'); - hotkeys('del', function (e) { + hotkeys('del', (e) => { expect(e.keyCode).toBe(46); }); - hotkeys('delete', function (e) { + hotkeys('delete', (e) => { expect(e.keyCode).toBe(46); }); __triggerKeyboardEvent(document.body, 46); - hotkeys.unbind("delete"); - hotkeys.unbind("del"); + hotkeys.unbind('delete'); + hotkeys.unbind('del'); - hotkeys('home', function (e) { + hotkeys('home', (e) => { expect(e.keyCode).toBe(36); }); __triggerKeyboardEvent(document.body, 36); - hotkeys.unbind("home"); + hotkeys.unbind('home'); - hotkeys('pageup', function (e) { + hotkeys('pageup', (e) => { expect(e.keyCode).toBe(33); }); __triggerKeyboardEvent(document.body, 33); - hotkeys.unbind("pageup"); + hotkeys.unbind('pageup'); - hotkeys('pagedown', function (e) { + hotkeys('pagedown', (e) => { expect(e.keyCode).toBe(34); }); __triggerKeyboardEvent(document.body, 34); - hotkeys.unbind("pagedown"); + hotkeys.unbind('pagedown'); - hotkeys('end', function (e) { + hotkeys('end', (e) => { expect(e.keyCode).toBe(35); }); __triggerKeyboardEvent(document.body, 35); - hotkeys.unbind("end"); + hotkeys.unbind('end'); - hotkeys('right', function (e) { + hotkeys('right', (e) => { expect(e.keyCode).toBe(39); }); __triggerKeyboardEvent(document.body, 39); - hotkeys.unbind("right"); + hotkeys.unbind('right'); - hotkeys('down', function (e) { + hotkeys('down', (e) => { expect(e.keyCode).toBe(40); }); __triggerKeyboardEvent(document.body, 40); - hotkeys.unbind("down"); + hotkeys.unbind('down'); - hotkeys('esc', function (e) { + hotkeys('esc', (e) => { expect(e.keyCode).toBe(27); }); - hotkeys('escape', function (e) { + hotkeys('escape', (e) => { expect(e.keyCode).toBe(27); }); __triggerKeyboardEvent(document.body, 27); - hotkeys.unbind("esc"); - hotkeys.unbind("escape"); + hotkeys.unbind('esc'); + hotkeys.unbind('escape'); - hotkeys('CapsLock', function (e) { + hotkeys('CapsLock', (e) => { expect(e.keyCode).toBe(20); }); - hotkeys('⇪', function (e) { + hotkeys('⇪', (e) => { expect(e.keyCode).toBe(20); }); __triggerKeyboardEvent(document.body, 20); - hotkeys.unbind("⇪"); - hotkeys.unbind("CapsLock"); - }) + hotkeys.unbind('⇪'); + hotkeys.unbind('CapsLock'); + }); test('HotKeys Test Case', async () => { - hotkeys('w', (e) => { + hotkeys('w', (e) => { expect(e.keyCode).toBe(87); }); __triggerKeyboardEvent(document.body, 87); - hotkeys.unbind("w"); + hotkeys.unbind('w'); - hotkeys('b', (e) => { + hotkeys('b', (e) => { expect(e.keyCode).toBe(66); }); __triggerKeyboardEvent(document.body, 66); - hotkeys.unbind("b"); + hotkeys.unbind('b'); await hotkeys('a', async () => { await expect(hotkeys.isPressed('a')).toBeTruthy(); @@ -307,106 +312,105 @@ describe('\n Hotkeys.js Test Case.\n', () => { await expect(hotkeys.isPressed(65)).toBeTruthy(); }); __triggerKeyboardEvent(document.body, 65); - hotkeys.unbind("a"); - }) + hotkeys.unbind('a'); + }); test('HotKeys Key combination Test Case', async () => { - - hotkeys('⌘+d', function (e) { + hotkeys('⌘+d', (e) => { expect(e.keyCode).toBe(82); expect(e.metaKey).toBeTruthy(); return false; }); __triggerKeyboardEvent(document.body, 82, { - metaKey: true + metaKey: true, }); - hotkeys('alt+d', function (e) { + hotkeys('alt+d', (e) => { expect(e.keyCode).toBe(68); expect(e.altKey).toBeTruthy(); }); __triggerKeyboardEvent(document.body, 68, { - altKey: true + altKey: true, }); - hotkeys('shift+a', function (e) { + hotkeys('shift+a', (e) => { expect(e.keyCode).toBe(65); expect(e.shiftKey).toBeTruthy(); }); __triggerKeyboardEvent(document.body, 65, { - shiftKey: true + shiftKey: true, }); - hotkeys('⇧+a', function (e) { + hotkeys('⇧+a', (e) => { expect(e.keyCode).toBe(65); expect(e.shiftKey).toBeTruthy(); }); __triggerKeyboardEvent(document.body, 65, { - shiftKey: true + shiftKey: true, }); - hotkeys('⌘+a', function (e) { + hotkeys('⌘+a', (e) => { expect(e.keyCode).toBe(65); expect(e.metaKey).toBeTruthy(); }); __triggerKeyboardEvent(document.body, 65, { - metaKey: true + metaKey: true, }); - hotkeys.unbind("⌘+a"); + hotkeys.unbind('⌘+a'); - hotkeys('⌃+a', function (e) { + hotkeys('⌃+a', (e) => { expect(e.keyCode).toBe(65); expect(e.ctrlKey).toBeTruthy(); }); __triggerKeyboardEvent(document.body, 65, { - ctrlKey: true + ctrlKey: true, }); - hotkeys.unbind("⌃+a") + hotkeys.unbind('⌃+a'); - hotkeys('⌥+a', function (e) { + hotkeys('⌥+a', (e) => { expect(e.keyCode).toBe(65); expect(e.altKey).toBeTruthy(); }); __triggerKeyboardEvent(document.body, 65, { - altKey: true + altKey: true, }); - hotkeys('ctrl+,,ctrl+d', function (e) { + hotkeys('ctrl+,,ctrl+d', (e) => { expect(e.keyCode).toBe(188); expect(e.ctrlKey).toBeTruthy(); }); __triggerKeyboardEvent(document.body, 188, { - ctrlKey: true + ctrlKey: true, }); - hotkeys.unbind("ctrl+,,ctrl+d") + hotkeys.unbind('ctrl+,,ctrl+d'); - hotkeys('Ctrl+A', function (e) { + hotkeys('Ctrl+A', (e) => { expect(e.keyCode).toBe(65); expect(e.ctrlKey).toBeTruthy(); }); __triggerKeyboardEvent(document.body, 65, { - ctrlKey: true + ctrlKey: true, }); - hotkeys.unbind("Ctrl+A") + hotkeys.unbind('Ctrl+A'); - hotkeys('CTRL+A', function (e) { + hotkeys('CTRL+A', (e) => { expect(e.keyCode).toBe(65); expect(e.ctrlKey).toBeTruthy(); }); __triggerKeyboardEvent(document.body, 65, { - ctrlKey: true + ctrlKey: true, }); - hotkeys.unbind("CTRL+A") + hotkeys.unbind('CTRL+A'); - hotkeys('⌃+a', function (e) { + hotkeys('⌃+a', (e) => { expect(e.keyCode).toBe(65); expect(hotkeys.getScope()).toBe('all'); }); __triggerKeyboardEvent(document.body, 65, { - ctrlKey: true + ctrlKey: true, }); - hotkeys.unbind("⌃+a"); - }) + hotkeys.unbind('⌃+a'); + }); // const _modifier = { //修饰键 // '⇧': 16, shift: 16, @@ -417,20 +421,20 @@ describe('\n Hotkeys.js Test Case.\n', () => { test('HotKeys modifier key ⌘,cmd,command Test Case', async () => { await __triggerKeyboardEvent(document.body, 65, { - shiftKey: true + shiftKey: true, }); await hotkeys.unbind('shift+a'); }); test('HotKeys modifier key ⌘,cmd,command Test Case', async () => { // left key - await hotkeys('*', function (e) { + await hotkeys('*', (e) => { expect(e.keyCode).toBe(isff ? 224 : 91); }); await __triggerKeyboardEvent(document.body, isff ? 224 : 91); await hotkeys.unbind('*'); // right key - await hotkeys('*', function (e) { + await hotkeys('*', (e) => { expect(e.keyCode).toBe(isff ? 224 : 93); }); await __triggerKeyboardEvent(document.body, isff ? 224 : 93); @@ -438,7 +442,7 @@ describe('\n Hotkeys.js Test Case.\n', () => { }); test('HotKeys modifier key ⌃,ctrl,control Test Case', async () => { - await hotkeys('*', function (e) { + await hotkeys('*', (e) => { expect(e.keyCode).toBe(17); }); await __triggerKeyboardEvent(document.body, 17); @@ -446,7 +450,7 @@ describe('\n Hotkeys.js Test Case.\n', () => { }); test('HotKeys modifier key ⌥,alt,option Test Case', async () => { - await hotkeys('*', function (e) { + await hotkeys('*', (e) => { expect(e.keyCode).toBe(18); }); await __triggerKeyboardEvent(document.body, 18); @@ -454,7 +458,7 @@ describe('\n Hotkeys.js Test Case.\n', () => { }); test('HotKeys modifier key ⇧,shift Test Case', async () => { - await hotkeys('*', function (e) { + await hotkeys('*', (e) => { expect(e.keyCode).toBe(16); }); await __triggerKeyboardEvent(document.body, 16); @@ -462,7 +466,6 @@ describe('\n Hotkeys.js Test Case.\n', () => { }); test('HotKeys modifier scope,setScope,getScope,deleteScope Test Case', async () => { - await hotkeys('⌃+a', 'scope1', async (e) => { expect(e.keyCode).toBe(65); expect(hotkeys.getScope()).toBe('scope1'); @@ -470,7 +473,7 @@ describe('\n Hotkeys.js Test Case.\n', () => { }); await hotkeys.setScope('scope1'); await __triggerKeyboardEvent(document.body, 65, { - ctrlKey: true + ctrlKey: true, }); await hotkeys('⌃+a', 'scope2', async (e) => { @@ -480,7 +483,7 @@ describe('\n Hotkeys.js Test Case.\n', () => { }); await hotkeys.setScope('scope2'); await __triggerKeyboardEvent(document.body, 65, { - ctrlKey: true + ctrlKey: true, }); await hotkeys('⌃+a', 'scope3', async (e) => { @@ -490,14 +493,14 @@ describe('\n Hotkeys.js Test Case.\n', () => { }); await hotkeys.setScope('scope3'); await __triggerKeyboardEvent(document.body, 65, { - ctrlKey: true + ctrlKey: true, }); expect(hotkeys.getScope()).toBe('all'); - }) + }); test('HotKeys modifier noConflict Test Case', async () => { const keys = await hotkeys.noConflict(true); - await keys('a', function (e) { + await keys('a', (e) => { expect(e.keyCode).toBe(65); expect(e.which).toBe(65); }); @@ -505,6 +508,6 @@ describe('\n Hotkeys.js Test Case.\n', () => { }); afterAll(async () => { - await browser.close() - }) -}) + await browser.close(); + }); +});