message.test.js 2.7 KB

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