AnalyticsUsersTab.test.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { render, screen, within } from '@testing-library/react';
  2. import $ from 'jquery';
  3. import React from 'react';
  4. import { createTheme } from '@grafana/data';
  5. import { DashboardModel } from 'app/features/dashboard/state';
  6. import { getMockDailySummaries } from '../__mocks__/dailySummariesMocks';
  7. import { getMockRecentUsers } from '../__mocks__/recentUsersMocks';
  8. import { getDashboardUsersInfo, DashboardDailySummaryDTO, UserViewDTO } from '../api';
  9. import { AnalyticsUsersTab, Props } from './AnalyticsUsersTab';
  10. beforeEach(() => {
  11. jest.clearAllMocks();
  12. });
  13. jest.mock(
  14. 'react-virtualized-auto-sizer',
  15. () =>
  16. ({ children }: any) =>
  17. children({ height: 600, width: 600 })
  18. );
  19. jest.mock('../api', () => {
  20. return {
  21. getDashboardUsersInfo: jest.fn((dashboardId: number) => {
  22. const recentUsers = getMockRecentUsers();
  23. return Promise.resolve({
  24. creator: dashboardId ? recentUsers[0] : null,
  25. lastEditor: dashboardId ? recentUsers[1] : null,
  26. });
  27. }),
  28. };
  29. });
  30. //@ts-ignore
  31. $.plot = jest.fn();
  32. const setup = (dashboard: DashboardModel, dailySummaries: DashboardDailySummaryDTO[], userViews: UserViewDTO[]) => {
  33. const props: Props = {
  34. dashboard,
  35. dailySummaries,
  36. userViews,
  37. theme: createTheme(),
  38. setDrawerOpen: jest.fn() as any,
  39. };
  40. render(<AnalyticsUsersTab {...props} />);
  41. };
  42. describe('Render', () => {
  43. it('should render empty component - no data', async () => {
  44. setup(new DashboardModel({ id: 0 }), [], []);
  45. expect(screen.queryByLabelText('User information box')).not.toBeInTheDocument();
  46. expect(screen.queryByRole('main')).not.toBeInTheDocument();
  47. });
  48. it('should render dashboard meta information', async () => {
  49. setup(new DashboardModel({ id: 1 }), [], []);
  50. expect(getDashboardUsersInfo).toHaveBeenCalledTimes(1);
  51. expect(await screen.findAllByLabelText('User information box')).toHaveLength(2);
  52. expect(screen.queryByRole('main')).not.toBeInTheDocument();
  53. });
  54. it('should render views from daily summaries', async () => {
  55. setup(new DashboardModel({ id: 0 }), getMockDailySummaries(), []);
  56. expect(await screen.findByRole('main')).toBeInTheDocument();
  57. expect(screen.queryByLabelText('User information box')).not.toBeInTheDocument();
  58. expect(screen.queryByLabelText('Graph container')).toBeInTheDocument();
  59. });
  60. it('should render recent users information', async () => {
  61. const userViews = getMockRecentUsers();
  62. setup(new DashboardModel({ id: 0 }, { showSettings: true }), [], userViews);
  63. expect(await screen.findByRole('main')).toBeInTheDocument();
  64. expect(screen.getAllByLabelText('User information box')).toHaveLength(1);
  65. expect(screen.getByLabelText('User box title')).toHaveTextContent('Last viewed');
  66. expect(screen.getByLabelText('Version history button')).toBeInTheDocument();
  67. expect(screen.getByRole('table')).toBeInTheDocument();
  68. expect(screen.getAllByRole('row')).toHaveLength(userViews.length + 1);
  69. expect(screen.getAllByRole('cell')).toHaveLength(3 * userViews.length);
  70. const { queryAllByLabelText } = within(screen.getByRole('table'));
  71. expect(queryAllByLabelText('Avatar icon').concat(queryAllByLabelText('Initials icon'))).toHaveLength(
  72. userViews.length
  73. );
  74. });
  75. it('should not render version history button', async () => {
  76. const userViews = getMockRecentUsers();
  77. setup(new DashboardModel({ id: 0 }, { canSave: false }), [], userViews);
  78. expect(await screen.findByRole('main')).toBeInTheDocument();
  79. expect(screen.queryByLabelText('Version history button')).not.toBeInTheDocument();
  80. expect(screen.getByRole('table')).toBeInTheDocument();
  81. expect(screen.getAllByRole('row')).toHaveLength(userViews.length + 1);
  82. });
  83. });