From 4e94695c758215a950fe53911e1c1e30e47b9c98 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 4 Nov 2017 23:55:09 +0900 Subject: add key utils --- test/shared/utils/keys.test.js | 133 +++++++++++++++++++++++++++++++++++++++++ test/shared/utils/re.test.js | 20 +++++++ 2 files changed, 153 insertions(+) create mode 100644 test/shared/utils/keys.test.js create mode 100644 test/shared/utils/re.test.js (limited to 'test/shared/utils') diff --git a/test/shared/utils/keys.test.js b/test/shared/utils/keys.test.js new file mode 100644 index 0000000..77e2b12 --- /dev/null +++ b/test/shared/utils/keys.test.js @@ -0,0 +1,133 @@ +import { expect } from 'chai'; +import * as keys from 'shared/utils/keys'; + +describe("keys util", () => { + describe('fromKeyboardEvent', () => { + it('returns from keyboard input Ctrl+X', () => { + let k = keys.fromKeyboardEvent({ + key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true + }); + expect(k.key).to.equal('x'); + expect(k.shiftKey).to.be.false; + expect(k.ctrlKey).to.be.true; + expect(k.altKey).to.be.false; + expect(k.metaKey).to.be.true; + }); + + it('returns from keyboard input Shift+Esc', () => { + let k = keys.fromKeyboardEvent({ + key: 'Escape', shiftKey: true, ctrlKey: false, altKey: false, metaKey: true + }); + expect(k.key).to.equal('Esc'); + expect(k.shiftKey).to.be.true; + expect(k.ctrlKey).to.be.false; + expect(k.altKey).to.be.false; + expect(k.metaKey).to.be.true; + }); + }); + + describe('fromMapKey', () => { + it('return for X', () => { + let key = keys.fromMapKey('x'); + expect(key.key).to.equal('x'); + expect(key.shiftKey).to.be.false; + expect(key.ctrlKey).to.be.false; + expect(key.altKey).to.be.false; + expect(key.metaKey).to.be.false; + }); + + it('return for Shift+X', () => { + let key = keys.fromMapKey('X'); + expect(key.key).to.equal('X'); + expect(key.shiftKey).to.be.true; + expect(key.ctrlKey).to.be.false; + expect(key.altKey).to.be.false; + expect(key.metaKey).to.be.false; + }); + + it('return for Ctrl+X', () => { + let key = keys.fromMapKey(''); + expect(key.key).to.equal('x'); + expect(key.shiftKey).to.be.false; + expect(key.ctrlKey).to.be.true; + expect(key.altKey).to.be.false; + expect(key.metaKey).to.be.false; + }); + + it('returns for Ctrl+Meta+X', () => { + let key = keys.fromMapKey(''); + expect(key.key).to.equal('x'); + expect(key.shiftKey).to.be.false; + expect(key.ctrlKey).to.be.true; + expect(key.altKey).to.be.false; + expect(key.metaKey).to.be.true; + }); + + it('returns for Ctrl+Shift+x', () => { + let key = keys.fromMapKey(''); + expect(key.key).to.equal('X'); + expect(key.shiftKey).to.be.true; + expect(key.ctrlKey).to.be.true; + expect(key.altKey).to.be.false; + expect(key.metaKey).to.be.false; + }); + + it('returns for Shift+Esc', () => { + let key = keys.fromMapKey(''); + expect(key.key).to.equal('Esc'); + expect(key.shiftKey).to.be.true; + expect(key.ctrlKey).to.be.false; + expect(key.altKey).to.be.false; + expect(key.metaKey).to.be.false; + }); + + it('returns for Ctrl+Esc', () => { + let key = keys.fromMapKey(''); + expect(key.key).to.equal('Esc'); + expect(key.shiftKey).to.be.false; + expect(key.ctrlKey).to.be.true; + expect(key.altKey).to.be.false; + expect(key.metaKey).to.be.false; + }); + }); + + describe('fromMapKeys', () => { + it('returns mapped keys for Shift+Esc', () => { + let keyArray = keys.fromMapKeys(''); + expect(keyArray).to.have.lengthOf(1); + expect(keyArray[0].key).to.equal('Esc'); + expect(keyArray[0].shiftKey).to.be.true; + }); + + it('returns mapped keys for ad', () => { + let keyArray = keys.fromMapKeys('ad'); + expect(keyArray).to.have.lengthOf(5); + expect(keyArray[0].key).to.equal('a'); + expect(keyArray[1].ctrlKey).to.be.true; + expect(keyArray[1].key).to.equal('b'); + expect(keyArray[2].altKey).to.be.true; + expect(keyArray[2].key).to.equal('c'); + expect(keyArray[3].key).to.equal('d'); + expect(keyArray[4].metaKey).to.be.true; + expect(keyArray[4].key).to.equal('e'); + }); + }) + + describe('equals', () => { + expect(keys.equals({ + key: 'x', + ctrlKey: true, + }, { + key: 'x', + ctrlKey: true, + })).to.be.true; + + expect(keys.equals({ + key: 'X', + shiftKey: true, + }, { + key: 'x', + ctrlKey: true, + })).to.be.false; + }); +}); diff --git a/test/shared/utils/re.test.js b/test/shared/utils/re.test.js new file mode 100644 index 0000000..9ed6521 --- /dev/null +++ b/test/shared/utils/re.test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai'; +import * as re from 'shared/utils/re'; + +describe("re util", () => { + it('matches by pattern', () => { + let regex = re.fromWildcard('*.example.com/*'); + expect('foo.example.com/bar').to.match(regex); + expect('foo.example.com').not.to.match(regex); + expect('example.com/bar').not.to.match(regex); + + regex = re.fromWildcard('example.com/*') + expect('example.com/foo').to.match(regex); + expect('example.com/').to.match(regex); + + regex = re.fromWildcard('example.com/*bar') + expect('example.com/foobar').to.match(regex); + expect('example.com/bar').to.match(regex); + expect('example.com/foobarfoo').not.to.match(regex); + }) +}); -- cgit v1.2.3