DashboardRow.test.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { render, screen } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import { mount } from 'enzyme';
  4. import React from 'react';
  5. import { PanelModel } from '../../state/PanelModel';
  6. import { DashboardRow } from './DashboardRow';
  7. describe('DashboardRow', () => {
  8. let wrapper: any, panel: PanelModel, dashboardMock: any;
  9. beforeEach(() => {
  10. dashboardMock = {
  11. toggleRow: jest.fn(),
  12. on: jest.fn(),
  13. meta: {
  14. canEdit: true,
  15. },
  16. events: { subscribe: jest.fn() },
  17. };
  18. panel = new PanelModel({ collapsed: false });
  19. wrapper = mount(<DashboardRow panel={panel} dashboard={dashboardMock} />);
  20. });
  21. it('Should not have collapsed class when collaped is false', () => {
  22. expect(wrapper.find('.dashboard-row')).toHaveLength(1);
  23. expect(wrapper.find('.dashboard-row--collapsed')).toHaveLength(0);
  24. });
  25. it('Should collapse when the panel is collapsed', async () => {
  26. const panel = new PanelModel({ collapsed: true });
  27. render(<DashboardRow panel={panel} dashboard={dashboardMock} />);
  28. const row = screen.getByTestId('dashboard-row-container');
  29. expect(row).toHaveClass('dashboard-row--collapsed');
  30. });
  31. it('Should collapse after clicking title', async () => {
  32. render(<DashboardRow panel={panel} dashboard={dashboardMock} />);
  33. await userEvent.click(screen.getByTestId('data-testid dashboard-row-title-'));
  34. expect(dashboardMock.toggleRow.mock.calls).toHaveLength(1);
  35. });
  36. it('Should subscribe to event during mount', () => {
  37. expect(dashboardMock.events.subscribe.mock.calls).toHaveLength(1);
  38. });
  39. it('should have two actions as admin', () => {
  40. expect(wrapper.find('.dashboard-row__actions .pointer')).toHaveLength(2);
  41. });
  42. it('should not show row drag handle when cannot edit', () => {
  43. dashboardMock.meta.canEdit = false;
  44. wrapper = mount(<DashboardRow panel={panel} dashboard={dashboardMock} />);
  45. expect(wrapper.find('.dashboard-row__drag')).toHaveLength(0);
  46. });
  47. it('should have zero actions when cannot edit', () => {
  48. dashboardMock.meta.canEdit = false;
  49. panel = new PanelModel({ collapsed: false });
  50. wrapper = mount(<DashboardRow panel={panel} dashboard={dashboardMock} />);
  51. expect(wrapper.find('.dashboard-row__actions .pointer')).toHaveLength(0);
  52. });
  53. });