chat-pane.test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 {
  7. renderWithChatContext,
  8. cleanUpContext
  9. } from '../../../helpers/render-with-context'
  10. import {
  11. stubMathJax,
  12. stubUIConfig,
  13. tearDownMathJaxStubs,
  14. tearDownUIConfigStubs
  15. } from './stubs'
  16. describe('<ChatPane />', function() {
  17. const user = {
  18. id: 'fake_user',
  19. first_name: 'fake_user_first_name',
  20. email: 'fake@example.com'
  21. }
  22. const testMessages = [
  23. {
  24. id: 'msg_1',
  25. content: 'a message',
  26. user,
  27. timestamp: new Date().getTime()
  28. },
  29. {
  30. id: 'msg_2',
  31. content: 'another message',
  32. user,
  33. timestamp: new Date().getTime()
  34. }
  35. ]
  36. beforeEach(function() {
  37. fetchMock.reset()
  38. cleanUpContext()
  39. stubUIConfig()
  40. stubMathJax()
  41. })
  42. afterEach(function() {
  43. tearDownUIConfigStubs()
  44. tearDownMathJaxStubs()
  45. })
  46. it('renders multiple messages', async function() {
  47. fetchMock.get(/messages/, testMessages)
  48. renderWithChatContext(<ChatPane />, { user })
  49. await screen.findByText('a message')
  50. await screen.findByText('another message')
  51. })
  52. it('A loading spinner is rendered while the messages are loading, then disappears', async function() {
  53. fetchMock.get(/messages/, [])
  54. renderWithChatContext(<ChatPane />, { user })
  55. await waitForElementToBeRemoved(() => screen.getByText('Loading…'))
  56. })
  57. describe('"send your first message" placeholder', function() {
  58. it('is rendered when there are no messages ', async function() {
  59. fetchMock.get(/messages/, [])
  60. renderWithChatContext(<ChatPane />, { user })
  61. await screen.findByText('Send your first message to your collaborators')
  62. })
  63. it('is not rendered when messages are displayed', function() {
  64. fetchMock.get(/messages/, testMessages)
  65. renderWithChatContext(<ChatPane />, { user })
  66. expect(
  67. screen.queryByText('Send your first message to your collaborators')
  68. ).to.not.exist
  69. })
  70. })
  71. })