Skip to content

Commit 4cd8dbd

Browse files
committed
refactor(tests): enhance setupMocks to handle mutation loading states in ApiKeysPage tests
1 parent 61559e3 commit 4cd8dbd

File tree

1 file changed

+8
-31
lines changed

1 file changed

+8
-31
lines changed

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

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('ApiKeysPage Component', () => {
7676
})
7777
}
7878

79-
const setupMocks = (overrides = {}) => {
79+
const setupMocks = (overrides = {}, mutationLoading = { create: false, revoke: false }) => {
8080
mockUseQuery.mockReturnValue({
8181
data: mockApiKeys,
8282
loading: false,
@@ -87,10 +87,10 @@ describe('ApiKeysPage Component', () => {
8787

8888
mockUseMutation.mockImplementation((mutation, options) => {
8989
if (mutation === CreateApiKeyDocument) {
90-
return [createMutationFn(mockCreateMutation, options), { loading: false }]
90+
return [createMutationFn(mockCreateMutation, options), { loading: mutationLoading.create }]
9191
}
9292
if (mutation === RevokeApiKeyDocument) {
93-
return [createMutationFn(mockRevokeMutation, options), { loading: false }]
93+
return [createMutationFn(mockRevokeMutation, options), { loading: mutationLoading.revoke }]
9494
}
9595
return [jest.fn(), { loading: false }]
9696
})
@@ -254,9 +254,8 @@ describe('ApiKeysPage Component', () => {
254254
describe('API Key Revocation', () => {
255255
test('revokes API key after confirmation', async () => {
256256
render(<ApiKeysPage />)
257-
const row = (await screen.findByText('mock key 1')).closest('tr')
258-
expect(row).not.toBeNull()
259-
fireEvent.click(within(row!).getByRole('button'))
257+
const row = (await screen.findByText('mock key 1')).closest('tr')!
258+
fireEvent.click(within(row).getByRole('button'))
260259

261260
const dialog = await screen.findByRole('dialog')
262261
expect(within(dialog).getByText(/Are you sure you want to revoke/)).toBeInTheDocument()
@@ -269,9 +268,8 @@ describe('ApiKeysPage Component', () => {
269268

270269
test('cancels revocation when cancel button is clicked', async () => {
271270
render(<ApiKeysPage />)
272-
const row = (await screen.findByText('mock key 1')).closest('tr')
273-
expect(row).not.toBeNull()
274-
fireEvent.click(within(row!).getByRole('button'))
271+
const row = (await screen.findByText('mock key 1')).closest('tr')!
272+
fireEvent.click(within(row).getByRole('button'))
275273

276274
const dialog = await screen.findByRole('dialog')
277275
fireEvent.click(within(dialog).getByRole('button', { name: /Cancel/i }))
@@ -315,28 +313,7 @@ describe('ApiKeysPage Component', () => {
315313

316314
describe('Edge Cases', () => {
317315
test('disables create button when createLoading is true', async () => {
318-
const createLoadingMutation = (mutation: unknown, options?: unknown) => {
319-
if (mutation === CreateApiKeyDocument) {
320-
return [
321-
createMutationFn(
322-
mockCreateMutation,
323-
options as { onCompleted?: (data: unknown) => void }
324-
),
325-
{ loading: true },
326-
]
327-
}
328-
if (mutation === RevokeApiKeyDocument) {
329-
return [
330-
createMutationFn(
331-
mockRevokeMutation,
332-
options as { onCompleted?: (data: unknown) => void }
333-
),
334-
{ loading: false },
335-
]
336-
}
337-
return [jest.fn(), { loading: false }]
338-
}
339-
mockUseMutation.mockImplementation(createLoadingMutation)
316+
setupMocks({}, { create: true, revoke: false })
340317

341318
render(<ApiKeysPage />)
342319
await openCreateModal()

0 commit comments

Comments
 (0)