|
| 1 | +import { createTaskAction, deleteTaskAction } from '@/app/actions/kanban.actions'; |
| 2 | +import { createClient } from '@/utils/supabase/server'; |
| 3 | +import { verifyBoardAccess, verifyTaskAccess } from '@/utils/board-access'; |
| 4 | +import { revalidatePath } from 'next/cache'; |
| 5 | + |
| 6 | +// Mocks |
| 7 | +jest.mock('@/utils/supabase/server', () => ({ |
| 8 | + createClient: jest.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('@/utils/supabase/admin', () => ({ |
| 12 | + createAdminClient: jest.fn().mockReturnValue({ |
| 13 | + from: jest.fn().mockReturnValue({ |
| 14 | + insert: jest.fn(), |
| 15 | + }) |
| 16 | + }), |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('@/utils/board-access', () => ({ |
| 20 | + verifyBoardAccess: jest.fn(), |
| 21 | + verifyAllBoardsAccess: jest.fn(), |
| 22 | + verifyTaskAccess: jest.fn(), |
| 23 | + validateString: jest.fn((str) => str), |
| 24 | +})); |
| 25 | + |
| 26 | +jest.mock('next/cache', () => ({ |
| 27 | + revalidatePath: jest.fn(), |
| 28 | +})); |
| 29 | + |
| 30 | +describe('Kanban / Task Server Actions Test', () => { |
| 31 | + let mockSupabase: any; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + jest.clearAllMocks(); |
| 35 | + |
| 36 | + mockSupabase = { |
| 37 | + auth: { |
| 38 | + getUser: jest.fn().mockResolvedValue({ data: { user: { id: 'user_123' } }, error: null }), |
| 39 | + }, |
| 40 | + from: jest.fn().mockReturnThis(), |
| 41 | + select: jest.fn().mockReturnThis(), |
| 42 | + insert: jest.fn().mockReturnThis(), |
| 43 | + update: jest.fn().mockReturnThis(), |
| 44 | + delete: jest.fn().mockReturnThis(), |
| 45 | + eq: jest.fn().mockReturnThis(), |
| 46 | + single: jest.fn().mockResolvedValue({ data: { board_id: 1, id: 1 }, error: null }), |
| 47 | + }; |
| 48 | + |
| 49 | + (createClient as jest.Mock).mockResolvedValue(mockSupabase); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('createTaskAction', () => { |
| 53 | + it('creates a task successfully and revalidates page cache', async () => { |
| 54 | + await createTaskAction({ column_id: 1, title: 'New Valid Task', position: 1 } as any); |
| 55 | + |
| 56 | + // Verification logic passes |
| 57 | + expect(verifyBoardAccess).toHaveBeenCalled(); |
| 58 | + |
| 59 | + // Makes DB insertion calls |
| 60 | + expect(mockSupabase.from).toHaveBeenCalledWith('tasks'); |
| 61 | + expect(mockSupabase.insert).toHaveBeenCalled(); |
| 62 | + |
| 63 | + // Updates Next.js Router cache |
| 64 | + expect(revalidatePath).toHaveBeenCalledWith('/projects'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('throws error if user is unauthenticated', async () => { |
| 68 | + mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null }, error: new Error('Auth Err') }); |
| 69 | + await expect(createTaskAction({ column_id: 1, title: 'Task' } as any)).rejects.toThrow('Unauthorized'); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('deleteTaskAction', () => { |
| 74 | + it('deletes a task successfully with proper row level security simulation', async () => { |
| 75 | + await deleteTaskAction(10); |
| 76 | + expect(verifyTaskAccess).toHaveBeenCalledWith(expect.any(Object), 'user_123', 10); |
| 77 | + expect(mockSupabase.delete).toHaveBeenCalled(); |
| 78 | + expect(revalidatePath).toHaveBeenCalledWith('/projects'); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments