Skip to content

Commit 9439a2a

Browse files
authored
Merge pull request #12 from fullstacks-academy/add-unit-test
add unit test
2 parents 7b64d2a + 80c258e commit 9439a2a

File tree

8 files changed

+372
-17
lines changed

8 files changed

+372
-17
lines changed

.github/workflows/verify.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ jobs:
2020
- run: pnpm format
2121
- run: pnpm lint
2222
- run: pnpm build
23+
- run: pnpm test

.husky/pre-push

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pnpm run test

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"lint": "eslint .",
1111
"preview": "vite preview",
1212
"format": "prettier --check ./src",
13-
"prepare": "husky"
13+
"prepare": "husky",
14+
"test": "vitest"
1415
},
1516
"dependencies": {
1617
"@dnd-kit/core": "6.3.1",
@@ -37,6 +38,7 @@
3738
"prettier": "3.6.2",
3839
"tailwindcss": "4.1.14",
3940
"typescript": "5.9.3",
40-
"vite": "7.1.10"
41+
"vite": "7.1.10",
42+
"vitest": "3.2.4"
4143
}
4244
}

pnpm-lock.yaml

Lines changed: 316 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Board.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
useReorderColumns,
2323
useReorderTasks,
2424
} from "../lib/useBoard";
25+
import { groupTasksByColumn } from "../utils/groupTaskByColumn";
2526
import { NewColumn } from "./NewColumn";
2627
import { SortableColumn } from "./SortableColumn";
2728
import { TaskCard } from "./TaskCard";
@@ -77,16 +78,7 @@ export function Board() {
7778

7879
const { columns, tasks } = boardData;
7980

80-
const tasksByColumn = tasks.reduce(
81-
(acc, task) => {
82-
if (!acc[task.column_id]) {
83-
acc[task.column_id] = [];
84-
}
85-
acc[task.column_id].push(task);
86-
return acc;
87-
},
88-
{} as Record<string, Task[]>,
89-
);
81+
const tasksByColumn = groupTasksByColumn(tasks);
9082

9183
const handleDragStart = (event: DragStartEvent) => {
9284
const { active } = event;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import type { Task } from "../lib/apiClient";
4+
5+
import { groupTasksByColumn } from "./groupTaskByColumn";
6+
7+
describe("groupTasksByColumn", () => {
8+
it("should group tasks by column", () => {
9+
const tasks: Task[] = [
10+
{
11+
id: "1",
12+
column_id: "1",
13+
title: "Task 1",
14+
description: null,
15+
order_index: 0,
16+
created_at: new Date().toISOString(),
17+
},
18+
{
19+
id: "2",
20+
column_id: "1",
21+
title: "Task 2",
22+
description: null,
23+
order_index: 0,
24+
created_at: new Date().toISOString(),
25+
},
26+
{
27+
id: "3",
28+
column_id: "10",
29+
title: "Task 3",
30+
description: null,
31+
order_index: 0,
32+
created_at: new Date().toISOString(),
33+
},
34+
];
35+
36+
const columns = groupTasksByColumn(tasks);
37+
38+
expect(columns).toEqual({
39+
"1": [tasks[0], tasks[1]],
40+
"10": [tasks[2]],
41+
});
42+
});
43+
});

src/utils/groupTaskByColumn.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { Task } from "../lib/apiClient";
2+
3+
export const groupTasksByColumn = (tasks: Task[]) =>
4+
Object.groupBy(tasks, (t) => t.column_id);

tsconfig.app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
44
"target": "ES2022",
55
"useDefineForClassFields": true,
6-
"lib": ["ES2022", "DOM", "DOM.Iterable"],
6+
"lib": ["ES2024", "DOM", "DOM.Iterable"],
77
"module": "ESNext",
88
"skipLibCheck": true,
99

0 commit comments

Comments
 (0)