chat-pane.test.js 2.0 KB

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