chat-pane.test.js 2.7 KB

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