GettingMessagesTests.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const { ObjectId } = require('../../../app/js/mongodb')
  2. const { expect } = require('chai')
  3. const ChatClient = require('./helpers/ChatClient')
  4. const ChatApp = require('./helpers/ChatApp')
  5. describe('Getting messages', async function () {
  6. const userId1 = ObjectId().toString()
  7. const userId2 = ObjectId().toString()
  8. const content1 = 'foo bar'
  9. const content2 = 'hello world'
  10. before(async function () {
  11. await ChatApp.ensureRunning()
  12. })
  13. describe('globally', async function () {
  14. const projectId = ObjectId().toString()
  15. before(async function () {
  16. const { response } = await ChatClient.sendGlobalMessage(
  17. projectId,
  18. userId1,
  19. content1
  20. )
  21. expect(response.statusCode).to.equal(201)
  22. const { response: response2 } = await ChatClient.sendGlobalMessage(
  23. projectId,
  24. userId2,
  25. content2
  26. )
  27. expect(response2.statusCode).to.equal(201)
  28. })
  29. it('should contain the messages and populated users when getting the messages', async function () {
  30. const { response, body: messages } = await ChatClient.getGlobalMessages(
  31. projectId
  32. )
  33. expect(response.statusCode).to.equal(200)
  34. expect(messages.length).to.equal(2)
  35. messages.reverse()
  36. expect(messages[0].content).to.equal(content1)
  37. expect(messages[0].user_id).to.equal(userId1)
  38. expect(messages[1].content).to.equal(content2)
  39. expect(messages[1].user_id).to.equal(userId2)
  40. })
  41. })
  42. describe('from all the threads', async function () {
  43. const projectId = ObjectId().toString()
  44. const threadId1 = ObjectId().toString()
  45. const threadId2 = ObjectId().toString()
  46. before(async function () {
  47. const { response } = await ChatClient.sendMessage(
  48. projectId,
  49. threadId1,
  50. userId1,
  51. 'one'
  52. )
  53. expect(response.statusCode).to.equal(201)
  54. const { response: response2 } = await ChatClient.sendMessage(
  55. projectId,
  56. threadId2,
  57. userId2,
  58. 'two'
  59. )
  60. expect(response2.statusCode).to.equal(201)
  61. const { response: response3 } = await ChatClient.sendMessage(
  62. projectId,
  63. threadId1,
  64. userId1,
  65. 'three'
  66. )
  67. expect(response3.statusCode).to.equal(201)
  68. const { response: response4 } = await ChatClient.sendMessage(
  69. projectId,
  70. threadId2,
  71. userId2,
  72. 'four'
  73. )
  74. expect(response4.statusCode).to.equal(201)
  75. })
  76. it('should contain a dictionary of threads with messages with populated users', async function () {
  77. const { response, body: threads } = await ChatClient.getThreads(projectId)
  78. expect(response.statusCode).to.equal(200)
  79. expect(Object.keys(threads).length).to.equal(2)
  80. const thread1 = threads[threadId1]
  81. expect(thread1.messages.length).to.equal(2)
  82. const thread2 = threads[threadId2]
  83. expect(thread2.messages.length).to.equal(2)
  84. expect(thread1.messages[0].content).to.equal('one')
  85. expect(thread1.messages[0].user_id).to.equal(userId1)
  86. expect(thread1.messages[1].content).to.equal('three')
  87. expect(thread1.messages[1].user_id).to.equal(userId1)
  88. expect(thread2.messages[0].content).to.equal('two')
  89. expect(thread2.messages[0].user_id).to.equal(userId2)
  90. expect(thread2.messages[1].content).to.equal('four')
  91. expect(thread2.messages[1].user_id).to.equal(userId2)
  92. })
  93. })
  94. })