diff --git a/src/__tests__/todoItem.spec.js b/src/__tests__/todoItem.spec.js index decc062..d787407 100644 --- a/src/__tests__/todoItem.spec.js +++ b/src/__tests__/todoItem.spec.js @@ -1,7 +1,52 @@ import TodoItem from "../vo/TodoItem"; -describe("테스트 그룹", () => { - test("테스트", () => { - expect(0).toEqual(0); +describe("할 일을 만들 수 있다.", () => { + test("todo item 생성하기", () => { + const todoItem = new TodoItem(1, "오늘은 술 먹는날"); + + expect(todoItem.task).toEqual("오늘은 술 먹는날"); + }); +}); + +describe("할 일을 업데이트 할 수 있다.", () => { + test("todo item 업데이트 하기", () => { + const todoItem = new TodoItem(1, "오늘은 술 먹는날"); + todoItem.updateTask("오늘은 그냥 안 마실래"); + expect(todoItem.task).toEqual("오늘은 그냥 안 마실래"); + }); +}); + +describe("할 일을 완료로 바꿀 수 있다.", () => { + test("todo item 완료로 바꾸기", () => { + const todoItem = new TodoItem(1, "오늘은 술 먹는날"); + todoItem.setComplete(); + expect(todoItem.completed).toBeTruthy(); + }); + test("todo item 미완료로 바꾸기", () => { + const todoItem = new TodoItem(1, "오늘은 술 먹는날"); + todoItem.unsetComplete(); + expect(todoItem.completed).toBeFalsy(); + }); +}); + +describe("할 일에 날짜가 들어간다.", () => { + test("todo item이 오늘 만들었으면, isToday가 true이다.", () => { + const sourceDate = new Date(new Date()); + const todoItem = new TodoItem(1, "오늘은 술 먹는날", sourceDate); + const targetDate = new Date(new Date()); + expect(todoItem.equalsDayOfCreatedAt(targetDate)).toBeTruthy(); + }); + test("todo item이 어제 만들었으면, isToday가 false이다.", () => { + const sourceDate = new Date('2022-05-08T10:00:00'); + const todoItem = new TodoItem(1, "오늘은 술 먹는날", sourceDate); + const targetDate = new Date('2022-05-09T10:00:00'); + expect(todoItem.equalsDayOfCreatedAt(targetDate)).toBeFalsy(); + }); +}); + +describe("할 일을 완료로 생성할 수 수 있다.", () => { + test("todo item 완료로 만들기", () => { + const todoItem = new TodoItem(1, "오늘은 술 먹는날", new Date(), true); + expect(todoItem.completed).toBeTruthy(); }); -}); \ No newline at end of file +}); diff --git a/src/__tests__/todoList.spec.js b/src/__tests__/todoList.spec.js index 17d1d5b..a24dd59 100644 --- a/src/__tests__/todoList.spec.js +++ b/src/__tests__/todoList.spec.js @@ -1,8 +1,70 @@ import TodoItem from "../vo/TodoItem"; import TodoList from "../vo/TodoList"; -describe("테스트 그룹", () => { - test("테스트", () => { - expect(0).toEqual(0); +let todoItem1, todoItem2, todoItem3, todoItem4, todoItem5; +let todoList; + +beforeEach(() => { + todoItem1 = new TodoItem(1, "할 일 1", new Date()); + todoItem2 = new TodoItem(2, "할 일 2", new Date()); + todoItem3 = new TodoItem(3, "할 일 3", new Date()); + todoItem4 = new TodoItem(4, "할 일 4", new Date()); + todoItem5 = new TodoItem(5, "할 일 5", new Date()); + todoList = new TodoList([todoItem1, todoItem2, todoItem3, todoItem4, todoItem5], new Date()); +}); + + +describe("할 일 목록을 가지고 있다.", () => { + test("5개를 만들면, 5개가 있다.", () => { + expect(todoList.items).toHaveLength(5); + }); +}); + +describe("할 일 목록에서 삭제를 할 수 있다.", () => { + test("5개의 할 일이 있는데, id가 3인 할 일을 삭제할 수 있다..", () => { + todoList.removeTodoItem(3); + expect(todoList.items).toHaveLength(4); + expect(todoList.items.some((todoItem) => todoItem.id === 3)).toBeFalsy(); + }); +}); + +describe("할 일 목록에서 할 일을 추가할 수 있다.", () => { + test("5개의 할 일이 있는데, id가 6인 할 일을 추가할 수 있다.", () => { + const todoItem6 = new TodoItem(6, '할 일 6', new Date()); + + todoList.pushTodoItem(todoItem6); + + expect(todoList.items).toHaveLength(6); + expect(todoList.items.some((todoItem) => todoItem.id === 6)).toBeTruthy(); + }); +}); + +describe("생성한 할 일들 중에서 오늘 할 일, 지난 할 일 구분하기.", () => { + test("5개의 할 일이 있는데, 2번 빼고 오늘 만든 일이다.", () => { + jest.spyOn(todoItem2, "equalsDayOfCreatedAt").mockImplementation(() => false); + + expect(todoList.equalsDayItems).toHaveLength(4); + expect(todoList.equalsDayItems.some((todoItem) => todoItem.id === 2)).toBeFalsy(); + }); + + test("5개의 할 일이 있는데, id 2번만 지난 할 일이다.", () => { + jest.spyOn(todoItem2, "equalsDayOfCreatedAt").mockImplementation(() => false); + + expect(todoList.notEqualsDayItems).toHaveLength(1); + expect(todoList.notEqualsDayItems.some((todoItem) => todoItem.id === 2)).toBeTruthy(); + }); +}); + +describe("오늘 할 일 중 완료/미완료 구분하기", () => { + test("5개의 할 일이 있는데, 2번, 3번만 완료다.", () => { + jest.spyOn(todoItem2, "completed", "get").mockReturnValue(true); + jest.spyOn(todoItem3, "completed", "get").mockReturnValue(true); + expect(todoList.equalsDayAndCompletedItems).toHaveLength(2); + }); + test("5개의 할 일이 있는데, 2번, 4번만 미완료다.", () => { + jest.spyOn(todoItem1, "completed", "get").mockReturnValue(true); + jest.spyOn(todoItem3, "completed", "get").mockReturnValue(true); + jest.spyOn(todoItem5, "completed", "get").mockReturnValue(true); + expect(todoList.equalsDayAndNotCompletedItems).toHaveLength(2); }); }); diff --git a/src/vo/TodoItem.js b/src/vo/TodoItem.js index 2ac7088..bceb396 100644 --- a/src/vo/TodoItem.js +++ b/src/vo/TodoItem.js @@ -12,11 +12,33 @@ class TodoItem { _createdAt = ""; _completed = false; - constructor() { + constructor(id, task, createdAt, completed = false) { makeObservable(this, { _task: observable, _completed: observable, }); + this._id = id; + this._task = task; + this._createdAt = createdAt; + this._completed = completed; + } + + setComplete = () => { + this._completed = true; + } + + unsetComplete = () => { + this._completed = false; + } + + updateTask = (task) => { + this._task = task; + } + + equalsDayOfCreatedAt = (_targetDate) => { + const sourceDate = new Date(this._createdAt).setHours(0, 0, 0, 0); + const targetDate = new Date(_targetDate).setHours(0, 0, 0, 0); + return sourceDate === targetDate; } get id() { diff --git a/src/vo/TodoList.js b/src/vo/TodoList.js index 2685d97..81fba74 100644 --- a/src/vo/TodoList.js +++ b/src/vo/TodoList.js @@ -8,10 +8,50 @@ class TodoList { _items = []; _date = ""; - constructor() { + constructor(items, date) { makeObservable(this, { _items: observable, }); + + this._items = items; + this._date = date; + } + + get items() { + return this._items; + } + + removeTodoItem = (todoId) => { + const targetTodoItemIndex = this._items.findIndex( + (todo) => todo.id === todoId + ); + if (targetTodoItemIndex === -1) return; + this._items.splice(targetTodoItemIndex, 1); + } + + pushTodoItem = (todoItem) => { + this._items.push(todoItem); + } + + _equalsDayFilter = (todoItem) => todoItem.equalsDayOfCreatedAt(this._date); + _notEqualsDayFilter = (todoItem) => !todoItem.equalsDayOfCreatedAt(this._date); + _completedFilter = (todoItem) => todoItem.completed; + _notCompletedFilter = (todoItem) => !todoItem.completed; + + get equalsDayItems() { + return this._items.filter(this._equalsDayFilter); + } + + get notEqualsDayItems() { + return this._items.filter(this._notEqualsDayFilter); + } + + get equalsDayAndCompletedItems() { + return this.equalsDayItems.filter(this._completedFilter); + } + + get equalsDayAndNotCompletedItems() { + return this.equalsDayItems.filter(this._notCompletedFilter); } }