BrokerLogdir.spec.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import React from 'react';
  2. import { render, WithRoute } from 'lib/testHelpers';
  3. import { screen, waitFor } from '@testing-library/dom';
  4. import { clusterBrokerPath } from 'lib/paths';
  5. import fetchMock from 'fetch-mock';
  6. import { act } from '@testing-library/react';
  7. import Broker from 'components/Brokers/Broker/Broker';
  8. import {
  9. clusterStatsPayload,
  10. brokerLogDirsPayload,
  11. brokersPayload,
  12. } from 'components/Brokers/__test__/fixtures';
  13. const clusterName = 'local';
  14. const brokerId = 1;
  15. const fetchStatsUrl = `/api/clusters/${clusterName}/stats`;
  16. const fetchBrokersUrl = `/api/clusters/${clusterName}/brokers`;
  17. const fetchLogDirsUrl = `/api/clusters/${clusterName}/brokers/logdirs`;
  18. describe('BrokerLogdir Component', () => {
  19. afterEach(() => {
  20. fetchMock.reset();
  21. });
  22. const renderComponent = async () => {
  23. const fetchStatsMock = fetchMock.get(fetchStatsUrl, clusterStatsPayload);
  24. const fetchBrokersMock = fetchMock.get(fetchBrokersUrl, brokersPayload);
  25. await act(() => {
  26. render(
  27. <WithRoute path={clusterBrokerPath()}>
  28. <Broker />
  29. </WithRoute>,
  30. {
  31. initialEntries: [clusterBrokerPath(clusterName, brokerId)],
  32. }
  33. );
  34. });
  35. await waitFor(() => expect(fetchStatsMock.called()).toBeTruthy());
  36. expect(fetchBrokersMock.called()).toBeTruthy();
  37. };
  38. it('shows warning when server returns empty logDirs response', async () => {
  39. const fetchLogDirsMock = fetchMock.getOnce(fetchLogDirsUrl, [], {
  40. query: { broker: brokerId },
  41. });
  42. await renderComponent();
  43. await waitFor(() => expect(fetchLogDirsMock.called()).toBeTruthy());
  44. expect(screen.getByText('Log dir data not available')).toBeInTheDocument();
  45. });
  46. it('shows broker', async () => {
  47. const fetchLogDirsMock = fetchMock.getOnce(
  48. fetchLogDirsUrl,
  49. brokerLogDirsPayload,
  50. {
  51. query: { broker: brokerId },
  52. }
  53. );
  54. await renderComponent();
  55. await waitFor(() => expect(fetchLogDirsMock.called()).toBeTruthy());
  56. const topicCount = screen.getByText(3);
  57. const partitionsCount = screen.getByText(4);
  58. expect(topicCount).toBeInTheDocument();
  59. expect(partitionsCount).toBeInTheDocument();
  60. });
  61. });