Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes date picker alignment and date value not showing up while editing on task details page #1260

Merged
merged 23 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e46413e
fix: date picker alignment and prefilled
Ayushsanjdev Sep 15, 2024
95fda61
fix: test cases for dates and details
Ayushsanjdev Sep 16, 2024
d0b8746
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-s…
Ayushsanjdev Sep 16, 2024
a86689d
Merge branch 'develop' into fix/taskDates/1252
Ayushsanjdev Sep 16, 2024
2b5ba3a
update: test files
Ayushsanjdev Sep 20, 2024
0b7da03
refactor: detail content component
Ayushsanjdev Sep 20, 2024
f49fa5b
Merge branch 'develop' into fix/taskDates/1252
Ayushsanjdev Sep 20, 2024
98ba965
fix: useEffect to be conditional
Ayushsanjdev Sep 22, 2024
f8bc483
Improved test coverage for taskDates
Ayushsanjdev Sep 23, 2024
85fec87
Merge branch 'develop' into fix/taskDates/1252
Achintya-Chatterjee Sep 23, 2024
c58ed49
revert back to original change
Ayushsanjdev Sep 24, 2024
13d2141
fix: edge case of invalid date
Ayushsanjdev Sep 24, 2024
d79daeb
fix: redundancy in detailsContent
Ayushsanjdev Sep 24, 2024
b713acd
added more restriction to date validation
Ayushsanjdev Sep 25, 2024
770379f
fix: test case for invalid-date
Ayushsanjdev Sep 25, 2024
62d5de8
fix: test case with await
Ayushsanjdev Sep 25, 2024
0931abb
Merge branch 'develop' into fix/taskDates/1252
yesyash Sep 28, 2024
89e1d4c
updated test case for failure onsave
Ayushsanjdev Sep 29, 2024
fa6d4e8
refactor: detailsContent
Ayushsanjdev Oct 1, 2024
12a65be
Merge branch 'develop' into fix/taskDates/1252
Ayushsanjdev Oct 1, 2024
a6bd0f3
update test case for isValidDate utils
Ayushsanjdev Oct 2, 2024
30706f0
updated feature flag
Ayushsanjdev Oct 3, 2024
c76b739
fix: test case name
Ayushsanjdev Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion __mocks__/handlers/task-details.handler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TASKS_URL } from '@/constants/url';
import { rest } from 'msw';
const URL = process.env.NEXT_PUBLIC_BASE_URL;

Expand Down Expand Up @@ -135,4 +136,18 @@ const failedTaskDependencyDetails = rest.get(
}
);

export { taskDetailsHandler, failedTaskDependencyDetails };
const failedToUpdateTaskDetails = rest.patch(
`${TASKS_URL}/6KhcLU3yr45dzjQIVm0J`,
(req, res, ctx) => {
return res(
ctx.status(500),
ctx.json({ message: 'Failed to update the task details' })
);
}
);

export {
taskDetailsHandler,
failedTaskDependencyDetails,
failedToUpdateTaskDetails,
};
177 changes: 162 additions & 15 deletions __tests__/Unit/Components/Tasks/TaskDates.test.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,175 @@
import React from 'react';
import { Provider } from 'react-redux';
import { render, screen, fireEvent } from '@testing-library/react';
import { TaskDates } from '@/components/taskDetails/TaskDates';
import { store } from '@/app/store';

const mockSetNewEndOnDate = jest.fn();
const mockHandleBlurOfEndsOn = jest.fn();
jest.mock('@/hooks/useUserData', () => {
return () => ({
data: {
roles: {
admin: true,
super_user: false,
},
},
isUserAuthorized: true,
isSuccess: true,
});
});

const mockHandleEditedTaskDetails = jest.fn();

describe('TaskDates Component', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should render input field for End On date when in editing mode', () => {
render(
<TaskDates
isEditing={true}
isUserAuthorized={true}
startedOn="2024-03-30T11:20:00Z"
endsOn={1700000000}
newEndOnDate="2024-03-30"
setNewEndOnDate={mockSetNewEndOnDate}
handleBlurOfEndsOn={mockHandleBlurOfEndsOn}
isExtensionRequestPending={false}
taskId="1"
/>
<Provider store={store()}>
<TaskDates
isEditing={true}
startedOn="2024-03-30T11:20:00Z"
endsOn={1700000000}
setEditedTaskDetails={mockHandleEditedTaskDetails}
isExtensionRequestPending={false}
taskId="1"
/>
</Provider>
);

const input = screen.getByTestId(
'endsOnTaskDetails'
) as HTMLInputElement;
fireEvent.change(input, { target: { value: '2024-04-15' } });
fireEvent.blur(input);
expect(input.value).toBe('2024-04-15');
});

