message.test.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. import { User, UserId } from '@ol-types/user'
  6. import { Message as MessageType } from '@/features/chat/context/chat-context'
  7. describe('<Message />', function () {
  8. const currentUser: User = {
  9. id: 'fake_user' as UserId,
  10. first_name: 'fake_user_first_name',
  11. email: 'fake@example.com',
  12. }
  13. beforeEach(function () {
  14. window.metaAttributesCache.set('ol-user', currentUser)
  15. stubMathJax()
  16. })
  17. afterEach(function () {
  18. tearDownMathJaxStubs()
  19. })
  20. it('renders a basic message', function () {
  21. const message: MessageType = {
  22. contents: ['a message'],
  23. user: currentUser,
  24. id: 'msg_1',
  25. timestamp: new Date('2025-01-01T00:00:00.000Z').getTime(),
  26. }
  27. render(<Message message={message} fromSelf />)
  28. screen.getByText('a message')
  29. })
  30. it('renders a message with multiple contents', function () {
  31. const message: MessageType = {
  32. contents: ['a message', 'another message'],
  33. user: currentUser,
  34. id: 'msg_1',
  35. timestamp: new Date('2025-01-01T00:00:00.000Z').getTime(),
  36. }
  37. render(<Message message={message} fromSelf />)
  38. screen.getByText('a message')
  39. screen.getByText('another message')
  40. })
  41. it('renders HTML links within messages', function () {
  42. const message: MessageType = {
  43. contents: [
  44. 'a message with a <a href="https://overleaf.com">link to Overleaf</a>',
  45. ],
  46. user: currentUser,
  47. id: 'msg_1',
  48. timestamp: new Date('2025-01-01T00:00:00.000Z').getTime(),
  49. }
  50. render(<Message message={message} fromSelf />)
  51. screen.getByRole('link', { name: 'https://overleaf.com' })
  52. })
  53. describe('when the message is from the user themselves', function () {
  54. const message: MessageType = {
  55. contents: ['a message'],
  56. user: currentUser,
  57. id: 'msg_1',
  58. timestamp: new Date('2025-01-01T00:00:00.000Z').getTime(),
  59. }
  60. it('does not render the user name nor the email', function () {
  61. render(<Message message={message} fromSelf />)
  62. expect(screen.queryByText(currentUser.first_name!)).to.not.exist
  63. expect(screen.queryByText(currentUser.email)).to.not.exist
  64. })
  65. })
  66. describe('when the message is from other user', function () {
  67. const otherUser: User = {
  68. id: 'other_user' as UserId,
  69. first_name: 'other_user_first_name',
  70. email: 'other@example.com',
  71. }
  72. const message: MessageType = {
  73. contents: ['a message'],
  74. user: otherUser,
  75. id: 'msg_1',
  76. timestamp: new Date('2025-01-01T00:00:00.000Z').getTime(),
  77. }
  78. it('should render the other user name', function () {
  79. render(<Message message={message} fromSelf={false} />)
  80. screen.getByText(otherUser.first_name!)
  81. })
  82. it('should render the other user email when their name is not available', function () {
  83. const msg: MessageType = {
  84. contents: message.contents,
  85. user: {
  86. id: otherUser.id,
  87. email: 'other@example.com',
  88. },
  89. id: 'msg_1',
  90. timestamp: new Date('2025-01-01T00:00:00.000Z').getTime(),
  91. }
  92. render(<Message message={msg} fromSelf={false} />)
  93. expect(screen.queryByText(otherUser.first_name!)).to.not.exist
  94. screen.getByText(msg.user!.email)
  95. })
  96. })
  97. })