Dashboard.spec.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import React from 'react';
  2. import { useClusters } from 'lib/hooks/api/clusters';
  3. import Dashboard from 'components/Dashboard/Dashboard';
  4. import { Cluster, ServerStatus } from 'generated-sources';
  5. import { render } from 'lib/testHelpers';
  6. interface DataType {
  7. data: Cluster[] | undefined;
  8. }
  9. jest.mock('lib/hooks/api/clusters');
  10. const mockedNavigate = jest.fn();
  11. jest.mock('react-router-dom', () => ({
  12. ...jest.requireActual('react-router-dom'),
  13. useNavigate: () => mockedNavigate,
  14. }));
  15. describe('Dashboard component', () => {
  16. const renderComponent = (hasDynamicConfig: boolean, data: DataType) => {
  17. const useClustersMock = useClusters as jest.Mock;
  18. useClustersMock.mockReturnValue(data);
  19. render(<Dashboard />, {
  20. globalSettings: { hasDynamicConfig },
  21. });
  22. };
  23. it('redirects to new cluster configuration page if there are no clusters and dynamic config is enabled', async () => {
  24. await renderComponent(true, { data: undefined });
  25. expect(mockedNavigate).toHaveBeenCalled();
  26. });
  27. it('should not navigate to new cluster config page when there are clusters', async () => {
  28. await renderComponent(true, {
  29. data: [{ name: 'Cluster 1', status: ServerStatus.ONLINE }],
  30. });
  31. expect(mockedNavigate).not.toHaveBeenCalled();
  32. });
  33. it('should not navigate to new cluster config page when there are no clusters and hasDynamicConfig is false', async () => {
  34. await renderComponent(false, {
  35. data: [],
  36. });
  37. expect(mockedNavigate).not.toHaveBeenCalled();
  38. });
  39. });