message.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.user = currentUser
  13. stubMathJax()
  14. })
  15. afterEach(function () {
  16. delete window.user
  17. tearDownMathJaxStubs()
  18. })
  19. it('renders a basic message', function () {
  20. const message = {
  21. contents: ['a message'],
  22. user: currentUser,
  23. }
  24. render(<Message userId={currentUser.id} message={message} />)
  25. screen.getByText('a message')
  26. })
  27. it('renders a message with multiple contents', function () {
  28. const message = {
  29. contents: ['a message', 'another message'],
  30. user: currentUser,
  31. }
  32. render(<Message userId={currentUser.id} message={message} />)
  33. screen.getByText('a message')
  34. screen.getByText('another message')
  35. })
  36. it('renders HTML links within messages', function () {
  37. const message = {
  38. contents: [
  39. 'a message with a <a href="https://overleaf.com">link to Overleaf</a>',
  40. ],
  41. user: currentUser,
  42. }
  43. render(<Message userId={currentUser.id} message={message} />)
  44. screen.getByRole('link', { name: 'https://overleaf.com' })
  45. })
  46. describe('when the message is from the user themselves', function () {
  47. const message = {
  48. contents: ['a message'],
  49. user: currentUser,
  50. }
  51. it('does not render the user name nor the email', function () {
  52. render(<Message userId={currentUser.id} message={message} />)
  53. expect(screen.queryByText(currentUser.first_name)).to.not.exist
  54. expect(screen.queryByText(currentUser.email)).to.not.exist
  55. })
  56. })
  57. describe('when the message is from other user', function () {
  58. const otherUser = {
  59. id: 'other_user',
  60. first_name: 'other_user_first_name',
  61. }
  62. const message = {
  63. contents: ['a message'],
  64. user: otherUser,
  65. }
  66. it('should render the other user name', function () {
  67. render(<Message userId={currentUser.id} message={message} />)
  68. screen.getByText(otherUser.first_name)
  69. })
  70. it('should render the other user email when their name is not available', function () {
  71. const msg = {
  72. contents: message.contents,
  73. user: {
  74. id: otherUser.id,
  75. email: 'other@example.com',
  76. },
  77. }
  78. render(<Message userId={currentUser.id} message={msg} />)
  79. expect(screen.queryByText(otherUser.first_name)).to.not.exist
  80. screen.getByText(msg.user.email)
  81. })
  82. })
  83. })