|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +const { queryClient } = vi.hoisted(() => ({ |
| 4 | + queryClient: { |
| 5 | + cancelQueries: vi.fn().mockResolvedValue(undefined), |
| 6 | + invalidateQueries: vi.fn().mockResolvedValue(undefined), |
| 7 | + getQueryData: vi.fn(), |
| 8 | + getQueriesData: vi.fn(() => []), |
| 9 | + setQueryData: vi.fn(), |
| 10 | + setQueriesData: vi.fn(), |
| 11 | + }, |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@tanstack/react-query', () => ({ |
| 15 | + keepPreviousData: {}, |
| 16 | + useQuery: vi.fn(), |
| 17 | + useQueryClient: vi.fn(() => queryClient), |
| 18 | + useMutation: vi.fn((options) => options), |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@/lib/api/client/request', () => ({ requestJson: vi.fn() })) |
| 22 | + |
| 23 | +import { useUpdateWorkspaceCredential } from '@/hooks/queries/credentials' |
| 24 | + |
| 25 | +const CREDENTIAL_ID = 'cred-1' |
| 26 | + |
| 27 | +const existing = { |
| 28 | + id: CREDENTIAL_ID, |
| 29 | + workspaceId: 'workspace-1', |
| 30 | + type: 'env_workspace' as const, |
| 31 | + displayName: 'STRIPE_API_KEY', |
| 32 | + description: 'old description', |
| 33 | + providerId: null, |
| 34 | + accountId: null, |
| 35 | + envKey: 'STRIPE_API_KEY', |
| 36 | + envOwnerUserId: null, |
| 37 | + createdBy: 'user-1', |
| 38 | + createdAt: '2026-01-01T00:00:00.000Z', |
| 39 | + updatedAt: '2026-01-02T00:00:00.000Z', |
| 40 | + role: 'admin' as const, |
| 41 | +} |
| 42 | + |
| 43 | +/** Replays the detail-cache updater the mutation hands to `setQueryData`. */ |
| 44 | +function detailAfterMutate(cached: typeof existing | null) { |
| 45 | + const detailCall = queryClient.setQueryData.mock.calls.find( |
| 46 | + ([key]) => Array.isArray(key) && key.includes('detail') |
| 47 | + ) |
| 48 | + const updater = detailCall?.[1] as (old: unknown) => unknown |
| 49 | + return updater(cached) |
| 50 | +} |
| 51 | + |
| 52 | +describe('useUpdateWorkspaceCredential optimistic detail cache', () => { |
| 53 | + beforeEach(() => { |
| 54 | + vi.clearAllMocks() |
| 55 | + queryClient.getQueryData.mockReturnValue(existing) |
| 56 | + queryClient.getQueriesData.mockReturnValue([]) |
| 57 | + }) |
| 58 | + |
| 59 | + it('patches the detail cache so a detail-backed editor stops being dirty after save', async () => { |
| 60 | + const mutation = useUpdateWorkspaceCredential() as any |
| 61 | + await mutation.onMutate({ credentialId: CREDENTIAL_ID, description: 'new description' }) |
| 62 | + |
| 63 | + expect(detailAfterMutate(existing)).toMatchObject({ description: 'new description' }) |
| 64 | + }) |
| 65 | + |
| 66 | + it('clears the detail description when the edit passes null', async () => { |
| 67 | + const mutation = useUpdateWorkspaceCredential() as any |
| 68 | + await mutation.onMutate({ credentialId: CREDENTIAL_ID, description: null }) |
| 69 | + |
| 70 | + expect(detailAfterMutate(existing)).toMatchObject({ description: null }) |
| 71 | + }) |
| 72 | + |
| 73 | + it('leaves untouched fields alone when only displayName changes', async () => { |
| 74 | + const mutation = useUpdateWorkspaceCredential() as any |
| 75 | + await mutation.onMutate({ credentialId: CREDENTIAL_ID, displayName: 'RENAMED' }) |
| 76 | + |
| 77 | + expect(detailAfterMutate(existing)).toMatchObject({ |
| 78 | + displayName: 'RENAMED', |
| 79 | + description: 'old description', |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('rolls the detail cache back when the update fails', async () => { |
| 84 | + const mutation = useUpdateWorkspaceCredential() as any |
| 85 | + const context = await mutation.onMutate({ |
| 86 | + credentialId: CREDENTIAL_ID, |
| 87 | + description: 'new description', |
| 88 | + }) |
| 89 | + queryClient.setQueryData.mockClear() |
| 90 | + |
| 91 | + mutation.onError(new Error('boom'), { credentialId: CREDENTIAL_ID }, context) |
| 92 | + |
| 93 | + expect(queryClient.setQueryData).toHaveBeenCalledWith( |
| 94 | + expect.arrayContaining(['detail']), |
| 95 | + existing |
| 96 | + ) |
| 97 | + }) |
| 98 | +}) |
0 commit comments