chat-pane.test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 { renderWithChatContext } 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. global.localStorage.clear()
  37. stubChatStore({ user: currentUser })
  38. stubUIConfig()
  39. stubMathJax()
  40. fetchMock.reset()
  41. })
  42. afterEach(function() {
  43. tearDownChatStore()
  44. tearDownUIConfigStubs()
  45. tearDownMathJaxStubs()
  46. fetchMock.reset()
  47. })
  48. it('renders multiple messages', async function() {
  49. fetchMock.get(/messages/, testMessages)
  50. // unmounting before `beforeEach` block is executed is required to prevent cleanup errors
  51. const { unmount } = renderWithChatContext(<ChatPane />)
  52. await screen.findByText('a message')
  53. await screen.findByText('another message')
  54. unmount()
  55. })
  56. it('A loading spinner is rendered while the messages are loading, then disappears', async function() {
  57. fetchMock.get(/messages/, [])
  58. const { unmount } = renderWithChatContext(<ChatPane />)
  59. await waitForElementToBeRemoved(() => screen.getByText('Loading…'))
  60. unmount()
  61. })
  62. describe('"send your first message" placeholder', function() {
  63. it('is rendered when there are no messages ', async function() {
  64. fetchMock.get(/messages/, [])
  65. const { unmount } = renderWithChatContext(<ChatPane />)
  66. await screen.findByText('Send your first message to your collaborators')
  67. unmount()
  68. })
  69. it('is not rendered when messages are displayed', function() {
  70. fetchMock.get(/messages/, testMessages)
  71. const { unmount } = renderWithChatContext(<ChatPane />)
  72. expect(
  73. screen.queryByText('Send your first message to your collaborators')
  74. ).to.not.exist
  75. unmount()
  76. })
  77. })
  78. })