Skip to content

Commit

Permalink
Merge pull request #40 from nori-dongsan/feat/#30
Browse files Browse the repository at this point in the history
feat: 댓글, 커뮤 test 추가
  • Loading branch information
gunom authored Jul 22, 2022
2 parents cfae55d + cfaca80 commit 946e6cf
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 6 deletions.
67 changes: 67 additions & 0 deletions test/unit/services/BoardCommentService.test.ts
Original file line number Diff line number Diff line change
@@ -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
}])
})
})
53 changes: 47 additions & 6 deletions test/unit/services/BoardService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
})
})

0 comments on commit 946e6cf

Please sign in to comment.