message.test.js 2.8 KB

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