diff --git a/test/unit/services/BoardCommentService.test.ts b/test/unit/services/BoardCommentService.test.ts new file mode 100644 index 0000000..40bb770 --- /dev/null +++ b/test/unit/services/BoardCommentService.test.ts @@ -0,0 +1,67 @@ +import { Connection } from "typeorm" +import { BoardCommentRepository } from "../../../src/repositories/BoardCommentRepository" +import { UserRepository } from "../../../src/repositories/UserRepository"; +import { BoardService } from "../../../src/services/BoardService"; +import { createMemoryDatabase } from "../../utils/CreateMemoryDatabase"; +import { UserSeed } from "../../utils/seeds/UserTestSeed"; +import { BoardCommentService } from "../../../src/services/BoardCommentService" +import { BoardRepository } from "../../../src/repositories/BoardRepository"; +import { BoardSeed } from "../../utils/seeds/BoardTestSeed"; +import { UserService } from "../../../src/services/UserService" +import { BoardCommentCreateDto } from "../../../src/dtos/BoardCommentDto"; + +describe("BoardCommentService", () => { + let db: Connection; + let boardCommentRepository: BoardCommentRepository; + let userRepository: UserRepository; + let boardCommentService: BoardCommentService; + let boardService: BoardService; + let boardRepository: BoardRepository; + let userService: UserService; + + beforeAll(async () => { + db = await createMemoryDatabase(); + userRepository = db.getCustomRepository(UserRepository); + await userRepository.save(UserSeed); + boardRepository = db.getCustomRepository(BoardRepository); + await boardRepository.save(BoardSeed); + boardCommentRepository = db.getCustomRepository(BoardCommentRepository); + userService = new UserService(userRepository); + boardService = new BoardService(boardRepository); + boardCommentService = new BoardCommentService(boardCommentRepository); + }) + + afterAll(() => db.close()); + + const boardId = 4 + const userId = 1 + + it("댓글 생성", async () => { + const board = await boardService.get(boardId) + const user = await userService.getUser(userId) + const commentCreateDto = new BoardCommentCreateDto + commentCreateDto.board = board! + commentCreateDto.user = user! + commentCreateDto.content = "test" + + await boardCommentService.create(commentCreateDto) + + const comment = await boardCommentService.getListByBoard(board!) + const commentDto = comment?.map((value) => { + return { + id: value.id, + content: value.content, + createAt: `2022-07-20T16:13:28.000Z`, + updateAt: `2022-07-20T16:13:28.000Z`, + userId: value.user.id + } + }) + expect(commentDto).toEqual([{ + id: 1, + content: 'test', + createAt: '2022-07-20T16:13:28.000Z', + updateAt: '2022-07-20T16:13:28.000Z', + userId: 1 + }]) + }) +}) \ No newline at end of file diff --git a/test/unit/services/BoardService.test.ts b/test/unit/services/BoardService.test.ts index baf5904..e7bb442 100644 --- a/test/unit/services/BoardService.test.ts +++ b/test/unit/services/BoardService.test.ts @@ -6,6 +6,7 @@ import { BoardSeed } from "../../utils/seeds/BoardTestSeed"; import { UserRepository } from "../../../src/repositories/UserRepository"; import { UserSeed } from "../../utils/seeds/UserTestSeed"; import { Board } from "../../../src/entities/Board"; +import { BoardDto, BoardPutDto } from "../../../src/dtos/BoardDto"; describe("BoardService", () => { let db: Connection; @@ -26,30 +27,70 @@ describe("BoardService", () => { afterAll(() => db.close()); it("전체 게시물 리스트를 조회한다.", async () => { - const boards = await boardService.getList(); + const boards = await boardService.getList(1); const boardDtos = boards.map((value: Board) => { const boardDto = { id: value.id, section: value.section, title: value.title, content: value.content, - createdAt: value.createdAt, - updatedAt: value.updatedAt } return boardDto }) expect(boardDtos).toEqual([ { id: 4, - section: '', + section: undefined, title: 'test', content: 'testtest', - createdAt: new Date("2022-07-16T00:07:46.889Z"), - updatedAt: new Date("2022-07-16T00:07:46.889Z") } ]) }) + const boardId = 4 + it("게시물 조회", async () => { + const board = await boardService.get(boardId) + const boardDto = new BoardDto() + if (board) { + boardDto.author = false + boardDto.title = board.title + boardDto.content = board.content + } + expect(boardDto).toEqual({ + author: false, + title: 'test', + content: 'testtest', + replyCount: 0, + likeCount: 0 + }) + }) + + it("게시물 수정", async () => { + const boardPutDto = new BoardPutDto() + boardPutDto.boardId = boardId + boardPutDto.content = 'test2' + boardPutDto.title = 'title2' + await boardService.put(boardPutDto) + const board = await boardService.get(boardId) + const boardDto = new BoardDto() + if (board) { + boardDto.author = false + boardDto.title = board.title + boardDto.content = board.content + } + expect(boardDto).toEqual({ + author: false, + title: 'title2', + content: 'test2', + replyCount: 0, + likeCount: 0 + }) + }) + it("게시물 삭제", async () => { + await boardService.delete(boardId) + const board = await boardService.get(boardId) + expect(board).toEqual(undefined) + }) }) \ No newline at end of file