|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { BookingService } from './booking.service'; |
| 3 | +import { BookingRepository } from '../infrastructure/repositories/booking.repository'; |
| 4 | +import { Booking } from '../domain/booking.entity'; |
| 5 | +import { CreateBookingDto } from '../presentation/dto/create-booking.dto'; |
| 6 | +import { UpdateBookingDto } from '../presentation/dto/update-booking.dto'; |
| 7 | + |
| 8 | +describe('BookingService', () => { |
| 9 | + let service: BookingService; |
| 10 | + let repository: BookingRepository; |
| 11 | + |
| 12 | + const mockBookingRepository = { |
| 13 | + findAll: jest.fn(), |
| 14 | + create: jest.fn(), |
| 15 | + findById: jest.fn(), |
| 16 | + update: jest.fn(), |
| 17 | + delete: jest.fn(), |
| 18 | + }; |
| 19 | + |
| 20 | + const mockBooking = new Booking( |
| 21 | + '60d5f5070000000000000000', |
| 22 | + 'John Doe', |
| 23 | + new Date(), |
| 24 | + 'pending', |
| 25 | + new Date(), |
| 26 | + new Date(), |
| 27 | + ); |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + const module: TestingModule = await Test.createTestingModule({ |
| 31 | + providers: [ |
| 32 | + BookingService, |
| 33 | + { |
| 34 | + provide: BookingRepository, |
| 35 | + useValue: mockBookingRepository, |
| 36 | + }, |
| 37 | + ], |
| 38 | + }).compile(); |
| 39 | + |
| 40 | + service = module.get<BookingService>(BookingService); |
| 41 | + repository = module.get<BookingRepository>(BookingRepository); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should be defined', () => { |
| 45 | + expect(service).toBeDefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('findAll', () => { |
| 49 | + it('should return an array of bookings', async () => { |
| 50 | + const result = [mockBooking]; |
| 51 | + mockBookingRepository.findAll.mockResolvedValue(result); |
| 52 | + |
| 53 | + expect(await service.findAll()).toBe(result); |
| 54 | + expect(repository.findAll).toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('create', () => { |
| 59 | + it('should create a new booking with pending status', async () => { |
| 60 | + const createDto: CreateBookingDto = { |
| 61 | + customerName: 'John Doe', |
| 62 | + date: new Date(), |
| 63 | + }; |
| 64 | + mockBookingRepository.create.mockResolvedValue(mockBooking); |
| 65 | + |
| 66 | + expect(await service.create(createDto)).toBe(mockBooking); |
| 67 | + expect(repository.create).toHaveBeenCalledWith({ |
| 68 | + ...createDto, |
| 69 | + status: 'pending', |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('findOne', () => { |
| 75 | + it('should return a booking if found', async () => { |
| 76 | + mockBookingRepository.findById.mockResolvedValue(mockBooking); |
| 77 | + |
| 78 | + expect(await service.findOne('1')).toBe(mockBooking); |
| 79 | + expect(repository.findById).toHaveBeenCalledWith('1'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should throw an error if booking not found', async () => { |
| 83 | + mockBookingRepository.findById.mockResolvedValue(null); |
| 84 | + |
| 85 | + await expect(service.findOne('1')).rejects.toThrow( |
| 86 | + 'Booking with ID 1 not found', |
| 87 | + ); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('update', () => { |
| 92 | + it('should return updated booking if successful', async () => { |
| 93 | + const updateDto: UpdateBookingDto = { customerName: 'Jane Doe' }; |
| 94 | + const updatedBooking = { ...mockBooking, customerName: 'Jane Doe' }; |
| 95 | + mockBookingRepository.update.mockResolvedValue(updatedBooking); |
| 96 | + |
| 97 | + expect(await service.update('1', updateDto)).toBe(updatedBooking); |
| 98 | + expect(repository.update).toHaveBeenCalledWith('1', updateDto); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should throw an error if booking not found', async () => { |
| 102 | + mockBookingRepository.update.mockResolvedValue(null); |
| 103 | + |
| 104 | + await expect(service.update('1', {})).rejects.toThrow( |
| 105 | + 'Booking with ID 1 not found', |
| 106 | + ); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('remove', () => { |
| 111 | + it('should delete booking if found', async () => { |
| 112 | + mockBookingRepository.delete.mockResolvedValue(true); |
| 113 | + |
| 114 | + await expect(service.remove('1')).resolves.toBeUndefined(); |
| 115 | + expect(repository.delete).toHaveBeenCalledWith('1'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should throw an error if booking not found', async () => { |
| 119 | + mockBookingRepository.delete.mockResolvedValue(false); |
| 120 | + |
| 121 | + await expect(service.remove('1')).rejects.toThrow( |
| 122 | + 'Booking with ID 1 not found', |
| 123 | + ); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments