chat-pane.test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React from 'react'
  2. import { expect } from 'chai'
  3. import {
  4. render,
  5. screen,
  6. waitForElementToBeRemoved
  7. } from '@testing-library/react'
  8. import fetchMock from 'fetch-mock'
  9. import ChatPane from '../../../../../frontend/js/features/chat/components/chat-pane'
  10. import {
  11. stubChatStore,
  12. stubMathJax,
  13. stubUIConfig,
  14. tearDownChatStore,
  15. tearDownMathJaxStubs,
  16. tearDownUIConfigStubs
  17. } from './stubs'
  18. describe('<ChatPane />', function() {
  19. const currentUser = {
  20. id: 'fake_user',
  21. first_name: 'fake_user_first_name',
  22. email: 'fake@example.com'
  23. }
  24. const testMessages = [
  25. {
  26. content: 'a message',
  27. user: currentUser,
  28. timestamp: new Date().getTime()
  29. },
  30. {
  31. content: 'another message',
  32. user: currentUser,
  33. timestamp: new Date().getTime()
  34. }
  35. ]
  36. beforeEach(function() {
  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. render(<ChatPane resetUnreadMessages={() => {}} />)
  51. await screen.findByText('a message')
  52. await screen.findByText('another message')
  53. })
  54. it('A loading spinner is rendered while the messages are loading, then disappears', async function() {
  55. fetchMock.get(/messages/, [])
  56. render(<ChatPane resetUnreadMessages={() => {}} />)
  57. await waitForElementToBeRemoved(() => screen.getByText('Loading…'))
  58. })
  59. describe('"send your first message" placeholder', function() {
  60. it('is rendered when there are no messages ', async function() {
  61. fetchMock.get(/messages/, [])
  62. render(<ChatPane resetUnreadMessages={() => {}} />)
  63. await screen.findByText('Send your first message to your collaborators')
  64. })
  65. it('is not rendered when messages are displayed', function() {
  66. fetchMock.get(/messages/, testMessages)
  67. render(<ChatPane resetUnreadMessages={() => {}} />)
  68. expect(
  69. screen.queryByText('Send your first message to your collaborators')
  70. ).to.not.exist
  71. })
  72. })
  73. })