chat-pane.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. const testMessages = [
  21. {
  22. id: 'msg_1',
  23. content: 'a message',
  24. user,
  25. timestamp: new Date().getTime(),
  26. },
  27. {
  28. id: 'msg_2',
  29. content: 'another message',
  30. user,
  31. timestamp: new Date().getTime(),
  32. },
  33. ]
  34. beforeEach(function () {
  35. fetchMock.reset()
  36. cleanUpContext()
  37. stubMathJax()
  38. })
  39. afterEach(function () {
  40. tearDownMathJaxStubs()
  41. })
  42. it('renders multiple messages', async function () {
  43. fetchMock.get(/messages/, testMessages)
  44. renderWithChatContext(<ChatPane />, { user })
  45. await screen.findByText('a message')
  46. await screen.findByText('another message')
  47. })
  48. it('provides error message with reload button on FetchError', async function () {
  49. fetchMock.get(/messages/, 500)
  50. renderWithChatContext(<ChatPane />, { user })
  51. // should have hit a FetchError and will prompt user to reconnect
  52. await screen.findByText('Try again')
  53. // bring chat back up
  54. fetchMock.reset()
  55. fetchMock.get(/messages/, [])
  56. const reconnectButton = screen.getByRole('button', {
  57. name: 'Try again',
  58. })
  59. expect(reconnectButton).to.exist
  60. // should now reconnect with placeholder message
  61. fireEvent.click(reconnectButton)
  62. await screen.findByText('Send your first message to your collaborators')
  63. })
  64. it('a loading spinner is rendered while the messages are loading, then disappears', async function () {
  65. fetchMock.get(/messages/, [])
  66. renderWithChatContext(<ChatPane />, { user })
  67. await waitForElementToBeRemoved(() => screen.getByText('Loading…'))
  68. })
  69. describe('"send your first message" placeholder', function () {
  70. it('is rendered when there are no messages ', async function () {
  71. fetchMock.get(/messages/, [])
  72. renderWithChatContext(<ChatPane />, { user })
  73. await screen.findByText('Send your first message to your collaborators')
  74. })
  75. it('is not rendered when messages are displayed', function () {
  76. fetchMock.get(/messages/, testMessages)
  77. renderWithChatContext(<ChatPane />, { user })
  78. expect(
  79. screen.queryByText('Send your first message to your collaborators')
  80. ).to.not.exist
  81. })
  82. })
  83. })