diff --git a/__mocks__/handlers/task-details.handler.ts b/__mocks__/handlers/task-details.handler.ts index 6a68faea3..3fbf54b6b 100644 --- a/__mocks__/handlers/task-details.handler.ts +++ b/__mocks__/handlers/task-details.handler.ts @@ -34,6 +34,37 @@ const taskDetailsHandler = [ }) ); }), + rest.get(`${URL}/tasks/6KhcLU3yr45dzjQIVm0k/details`, (_, res, ctx) => { + return res( + ctx.status(200), + ctx.json({ + message: 'task returned successfully', + taskData: { + id: '6KhcLU3yr45dzjQIVm0k', + isNoteworthy: true, + lossRate: { + dinero: 0, + neelam: 0, + }, + endsOn: 1618790410, + title: 'test 1 for drag and drop', + status: 'assigned', + assignee: 'ankur', + links: ['null'], + dependsOn: ['null'], + percentCompleted: 0, + type: 'feature', + priority: 'high', + featureUrl: 'https://www.sampleUrl.com', + startedOn: 1617062400, + completionAward: { + neelam: 0, + dinero: 110, + }, + }, + }) + ); + }), rest.patch(`${URL}/tasks/:taskId`, (_, res, ctx) => { return res(ctx.status(204)); }), diff --git a/__tests__/Unit/Components/Select/Select.test.tsx b/__tests__/Unit/Components/Select/Select.test.tsx new file mode 100644 index 000000000..3576c966d --- /dev/null +++ b/__tests__/Unit/Components/Select/Select.test.tsx @@ -0,0 +1,197 @@ +import { render, fireEvent, screen } from '@testing-library/react'; +import { Select } from '@/components/Select'; + +const sampleOptions = [ + { label: 'Option 1', value: 'option1' }, + { label: 'Option 2', value: 'option2' }, + { label: 'Option 3', value: 'option3' }, +]; + +const onSelectMock = jest.fn(); +describe('Select', () => { + test('renders with initial state', () => { + const value = sampleOptions[0]; + render( + onSelectMock()} + options={sampleOptions} + /> + ); + const selectContainer = screen?.getByRole('button', { + name: value.label, + }); + + fireEvent.click(selectContainer); + + expect(screen?.queryByTestId('options')).toHaveClass('show'); + }); + + test('selects an option', () => { + const value = sampleOptions[0]; + const onChangeMock = jest.fn(); + render( + + ); + + const option2 = screen?.getByText('Option 2'); + fireEvent.pointerEnter(option2); + + expect(onChangeMock).toHaveBeenCalledWith(sampleOptions[1]); + }); + + test('handler should not be called when event target is not a child of container', () => { + const value = sampleOptions[0]; + const onChangeMock = jest.fn(); + render( + + ); + const selectContainer = screen.getByRole('button'); + // Simulate pressing the "ArrowDown" key to open the dropdown. + fireEvent.keyDown(selectContainer, { + key: 'ArrowDown', + code: 'ArrowDown', + }); + // The dropdown should be visible after the key press. + expect(screen.getByTestId('options')).toBeVisible(); + + // Simulate pressing the "ArrowDown" key to open the dropdown. + fireEvent.keyDown(selectContainer, { + key: 'ArrowDown', + code: 'ArrowDown', + }); + + // The second option should be highlighted. + expect(screen.getByText('Option 2')).toHaveClass('highlighted'); + }); + + test('using escape key for closing the options', () => { + const value = sampleOptions[0]; + const onChangeMock = jest.fn(); + render( + + ); + const selectContainer = screen.getByTestId('selected-option'); + const optionsList = screen.getByTestId('options'); + + // Open the dropdown by simulating a click on the select container + fireEvent.click(selectContainer); + expect(optionsList).toBeVisible(); + + // Simulate blur event on the select container + fireEvent.blur(selectContainer); + + // The dropdown should be closed after the container loses focus + expect(screen?.queryByTestId('options')).not.toHaveClass('show'); + }); + + test('handler should set the highlighted index when an option is hovered (onMouseEnter event)', () => { + const value = sampleOptions[0]; + const onChangeMock = jest.fn(); + render( +