GettingMessagesTests.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. before(async function () {
  7. this.user_id1 = ObjectId().toString()
  8. this.user_id2 = ObjectId().toString()
  9. this.content1 = 'foo bar'
  10. this.content2 = 'hello world'
  11. await ChatApp.ensureRunning()
  12. })
  13. describe('globally', async function () {
  14. before(async function () {
  15. this.project_id = ObjectId().toString()
  16. await ChatClient.sendGlobalMessage(
  17. this.project_id,
  18. this.user_id1,
  19. this.content1
  20. )
  21. await ChatClient.sendGlobalMessage(
  22. this.project_id,
  23. this.user_id2,
  24. this.content2
  25. )
  26. })
  27. it('should contain the messages and populated users when getting the messages', async function () {
  28. const { body: messages } = await ChatClient.getGlobalMessages(
  29. this.project_id
  30. )
  31. expect(messages.length).to.equal(2)
  32. messages.reverse()
  33. expect(messages[0].content).to.equal(this.content1)
  34. expect(messages[0].user_id).to.equal(this.user_id1)
  35. expect(messages[1].content).to.equal(this.content2)
  36. expect(messages[1].user_id).to.equal(this.user_id2)
  37. })
  38. })
  39. describe('from all the threads', async function () {
  40. before(async function () {
  41. this.project_id = ObjectId().toString()
  42. this.thread_id1 = ObjectId().toString()
  43. this.thread_id2 = ObjectId().toString()
  44. await ChatClient.sendMessage(
  45. this.project_id,
  46. this.thread_id1,
  47. this.user_id1,
  48. 'one'
  49. )
  50. await ChatClient.sendMessage(
  51. this.project_id,
  52. this.thread_id2,
  53. this.user_id2,
  54. 'two'
  55. )
  56. await ChatClient.sendMessage(
  57. this.project_id,
  58. this.thread_id1,
  59. this.user_id1,
  60. 'three'
  61. )
  62. await ChatClient.sendMessage(
  63. this.project_id,
  64. this.thread_id2,
  65. this.user_id2,
  66. 'four'
  67. )
  68. })
  69. it('should contain a dictionary of threads with messages with populated users', async function () {
  70. const { body: threads } = await ChatClient.getThreads(this.project_id)
  71. expect(Object.keys(threads).length).to.equal(2)
  72. const thread1 = threads[this.thread_id1]
  73. expect(thread1.messages.length).to.equal(2)
  74. const thread2 = threads[this.thread_id2]
  75. expect(thread2.messages.length).to.equal(2)
  76. expect(thread1.messages[0].content).to.equal('one')
  77. expect(thread1.messages[0].user_id).to.equal(this.user_id1)
  78. expect(thread1.messages[1].content).to.equal('three')
  79. expect(thread1.messages[1].user_id).to.equal(this.user_id1)
  80. expect(thread2.messages[0].content).to.equal('two')
  81. expect(thread2.messages[0].user_id).to.equal(this.user_id2)
  82. expect(thread2.messages[1].content).to.equal('four')
  83. expect(thread2.messages[1].user_id).to.equal(this.user_id2)
  84. })
  85. })
  86. })