2020-06-22 00:13:29 +10:00
|
|
|
import chatService from '../../../../../src/services/chat_service/chat_service.js'
|
|
|
|
|
|
|
|
const message1 = {
|
|
|
|
id: '9wLkdcmQXD21Oy8lEX',
|
2020-10-29 21:33:06 +11:00
|
|
|
idempotency_key: '1',
|
2020-06-22 00:13:29 +10:00
|
|
|
created_at: (new Date('2020-06-22T18:45:53.000Z'))
|
|
|
|
}
|
|
|
|
|
|
|
|
const message2 = {
|
|
|
|
id: '9wLkdp6ihaOVdNj8Wu',
|
2020-10-29 21:33:06 +11:00
|
|
|
idempotency_key: '2',
|
2020-06-22 00:13:29 +10:00
|
|
|
account_id: '9vmRb29zLQReckr5ay',
|
|
|
|
created_at: (new Date('2020-06-22T18:45:56.000Z'))
|
|
|
|
}
|
|
|
|
|
|
|
|
const message3 = {
|
|
|
|
id: '9wLke9zL4Dy4OZR2RM',
|
2020-10-29 21:33:06 +11:00
|
|
|
idempotency_key: '3',
|
2020-06-22 00:13:29 +10:00
|
|
|
account_id: '9vmRb29zLQReckr5ay',
|
|
|
|
created_at: (new Date('2020-07-22T18:45:59.000Z'))
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:19:36 +10:00
|
|
|
describe('chatService', () => {
|
2020-06-22 00:13:29 +10:00
|
|
|
describe('.add', () => {
|
|
|
|
it("Doesn't add duplicates", () => {
|
|
|
|
const chat = chatService.empty()
|
|
|
|
chatService.add(chat, { messages: [ message1 ] })
|
|
|
|
chatService.add(chat, { messages: [ message1 ] })
|
|
|
|
expect(chat.messages.length).to.eql(1)
|
|
|
|
|
|
|
|
chatService.add(chat, { messages: [ message2 ] })
|
|
|
|
expect(chat.messages.length).to.eql(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Updates minId and lastMessage and newMessageCount', () => {
|
|
|
|
const chat = chatService.empty()
|
|
|
|
|
|
|
|
chatService.add(chat, { messages: [ message1 ] })
|
2020-09-16 09:34:19 +10:00
|
|
|
expect(chat.maxId).to.eql(message1.id)
|
2020-06-22 00:13:29 +10:00
|
|
|
expect(chat.minId).to.eql(message1.id)
|
|
|
|
expect(chat.newMessageCount).to.eql(1)
|
|
|
|
|
|
|
|
chatService.add(chat, { messages: [ message2 ] })
|
2020-09-16 09:34:19 +10:00
|
|
|
expect(chat.maxId).to.eql(message2.id)
|
2020-06-22 00:13:29 +10:00
|
|
|
expect(chat.minId).to.eql(message1.id)
|
|
|
|
expect(chat.newMessageCount).to.eql(2)
|
|
|
|
|
|
|
|
chatService.resetNewMessageCount(chat)
|
|
|
|
expect(chat.newMessageCount).to.eql(0)
|
2020-11-02 18:42:09 +11:00
|
|
|
expect(chat.lastSeenMessageId).to.eql(message2.id)
|
2020-06-22 00:13:29 +10:00
|
|
|
|
2020-11-02 18:42:09 +11:00
|
|
|
// Add message with higher id
|
|
|
|
chatService.add(chat, { messages: [ message3 ] })
|
2020-06-22 00:13:29 +10:00
|
|
|
expect(chat.newMessageCount).to.eql(1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('.delete', () => {
|
|
|
|
it('Updates minId and lastMessage', () => {
|
|
|
|
const chat = chatService.empty()
|
|
|
|
|
|
|
|
chatService.add(chat, { messages: [ message1 ] })
|
|
|
|
chatService.add(chat, { messages: [ message2 ] })
|
|
|
|
chatService.add(chat, { messages: [ message3 ] })
|
|
|
|
|
2020-09-16 09:34:19 +10:00
|
|
|
expect(chat.maxId).to.eql(message3.id)
|
2020-06-22 00:13:29 +10:00
|
|
|
expect(chat.minId).to.eql(message1.id)
|
|
|
|
|
|
|
|
chatService.deleteMessage(chat, message3.id)
|
2020-09-16 09:34:19 +10:00
|
|
|
expect(chat.maxId).to.eql(message2.id)
|
2020-06-22 00:13:29 +10:00
|
|
|
expect(chat.minId).to.eql(message1.id)
|
|
|
|
|
|
|
|
chatService.deleteMessage(chat, message1.id)
|
2020-09-16 09:34:19 +10:00
|
|
|
expect(chat.maxId).to.eql(message2.id)
|
2020-06-22 00:13:29 +10:00
|
|
|
expect(chat.minId).to.eql(message2.id)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('.getView', () => {
|
|
|
|
it('Inserts date separators', () => {
|
|
|
|
const chat = chatService.empty()
|
|
|
|
|
|
|
|
chatService.add(chat, { messages: [ message1 ] })
|
|
|
|
chatService.add(chat, { messages: [ message2 ] })
|
|
|
|
chatService.add(chat, { messages: [ message3 ] })
|
|
|
|
|
|
|
|
const view = chatService.getView(chat)
|
|
|
|
expect(view.map(i => i.type)).to.eql(['date', 'message', 'message', 'date', 'message'])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|