Skip to content

Commit e41f510

Browse files
committed
refactor(tests): fix nesting depth and ReDoS vulnerability in API Keys tests
1 parent cf94b55 commit e41f510

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

frontend/__tests__/unit/pages/ApiKeysPage.test.tsx

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ describe('ApiKeysPage Component', () => {
6363
const mockCreateMutation = jest.fn()
6464
const mockRevokeMutation = jest.fn()
6565

66+
// Helper to create mutation function with callback support
67+
const createMutationFn = (mockFn: jest.Mock, options?: { onCompleted?: (data: unknown) => void }) => {
68+
return jest.fn(async (vars) => {
69+
const result = await mockFn(vars)
70+
if (options?.onCompleted) {
71+
options.onCompleted(result.data)
72+
}
73+
return result
74+
})
75+
}
76+
6677
const setupMocks = (overrides = {}) => {
6778
mockUseQuery.mockReturnValue({
6879
data: mockApiKeys,
@@ -74,20 +85,10 @@ describe('ApiKeysPage Component', () => {
7485

7586
mockUseMutation.mockImplementation((mutation, options) => {
7687
if (mutation === CreateApiKeyDocument) {
77-
const mutationFn = jest.fn(async (vars) => {
78-
const result = await mockCreateMutation(vars)
79-
if (options?.onCompleted) options.onCompleted(result.data)
80-
return result
81-
})
82-
return [mutationFn, { loading: false }]
88+
return [createMutationFn(mockCreateMutation, options), { loading: false }]
8389
}
8490
if (mutation === RevokeApiKeyDocument) {
85-
const mutationFn = jest.fn(async (vars) => {
86-
const result = await mockRevokeMutation(vars)
87-
if (options?.onCompleted) options.onCompleted(result.data)
88-
return result
89-
})
90-
return [mutationFn, { loading: false }]
91+
return [createMutationFn(mockRevokeMutation, options), { loading: false }]
9192
}
9293
return [jest.fn(), { loading: false }]
9394
})
@@ -307,17 +308,13 @@ describe('ApiKeysPage Component', () => {
307308

308309
describe('Edge Cases', () => {
309310
test('disables create button when createLoading is true', async () => {
310-
mockUseMutation.mockImplementation((mutation, options) => {
311+
const createLoadingMutation = (mutation: unknown, options?: unknown) => {
311312
if (mutation === CreateApiKeyDocument) {
312-
const mutationFn = jest.fn(async (vars) => {
313-
const result = await mockCreateMutation(vars)
314-
if (options?.onCompleted) options.onCompleted(result.data)
315-
return result
316-
})
317-
return [mutationFn, { loading: true }]
313+
return [createMutationFn(mockCreateMutation, options as { onCompleted?: (data: unknown) => void }), { loading: true }]
318314
}
319315
return [jest.fn(), { loading: false }]
320-
})
316+
}
317+
mockUseMutation.mockImplementation(createLoadingMutation)
321318

322319
render(<ApiKeysPage />)
323320
await openCreateModal()

0 commit comments

Comments
 (0)