online-users-widget.test.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { fireEvent, render, screen } from '@testing-library/react'
  4. import OnlineUsersWidget from '../../../../../frontend/js/features/editor-navigation-toolbar/components/online-users-widget'
  5. describe('<OnlineUsersWidget />', function () {
  6. const defaultProps = {
  7. onlineUsers: [
  8. {
  9. id: 'test_user',
  10. user_id: 'test_user',
  11. name: 'test_user',
  12. email: 'test_email',
  13. },
  14. {
  15. id: 'another_test_user',
  16. user_id: 'another_test_user',
  17. name: 'another_test_user',
  18. email: 'another_test_email',
  19. },
  20. ],
  21. goToUser: () => {},
  22. }
  23. describe('with less than 4 users', function () {
  24. it('displays user initials', function () {
  25. render(<OnlineUsersWidget {...defaultProps} />)
  26. screen.getByText('t')
  27. screen.getByText('a')
  28. })
  29. it('displays user name in a tooltip', async function () {
  30. render(<OnlineUsersWidget {...defaultProps} />)
  31. const icon = screen.getByText('t')
  32. fireEvent.mouseOver(icon)
  33. await screen.findByRole('tooltip', { name: 'test_user' })
  34. })
  35. it('calls "goToUser" when the user initial is clicked', function () {
  36. const props = {
  37. ...defaultProps,
  38. goToUser: sinon.stub(),
  39. }
  40. render(<OnlineUsersWidget {...props} />)
  41. const icon = screen.getByText('t')
  42. fireEvent.click(icon)
  43. expect(props.goToUser).to.be.calledWith({
  44. id: 'test_user',
  45. user_id: 'test_user',
  46. name: 'test_user',
  47. email: 'test_email',
  48. })
  49. })
  50. })
  51. describe('with 4 users and more', function () {
  52. const props = {
  53. ...defaultProps,
  54. onlineUsers: defaultProps.onlineUsers.concat([
  55. {
  56. id: 'user_3',
  57. user_id: 'user_3',
  58. name: 'user_3',
  59. email: 'user_3',
  60. },
  61. {
  62. id: 'user_4',
  63. user_id: 'user_4',
  64. name: 'user_4',
  65. email: 'user_4',
  66. },
  67. ]),
  68. }
  69. it('displays the count of users', function () {
  70. render(<OnlineUsersWidget {...props} />)
  71. screen.getByText('4')
  72. })
  73. it('displays user names on hover', function () {
  74. render(<OnlineUsersWidget {...props} />)
  75. const toggleButton = screen.getByRole('button')
  76. fireEvent.click(toggleButton)
  77. screen.getByText('test_user')
  78. screen.getByText('another_test_user')
  79. screen.getByText('user_3')
  80. screen.getByText('user_4')
  81. })
  82. it('calls "goToUser" when the user name is clicked', function () {
  83. const testProps = {
  84. ...props,
  85. goToUser: sinon.stub(),
  86. }
  87. render(<OnlineUsersWidget {...testProps} />)
  88. const toggleButton = screen.getByRole('button')
  89. fireEvent.click(toggleButton)
  90. const icon = screen.getByText('user_3')
  91. fireEvent.click(icon)
  92. expect(testProps.goToUser).to.be.calledWith({
  93. id: 'user_3',
  94. user_id: 'user_3',
  95. name: 'user_3',
  96. email: 'user_3',
  97. })
  98. })
  99. })
  100. })