chat-pane.test.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { expect } from 'chai'
  2. import {
  3. fireEvent,
  4. screen,
  5. waitForElementToBeRemoved,
  6. } from '@testing-library/react'
  7. import fetchMock from 'fetch-mock'
  8. import ChatPane from '../../../../../frontend/js/features/chat/components/chat-pane'
  9. import {
  10. renderWithChatContext,
  11. cleanUpContext,
  12. } from '../../../helpers/render-with-context'
  13. import { stubMathJax, tearDownMathJaxStubs } from './stubs'
  14. describe('<ChatPane />', function () {
  15. const user = {
  16. id: 'fake_user',
  17. first_name: 'fake_user_first_name',
  18. email: 'fake@example.com',
  19. }
  20. beforeEach(function () {
  21. window.metaAttributesCache = new Map()
  22. window.metaAttributesCache.set('ol-user', user)
  23. })
  24. afterEach(function () {
  25. fetchMock.reset()
  26. window.metaAttributesCache = new Map()
  27. })
  28. const testMessages = [
  29. {
  30. id: 'msg_1',
  31. content: 'a message',
  32. user,
  33. timestamp: new Date().getTime(),
  34. },
  35. {
  36. id: 'msg_2',
  37. content: 'another message',
  38. user,
  39. timestamp: new Date().getTime(),
  40. },
  41. ]
  42. beforeEach(function () {
  43. fetchMock.reset()
  44. cleanUpContext()
  45. stubMathJax()
  46. })
  47. afterEach(function () {
  48. tearDownMathJaxStubs()
  49. })
  50. it('renders multiple messages', async function () {
  51. fetchMock.get(/messages/, testMessages)
  52. renderWithChatContext(<ChatPane />, { user })
  53. await screen.findByText('a message')
  54. await screen.findByText('another message')
  55. })
  56. it('provides error message with reload button on FetchError', async function () {
  57. fetchMock.get(/messages/, 500)
  58. renderWithChatContext(<ChatPane />, { user })
  59. // should have hit a FetchError and will prompt user to reconnect
  60. await screen.findByText('Try again')
  61. // bring chat back up
  62. fetchMock.reset()
  63. fetchMock.get(/messages/, [])
  64. const reconnectButton = screen.getByRole('button', {
  65. name: 'Try again',
  66. })
  67. expect(reconnectButton).to.exist
  68. // should now reconnect with placeholder message
  69. fireEvent.click(reconnectButton)
  70. await screen.findByText('Send your first message to your collaborators')
  71. })
  72. it('a loading spinner is rendered while the messages are loading, then disappears', async function () {
  73. fetchMock.get(/messages/, [], { delay: 1000 })
  74. renderWithChatContext(<ChatPane />, { user })
  75. // not displayed initially
  76. expect(screen.queryByText('Loading…')).to.not.exist
  77. // eventually displayed
  78. await screen.findByText('Loading…')
  79. // eventually removed when the fetch call returns
  80. await waitForElementToBeRemoved(() => screen.getByText('Loading…'))
  81. })
  82. describe('"send your first message" placeholder', function () {
  83. it('is rendered when there are no messages ', async function () {
  84. fetchMock.get(/messages/, [])
  85. renderWithChatContext(<ChatPane />, { user })
  86. await screen.findByText('Send your first message to your collaborators')
  87. })
  88. it('is not rendered when messages are displayed', function () {
  89. fetchMock.get(/messages/, testMessages)
  90. renderWithChatContext(<ChatPane />, { user })
  91. expect(
  92. screen.queryByText('Send your first message to your collaborators')
  93. ).to.not.exist
  94. })
  95. })
  96. })