GettingMessagesTests.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const { ObjectId } = require('../../../app/js/mongodb')
  2. const { expect } = require('chai')
  3. const async = require('async')
  4. const ChatClient = require('./helpers/ChatClient')
  5. const ChatApp = require('./helpers/ChatApp')
  6. describe('Getting messages', function () {
  7. before(function (done) {
  8. this.user_id1 = ObjectId().toString()
  9. this.user_id2 = ObjectId().toString()
  10. this.content1 = 'foo bar'
  11. this.content2 = 'hello world'
  12. ChatApp.ensureRunning(done)
  13. })
  14. describe('globally', function () {
  15. before(function (done) {
  16. this.project_id = ObjectId().toString()
  17. async.series(
  18. [
  19. cb =>
  20. ChatClient.sendGlobalMessage(
  21. this.project_id,
  22. this.user_id1,
  23. this.content1,
  24. cb
  25. ),
  26. cb =>
  27. ChatClient.sendGlobalMessage(
  28. this.project_id,
  29. this.user_id2,
  30. this.content2,
  31. cb
  32. ),
  33. ],
  34. done
  35. )
  36. })
  37. it('should contain the messages and populated users when getting the messages', function (done) {
  38. ChatClient.getGlobalMessages(
  39. this.project_id,
  40. (error, response, messages) => {
  41. if (error) return done(error)
  42. expect(messages.length).to.equal(2)
  43. messages.reverse()
  44. expect(messages[0].content).to.equal(this.content1)
  45. expect(messages[0].user_id).to.equal(this.user_id1)
  46. expect(messages[1].content).to.equal(this.content2)
  47. expect(messages[1].user_id).to.equal(this.user_id2)
  48. done()
  49. }
  50. )
  51. })
  52. })
  53. describe('from all the threads', function () {
  54. before(function (done) {
  55. this.project_id = ObjectId().toString()
  56. this.thread_id1 = ObjectId().toString()
  57. this.thread_id2 = ObjectId().toString()
  58. async.series(
  59. [
  60. cb =>
  61. ChatClient.sendMessage(
  62. this.project_id,
  63. this.thread_id1,
  64. this.user_id1,
  65. 'one',
  66. cb
  67. ),
  68. cb =>
  69. ChatClient.sendMessage(
  70. this.project_id,
  71. this.thread_id2,
  72. this.user_id2,
  73. 'two',
  74. cb
  75. ),
  76. cb =>
  77. ChatClient.sendMessage(
  78. this.project_id,
  79. this.thread_id1,
  80. this.user_id1,
  81. 'three',
  82. cb
  83. ),
  84. cb =>
  85. ChatClient.sendMessage(
  86. this.project_id,
  87. this.thread_id2,
  88. this.user_id2,
  89. 'four',
  90. cb
  91. ),
  92. ],
  93. done
  94. )
  95. })
  96. it('should contain a dictionary of threads with messages with populated users', function (done) {
  97. ChatClient.getThreads(this.project_id, (error, response, threads) => {
  98. if (error) return done(error)
  99. expect(Object.keys(threads).length).to.equal(2)
  100. const thread1 = threads[this.thread_id1]
  101. expect(thread1.messages.length).to.equal(2)
  102. const thread2 = threads[this.thread_id2]
  103. expect(thread2.messages.length).to.equal(2)
  104. expect(thread1.messages[0].content).to.equal('one')
  105. expect(thread1.messages[0].user_id).to.equal(this.user_id1)
  106. expect(thread1.messages[1].content).to.equal('three')
  107. expect(thread1.messages[1].user_id).to.equal(this.user_id1)
  108. expect(thread2.messages[0].content).to.equal('two')
  109. expect(thread2.messages[0].user_id).to.equal(this.user_id2)
  110. expect(thread2.messages[1].content).to.equal('four')
  111. expect(thread2.messages[1].user_id).to.equal(this.user_id2)
  112. done()
  113. })
  114. })
  115. })
  116. })