chat-pane.test.jsx 3.2 KB

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