@@ -3,7 +3,7 @@ import { DataSource } from 'typeorm';
33import { ChatRepository } from './chat.repository' ;
44import { Chat } from './entities/chat.entity' ;
55import { PaginationService } from 'src/shared/services/pagination/pagination.service' ;
6- import { InternalServerErrorException } from '@nestjs/common' ;
6+ import { InternalServerErrorException , NotFoundException } from '@nestjs/common' ;
77import { CreateChatDto , GetChatsQueryDto } from './dto' ;
88
99describe ( 'ChatRepository' , ( ) => {
@@ -33,17 +33,26 @@ describe('ChatRepository', () => {
3333 last_message : null ,
3434 } ;
3535
36+ let mock_data_source : any ;
37+
3638 beforeEach ( async ( ) => {
3739 const mock_entity_manager = { } as any ;
3840
41+ const mock_user_repository = {
42+ findOne : jest . fn ( ) ,
43+ } ;
44+
45+ mock_data_source = {
46+ createEntityManager : jest . fn ( ( ) => mock_entity_manager ) ,
47+ getRepository : jest . fn ( ( ) => mock_user_repository ) ,
48+ } ;
49+
3950 const module : TestingModule = await Test . createTestingModule ( {
4051 providers : [
4152 ChatRepository ,
4253 {
4354 provide : DataSource ,
44- useValue : {
45- createEntityManager : jest . fn ( ( ) => mock_entity_manager ) ,
46- } ,
55+ useValue : mock_data_source ,
4756 } ,
4857 {
4958 provide : PaginationService ,
@@ -72,6 +81,11 @@ describe('ChatRepository', () => {
7281 describe ( 'createChat' , ( ) => {
7382 it ( 'should create a new chat successfully' , async ( ) => {
7483 const create_dto : CreateChatDto = { recipient_id : 'user-2' } ;
84+
85+ // Mock user repository to find recipient
86+ const mock_user_repository = mock_data_source . getRepository ( ) ;
87+ mock_user_repository . findOne . mockResolvedValue ( { id : 'user-2' , username : 'user2' } ) ;
88+
7589 jest . spyOn ( chat_repository , 'findOne' ) . mockResolvedValue ( null ) ;
7690
7791 const result = await chat_repository . createChat ( 'user-1' , create_dto ) ;
@@ -86,6 +100,11 @@ describe('ChatRepository', () => {
86100
87101 it ( 'should return existing chat if it exists' , async ( ) => {
88102 const create_dto : CreateChatDto = { recipient_id : 'user-2' } ;
103+
104+ // Mock user repository to find recipient
105+ const mock_user_repository = mock_data_source . getRepository ( ) ;
106+ mock_user_repository . findOne . mockResolvedValue ( { id : 'user-2' , username : 'user2' } ) ;
107+
89108 jest . spyOn ( chat_repository , 'findOne' ) . mockResolvedValue ( mock_chat as any ) ;
90109
91110 const result = await chat_repository . createChat ( 'user-1' , create_dto ) ;
@@ -94,9 +113,24 @@ describe('ChatRepository', () => {
94113 expect ( chat_repository . create ) . not . toHaveBeenCalled ( ) ;
95114 } ) ;
96115
116+ it ( 'should throw NotFoundException if recipient does not exist' , async ( ) => {
117+ const create_dto : CreateChatDto = { recipient_id : 'non-existent-user' } ;
118+
119+ // Mock user repository to not find recipient
120+ const mock_user_repository = mock_data_source . getRepository ( ) ;
121+ mock_user_repository . findOne . mockResolvedValue ( null ) ;
122+
123+ await expect ( chat_repository . createChat ( 'user-1' , create_dto ) ) . rejects . toThrow (
124+ NotFoundException
125+ ) ;
126+ } ) ;
127+
97128 it ( 'should throw InternalServerErrorException on error' , async ( ) => {
98129 const create_dto : CreateChatDto = { recipient_id : 'user-2' } ;
99- jest . spyOn ( chat_repository , 'findOne' ) . mockRejectedValue ( new Error ( 'DB error' ) ) ;
130+
131+ // Mock user repository to throw error
132+ const mock_user_repository = mock_data_source . getRepository ( ) ;
133+ mock_user_repository . findOne . mockRejectedValue ( new Error ( 'DB error' ) ) ;
100134
101135 await expect ( chat_repository . createChat ( 'user-1' , create_dto ) ) . rejects . toThrow (
102136 InternalServerErrorException
0 commit comments