chat-pane.test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React from 'react'
  2. import { expect } from 'chai'
  3. import { screen, waitForElementToBeRemoved } from '@testing-library/react'
  4. import fetchMock from 'fetch-mock'
  5. import ChatPane from '../../../../../frontend/js/features/chat/components/chat-pane'
  6. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  7. import {
  8. stubChatStore,
  9. stubMathJax,
  10. stubUIConfig,
  11. tearDownChatStore,
  12. tearDownMathJaxStubs,
  13. tearDownUIConfigStubs
  14. } from './stubs'
  15. describe('<ChatPane />', function() {
  16. const currentUser = {
  17. id: 'fake_user',
  18. first_name: 'fake_user_first_name',
  19. email: 'fake@example.com'
  20. }
  21. const testMessages = [
  22. {
  23. content: 'a message',
  24. user: currentUser,
  25. timestamp: new Date().getTime()
  26. },
  27. {
  28. content: 'another message',
  29. user: currentUser,
  30. timestamp: new Date().getTime()
  31. }
  32. ]
  33. beforeEach(function() {
  34. stubChatStore({ user: currentUser })
  35. stubUIConfig()
  36. stubMathJax()
  37. fetchMock.reset()
  38. })
  39. afterEach(function() {
  40. tearDownChatStore()
  41. tearDownUIConfigStubs()
  42. tearDownMathJaxStubs()
  43. fetchMock.reset()
  44. })
  45. it('renders multiple messages', async function() {
  46. fetchMock.get(/messages/, testMessages)
  47. // unmounting before `beforeEach` block is executed is required to prevent cleanup errors
  48. const { unmount } = renderWithEditorContext(
  49. <ChatPane resetUnreadMessages={() => {}} chatIsOpen />
  50. )
  51. await screen.findByText('a message')
  52. await screen.findByText('another message')
  53. unmount()
  54. })
  55. it('A loading spinner is rendered while the messages are loading, then disappears', async function() {
  56. fetchMock.get(/messages/, [])
  57. const { unmount } = renderWithEditorContext(
  58. <ChatPane resetUnreadMessages={() => {}} chatIsOpen />
  59. )
  60. await waitForElementToBeRemoved(() => screen.getByText('Loading…'))
  61. unmount()
  62. })
  63. describe('"send your first message" placeholder', function() {
  64. it('is rendered when there are no messages ', async function() {
  65. fetchMock.get(/messages/, [])
  66. const { unmount } = renderWithEditorContext(
  67. <ChatPane resetUnreadMessages={() => {}} chatIsOpen />
  68. )
  69. await screen.findByText('Send your first message to your collaborators')
  70. unmount()
  71. })
  72. it('is not rendered when messages are displayed', function() {
  73. fetchMock.get(/messages/, testMessages)
  74. const { unmount } = renderWithEditorContext(
  75. <ChatPane resetUnreadMessages={() => {}} chatIsOpen />
  76. )
  77. expect(
  78. screen.queryByText('Send your first message to your collaborators')
  79. ).to.not.exist
  80. unmount()
  81. })
  82. })
  83. })