chat_service.spec.js 2.8 KB

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