From 1b000a879825d49e05349d1dcc969c74fc793fa9 Mon Sep 17 00:00:00 2001 From: Philippe <83185129+phil3838@users.noreply.github.com> Date: Thu, 27 Mar 2025 14:23:06 -0400 Subject: [PATCH] test for Editor component --- .../components/Editor/Editor.test.tsx | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/client/src/__tests__/components/Editor/Editor.test.tsx b/client/src/__tests__/components/Editor/Editor.test.tsx index 878fedd..6c5dcd2 100644 --- a/client/src/__tests__/components/Editor/Editor.test.tsx +++ b/client/src/__tests__/components/Editor/Editor.test.tsx @@ -5,15 +5,18 @@ import Editor from '../../../components/Editor/Editor'; describe('Editor Component', () => { const mockOnValuesChange = jest.fn(); + const mockOnFocusQuestion = jest.fn(); const sampleProps = { label: 'Sample Label', values: ['Question 1', 'Question 2'], - onValuesChange: mockOnValuesChange + onValuesChange: mockOnValuesChange, + onFocusQuestion: mockOnFocusQuestion, }; beforeEach(() => { mockOnValuesChange.mockClear(); + mockOnFocusQuestion.mockClear(); }); test('renders the label correctly', () => { @@ -37,7 +40,7 @@ describe('Editor Component', () => { test('calls onValuesChange with updated values when a question is deleted', () => { render(); - const deleteButton = screen.getAllByLabelText('delete')[0]; + const deleteButton = screen.getAllByLabelText('delete')[0]; // Match original aria-label fireEvent.click(deleteButton); expect(mockOnValuesChange).toHaveBeenCalledWith(['Question 2']); }); @@ -47,4 +50,24 @@ describe('Editor Component', () => { const deleteButtons = screen.getAllByLabelText('delete'); expect(deleteButtons.length).toBe(2); }); + + test('calls onFocusQuestion with correct index when focus button is clicked', () => { + render(); + const focusButton = screen.getAllByLabelText('focus question')[1]; + fireEvent.click(focusButton); + expect(mockOnFocusQuestion).toHaveBeenCalledWith(1); + }); + + test('renders focus buttons for each question', () => { + render(); + const focusButtons = screen.getAllByLabelText('focus question'); + expect(focusButtons.length).toBe(2); + }); + + test('does not throw error when onFocusQuestion is not provided', () => { + const { onFocusQuestion, ...propsWithoutFocus } = sampleProps; + render(); + const focusButton = screen.getAllByLabelText('focus question')[0]; + expect(() => fireEvent.click(focusButton)).not.toThrow(); + }); }); \ No newline at end of file