Debian Codegraph @

packages/keyboard/tests/src/index.spec.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 /*-----------------------------------------------------------------------------
4 | Copyright (c) 2014-2017, PhosphorJS Contributors
5 |
6 | Distributed under the terms of the BSD 3-Clause License.
7 |
8 | The full license is in the file LICENSE, distributed with this software.
9 |----------------------------------------------------------------------------*/
10 import { expect } from 'chai';
11
12 import {
13 EN_US,
14 getKeyboardLayout,
15 KeycodeLayout,
16 setKeyboardLayout
17 } from '@lumino/keyboard';
18
19 describe('@lumino/keyboard', () => {
20 describe('getKeyboardLayout()', () => {
21 it('should return the global keyboard layout', () => {
22 expect(getKeyboardLayout()).to.equal(EN_US);
23 });
24 });
25
26 describe('setKeyboardLayout()', () => {
27 it('should set the global keyboard layout', () => {
28 let layout = new KeycodeLayout('ab-cd', {});
29 setKeyboardLayout(layout);
30 expect(getKeyboardLayout()).to.equal(layout);
31 setKeyboardLayout(EN_US);
32 expect(getKeyboardLayout()).to.equal(EN_US);
33 });
34 });
35
36 describe('KeycodeLayout', () => {
37 describe('#constructor()', () => {
38 it('should construct a new keycode layout', () => {
39 let layout = new KeycodeLayout('ab-cd', {});
40 expect(layout).to.be.an.instanceof(KeycodeLayout);
41 });
42 });
43
44 describe('#name', () => {
45 it('should be a human readable name of the layout', () => {
46 let layout = new KeycodeLayout('ab-cd', {});
47 expect(layout.name).to.equal('ab-cd');
48 });
49 });
50
51 describe('#keys()', () => {
52 it('should get an array of all key values supported by the layout', () => {
53 let layout = new KeycodeLayout('ab-cd', { 100: 'F' });
54 let keys = layout.keys();
55 expect(keys.length).to.equal(1);
56 expect(keys[0]).to.equal('F');
57 });
58 });
59
60 describe('#isValidKey()', () => {
61 it('should test whether the key is valid for the layout', () => {
62 let layout = new KeycodeLayout('foo', { 100: 'F' });
63 expect(layout.isValidKey('F')).to.equal(true);
64 expect(layout.isValidKey('A')).to.equal(false);
65 });
66
67 it('should treat modifier keys as valid', () => {
68 let layout = new KeycodeLayout('foo', { 100: 'F', 101: 'A' }, ['A']);
69 expect(layout.isValidKey('A')).to.equal(true);
70 });
71 });
72
73 describe('#isModifierKey()', () => {
74 it('should test whether the key is modifier for the layout', () => {
75 let layout = new KeycodeLayout('foo', { 100: 'F', 101: 'A' }, ['A']);
76 expect(layout.isModifierKey('F')).to.equal(false);
77 expect(layout.isModifierKey('A')).to.equal(true);
78 });
79
80 it('should return false for keys that are not in the layout', () => {
81 let layout = new KeycodeLayout('foo', { 100: 'F', 101: 'A' }, ['A']);
82 expect(layout.isModifierKey('B')).to.equal(false);
83 });
84 });
85
86 describe('#keyForKeydownEvent()', () => {
87 it('should get the key for a `keydown` event', () => {
88 let layout = new KeycodeLayout('foo', { 100: 'F' });
89 let event = new KeyboardEvent('keydown', { keyCode: 100 });
90 let key = layout.keyForKeydownEvent(event as KeyboardEvent);
91 expect(key).to.equal('F');
92 });
93
94 it('should return an empty string if the code is not valid', () => {
95 let layout = new KeycodeLayout('foo', { 100: 'F' });
96 let event = new KeyboardEvent('keydown', { keyCode: 101 });
97 let key = layout.keyForKeydownEvent(event as KeyboardEvent);
98 expect(key).to.equal('');
99 });
100 });
101
102 describe('.extractKeys()', () => {
103 it('should extract the keys from a code map', () => {
104 let keys: KeycodeLayout.CodeMap = { 70: 'F', 71: 'G', 72: 'H' };
105 let goal: KeycodeLayout.KeySet = { F: true, G: true, H: true };
106 expect(KeycodeLayout.extractKeys(keys)).to.deep.equal(goal);
107 });
108 });
109
110 describe('.convertToKeySet()', () => {
111 it('should convert key array to key set', () => {
112 let keys: string[] = ['F', 'G', 'H'];
113 let goal: KeycodeLayout.KeySet = { F: true, G: true, H: true };
114 expect(KeycodeLayout.convertToKeySet(keys)).to.deep.equal(goal);
115 });
116 });
117 });
118
119 describe('EN_US', () => {
120 it('should be a keycode layout', () => {
121 expect(EN_US).to.be.an.instanceof(KeycodeLayout);
122 });
123
124 it('should have standardized keys', () => {
125 expect(EN_US.isValidKey('A')).to.equal(true);
126 expect(EN_US.isValidKey('Z')).to.equal(true);
127 expect(EN_US.isValidKey('0')).to.equal(true);
128 expect(EN_US.isValidKey('a')).to.equal(false);
129 });
130
131 it('should have modifier keys', () => {
132 expect(EN_US.isValidKey('Shift')).to.equal(true);
133 expect(EN_US.isValidKey('Ctrl')).to.equal(true);
134 expect(EN_US.isValidKey('Alt')).to.equal(true);
135 expect(EN_US.isValidKey('Meta')).to.equal(true);
136 });
137
138 it('should correctly detect modifier keys', () => {
139 expect(EN_US.isModifierKey('Shift')).to.equal(true);
140 expect(EN_US.isModifierKey('Ctrl')).to.equal(true);
141 expect(EN_US.isModifierKey('Alt')).to.equal(true);
142 expect(EN_US.isModifierKey('Meta')).to.equal(true);
143 });
144 });
145 });