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

202002491 #2

Merged
merged 1 commit into from
May 14, 2022
Merged
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
53 changes: 49 additions & 4 deletions src/__tests__/todoItem.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
68 changes: 65 additions & 3 deletions src/__tests__/todoList.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
24 changes: 23 additions & 1 deletion src/vo/TodoItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
42 changes: 41 additions & 1 deletion src/vo/TodoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down