message.test.jsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { expect } from 'chai'
  2. import { render, screen } from '@testing-library/react'
  3. import Message from '../../../../../frontend/js/features/chat/components/message'
  4. import { stubMathJax, tearDownMathJaxStubs } from './stubs'
  5. describe('<Message />', function () {
  6. const currentUser = {
  7. id: 'fake_user',
  8. first_name: 'fake_user_first_name',
  9. email: 'fake@example.com',
  10. }
  11. beforeEach(function () {
  12. window.metaAttributesCache.set('ol-user', currentUser)
  13. stubMathJax()
  14. })
  15. afterEach(function () {
  16. tearDownMathJaxStubs()
  17. })
  18. it('renders a basic message', function () {
  19. const message = {
  20. contents: ['a message'],
  21. user: currentUser,
  22. }
  23. render(<Message message={message} fromSelf />)
  24. screen.getByText('a message')
  25. })
  26. it('renders a message with multiple contents', function () {
  27. const message = {
  28. contents: ['a message', 'another message'],
  29. user: currentUser,
  30. }
  31. render(<Message message={message} fromSelf />)
  32. screen.getByText('a message')
  33. screen.getByText('another message')
  34. })
  35. it('renders HTML links within messages', function () {
  36. const message = {
  37. contents: [
  38. 'a message with a <a href="https://overleaf.com">link to Overleaf</a>',
  39. ],
  40. user: currentUser,
  41. }
  42. render(<Message message={message} fromSelf />)
  43. screen.getByRole('link', { name: 'https://overleaf.com' })
  44. })
  45. describe('when the message is from the user themselves', function () {
  46. const message = {
  47. contents: ['a message'],
  48. user: currentUser,
  49. }
  50. it('does not render the user name nor the email', function () {
  51. render(<Message message={message} fromSelf />)
  52. expect(screen.queryByText(currentUser.first_name)).to.not.exist
  53. expect(screen.queryByText(currentUser.email)).to.not.exist
  54. })
  55. })
  56. describe('when the message is from other user', function () {
  57. const otherUser = {
  58. id: 'other_user',
  59. first_name: 'other_user_first_name',
  60. }
  61. const message = {
  62. contents: ['a message'],
  63. user: otherUser,
  64. }
  65. it('should render the other user name', function () {
  66. render(<Message message={message} />)
  67. screen.getByText(otherUser.first_name)
  68. })
  69. it('should render the other user email when their name is not available', function () {
  70. const msg = {
  71. contents: message.contents,
  72. user: {
  73. id: otherUser.id,
  74. email: 'other@example.com',
  75. },
  76. }
  77. render(<Message message={msg} />)
  78. expect(screen.queryByText(otherUser.first_name)).to.not.exist
  79. screen.getByText(msg.user.email)
  80. })
  81. })
  82. })