Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 10 additions & 4 deletions frontend/src/components/HomeComponents/Tasks/Tasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { TaskDialog } from './TaskDialog';
import { TaskFormData } from '../../utils/types';

const db = new TasksDatabase();
const DEFAULT_ID_SORT_ORDER = 'desc';
export let syncTasksWithTwAndDb: () => any;

export const Tasks = (
Expand All @@ -86,7 +87,9 @@ export const Tasks = (
>({});
const [currentPage, setCurrentPage] = useState<number>(1);
const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('asc');
const [idSortOrder, setIdSortOrder] = useState<'asc' | 'desc'>('asc');
const [idSortOrder, setIdSortOrder] = useState<'asc' | 'desc'>(
DEFAULT_ID_SORT_ORDER
);

const [newTask, setNewTask] = useState<TaskFormData>({
description: '',
Expand Down Expand Up @@ -249,8 +252,10 @@ export const Tasks = (
.equals(props.email)
.toArray();

setTasks(sortTasksById(tasksFromDB, 'desc'));
setTempTasks(sortTasksById(tasksFromDB, 'desc'));
const sortedTasks = sortTasksById(tasksFromDB, DEFAULT_ID_SORT_ORDER);
setTasks(sortedTasks);
setTempTasks(sortedTasks);
setIdSortOrder(DEFAULT_ID_SORT_ORDER);

const projectsSet = new Set(tasksFromDB.map((task) => task.project));
const filteredProjects = Array.from(projectsSet)
Expand Down Expand Up @@ -317,9 +322,10 @@ export const Tasks = (
.where('email')
.equals(user_email)
.toArray();
const sortedTasks = sortTasksById(updatedTasks, 'desc');
const sortedTasks = sortTasksById(updatedTasks, DEFAULT_ID_SORT_ORDER);
setTasks(sortedTasks);
setTempTasks(sortedTasks);
setIdSortOrder(DEFAULT_ID_SORT_ORDER);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sync reloads the rows in descending ID order. If I had sorted ascending before syncing, the stored direction would still be asc so the next click would apply descending order again and look like it did nothing. Resetting the direction here keeps it aligned with the rows on screen.


const projectsSet = new Set(sortedTasks.map((task) => task.project));
const filteredProjects = Array.from(projectsSet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2091,30 +2091,42 @@ describe('Tasks Component', () => {
});

describe('Column sorting', () => {
test('clicking the ID header toggles ID sort order', async () => {
const visibleTaskIds = () =>
screen
.getAllByTestId(/^task-row-/)
.map((row) => Number(row.id.replace('task-row-', '')));

test('the first ID click sorts ascending and the next restores descending order', async () => {
render(<Tasks {...mockProps} />);
await screen.findByText('Task 12');

const idHeader = screen.getByText('ID').closest('th') as HTMLElement;
// State starts as 'asc' while the loaded list is already desc, so the first
// click re-applies desc. The second click sorts ascending and reverses rows.
await act(async () => {
fireEvent.click(idHeader);
});
const afterFirst = screen
.getAllByTestId(/task-row-/)
.map((row) => row.getAttribute('data-testid'));
const idHeader = screen.getByRole('columnheader', { name: 'ID' });
const initialOrder = visibleTaskIds();
expect(initialOrder).toEqual([1, 17, 16, 15, 14, 13, 12, 11, 10, 9]);

await act(async () => {
fireEvent.click(idHeader);
});
fireEvent.click(idHeader);
expect(visibleTaskIds()).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

fireEvent.click(idHeader);
expect(visibleTaskIds()).toEqual(initialOrder);
});

test('the first ID click after sync sorts ascending again', async () => {
render(<Tasks {...mockProps} />);
await screen.findByText('Task 12');

const idHeader = screen.getByRole('columnheader', { name: 'ID' });
const initialOrder = visibleTaskIds();
fireEvent.click(idHeader);
expect(visibleTaskIds()).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

fireEvent.click(screen.getAllByText('Sync')[0]);
await waitFor(() => {
const afterSecond = screen
.getAllByTestId(/task-row-/)
.map((row) => row.getAttribute('data-testid'));
expect(afterSecond).not.toEqual(afterFirst);
expect(visibleTaskIds()).toEqual(initialOrder);
});

fireEvent.click(idHeader);
expect(visibleTaskIds()).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test sorts ascending before syncing so it exercises the stale-state case. It then checks the visible row order after sync and after one click. That catches the extra-click bug, which a test that only checks whether the rows eventually change would miss.

});

test('clicking the Status header reorders tasks by status', async () => {
Expand Down
Loading