ResetOffsets.spec.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import React from 'react';
  2. import fetchMock from 'fetch-mock';
  3. import { act, screen, waitFor } from '@testing-library/react';
  4. import userEvent from '@testing-library/user-event';
  5. import { render, WithRoute } from 'lib/testHelpers';
  6. import { clusterConsumerGroupResetOffsetsPath } from 'lib/paths';
  7. import { consumerGroupPayload } from 'redux/reducers/consumerGroups/__test__/fixtures';
  8. import ResetOffsets from 'components/ConsumerGroups/Details/ResetOffsets/ResetOffsets';
  9. const clusterName = 'cluster1';
  10. const { groupId } = consumerGroupPayload;
  11. const renderComponent = () =>
  12. render(
  13. <WithRoute path={clusterConsumerGroupResetOffsetsPath()}>
  14. <ResetOffsets />
  15. </WithRoute>,
  16. {
  17. initialEntries: [
  18. clusterConsumerGroupResetOffsetsPath(
  19. clusterName,
  20. consumerGroupPayload.groupId
  21. ),
  22. ],
  23. }
  24. );
  25. const resetConsumerGroupOffsetsMockCalled = () =>
  26. expect(
  27. fetchMock.called(
  28. `/api/clusters/${clusterName}/consumer-groups/${groupId}/offsets`
  29. )
  30. ).toBeTruthy();
  31. const selectresetTypeAndPartitions = async (resetType: string) => {
  32. await userEvent.click(screen.getByLabelText('Reset Type'));
  33. await userEvent.click(screen.getByText(resetType));
  34. await userEvent.click(screen.getByText('Select...'));
  35. await userEvent.click(screen.getByText('Partition #0'));
  36. };
  37. const resetConsumerGroupOffsetsWith = async (
  38. resetType: string,
  39. offset: null | number = null
  40. ) => {
  41. await userEvent.click(screen.getByLabelText('Reset Type'));
  42. const options = screen.getAllByText(resetType);
  43. await userEvent.click(options.length > 1 ? options[1] : options[0]);
  44. await userEvent.click(screen.getByText('Select...'));
  45. await userEvent.click(screen.getByText('Partition #0'));
  46. fetchMock.postOnce(
  47. `/api/clusters/${clusterName}/consumer-groups/${groupId}/offsets`,
  48. 200,
  49. {
  50. body: {
  51. topic: '__amazon_msk_canary',
  52. resetType,
  53. partitions: [0],
  54. partitionsOffsets: [{ partition: 0, offset }],
  55. },
  56. }
  57. );
  58. await userEvent.click(screen.getByText('Submit'));
  59. await waitFor(() => resetConsumerGroupOffsetsMockCalled());
  60. };
  61. describe('ResetOffsets', () => {
  62. afterEach(() => {
  63. fetchMock.reset();
  64. });
  65. it('renders progress bar for initial state', async () => {
  66. fetchMock.getOnce(
  67. `/api/clusters/${clusterName}/consumer-groups/${groupId}`,
  68. 404
  69. );
  70. await waitFor(() => renderComponent());
  71. expect(screen.getByRole('progressbar')).toBeInTheDocument();
  72. });
  73. describe('with consumer group', () => {
  74. describe('submit handles resetConsumerGroupOffsets', () => {
  75. beforeEach(async () => {
  76. const fetchConsumerGroupMock = fetchMock.getOnce(
  77. `/api/clusters/${clusterName}/consumer-groups/${groupId}`,
  78. consumerGroupPayload
  79. );
  80. await act(() => {
  81. renderComponent();
  82. });
  83. expect(fetchConsumerGroupMock.called()).toBeTruthy();
  84. });
  85. it('calls resetConsumerGroupOffsets with EARLIEST', async () => {
  86. await resetConsumerGroupOffsetsWith('EARLIEST');
  87. });
  88. it('calls resetConsumerGroupOffsets with LATEST', async () => {
  89. await resetConsumerGroupOffsetsWith('LATEST');
  90. });
  91. it('calls resetConsumerGroupOffsets with OFFSET', async () => {
  92. await selectresetTypeAndPartitions('OFFSET');
  93. fetchMock.postOnce(
  94. `/api/clusters/${clusterName}/consumer-groups/${groupId}/offsets`,
  95. 200,
  96. {
  97. body: {
  98. topic: '__amazon_msk_canary',
  99. resetType: 'OFFSET',
  100. partitions: [0],
  101. partitionsOffsets: [{ partition: 0, offset: 10 }],
  102. },
  103. }
  104. );
  105. await userEvent.click(screen.getAllByLabelText('Partition #0')[1]);
  106. await userEvent.keyboard('10');
  107. await userEvent.click(screen.getByText('Submit'));
  108. await resetConsumerGroupOffsetsMockCalled();
  109. });
  110. it('calls resetConsumerGroupOffsets with TIMESTAMP', async () => {
  111. await selectresetTypeAndPartitions('TIMESTAMP');
  112. const resetConsumerGroupOffsetsMock = fetchMock.postOnce(
  113. `/api/clusters/${clusterName}/consumer-groups/${groupId}/offsets`,
  114. 200,
  115. {
  116. body: {
  117. topic: '__amazon_msk_canary',
  118. resetType: 'OFFSET',
  119. partitions: [0],
  120. partitionsOffsets: [{ partition: 0, offset: 10 }],
  121. },
  122. }
  123. );
  124. await userEvent.click(screen.getByText('Submit'));
  125. await waitFor(() =>
  126. expect(
  127. screen.getByText("This field shouldn't be empty!")
  128. ).toBeInTheDocument()
  129. );
  130. await waitFor(() =>
  131. expect(
  132. resetConsumerGroupOffsetsMock.called(
  133. `/api/clusters/${clusterName}/consumer-groups/${groupId}/offsets`
  134. )
  135. ).toBeFalsy()
  136. );
  137. });
  138. });
  139. });
  140. });