chat-pane.test.jsx 3.1 KB

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