@cxl/keyboard 0.1.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test.js DELETED
@@ -1,151 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const spec_1 = require("@cxl/spec");
4
- const index_js_1 = require("@cxl/keyboard");
5
- exports.default = (0, spec_1.spec)('keyboard', s => {
6
- s.test('normalize', it => {
7
- it.should('handle empty and null inputs', a => {
8
- a.equal((0, index_js_1.normalize)(''), '');
9
- });
10
- it.should('parse single keys', a => {
11
- a.equal((0, index_js_1.normalize)(' x '), 'x');
12
- a.equal((0, index_js_1.normalize)('ctrl+x'), 'ctrl+x');
13
- });
14
- it.should('parse multiple keys', a => {
15
- a.equal((0, index_js_1.normalize)(' x y'), 'x y');
16
- a.equal((0, index_js_1.normalize)('x y'), 'x y');
17
- a.equal((0, index_js_1.normalize)('ctrl+shift+x'), 'ctrl+shift+x');
18
- });
19
- it.should('parse mod key', a => {
20
- a.equal((0, index_js_1.normalize)('mod+x'), 'ctrl+x');
21
- a.equal((0, index_js_1.normalize)('x+mod'), 'ctrl+x');
22
- a.equal((0, index_js_1.normalize)('MOD+X'), 'ctrl+x');
23
- });
24
- it.should('normalize shifted keys', a => {
25
- a.equal((0, index_js_1.normalize)('shift+5'), '%');
26
- a.equal((0, index_js_1.normalize)('shift+A'), 'shift+a');
27
- a.equal((0, index_js_1.normalize)('shift+y'), 'shift+y');
28
- a.equal((0, index_js_1.normalize)('shift+ctrl+5'), 'ctrl+%');
29
- });
30
- it.should('handle non character keys', a => {
31
- a.equal((0, index_js_1.normalize)(':'), ':');
32
- a.equal((0, index_js_1.normalize)('/'), '/');
33
- a.equal((0, index_js_1.normalize)('.'), '.');
34
- a.equal((0, index_js_1.normalize)('?'), '?');
35
- a.equal((0, index_js_1.normalize)('shift+/'), '?');
36
- a.equal((0, index_js_1.normalize)('shift+?'), '?');
37
- a.equal((0, index_js_1.normalize)('ctrl+:'), 'ctrl+:');
38
- });
39
- it.should('normalize special keys with modifiers', a => {
40
- a.equal((0, index_js_1.normalize)('ctrl+alt+del'), 'ctrl+alt+del');
41
- a.equal((0, index_js_1.normalize)('ctrl+shift+esc'), 'ctrl+shift+esc');
42
- });
43
- });
44
- s.test('parseKey', it => {
45
- it.should('correctly parse simple keys without modifiers', a => {
46
- const result = (0, index_js_1.parseKey)('a');
47
- a.equal(result.length, 1);
48
- a.equal(result[0].key, 'a');
49
- a.equal(result[0].shiftKey, false);
50
- a.equal(result[0].ctrlKey, false);
51
- a.equal(result[0].altKey, false);
52
- a.equal(result[0].metaKey, false);
53
- });
54
- it.should('correctly parse modifier keys', a => {
55
- const result = (0, index_js_1.parseKey)('shift+b');
56
- a.equal(result.length, 1);
57
- a.equal(result[0].key, 'b');
58
- a.equal(result[0].shiftKey, true);
59
- a.equal(result[0].ctrlKey, false);
60
- a.equal(result[0].altKey, false);
61
- a.equal(result[0].metaKey, false);
62
- });
63
- it.should('correctly parse multiple keys with modifiers', a => {
64
- const result = (0, index_js_1.parseKey)('ctrl+alt+del');
65
- a.equal(result.length, 1);
66
- a.equal(result[0].key, 'del');
67
- a.equal(result[0].shiftKey, false);
68
- a.equal(result[0].ctrlKey, true);
69
- a.equal(result[0].altKey, true);
70
- a.equal(result[0].metaKey, false);
71
- });
72
- it.should('handle key sequences separated by spaces', a => {
73
- const result = (0, index_js_1.parseKey)('ctrl+a ctrl+b');
74
- a.equal(result.length, 2);
75
- a.equal(result[0].key, 'a');
76
- a.equal(result[0].ctrlKey, true);
77
- a.equal(result[1].key, 'b');
78
- a.equal(result[1].ctrlKey, true);
79
- });
80
- it.should('map mod key to the correct modifier', a => {
81
- const defaultLayout = index_js_1.KeyboardLayoutData['en-US'];
82
- const result = (0, index_js_1.parseKey)('mod+c');
83
- a.equal(result.length, 1);
84
- a.equal(result[0].key, 'c');
85
- a.equal(result[0][defaultLayout.modKey], true);
86
- });
87
- it.should('handle shifted characters correctly', a => {
88
- const result = (0, index_js_1.parseKey)('shift+1');
89
- a.equal(result.length, 1);
90
- a.equal(result[0].key, '!'); // assuming shift+1 maps to '@' in shiftMap
91
- a.equal(result[0].shiftKey, false);
92
- });
93
- });
94
- s.test('handleKeyboard', it => {
95
- it.should('call onKey with the correct sequence', (a) => {
96
- // Setup a mock element
97
- const elementMock = a.element('div');
98
- const on = a.spyFn(elementMock, 'addEventListener');
99
- const off = a.spyFn(elementMock, 'removeEventListener');
100
- // Setup a mock callback
101
- const onKeyMock = (0, spec_1.mockFn)(key => (key === 'a b' ? true : false));
102
- // Register the handler
103
- const dispose = (0, index_js_1.handleKeyboard)({
104
- element: elementMock,
105
- onKey: onKeyMock,
106
- delay: 100,
107
- layout: undefined,
108
- capture: false,
109
- });
110
- // Simulate keydown events
111
- const eventA = new KeyboardEvent('keydown', { key: 'a' });
112
- const eventB = new KeyboardEvent('keydown', { key: 'b' });
113
- // This will call the handler linked to the event
114
- a.assert(typeof on.lastEvent?.arguments[1] === 'function');
115
- on.lastEvent?.arguments[1](eventA);
116
- a.equal(onKeyMock.lastArguments?.[0], 'a');
117
- on.lastEvent?.arguments[1](eventB);
118
- a.equal(onKeyMock.lastArguments?.[0], 'a b');
119
- // Cleanup
120
- dispose();
121
- a.equal(off.lastEvent?.called, 1);
122
- });
123
- it.should('reset sequence after delay', async (a) => {
124
- const elementMock = a.element('div');
125
- const on = a.spyFn(elementMock, 'addEventListener');
126
- const off = a.spyFn(elementMock, 'removeEventListener');
127
- const onKeyMock = (0, spec_1.mockFn)(key => (key === 'a b' ? true : false));
128
- const dispose = (0, index_js_1.handleKeyboard)({
129
- element: elementMock,
130
- onKey: onKeyMock,
131
- delay: 100,
132
- layout: undefined,
133
- capture: false,
134
- });
135
- // Simulate keydown event
136
- const eventA = new KeyboardEvent('keydown', { key: 'a' });
137
- a.assert(typeof on.lastEvent?.arguments[1] === 'function');
138
- on.lastEvent?.arguments[1](eventA);
139
- // Advance time beyond delay
140
- await new Promise(resolve => setTimeout(resolve, 200));
141
- a.equal(onKeyMock.lastArguments?.[0], 'a');
142
- // Simulate another keydown event
143
- const eventC = new KeyboardEvent('keydown', { key: 'c' });
144
- on.lastEvent?.arguments[1](eventC);
145
- a.equal(onKeyMock.lastArguments?.[0], 'c');
146
- // Cleanup
147
- dispose();
148
- a.equal(off.lastEvent?.called, 1);
149
- });
150
- });
151
- });