it('should not render input field for End On date when not in editing mode', () => {
render(
<Provider store={store()}>
<TaskDates
isEditing={false}
startedOn="2024-03-30T11:20:00Z"
endsOn={1700000000}
setEditedTaskDetails={mockHandleEditedTaskDetails}
isExtensionRequestPending={false}
taskId="1"
/>
</Provider>
);

const input = screen.queryByTestId('endsOnTaskDetails');
expect(input).toBeNull();
});

it('should display an extension icon, when isExtensionRequestPending is true', () => {
render(
<Provider store={store()}>
<TaskDates
isEditing={true}
startedOn="2024-03-30T11:20:00Z"
endsOn={1700000000}
setEditedTaskDetails={mockHandleEditedTaskDetails}
isExtensionRequestPending={true}
taskId="1"
/>
</Provider>
);

const extensionIcon = screen.getByTestId('extension-request-icon');
expect(extensionIcon).toBeInTheDocument();
});

it('should not update the input value if invalid date is entered', () => {
render(
<Provider store={store()}>
<TaskDates
isEditing={true}
startedOn="2024-03-30T11:20:00Z"
endsOn={null}
setEditedTaskDetails={mockHandleEditedTaskDetails}
isExtensionRequestPending={false}
taskId="1"
/>
</Provider>
);

const input = screen.getByTestId(
'endsOnTaskDetails'
) as HTMLInputElement;
fireEvent.change(input, { target: { value: 'invalid-date' } });
fireEvent.blur(input);
expect(input.value).toBe('');
});

it('should render correctly with admin role', () => {
render(
<Provider store={store()}>
<TaskDates
isEditing={true}
startedOn="2024-03-30T11:20:00Z"
endsOn={1700000000}
setEditedTaskDetails={mockHandleEditedTaskDetails}
isExtensionRequestPending={false}
taskId="1"
/>
</Provider>
);

const input = screen.getByTestId('endsOnTaskDetails');
expect(input).toBeInTheDocument();
fireEvent.blur(input);
expect(mockHandleBlurOfEndsOn).toHaveBeenCalled();
});

it('should render correctly with non-admin role', () => {
Ayushsanjdev marked this conversation as resolved.
Show resolved Hide resolved
jest.mock('@/hooks/useUserData', () => {
return () => ({
data: {
roles: {
admin: false,
super_user: true,
},
},
isUserAuthorized: true,
isSuccess: true,
});
});

render(
<Provider store={store()}>
<TaskDates
isEditing={true}
startedOn="2024-03-30T11:20:00Z"
endsOn={1700000000}
setEditedTaskDetails={mockHandleEditedTaskDetails}
isExtensionRequestPending={false}
taskId="1"
/>
</Provider>
);

const input = screen.getByTestId('endsOnTaskDetails');
expect(input).toBeInTheDocument();
});

it('should render the correct date when endsOn is null', () => {
render(
<Provider store={store()}>
<TaskDates
isEditing={true}
startedOn="2024-03-30T11:20:00Z"
endsOn={null}
setEditedTaskDetails={mockHandleEditedTaskDetails}
isExtensionRequestPending={false}
taskId="1"
/>
</Provider>
);

const input = screen.getByTestId(
'endsOnTaskDetails'
) as HTMLInputElement;
expect(input.value).toBe('');
});
});
107 changes: 99 additions & 8 deletions __tests__/Unit/Components/Tasks/TaskDetails.test.tsx
Ayushsanjdev marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { ButtonProps, TextAreaProps } from '@/interfaces/taskDetails.type';
import { ToastContainer } from 'react-toastify';
import * as progressQueries from '@/app/services/progressesApi';
import Details from '@/components/taskDetails/Details';
import { taskDetailsHandler } from '../../../../__mocks__/handlers/task-details.handler';
import {
failedToUpdateTaskDetails,
taskDetailsHandler,
} from '../../../../__mocks__/handlers/task-details.handler';
import { superUserSelfHandler } from '../../../../__mocks__/handlers/self.handler';
import convertTimeStamp from '@/helperFunctions/convertTimeStamp';
import { STARTED_ON, ENDS_ON } from '@/constants/constants';
Expand Down Expand Up @@ -49,6 +52,8 @@ jest.mock('@/hooks/useUserData', () => {
});

