chat-pane.test.jsx 3.2 KB

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