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

test: books 테스트 #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
94 changes: 94 additions & 0 deletions backend/src/books/books.controller.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Test, TestingModule } from '@nestjs/testing';
import { BooksController } from './books.controller';
import { BooksService } from './books.service';
import { Order, PaginationOptionsDto } from 'src/common/dtos/page-options.dto';
import { BookGetResponseDto } from './dto/books.dto';
import { Book } from 'src/entities';
import { NotFoundException } from '@nestjs/common';
import { BookDetailResponseDto } from './dto/books.dto';
import { BookIDDto } from './dto/books.dto';

Check warning on line 9 in backend/src/books/books.controller.test.ts

View workflow job for this annotation

GitHub Actions / autofix

'BookIDDto' is defined but never used

Check warning on line 9 in backend/src/books/books.controller.test.ts

View workflow job for this annotation

GitHub Actions / build (20)

'BookIDDto' is defined but never used

Check warning on line 9 in backend/src/books/books.controller.test.ts

View workflow job for this annotation

GitHub Actions / build (22)

'BookIDDto' is defined but never used

describe('BooksController', () => {
let controller: BooksController;
let service: BooksService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [BooksController],
providers: [
{
provide: BooksService,
useValue: {
findAll: jest.fn(),
},
},
],
}).compile();

controller = module.get<BooksController>(BooksController);
service = module.get<BooksService>(BooksService);
});

describe('findAll', () => {
it('should return a list of books with pagination and categories', async () => {
const paginationOption = {
page: 1,
take: 10,
order: Order.ASC,
} as PaginationOptionsDto;
const books = [
{ id: 1, title: 'Book 1', category: { name: 'Category 1' } },
{ id: 2, title: 'Book 2', category: { name: 'Category 1' } },
{ id: 3, title: 'Book 3', category: { name: 'Category 2' } },
] as Book[];
const count = 3;

jest.spyOn(service, 'findAll').mockResolvedValueOnce([books, count]);

const result: BookGetResponseDto =
await controller.findAll(paginationOption);

expect(result).toEqual({
items: books,
categories: [
{ name: 'Category 1', count: 2 },
{ name: 'Category 2', count: 1 },
],
meta: {
itemCount: books.length,
currentPage: paginationOption.page,
itemsPerPage: paginationOption.take,
totalItems: count,
totalPages: Math.ceil(count / paginationOption.take),
},
});
});

describe('findOne', () => {
it('should return a book detail if found', async () => {
const id = 1;
const book = {
id,
title: 'Book 1',
category: { name: 'Category 1' },
} as unknown as BookDetailResponseDto;

jest.spyOn(service, 'findOne').mockResolvedValueOnce(book);

const result = await controller.findOne({ id });

expect(result).toEqual(book);
});

it('should throw NotFoundException if book is not found', async () => {
const id = 1;

jest.spyOn(service, 'findOne').mockResolvedValueOnce(null as never);

await expect(controller.findOne({ id })).rejects.toThrow(
NotFoundException,
);
});
});
});
});
Loading