Skip to content

Commit

Permalink
test: Fix the unit tests for the methods removeMessages and getAvaila…
Browse files Browse the repository at this point in the history
…bleMessageCount
  • Loading branch information
gabrielmatau79 committed Oct 17, 2024
1 parent e76ac11 commit 67676b0
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions packages/server/src/websocket/websocket.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,23 @@ describe('WebsocketService', () => {
expect(result[1].encryptedMessage).toBe('test-message-2') // Verifica por 'id'
})

it('should getAvailableMessageCount of available messages from Redis', async () => {
// Mock Redis to return a predefined count of messages
jest.spyOn(redisMock, 'llen').mockResolvedValue(5)
it('should getAvailableMessageCount of available messages from Redis and MongoDB', async () => {
const connectionId = 'test-connection-id'

// Mock Redis response
jest.spyOn(redisMock, 'llen').mockResolvedValue(5) // simula 5 mensajes en Redis

// Mock MongoDB countDocuments response
storeQueuedMessageMock.countDocuments = jest.fn().mockResolvedValue(3) // simula 3 mensajes en MongoDB

// Execute the getAvailableMessageCount method
const result = await service.getAvailableMessageCount({
connectionId: 'test-connection-id',
id: '1',
})

// Verify that Redis was called with the correct key
expect(redisMock.llen).toHaveBeenCalledWith('connectionId:test-connection-id:queuemessages')

// Verify the returned count of available messages
expect(result).toBe(5)
expect(result).toBe(8) // 5 mensajes de Redis + 3 mensajes de MongoDB
expect(redisMock.llen).toHaveBeenCalledWith(`connectionId:${connectionId}:queuemessages`)
expect(storeQueuedMessageMock.countDocuments).toHaveBeenCalledWith({ connectionId })
})

it('should addmessage method to the queue and publish it to Redis', async () => {
Expand Down Expand Up @@ -214,7 +216,7 @@ describe('WebsocketService', () => {
// Verify that messages were removed from MongoDB
expect(storeQueuedMessageMock.deleteMany).toHaveBeenCalledWith({
connectionId: removeMessagesDto.connectionId,
_id: { $in: removeMessagesDto.messageIds.map((id) => new Object(id)) },
messageId: { $in: removeMessagesDto.messageIds.map((id) => new Object(id)) },
})
})
})

0 comments on commit 67676b0

Please sign in to comment.