message-list.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React from 'react'
  2. import sinon from 'sinon'
  3. import { expect } from 'chai'
  4. import { screen, render, fireEvent } from '@testing-library/react'
  5. import MessageList from '../../../../../frontend/js/features/chat/components/message-list'
  6. import {
  7. stubChatStore,
  8. stubMathJax,
  9. stubUIConfig,
  10. tearDownChatStore,
  11. tearDownMathJaxStubs,
  12. tearDownUIConfigStubs
  13. } from './stubs'
  14. describe('<MessageList />', function() {
  15. const currentUser = {
  16. id: 'fake_user',
  17. first_name: 'fake_user_first_name',
  18. email: 'fake@example.com'
  19. }
  20. function createMessages() {
  21. return [
  22. {
  23. id: 'test_msg_1',
  24. contents: ['a message'],
  25. user: currentUser,
  26. timestamp: new Date().getTime()
  27. },
  28. {
  29. id: 'test_msg_2',
  30. contents: ['another message'],
  31. user: currentUser,
  32. timestamp: new Date().getTime()
  33. }
  34. ]
  35. }
  36. before(function() {
  37. stubChatStore({ user: currentUser }) // required by ColorManager
  38. stubUIConfig()
  39. stubMathJax()
  40. })
  41. after(function() {
  42. tearDownChatStore()
  43. tearDownUIConfigStubs()
  44. tearDownMathJaxStubs()
  45. })
  46. it('renders multiple messages', function() {
  47. render(
  48. <MessageList
  49. userId={currentUser.id}
  50. messages={createMessages()}
  51. resetUnreadMessages={() => {}}
  52. />
  53. )
  54. screen.getByText('a message')
  55. screen.getByText('another message')
  56. })
  57. it('renders a single timestamp for all messages within 5 minutes', function() {
  58. const msgs = createMessages()
  59. msgs[0].timestamp = new Date(2019, 6, 3, 4, 23).getTime()
  60. msgs[1].timestamp = new Date(2019, 6, 3, 4, 27).getTime()
  61. render(
  62. <MessageList
  63. userId={currentUser.id}
  64. messages={msgs}
  65. resetUnreadMessages={() => {}}
  66. />
  67. )
  68. screen.getByText('4:23 am Wed, 3rd Jul 19')
  69. expect(screen.queryByText('4:27 am Wed, 3rd Jul 19')).to.not.exist
  70. })
  71. it('renders a timestamp for each messages separated for more than 5 minutes', function() {
  72. const msgs = createMessages()
  73. msgs[0].timestamp = new Date(2019, 6, 3, 4, 23).getTime()
  74. msgs[1].timestamp = new Date(2019, 6, 3, 4, 31).getTime()
  75. render(
  76. <MessageList
  77. userId={currentUser.id}
  78. messages={msgs}
  79. resetUnreadMessages={() => {}}
  80. />
  81. )
  82. screen.getByText('4:23 am Wed, 3rd Jul 19')
  83. screen.getByText('4:31 am Wed, 3rd Jul 19')
  84. })
  85. it('resets the number of unread messages after clicking on the input', function() {
  86. const resetUnreadMessages = sinon.stub()
  87. render(
  88. <MessageList
  89. userId={currentUser.id}
  90. messages={createMessages()}
  91. resetUnreadMessages={resetUnreadMessages}
  92. />
  93. )
  94. fireEvent.click(screen.getByRole('list'))
  95. expect(resetUnreadMessages).to.be.calledOnce
  96. })
  97. })