CloningCommentThreadsTests.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { ObjectId } from '../../../app/js/mongodb.js'
  2. import { expect } from 'chai'
  3. import * as ChatClient from './helpers/ChatClient.js'
  4. import * as ChatApp from './helpers/ChatApp.js'
  5. const user1Id = new ObjectId().toString()
  6. const user2Id = new ObjectId().toString()
  7. async function createCommentThread(projectId, threadId = new ObjectId()) {
  8. const { response: response1 } = await ChatClient.sendMessage(
  9. projectId,
  10. threadId.toString(),
  11. user1Id,
  12. 'message 1'
  13. )
  14. expect(response1.statusCode).to.equal(201)
  15. const { response: response2 } = await ChatClient.sendMessage(
  16. projectId,
  17. threadId,
  18. user2Id,
  19. 'message 2'
  20. )
  21. expect(response2.statusCode).to.equal(201)
  22. return threadId.toString()
  23. }
  24. describe('Cloning comment threads', async function () {
  25. const projectId = new ObjectId().toString()
  26. before(async function () {
  27. await ChatApp.ensureRunning()
  28. this.thread1Id = await createCommentThread(projectId)
  29. this.thread2Id = await createCommentThread(projectId)
  30. this.thread3Id = await createCommentThread(projectId)
  31. })
  32. describe('with non-orphaned threads', async function () {
  33. before(async function () {
  34. const {
  35. response: { body: result, statusCode },
  36. } = await ChatClient.duplicateCommentThreads(projectId, [this.thread3Id])
  37. this.result = result
  38. expect(statusCode).to.equal(200)
  39. expect(this.result).to.have.property('newThreads')
  40. this.newThreadId = this.result.newThreads[this.thread3Id].duplicateId
  41. })
  42. it('should duplicate threads', function () {
  43. expect(this.result.newThreads).to.have.property(this.thread3Id)
  44. expect(this.result.newThreads[this.thread3Id]).to.have.property(
  45. 'duplicateId'
  46. )
  47. expect(this.result.newThreads[this.thread3Id].duplicateId).to.not.equal(
  48. this.thread3Id
  49. )
  50. })
  51. it('should not duplicate other threads threads', function () {
  52. expect(this.result.newThreads).to.not.have.property(this.thread1Id)
  53. expect(this.result.newThreads).to.not.have.property(this.thread2Id)
  54. })
  55. it('should duplicate the messages in the thread', async function () {
  56. const {
  57. response: { body: threads },
  58. } = await ChatClient.getThreads(projectId)
  59. function ignoreId(comment) {
  60. return {
  61. ...comment,
  62. id: undefined,
  63. }
  64. }
  65. expect(threads[this.thread3Id].messages.map(ignoreId)).to.deep.equal(
  66. threads[this.newThreadId].messages.map(ignoreId)
  67. )
  68. })
  69. it('should have two separate unlinked threads', async function () {
  70. await ChatClient.sendMessage(
  71. projectId,
  72. this.newThreadId,
  73. user1Id,
  74. 'third message'
  75. )
  76. const {
  77. response: { body: threads },
  78. } = await ChatClient.getThreads(projectId)
  79. expect(threads[this.thread3Id].messages.length).to.equal(2)
  80. expect(threads[this.newThreadId].messages.length).to.equal(3)
  81. })
  82. })
  83. })