-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathDraggableTask.test.tsx
More file actions
142 lines (118 loc) · 3.93 KB
/
Copy pathDraggableTask.test.tsx
File metadata and controls
142 lines (118 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { act } from "react";
import { fireEvent, screen } from "@testing-library/react";
import { render } from "@web/__tests__/__mocks__/mock.render";
import { Task } from "@web/common/types/task.types";
import { DraggableTask } from "@web/views/Day/components/Task/DraggableTask";
import { TaskContext } from "@web/views/Day/context/TaskContext";
const mockTask: Task = {
id: "task-1",
title: "Test Task",
order: 0,
status: "todo",
createdAt: "2023-01-01",
};
const defaultTasksProps: NonNullable<React.ContextType<typeof TaskContext>> = {
tasks: [mockTask, { ...mockTask, id: "task-2", title: "Another Task" }],
editingTaskId: null,
editingTitle: "",
setSelectedTaskIndex: jest.fn(),
onCheckboxKeyDown: jest.fn(),
onInputBlur: jest.fn(),
onInputClick: jest.fn(),
onInputKeyDown: jest.fn(),
onTitleChange: jest.fn(),
onStatusToggle: jest.fn(),
migrateTask: jest.fn(),
deleteTask: jest.fn(),
reorderTasks: jest.fn(),
addTask: jest.fn(),
// Add missing properties with default values/mocks
isAddingTask: false,
isCancellingEdit: false,
selectedTaskIndex: -1,
undoState: null,
undoToastId: null,
restoreTask: jest.fn(),
focusOnCheckbox: jest.fn(),
focusOnInput: jest.fn(),
setEditingTitle: jest.fn(),
setEditingTaskId: jest.fn(),
setIsAddingTask: jest.fn(),
setIsCancellingEdit: jest.fn(),
toggleTaskStatus: jest.fn(),
updateTaskTitle: jest.fn(),
};
const renderDraggableTask = (
task = mockTask,
index = 0,
tasksProps = defaultTasksProps,
) => {
return act(() =>
render(<DraggableTask task={task} index={index} tasksProps={tasksProps} />),
);
};
describe("DraggableTask", () => {
it("should render the task", () => {
renderDraggableTask();
expect(screen.getByDisplayValue("Test Task")).toBeInTheDocument();
});
it("should render drag handle when there are multiple tasks", () => {
renderDraggableTask();
const dragHandle = screen.getByRole("button", {
name: /Reorder Test Task/i,
});
expect(dragHandle).toBeInTheDocument();
});
it("should not render the drag handle when there is only one task", () => {
const tasksProps = {
...defaultTasksProps,
tasks: [mockTask],
};
renderDraggableTask(mockTask, 0, tasksProps);
const dragHandle = screen.queryByRole("button", {
name: /Reorder Test Task/i,
});
expect(dragHandle).not.toBeInTheDocument();
});
it("should have accessible description", () => {
renderDraggableTask();
const dragHandle = screen.getByRole("button", {
name: /Reorder Test Task/i,
});
const descriptionId = dragHandle.getAttribute("aria-describedby");
expect(descriptionId).toBe(`description-${mockTask.id}`);
const description = screen.getByText(
"Press space to start dragging this task.",
);
expect(description).toBeInTheDocument();
expect(description.id).toBe(descriptionId);
});
it("should be hidden by default", () => {
renderDraggableTask();
const dragHandle = screen.getByRole("button", {
name: /Reorder Test Task/i,
});
// Hidden by default
expect(dragHandle).toHaveClass("opacity-0");
// Visible on hover (button)
expect(dragHandle).toHaveClass("hover:opacity-100");
// Visible on group hover (parent)
expect(dragHandle).toHaveClass("group-hover:opacity-100");
// Visible on focus
expect(dragHandle).toHaveClass("focus:opacity-100");
});
test("drag handle should be visible when dragging the handler button", () => {
const draggingTask = { ...mockTask, id: "task-dragging" };
renderDraggableTask(draggingTask);
const dragHandle = screen.getByRole("button", {
name: /Reorder Test Task/i,
});
expect(dragHandle).toHaveClass("opacity-0");
act(() => {
fireEvent.mouseOver(dragHandle);
fireEvent.mouseDown(dragHandle, { bubbles: true });
fireEvent.mouseMove(dragHandle, { clientY: 100 });
});
expect(dragHandle).toHaveClass("opacity-100");
});
});