1
+ import type { ComputedRef } from 'vue' ;
1
2
import { describe , expect , it , vi } from 'vitest' ;
2
- import { ref } from 'vue' ;
3
+ import { computed , ref } from 'vue' ;
3
4
import { useKeyboard } from './use-keyboard' ;
4
5
5
6
describe ( 'useKeyboard' , ( ) => {
@@ -9,15 +10,27 @@ describe('useKeyboard', () => {
9
10
return event ;
10
11
} ;
11
12
13
+ // Helper to build options with defaults and optional overrides
14
+ const createOptions = ( override : Partial < {
15
+ disabled : boolean ;
16
+ collapsible : boolean ;
17
+ primary : 'start' | 'end' ;
18
+ orientation : 'horizontal' | 'vertical' ;
19
+ minSizePercentage : ComputedRef < number > ;
20
+ maxSizePercentage : ComputedRef < number | undefined > ;
21
+ } > = { } ) => ( {
22
+ disabled : override . disabled ?? false ,
23
+ collapsible : override . collapsible ?? true ,
24
+ primary : override . primary ?? 'start' ,
25
+ orientation : override . orientation ?? 'horizontal' ,
26
+ minSizePercentage : override . minSizePercentage ?? computed ( ( ) => 0 ) ,
27
+ maxSizePercentage : override . maxSizePercentage ?? computed ( ( ) => void 0 ) ,
28
+ } ) ;
29
+
12
30
it ( 'should return handleKeydown function' , ( ) => {
13
31
const sizePercentage = ref ( 50 ) ;
14
32
const collapsed = ref ( false ) ;
15
- const options = {
16
- disabled : false ,
17
- collapsible : true ,
18
- primary : 'start' as const ,
19
- orientation : 'horizontal' as const ,
20
- } ;
33
+ const options = createOptions ( ) ;
21
34
22
35
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
23
36
@@ -27,12 +40,7 @@ describe('useKeyboard', () => {
27
40
it ( 'should do nothing when disabled' , ( ) => {
28
41
const sizePercentage = ref ( 50 ) ;
29
42
const collapsed = ref ( false ) ;
30
- const options = {
31
- disabled : true ,
32
- collapsible : true ,
33
- primary : 'start' as const ,
34
- orientation : 'horizontal' as const ,
35
- } ;
43
+ const options = createOptions ( { disabled : true } ) ;
36
44
37
45
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
38
46
const event = createMockKeyboardEvent ( 'ArrowRight' ) ;
@@ -47,12 +55,7 @@ describe('useKeyboard', () => {
47
55
it ( 'should decrease size on ArrowLeft when primary is start' , ( ) => {
48
56
const sizePercentage = ref ( 50 ) ;
49
57
const collapsed = ref ( false ) ;
50
- const options = {
51
- disabled : false ,
52
- collapsible : true ,
53
- primary : 'start' as const ,
54
- orientation : 'horizontal' as const ,
55
- } ;
58
+ const options = createOptions ( ) ;
56
59
57
60
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
58
61
const event = createMockKeyboardEvent ( 'ArrowLeft' ) ;
@@ -66,12 +69,7 @@ describe('useKeyboard', () => {
66
69
it ( 'should increase size on ArrowRight when primary is start' , ( ) => {
67
70
const sizePercentage = ref ( 50 ) ;
68
71
const collapsed = ref ( false ) ;
69
- const options = {
70
- disabled : false ,
71
- collapsible : true ,
72
- primary : 'start' as const ,
73
- orientation : 'horizontal' as const ,
74
- } ;
72
+ const options = createOptions ( ) ;
75
73
76
74
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
77
75
const event = createMockKeyboardEvent ( 'ArrowRight' ) ;
@@ -85,12 +83,7 @@ describe('useKeyboard', () => {
85
83
it ( 'should increase size on ArrowLeft when primary is end' , ( ) => {
86
84
const sizePercentage = ref ( 50 ) ;
87
85
const collapsed = ref ( false ) ;
88
- const options = {
89
- disabled : false ,
90
- collapsible : true ,
91
- primary : 'end' as const ,
92
- orientation : 'horizontal' as const ,
93
- } ;
86
+ const options = createOptions ( { primary : 'end' } ) ;
94
87
95
88
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
96
89
const event = createMockKeyboardEvent ( 'ArrowLeft' ) ;
@@ -105,12 +98,7 @@ describe('useKeyboard', () => {
105
98
it ( 'should decrease size on ArrowUp when primary is start' , ( ) => {
106
99
const sizePercentage = ref ( 50 ) ;
107
100
const collapsed = ref ( false ) ;
108
- const options = {
109
- disabled : false ,
110
- collapsible : true ,
111
- primary : 'start' as const ,
112
- orientation : 'vertical' as const ,
113
- } ;
101
+ const options = createOptions ( { orientation : 'vertical' } ) ;
114
102
115
103
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
116
104
const event = createMockKeyboardEvent ( 'ArrowUp' ) ;
@@ -123,12 +111,7 @@ describe('useKeyboard', () => {
123
111
it ( 'should increase size on ArrowDown when primary is start' , ( ) => {
124
112
const sizePercentage = ref ( 50 ) ;
125
113
const collapsed = ref ( false ) ;
126
- const options = {
127
- disabled : false ,
128
- collapsible : true ,
129
- primary : 'start' as const ,
130
- orientation : 'vertical' as const ,
131
- } ;
114
+ const options = createOptions ( { orientation : 'vertical' } ) ;
132
115
133
116
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
134
117
const event = createMockKeyboardEvent ( 'ArrowDown' ) ;
@@ -143,12 +126,7 @@ describe('useKeyboard', () => {
143
126
it ( 'should change by 10 when shift key is pressed' , ( ) => {
144
127
const sizePercentage = ref ( 50 ) ;
145
128
const collapsed = ref ( false ) ;
146
- const options = {
147
- disabled : false ,
148
- collapsible : true ,
149
- primary : 'start' as const ,
150
- orientation : 'horizontal' as const ,
151
- } ;
129
+ const options = createOptions ( ) ;
152
130
153
131
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
154
132
const event = createMockKeyboardEvent ( 'ArrowRight' , true ) ;
@@ -163,12 +141,7 @@ describe('useKeyboard', () => {
163
141
it ( 'should set to 0 on Home when primary is start' , ( ) => {
164
142
const sizePercentage = ref ( 50 ) ;
165
143
const collapsed = ref ( false ) ;
166
- const options = {
167
- disabled : false ,
168
- collapsible : true ,
169
- primary : 'start' as const ,
170
- orientation : 'horizontal' as const ,
171
- } ;
144
+ const options = createOptions ( ) ;
172
145
173
146
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
174
147
const event = createMockKeyboardEvent ( 'Home' ) ;
@@ -181,12 +154,7 @@ describe('useKeyboard', () => {
181
154
it ( 'should set to 100 on End when primary is start' , ( ) => {
182
155
const sizePercentage = ref ( 50 ) ;
183
156
const collapsed = ref ( false ) ;
184
- const options = {
185
- disabled : false ,
186
- collapsible : true ,
187
- primary : 'start' as const ,
188
- orientation : 'horizontal' as const ,
189
- } ;
157
+ const options = createOptions ( ) ;
190
158
191
159
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
192
160
const event = createMockKeyboardEvent ( 'End' ) ;
@@ -199,12 +167,7 @@ describe('useKeyboard', () => {
199
167
it ( 'should set to 100 on Home when primary is end' , ( ) => {
200
168
const sizePercentage = ref ( 50 ) ;
201
169
const collapsed = ref ( false ) ;
202
- const options = {
203
- disabled : false ,
204
- collapsible : true ,
205
- primary : 'end' as const ,
206
- orientation : 'horizontal' as const ,
207
- } ;
170
+ const options = createOptions ( { primary : 'end' } ) ;
208
171
209
172
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
210
173
const event = createMockKeyboardEvent ( 'Home' ) ;
@@ -219,12 +182,7 @@ describe('useKeyboard', () => {
219
182
it ( 'should toggle collapsed state on Enter when collapsible is true' , ( ) => {
220
183
const sizePercentage = ref ( 50 ) ;
221
184
const collapsed = ref ( false ) ;
222
- const options = {
223
- disabled : false ,
224
- collapsible : true ,
225
- primary : 'start' as const ,
226
- orientation : 'horizontal' as const ,
227
- } ;
185
+ const options = createOptions ( ) ;
228
186
229
187
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
230
188
const event = createMockKeyboardEvent ( 'Enter' ) ;
@@ -237,12 +195,7 @@ describe('useKeyboard', () => {
237
195
it ( 'should not toggle collapsed state on Enter when collapsible is false' , ( ) => {
238
196
const sizePercentage = ref ( 50 ) ;
239
197
const collapsed = ref ( false ) ;
240
- const options = {
241
- disabled : false ,
242
- collapsible : false ,
243
- primary : 'start' as const ,
244
- orientation : 'horizontal' as const ,
245
- } ;
198
+ const options = createOptions ( { collapsible : false } ) ;
246
199
247
200
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
248
201
const event = createMockKeyboardEvent ( 'Enter' ) ;
@@ -257,12 +210,7 @@ describe('useKeyboard', () => {
257
210
it ( 'should clamp size to 0 minimum' , ( ) => {
258
211
const sizePercentage = ref ( 2 ) ;
259
212
const collapsed = ref ( false ) ;
260
- const options = {
261
- disabled : false ,
262
- collapsible : true ,
263
- primary : 'start' as const ,
264
- orientation : 'horizontal' as const ,
265
- } ;
213
+ const options = createOptions ( ) ;
266
214
267
215
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
268
216
const event = createMockKeyboardEvent ( 'ArrowLeft' , true ) ;
@@ -275,12 +223,7 @@ describe('useKeyboard', () => {
275
223
it ( 'should clamp size to 100 maximum' , ( ) => {
276
224
const sizePercentage = ref ( 98 ) ;
277
225
const collapsed = ref ( false ) ;
278
- const options = {
279
- disabled : false ,
280
- collapsible : true ,
281
- primary : 'start' as const ,
282
- orientation : 'horizontal' as const ,
283
- } ;
226
+ const options = createOptions ( ) ;
284
227
285
228
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
286
229
const event = createMockKeyboardEvent ( 'ArrowRight' , true ) ;
@@ -294,12 +237,7 @@ describe('useKeyboard', () => {
294
237
it ( 'should ignore non-handled keys' , ( ) => {
295
238
const sizePercentage = ref ( 50 ) ;
296
239
const collapsed = ref ( false ) ;
297
- const options = {
298
- disabled : false ,
299
- collapsible : true ,
300
- primary : 'start' as const ,
301
- orientation : 'horizontal' as const ,
302
- } ;
240
+ const options = createOptions ( ) ;
303
241
304
242
const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
305
243
const event = createMockKeyboardEvent ( 'KeyA' ) ;
@@ -309,4 +247,65 @@ describe('useKeyboard', () => {
309
247
expect ( sizePercentage . value ) . toBe ( 50 ) ;
310
248
expect ( event . preventDefault ) . not . toHaveBeenCalled ( ) ;
311
249
} ) ;
250
+
251
+ describe ( 'custom min/max size percentages' , ( ) => {
252
+ it ( 'respects a custom minimum size percentage' , ( ) => {
253
+ const sizePercentage = ref ( 25 ) ;
254
+ const collapsed = ref ( false ) ;
255
+ const options = createOptions ( { minSizePercentage : computed ( ( ) => 20 ) } ) ;
256
+
257
+ const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
258
+
259
+ const event = new KeyboardEvent ( 'keydown' , { key : 'ArrowLeft' } ) ;
260
+ handleKeydown ( event ) ;
261
+
262
+ expect ( sizePercentage . value ) . toBe ( 24 ) ; // still above min -> decremented
263
+
264
+ for ( let i = 0 ; i < 10 ; i ++ ) handleKeydown ( new KeyboardEvent ( 'keydown' , { key : 'ArrowLeft' } ) ) ;
265
+
266
+ expect ( sizePercentage . value ) . toBe ( 20 ) ; // clamped to min
267
+ } ) ;
268
+
269
+ it ( 'respects a custom maximum size percentage' , ( ) => {
270
+ const sizePercentage = ref ( 75 ) ;
271
+ const collapsed = ref ( false ) ;
272
+ const options = createOptions ( { maxSizePercentage : computed ( ( ) => 80 ) } ) ;
273
+
274
+ const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
275
+
276
+ const event = new KeyboardEvent ( 'keydown' , { key : 'ArrowRight' } ) ;
277
+ handleKeydown ( event ) ;
278
+
279
+ expect ( sizePercentage . value ) . toBe ( 76 ) ;
280
+
281
+ for ( let i = 0 ; i < 10 ; i ++ ) handleKeydown ( new KeyboardEvent ( 'keydown' , { key : 'ArrowRight' } ) ) ;
282
+
283
+ expect ( sizePercentage . value ) . toBe ( 80 ) ; // clamped to max
284
+ } ) ;
285
+
286
+ it ( 'clamps both min and max simultaneously' , ( ) => {
287
+ const sizePercentage = ref ( 40 ) ;
288
+ const collapsed = ref ( false ) ;
289
+ const options = createOptions ( { minSizePercentage : computed ( ( ) => 30 ) , maxSizePercentage : computed ( ( ) => 60 ) } ) ;
290
+
291
+ const { handleKeydown } = useKeyboard ( sizePercentage , collapsed , options ) ;
292
+
293
+ // Grow past max with shift
294
+ handleKeydown ( new KeyboardEvent ( 'keydown' , { key : 'ArrowRight' , shiftKey : true } ) ) ; // +10 => 50
295
+ expect ( sizePercentage . value ) . toBe ( 50 ) ;
296
+
297
+ handleKeydown ( new KeyboardEvent ( 'keydown' , { key : 'ArrowRight' , shiftKey : true } ) ) ; // +10 => 60
298
+ expect ( sizePercentage . value ) . toBe ( 60 ) ;
299
+
300
+ handleKeydown ( new KeyboardEvent ( 'keydown' , { key : 'ArrowRight' , shiftKey : true } ) ) ; // attempt +10 => 70 -> clamp 60
301
+ expect ( sizePercentage . value ) . toBe ( 60 ) ;
302
+
303
+ // Shrink past min with shift
304
+ handleKeydown ( new KeyboardEvent ( 'keydown' , { key : 'ArrowLeft' , shiftKey : true } ) ) ; // -10 => 50
305
+ expect ( sizePercentage . value ) . toBe ( 50 ) ;
306
+
307
+ for ( let i = 0 ; i < 10 ; i ++ ) handleKeydown ( new KeyboardEvent ( 'keydown' , { key : 'ArrowLeft' , shiftKey : true } ) ) ;
308
+ expect ( sizePercentage . value ) . toBe ( 30 ) ;
309
+ } ) ;
310
+ } ) ;
312
311
} ) ;
0 commit comments