Skip to content

Commit 84d47dc

Browse files
committed
#446 - Add tests for getAllRequestsInOrder and enhance global hotkeys functionality
1 parent 04e1668 commit 84d47dc

File tree

2 files changed

+164
-20
lines changed

2 files changed

+164
-20
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { Folder } from 'shim/objects/folder';
3+
import { TrufosRequest } from 'shim/objects/request';
4+
5+
const getAllRequestsInOrder = (children: (Folder | TrufosRequest)[]): string[] => {
6+
const result: string[] = [];
7+
for (const child of children) {
8+
if (child.type === 'request') {
9+
result.push(child.id);
10+
} else if (child.type === 'folder') {
11+
result.push(...getAllRequestsInOrder(child.children || []));
12+
}
13+
}
14+
return result;
15+
};
16+
17+
describe('getAllRequestsInOrder', () => {
18+
it('should extract requests from flat structure', () => {
19+
const children: (Folder | TrufosRequest)[] = [
20+
{ type: 'request', id: 'req1' } as TrufosRequest,
21+
{ type: 'request', id: 'req2' } as TrufosRequest,
22+
{ type: 'request', id: 'req3' } as TrufosRequest,
23+
];
24+
25+
const result = getAllRequestsInOrder(children);
26+
expect(result).toEqual(['req1', 'req2', 'req3']);
27+
});
28+
29+
it('should extract requests from nested folders (one level)', () => {
30+
const children: (Folder | TrufosRequest)[] = [
31+
{ type: 'request', id: 'req1' } as TrufosRequest,
32+
{
33+
type: 'folder',
34+
id: 'folder1',
35+
children: [
36+
{ type: 'request', id: 'req2' } as TrufosRequest,
37+
{ type: 'request', id: 'req3' } as TrufosRequest,
38+
],
39+
} as Folder,
40+
{ type: 'request', id: 'req4' } as TrufosRequest,
41+
];
42+
43+
const result = getAllRequestsInOrder(children);
44+
expect(result).toEqual(['req1', 'req2', 'req3', 'req4']);
45+
});
46+
47+
it('should extract requests from deeply nested folders (multiple levels)', () => {
48+
const children: (Folder | TrufosRequest)[] = [
49+
{ type: 'request', id: 'req1' } as TrufosRequest,
50+
{
51+
type: 'folder',
52+
id: 'folder1',
53+
children: [
54+
{ type: 'request', id: 'req2' } as TrufosRequest,
55+
{
56+
type: 'folder',
57+
id: 'folder2',
58+
children: [
59+
{ type: 'request', id: 'req3' } as TrufosRequest,
60+
{
61+
type: 'folder',
62+
id: 'folder3',
63+
children: [
64+
{ type: 'request', id: 'req4' } as TrufosRequest,
65+
{ type: 'request', id: 'req5' } as TrufosRequest,
66+
],
67+
} as Folder,
68+
{ type: 'request', id: 'req6' } as TrufosRequest,
69+
],
70+
} as Folder,
71+
{ type: 'request', id: 'req7' } as TrufosRequest,
72+
],
73+
} as Folder,
74+
{ type: 'request', id: 'req8' } as TrufosRequest,
75+
];
76+
77+
const result = getAllRequestsInOrder(children);
78+
expect(result).toEqual(['req1', 'req2', 'req3', 'req4', 'req5', 'req6', 'req7', 'req8']);
79+
});
80+
81+
it('should handle empty folders', () => {
82+
const children: (Folder | TrufosRequest)[] = [
83+
{ type: 'request', id: 'req1' } as TrufosRequest,
84+
{
85+
type: 'folder',
86+
id: 'folder1',
87+
children: [],
88+
} as Folder,
89+
{ type: 'request', id: 'req2' } as TrufosRequest,
90+
];
91+
92+
const result = getAllRequestsInOrder(children);
93+
expect(result).toEqual(['req1', 'req2']);
94+
});
95+
96+
it('should handle complex nested structure with multiple branches', () => {
97+
const children: (Folder | TrufosRequest)[] = [
98+
{
99+
type: 'folder',
100+
id: 'api',
101+
children: [
102+
{ type: 'request', id: 'auth' } as TrufosRequest,
103+
{
104+
type: 'folder',
105+
id: 'users',
106+
children: [
107+
{ type: 'request', id: 'getUsers' } as TrufosRequest,
108+
{ type: 'request', id: 'createUser' } as TrufosRequest,
109+
{
110+
type: 'folder',
111+
id: 'userDetails',
112+
children: [
113+
{ type: 'request', id: 'getUserById' } as TrufosRequest,
114+
{ type: 'request', id: 'updateUser' } as TrufosRequest,
115+
{ type: 'request', id: 'deleteUser' } as TrufosRequest,
116+
],
117+
} as Folder,
118+
],
119+
} as Folder,
120+
],
121+
} as Folder,
122+
{
123+
type: 'folder',
124+
id: 'products',
125+
children: [
126+
{ type: 'request', id: 'getProducts' } as TrufosRequest,
127+
{ type: 'request', id: 'createProduct' } as TrufosRequest,
128+
],
129+
} as Folder,
130+
];
131+
132+
const result = getAllRequestsInOrder(children);
133+
expect(result).toEqual([
134+
'auth',
135+
'getUsers',
136+
'createUser',
137+
'getUserById',
138+
'updateUser',
139+
'deleteUser',
140+
'getProducts',
141+
'createProduct',
142+
]);
143+
});
144+
});