const mockNavigateToUpdateProgressPage = jest.fn();
const mockHandleEditedTaskDetails = jest.fn();

describe('TaskDetails Page', () => {
it('Should render title', async () => {
const { getByText } = renderWithRouter(
Expand Down Expand Up @@ -323,13 +328,72 @@ test('should call onSave and reset state when clicked', async () => {
await waitFor(() => {
const editButton = screen.getByRole('button', { name: 'Edit' });
fireEvent.click(editButton);
const input = screen.getByTestId(
'endsOnTaskDetails'
) as HTMLInputElement;
fireEvent.change(input, { target: { value: '2024-04-15' } });
fireEvent.blur(input);
expect(input.value).toBe('2024-04-15');
const saveButton = screen.getByRole('button', { name: 'Save' });
fireEvent.click(saveButton);
});

await waitFor(() => {
const saveButton = screen.getByRole('button', { name: 'Save' });
fireEvent.click(saveButton);
expect(
screen.getByRole('button', { name: 'Saving...' })
).toBeInTheDocument();
});

await waitFor(() => {
const editButtonAfterSave = screen.getByRole('button', {
name: 'Edit',
});
expect(editButtonAfterSave).toBeInTheDocument();
});
});

test('should call onSave and show error toast when save fails', async () => {
server.use(failedToUpdateTaskDetails);

renderWithRouter(
<Provider store={store()}>
<TaskDetails taskID={details.taskID} />
<ToastContainer />
</Provider>,
{}
);

await waitFor(() => {
const editButton = screen.getByRole('button', { name: 'Edit' });
expect(editButton).toBeInTheDocument();
fireEvent.click(editButton);
});

const input = screen.getByTestId('endsOnTaskDetails') as HTMLInputElement;
fireEvent.change(input, { target: { value: '2024-04-15' } });
fireEvent.blur(input);

expect(input.value).toBe('2024-04-15');

const saveButton = screen.getByRole('button', { name: 'Save' });
fireEvent.click(saveButton);

await waitFor(() => {
expect(
screen.getByRole('button', { name: 'Saving...' })
).toBeInTheDocument();
});

await waitFor(() => {
expect(
screen.getByText(/Failed to update the task details/i)
).toBeInTheDocument();
});

await waitFor(() => {
const editButtonAfterSave = screen.getByRole('button', {
name: 'Edit',
});
expect(editButtonAfterSave).toBeInTheDocument();
});
});

Expand All @@ -351,11 +415,12 @@ test('should update the title and description with the new values', async () =>
fireEvent.change(textareaElement, {
target: { name: 'title', value: 'New Title' },
});
await waitFor(async () => {
const saveButton = screen.getByRole('button', { name: 'Save' });
fireEvent.click(saveButton);
expect(await screen.findByText(/Successfully saved/i)).not.toBeNull();

const saveButton = await screen.findByRole('button', {
name: 'Save',
});
fireEvent.click(saveButton);
expect(screen.findByText(/Successfully saved/i)).not.toBeNull();
});
test('should not update the title and description with the same values', async () => {
server.use(...taskDetailsHandler);
Expand Down Expand Up @@ -701,4 +766,30 @@ describe('Details component', () => {
throw error;
}
});

it('Renders an input with prefilled data provided, when isEditing is true', () => {
const task = {
id: 'L1SDW6O835o0EI8ZmvRc',
endedOn: 1700000000,
};
const formattedEndsOn = convertTimeStamp(task.endedOn);
const expectedDate = new Date(formattedEndsOn).toLocaleDateString(
'en-CA'
);

renderWithRouter(
<Details
detailType={ENDS_ON}
value={formattedEndsOn}
isEditing
setEditedTaskDetails={mockHandleEditedTaskDetails}
/>
);

const input = screen.getByTestId(
'endsOnTaskDetails'
) as HTMLInputElement;

expect(input.defaultValue).toBe(expectedDate);
});
});
Loading
Loading