chat-pane.test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. id: 'msg_1',
  24. content: 'a message',
  25. user: currentUser,
  26. timestamp: new Date().getTime()
  27. },
  28. {
  29. id: 'msg_2',
  30. content: 'another message',
  31. user: currentUser,
  32. timestamp: new Date().getTime()
  33. }
  34. ]
  35. beforeEach(function() {
  36. stubChatStore({ user: currentUser })
  37. stubUIConfig()
  38. stubMathJax()
  39. fetchMock.reset()
  40. })
  41. afterEach(function() {
  42. tearDownChatStore()
  43. tearDownUIConfigStubs()
  44. tearDownMathJaxStubs()
  45. fetchMock.reset()
  46. })
  47. it('renders multiple messages', async function() {
  48. fetchMock.get(/messages/, testMessages)
  49. // unmounting before `beforeEach` block is executed is required to prevent cleanup errors
  50. const { unmount } = renderWithEditorContext(
  51. <ChatPane resetUnreadMessages={() => {}} chatIsOpen />
  52. )
  53. await screen.findByText('a message')
  54. await screen.findByText('another message')
  55. unmount()
  56. })
  57. it('A loading spinner is rendered while the messages are loading, then disappears', async function() {
  58. fetchMock.get(/messages/, [])
  59. const { unmount } = renderWithEditorContext(
  60. <ChatPane resetUnreadMessages={() => {}} chatIsOpen />
  61. )
  62. await waitForElementToBeRemoved(() => screen.getByText('Loading…'))
  63. unmount()
  64. })
  65. describe('"send your first message" placeholder', function() {
  66. it('is rendered when there are no messages ', async function() {
  67. fetchMock.get(/messages/, [])
  68. const { unmount } = renderWithEditorContext(
  69. <ChatPane resetUnreadMessages={() => {}} chatIsOpen />
  70. )
  71. await screen.findByText('Send your first message to your collaborators')
  72. unmount()
  73. })
  74. it('is not rendered when messages are displayed', function() {
  75. fetchMock.get(/messages/, testMessages)
  76. const { unmount } = renderWithEditorContext(
  77. <ChatPane resetUnreadMessages={() => {}} chatIsOpen />
  78. )
  79. expect(
  80. screen.queryByText('Send your first message to your collaborators')
  81. ).to.not.exist
  82. unmount()
  83. })
  84. })
  85. })