|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { BooksController } from './books.controller'; |
| 3 | +import { BooksService } from './books.service'; |
| 4 | +import { Order, PaginationOptionsDto } from 'src/common/dtos/page-options.dto'; |
| 5 | +import { BookGetResponseDto } from './dto/books.dto'; |
| 6 | +import { Book } from 'src/entities'; |
| 7 | +import { NotFoundException } from '@nestjs/common'; |
| 8 | +import { BookDetailResponseDto } from './dto/books.dto'; |
| 9 | +import { BookIDDto } from './dto/books.dto'; |
| 10 | + |
| 11 | +describe('BooksController', () => { |
| 12 | + let controller: BooksController; |
| 13 | + let service: BooksService; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + const module: TestingModule = await Test.createTestingModule({ |
| 17 | + controllers: [BooksController], |
| 18 | + providers: [ |
| 19 | + { |
| 20 | + provide: BooksService, |
| 21 | + useValue: { |
| 22 | + findAll: jest.fn(), |
| 23 | + }, |
| 24 | + }, |
| 25 | + ], |
| 26 | + }).compile(); |
| 27 | + |
| 28 | + controller = module.get<BooksController>(BooksController); |
| 29 | + service = module.get<BooksService>(BooksService); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('findAll', () => { |
| 33 | + it('should return a list of books with pagination and categories', async () => { |
| 34 | + const paginationOption = { |
| 35 | + page: 1, |
| 36 | + take: 10, |
| 37 | + order: Order.ASC, |
| 38 | + } as PaginationOptionsDto; |
| 39 | + const books = [ |
| 40 | + { id: 1, title: 'Book 1', category: { name: 'Category 1' } }, |
| 41 | + { id: 2, title: 'Book 2', category: { name: 'Category 1' } }, |
| 42 | + { id: 3, title: 'Book 3', category: { name: 'Category 2' } }, |
| 43 | + ] as Book[]; |
| 44 | + const count = 3; |
| 45 | + |
| 46 | + jest.spyOn(service, 'findAll').mockResolvedValueOnce([books, count]); |
| 47 | + |
| 48 | + const result: BookGetResponseDto = |
| 49 | + await controller.findAll(paginationOption); |
| 50 | + |
| 51 | + expect(result).toEqual({ |
| 52 | + items: books, |
| 53 | + categories: [ |
| 54 | + { name: 'Category 1', count: 2 }, |
| 55 | + { name: 'Category 2', count: 1 }, |
| 56 | + ], |
| 57 | + meta: { |
| 58 | + itemCount: books.length, |
| 59 | + currentPage: paginationOption.page, |
| 60 | + itemsPerPage: paginationOption.take, |
| 61 | + totalItems: count, |
| 62 | + totalPages: Math.ceil(count / paginationOption.take), |
| 63 | + }, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('findOne', () => { |
| 68 | + it('should return a book detail if found', async () => { |
| 69 | + const id = 1; |
| 70 | + const book = { |
| 71 | + id, |
| 72 | + title: 'Book 1', |
| 73 | + category: { name: 'Category 1' }, |
| 74 | + } as unknown as BookDetailResponseDto; |
| 75 | + |
| 76 | + jest.spyOn(service, 'findOne').mockResolvedValueOnce(book); |
| 77 | + |
| 78 | + const result = await controller.findOne({ id }); |
| 79 | + |
| 80 | + expect(result).toEqual(book); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should throw NotFoundException if book is not found', async () => { |
| 84 | + const id = 1; |
| 85 | + |
| 86 | + jest.spyOn(service, 'findOne').mockResolvedValueOnce(null as never); |
| 87 | + |
| 88 | + await expect(controller.findOne({ id })).rejects.toThrow( |
| 89 | + NotFoundException, |
| 90 | + ); |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments