chat-pane.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React from 'react'
  2. import { expect } from 'chai'
  3. import { screen, render } from '@testing-library/react'
  4. import ChatPane from '../../../../../frontend/js/features/chat/components/chat-pane'
  5. import {
  6. stubGlobalUser,
  7. stubMathJax,
  8. stubUIConfig,
  9. tearDownGlobalUserStub,
  10. tearDownMathJaxStubs,
  11. tearDownUIConfigStubs
  12. } from './stubs'
  13. describe('<ChatPane />', function() {
  14. const currentUser = {
  15. id: 'fake_user',
  16. first_name: 'fake_user_first_name',
  17. email: 'fake@example.com'
  18. }
  19. function createMessages() {
  20. return [
  21. {
  22. contents: ['a message'],
  23. user: currentUser,
  24. timestamp: new Date()
  25. },
  26. {
  27. contents: ['another message'],
  28. user: currentUser,
  29. timestamp: new Date()
  30. }
  31. ]
  32. }
  33. before(function() {
  34. stubGlobalUser(currentUser) // required by ColorManager
  35. stubUIConfig()
  36. stubMathJax()
  37. })
  38. after(function() {
  39. tearDownGlobalUserStub()
  40. tearDownUIConfigStubs()
  41. tearDownMathJaxStubs()
  42. })
  43. it('renders multiple messages', function() {
  44. render(
  45. <ChatPane
  46. loadMoreMessages={() => {}}
  47. sendMessage={() => {}}
  48. userId={currentUser.id}
  49. messages={createMessages()}
  50. resetUnreadMessages={() => {}}
  51. />
  52. )
  53. screen.getByText('a message')
  54. screen.getByText('another message')
  55. })
  56. describe('loading spinner', function() {
  57. it('is rendered while the messages is loading', function() {
  58. render(
  59. <ChatPane
  60. loading
  61. loadMoreMessages={() => {}}
  62. sendMessage={() => {}}
  63. userId={currentUser.id}
  64. messages={createMessages()}
  65. resetUnreadMessages={() => {}}
  66. />
  67. )
  68. screen.getByText('Loading…')
  69. })
  70. it('is not rendered when the messages are not loading', function() {
  71. render(
  72. <ChatPane
  73. loading={false}
  74. loadMoreMessages={() => {}}
  75. sendMessage={() => {}}
  76. userId={currentUser.id}
  77. messages={createMessages()}
  78. resetUnreadMessages={() => {}}
  79. />
  80. )
  81. })
  82. expect(screen.queryByText('Loading…')).to.not.exist
  83. })
  84. describe('"send your first message" placeholder', function() {
  85. it('is rendered when there are no messages ', function() {
  86. render(
  87. <ChatPane
  88. loadMoreMessages={() => {}}
  89. sendMessage={() => {}}
  90. userId={currentUser.id}
  91. messages={[]}
  92. resetUnreadMessages={() => {}}
  93. />
  94. )
  95. screen.getByText('Send your first message to your collaborators')
  96. })
  97. it('is not rendered when there are some messages', function() {
  98. render(
  99. <ChatPane
  100. loading={false}
  101. loadMoreMessages={() => {}}
  102. sendMessage={() => {}}
  103. userId={currentUser.id}
  104. messages={createMessages()}
  105. resetUnreadMessages={() => {}}
  106. />
  107. )
  108. })
  109. expect(screen.queryByText('Send your first message to your collaborators'))
  110. .to.not.exist
  111. })
  112. })