PresenceIndicators.test.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { render, screen, waitFor } from '@testing-library/react';
  2. import React from 'react';
  3. import { DashboardModel } from 'app/features/dashboard/state';
  4. import { PresenceIndicators, PresenceIndicatorsProps } from './PresenceIndicators';
  5. import { getMockRecentUsers } from './__mocks__/recentUsersMocks';
  6. import { getRecentUsers } from './api';
  7. beforeEach(() => {
  8. jest.clearAllMocks();
  9. jest.useFakeTimers();
  10. });
  11. afterEach(() => {
  12. jest.useRealTimers();
  13. });
  14. jest.mock('./api', () => {
  15. return {
  16. getRecentUsers: jest.fn((dashboardId: number) => Promise.resolve(getMockRecentUsers().slice(0, dashboardId))),
  17. };
  18. });
  19. jest.mock('app/core/services/context_srv', () => {
  20. return {
  21. contextSrv: { user: { id: 1 } },
  22. };
  23. });
  24. const setup = (dashboardId: number) => {
  25. const props: PresenceIndicatorsProps = {
  26. dashboard: new DashboardModel({ id: dashboardId }, { url: 'testdashboard' }),
  27. openDrawer: jest.fn(),
  28. };
  29. render(<PresenceIndicators {...props} />);
  30. };
  31. describe('Render', () => {
  32. it('should render component - no dashboard', async () => {
  33. setup(0);
  34. expect(getRecentUsers).not.toHaveBeenCalled();
  35. await waitFor(() => screen.queryByLabelText('Presence indicators container'));
  36. expect(screen.queryByLabelText('Presence indicators container')).not.toBeInTheDocument();
  37. });
  38. it('should render component - only current user', async () => {
  39. setup(1);
  40. expect(getRecentUsers).toHaveBeenCalledTimes(1);
  41. expect(getRecentUsers).toHaveBeenCalledWith(1, 14);
  42. await waitFor(() => screen.queryByLabelText('Presence indicators container'));
  43. expect(screen.queryByLabelText('Presence indicators container')).not.toBeInTheDocument();
  44. });
  45. it('should render component - few users (all should be displayed)', async () => {
  46. setup(3);
  47. expect(getRecentUsers).toHaveBeenCalledTimes(1);
  48. expect(getRecentUsers).toHaveBeenCalledWith(3, 14);
  49. expect(await screen.findAllByLabelText('Avatar icon')).toHaveLength(1);
  50. expect(await screen.findAllByLabelText('Initials icon')).toHaveLength(1);
  51. expect(screen.queryByLabelText('More users icon')).not.toBeInTheDocument();
  52. });
  53. it('should render component - more users (more icon should be displayed)', async () => {
  54. setup(6);
  55. expect(getRecentUsers).toHaveBeenCalledTimes(1);
  56. expect(getRecentUsers).toHaveBeenCalledWith(6, 14);
  57. expect(await screen.findAllByLabelText('Avatar icon')).toHaveLength(2);
  58. expect(await screen.findAllByLabelText('Initials icon')).toHaveLength(1);
  59. expect(screen.getByLabelText('More users icon')).toBeInTheDocument();
  60. });
  61. });