chat_service.spec.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import chatService from '../../../../../src/services/chat_service/chat_service.js'
  2. const message1 = {
  3. id: '9wLkdcmQXD21Oy8lEX',
  4. idempotency_key: '1',
  5. created_at: (new Date('2020-06-22T18:45:53.000Z'))
  6. }
  7. const message2 = {
  8. id: '9wLkdp6ihaOVdNj8Wu',
  9. idempotency_key: '2',
  10. account_id: '9vmRb29zLQReckr5ay',
  11. created_at: (new Date('2020-06-22T18:45:56.000Z'))
  12. }
  13. const message3 = {
  14. id: '9wLke9zL4Dy4OZR2RM',
  15. idempotency_key: '3',
  16. account_id: '9vmRb29zLQReckr5ay',
  17. created_at: (new Date('2020-07-22T18:45:59.000Z'))
  18. }
  19. describe('chatService', () => {
  20. describe('.add', () => {
  21. it("Doesn't add duplicates", () => {
  22. const chat = chatService.empty()
  23. chatService.add(chat, { messages: [ message1 ] })
  24. chatService.add(chat, { messages: [ message1 ] })
  25. expect(chat.messages.length).to.eql(1)
  26. chatService.add(chat, { messages: [ message2 ] })
  27. expect(chat.messages.length).to.eql(2)
  28. })
  29. it('Updates minId and lastMessage and newMessageCount', () => {
  30. const chat = chatService.empty()
  31. chatService.add(chat, { messages: [ message1 ] })
  32. expect(chat.maxId).to.eql(message1.id)
  33. expect(chat.minId).to.eql(message1.id)
  34. expect(chat.newMessageCount).to.eql(1)
  35. chatService.add(chat, { messages: [ message2 ] })
  36. expect(chat.maxId).to.eql(message2.id)
  37. expect(chat.minId).to.eql(message1.id)
  38. expect(chat.newMessageCount).to.eql(2)
  39. chatService.resetNewMessageCount(chat)
  40. expect(chat.newMessageCount).to.eql(0)
  41. expect(chat.lastSeenMessageId).to.eql(message2.id)
  42. // Add message with higher id
  43. chatService.add(chat, { messages: [ message3 ] })
  44. expect(chat.newMessageCount).to.eql(1)
  45. })
  46. })
  47. describe('.delete', () => {
  48. it('Updates minId and lastMessage', () => {
  49. const chat = chatService.empty()
  50. chatService.add(chat, { messages: [ message1 ] })
  51. chatService.add(chat, { messages: [ message2 ] })
  52. chatService.add(chat, { messages: [ message3 ] })
  53. expect(chat.maxId).to.eql(message3.id)
  54. expect(chat.minId).to.eql(message1.id)
  55. chatService.deleteMessage(chat, message3.id)
  56. expect(chat.maxId).to.eql(message2.id)
  57. expect(chat.minId).to.eql(message1.id)
  58. chatService.deleteMessage(chat, message1.id)
  59. expect(chat.maxId).to.eql(message2.id)
  60. expect(chat.minId).to.eql(message2.id)
  61. })
  62. })
  63. describe('.getView', () => {
  64. it('Inserts date separators', () => {
  65. const chat = chatService.empty()
  66. chatService.add(chat, { messages: [ message1 ] })
  67. chatService.add(chat, { messages: [ message2 ] })
  68. chatService.add(chat, { messages: [ message3 ] })
  69. const view = chatService.getView(chat)
  70. expect(view.map(i => i.type)).to.eql(['date', 'message', 'message', 'date', 'message'])
  71. })
  72. })
  73. })