message.test.js 2.9 KB

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