src/renderer/hooks/useGlobalHotkeys.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { useEffect, useCallback } from 'react';
22
import { useCollectionActions, useCollectionStore, selectRequest } from '@/state/collectionStore';
3+
import { Folder } from 'shim/objects/folder';
4+
import { TrufosRequest } from 'shim/objects/request';
35

46
interface UseGlobalHotkeysProps {
57
onSendRequest?: () => void;
68
onSaveRequest?: () => void;
79
onSwitchRequestTab?: (tabIndex: number) => void;
810
onSwitchResponseTab?: (tabIndex: number) => void;
911
onCreateNewRequest?: () => void;
12+
onShowHelp?: () => void;
1013
}
1114

1215
export function useGlobalHotkeys({
@@ -15,18 +18,20 @@ export function useGlobalHotkeys({
1518
onSwitchRequestTab,
1619
onSwitchResponseTab,
1720
onCreateNewRequest,
21+
onShowHelp,
1822
}: UseGlobalHotkeysProps = {}) {
1923
const { setSelectedRequest, setFolderOpen } = useCollectionActions();
20-
const selectedRequestId = useCollectionStore((state) => state.selectedRequestId);
21-
const collection = useCollectionStore((state) => state.collection);
22-
const currentRequest = useCollectionStore(selectRequest);
23-
const folders = useCollectionStore((state) => state.folders);
2424

2525
const navigateToAdjacentRequest = useCallback(
2626
(direction: 'up' | 'down') => {
27+
const state = useCollectionStore.getState();
28+
const selectedRequestId = state.selectedRequestId;
29+
const collection = state.collection;
30+
const folders = state.folders;
31+
2732
if (!selectedRequestId || !collection?.children) return;
2833

29-
const getAllRequestsInOrder = (children: any[]): string[] => {
34+
const getAllRequestsInOrder = (children: (Folder | TrufosRequest)[]): string[] => {
3035
const result: string[] = [];
3136
for (const child of children) {
3237
if (child.type === 'request') {
@@ -52,8 +57,7 @@ export function useGlobalHotkeys({
5257

5358
const nextRequestId = orderedRequestIds[nextIndex];
5459
if (nextRequestId) {
55-
const store = useCollectionStore.getState();
56-
const targetRequest = store.requests.get(nextRequestId);
60+
const targetRequest = state.requests.get(nextRequestId);
5761

5862
if (targetRequest) {
5963
let currentParentId = targetRequest.parentId;
@@ -71,14 +75,7 @@ export function useGlobalHotkeys({
7175
setSelectedRequest(nextRequestId);
7276
}
7377
},
74-
[
75-
selectedRequestId,
76-
collection?.children,
77-
collection?.id,
78-
setSelectedRequest,
79-
setFolderOpen,
80-
folders,
81-
]
78+
[setSelectedRequest, setFolderOpen]
8279
);
8380

8481
const handleKeyDown = useCallback(
@@ -92,7 +89,6 @@ export function useGlobalHotkeys({
9289
event.stopPropagation();
9390
};
9491

95-
// Check if we're in an input field to avoid conflicts
9692
const target = event.target as HTMLElement;
9793
const isInInput =
9894
target.tagName === 'INPUT' ||
@@ -130,9 +126,13 @@ export function useGlobalHotkeys({
130126
break;
131127

132128
case 's':
133-
if (onSaveRequest && currentRequest?.draft && !isInInput) {
134-
preventDefault();
135-
onSaveRequest();
129+
if (!isInInput) {
130+
const state = useCollectionStore.getState();
131+
const currentRequest = selectRequest(state);
132+
if (onSaveRequest && currentRequest?.draft) {
133+
preventDefault();
134+
onSaveRequest();
135+
}
136136
}
137137
break;
138138

@@ -185,8 +185,8 @@ export function useGlobalHotkeys({
185185
onSwitchRequestTab,
186186
onSwitchResponseTab,
187187
onCreateNewRequest,
188+
onShowHelp,
188189
navigateToAdjacentRequest,
189-
currentRequest?.draft,
190190
]
191191
);
192192

0 commit comments

Comments
 (0)