online-users-widget.test.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { fireEvent, render, screen } from '@testing-library/react'
  4. import { OnlineUsersWidget } from '@/features/editor-navigation-toolbar/components/online-users-widget'
  5. const names = ['alice', 'bob', 'charlie', 'dave', 'erin', 'frank', 'grace']
  6. function makeUsers(count: number) {
  7. return Array.from({ length: count }, (_, index) => ({
  8. id: `user_${index + 1}`,
  9. user_id: `user_id_${index + 1}`,
  10. name: names[index % names.length],
  11. email: `${names[index % names.length]}_email`,
  12. }))
  13. }
  14. describe('<OnlineUsersWidget />', function () {
  15. const defaultProps = {
  16. onlineUsers: makeUsers(2),
  17. goToUser: sinon.stub(),
  18. }
  19. describe('with less than 5 users', function () {
  20. it('displays user initials', function () {
  21. render(<OnlineUsersWidget {...defaultProps} />)
  22. screen.getByText('a')
  23. screen.getByText('b')
  24. })
  25. it('displays user name in a tooltip', async function () {
  26. render(<OnlineUsersWidget {...defaultProps} />)
  27. const icon = screen.getByText('a')
  28. fireEvent.mouseOver(icon)
  29. await screen.findByRole('tooltip', { name: 'alice' })
  30. })
  31. it('calls "goToUser" when the user initial is clicked', function () {
  32. render(<OnlineUsersWidget {...defaultProps} />)
  33. const icon = screen.getByText('a')
  34. fireEvent.click(icon)
  35. expect(defaultProps.goToUser).to.be.calledWith({
  36. id: 'user_1',
  37. user_id: 'user_id_1',
  38. name: 'alice',
  39. email: 'alice_email',
  40. })
  41. })
  42. })
  43. describe('with 5 users', function () {
  44. const props = {
  45. ...defaultProps,
  46. onlineUsers: makeUsers(5),
  47. }
  48. it('displays user initials', function () {
  49. render(<OnlineUsersWidget {...props} />)
  50. screen.getByText('a')
  51. screen.getByText('b')
  52. screen.getByText('c')
  53. screen.getByText('d')
  54. screen.getByText('e')
  55. })
  56. })
  57. describe('with more than 5 users', function () {
  58. const props = {
  59. ...defaultProps,
  60. onlineUsers: makeUsers(7),
  61. }
  62. it('displays a maximum of 4 user initials and an overflow icon', function () {
  63. render(<OnlineUsersWidget {...props} />)
  64. screen.getByText('a')
  65. screen.getByText('b')
  66. screen.getByText('c')
  67. screen.getByText('d')
  68. screen.getByText('+3')
  69. })
  70. it('displays the remaining users in a dropdown when the overflow icon is clicked', async function () {
  71. render(<OnlineUsersWidget {...props} />)
  72. const overflowButton = screen.getByText('+3')
  73. fireEvent.click(overflowButton)
  74. await screen.findByText('erin')
  75. await screen.findByText('frank')
  76. await screen.findByText('grace')
  77. })
  78. it('calls "goToUser" when a user in the overflow dropdown is clicked', async function () {
  79. const props = {
  80. ...defaultProps,
  81. onlineUsers: makeUsers(7),
  82. }
  83. render(<OnlineUsersWidget {...props} />)
  84. const overflowButton = screen.getByText('+3')
  85. fireEvent.click(overflowButton)
  86. const frankButton = await screen.findByText('frank')
  87. fireEvent.click(frankButton)
  88. expect(props.goToUser).to.be.calledWith({
  89. id: 'user_6',
  90. user_id: 'user_id_6',
  91. name: 'frank',
  92. email: 'frank_email',
  93. })
  94. })
  95. })
  96. })