From 8d2866a2a7fbfd2a8a4960e36a5b856f5691173e Mon Sep 17 00:00:00 2001 From: RohitSutar1823 <252551965+RohitSutar1823@users.noreply.github.com> Date: Mon, 9 Nov 2026 07:38:38 +0000 Subject: [PATCH] test(Tasks): cover add task submission flow Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Tasks/__tests__/Tasks.test.tsx | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/frontend/src/components/HomeComponents/Tasks/__tests__/Tasks.test.tsx b/frontend/src/components/HomeComponents/Tasks/__tests__/Tasks.test.tsx index e17689ca..830356cb 100644 --- a/frontend/src/components/HomeComponents/Tasks/__tests__/Tasks.test.tsx +++ b/frontend/src/components/HomeComponents/Tasks/__tests__/Tasks.test.tsx @@ -975,6 +975,94 @@ describe('Tasks Component', () => { expect(newProjectInput).toHaveValue('My Fresh Project'); }); + describe('Add Task Submission', () => { + const hooks = require('../hooks'); + + test('submits the form to the backend and resets the dialog on success', async () => { + render(); + + await screen.findByText('Task 1'); + fireEvent.click(screen.getByRole('button', { name: /add task/i })); + + fireEvent.change(screen.getByLabelText('Description *'), { + target: { value: 'Plan release notes' }, + }); + fireEvent.change(screen.getByLabelText('Priority'), { + target: { value: 'H' }, + }); + + const dialog = screen.getByRole('dialog'); + fireEvent.click( + within(dialog).getByRole('button', { name: /add task/i }) + ); + + await waitFor(() => { + expect(hooks.addTaskToBackend).toHaveBeenCalledWith( + expect.objectContaining({ + email: mockProps.email, + encryptionSecret: mockProps.encryptionSecret, + UUID: mockProps.UUID, + description: 'Plan release notes', + priority: 'H', + project: '', + due: undefined, + start: '', + entry: '', + wait: '', + end: '', + recur: '', + tags: [], + annotations: [], + depends: [], + }) + ); + }); + + await waitFor(() => { + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); + }); + + fireEvent.click(screen.getByRole('button', { name: /add task/i })); + expect(screen.getByLabelText('Description *')).toHaveValue(''); + }); + + test('keeps the dialog open when submission fails', async () => { + const submissionError = new Error('Backend unavailable'); + hooks.addTaskToBackend.mockRejectedValueOnce(submissionError); + const consoleError = jest + .spyOn(console, 'error') + .mockImplementation(() => {}); + + try { + render(); + + await screen.findByText('Task 1'); + fireEvent.click(screen.getByRole('button', { name: /add task/i })); + fireEvent.change(screen.getByLabelText('Description *'), { + target: { value: 'Keep this draft' }, + }); + + const dialog = screen.getByRole('dialog'); + fireEvent.click( + within(dialog).getByRole('button', { name: /add task/i }) + ); + + await waitFor(() => { + expect(consoleError).toHaveBeenCalledWith( + 'Failed to add task:', + submissionError + ); + }); + expect(screen.getByRole('dialog')).toBeInTheDocument(); + expect(screen.getByLabelText('Description *')).toHaveValue( + 'Keep this draft' + ); + } finally { + consoleError.mockRestore(); + } + }); + }); + // Task Dependencies Tests describe('Task Dependencies', () => { test('passes allTasks prop to AddTaskDialog for dependency selection', async () => {