chat_service.spec.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // TODO: only
  17. describe.only('chatService', () => {
  18. describe('.add', () => {
  19. it("Doesn't add duplicates", () => {
  20. const chat = chatService.empty()
  21. chatService.add(chat, { messages: [ message1 ] })
  22. chatService.add(chat, { messages: [ message1 ] })
  23. expect(chat.messages.length).to.eql(1)
  24. chatService.add(chat, { messages: [ message2 ] })
  25. expect(chat.messages.length).to.eql(2)
  26. })
  27. it('Updates minId and lastMessage and newMessageCount', () => {
  28. const chat = chatService.empty()
  29. chatService.add(chat, { messages: [ message1 ] })
  30. expect(chat.lastMessage.id).to.eql(message1.id)
  31. expect(chat.minId).to.eql(message1.id)
  32. expect(chat.newMessageCount).to.eql(1)
  33. chatService.add(chat, { messages: [ message2 ] })
  34. expect(chat.lastMessage.id).to.eql(message2.id)
  35. expect(chat.minId).to.eql(message1.id)
  36. expect(chat.newMessageCount).to.eql(2)
  37. chatService.resetNewMessageCount(chat)
  38. expect(chat.newMessageCount).to.eql(0)
  39. const createdAt = new Date()
  40. createdAt.setSeconds(createdAt.getSeconds() + 10)
  41. chatService.add(chat, { messages: [ { message3, created_at: createdAt } ] })
  42. expect(chat.newMessageCount).to.eql(1)
  43. })
  44. })
  45. describe('.delete', () => {
  46. it('Updates minId and lastMessage', () => {
  47. const chat = chatService.empty()
  48. chatService.add(chat, { messages: [ message1 ] })
  49. chatService.add(chat, { messages: [ message2 ] })
  50. chatService.add(chat, { messages: [ message3 ] })
  51. expect(chat.lastMessage.id).to.eql(message3.id)
  52. expect(chat.minId).to.eql(message1.id)
  53. chatService.deleteMessage(chat, message3.id)
  54. expect(chat.lastMessage.id).to.eql(message2.id)
  55. expect(chat.minId).to.eql(message1.id)
  56. chatService.deleteMessage(chat, message1.id)
  57. expect(chat.lastMessage.id).to.eql(message2.id)
  58. expect(chat.minId).to.eql(message2.id)
  59. })
  60. })
  61. describe('.getView', () => {
  62. it('Inserts date separators', () => {
  63. const chat = chatService.empty()
  64. chatService.add(chat, { messages: [ message1 ] })
  65. chatService.add(chat, { messages: [ message2 ] })
  66. chatService.add(chat, { messages: [ message3 ] })
  67. const view = chatService.getView(chat)
  68. expect(view.map(i => i.type)).to.eql(['date', 'message', 'message', 'date', 'message'])
  69. })
  70. })
  71. })