Overview.spec.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React from 'react';
  2. import { screen } from '@testing-library/react';
  3. import { render, WithRoute } from 'lib/testHelpers';
  4. import Overview from 'components/Topics/Topic/Overview/Overview';
  5. import { theme } from 'theme/theme';
  6. import { CleanUpPolicy, Topic } from 'generated-sources';
  7. import ClusterContext from 'components/contexts/ClusterContext';
  8. import userEvent from '@testing-library/user-event';
  9. import { clusterTopicPath } from 'lib/paths';
  10. import { Replica } from 'components/Topics/Topic/Overview/Overview.styled';
  11. import { useTopicDetails } from 'lib/hooks/api/topics';
  12. import { useAppDispatch } from 'lib/hooks/redux';
  13. import {
  14. externalTopicPayload,
  15. internalTopicPayload,
  16. } from 'lib/fixtures/topics';
  17. const clusterName = 'local';
  18. const topicName = 'topic';
  19. const defaultContextValues = {
  20. isReadOnly: false,
  21. hasKafkaConnectConfigured: true,
  22. hasSchemaRegistryConfigured: true,
  23. isTopicDeletionAllowed: true,
  24. };
  25. jest.mock('lib/hooks/api/topics', () => ({
  26. useTopicDetails: jest.fn(),
  27. }));
  28. const unwrapMock = jest.fn();
  29. jest.mock('lib/hooks/redux', () => ({
  30. ...jest.requireActual('lib/hooks/redux'),
  31. useAppDispatch: jest.fn(),
  32. }));
  33. describe('Overview', () => {
  34. const renderComponent = (
  35. topic: Topic = externalTopicPayload,
  36. context = defaultContextValues
  37. ) => {
  38. (useTopicDetails as jest.Mock).mockImplementation(() => ({
  39. data: topic,
  40. }));
  41. const path = clusterTopicPath(clusterName, topicName);
  42. return render(
  43. <WithRoute path={clusterTopicPath()}>
  44. <ClusterContext.Provider value={context}>
  45. <Overview />
  46. </ClusterContext.Provider>
  47. </WithRoute>,
  48. { initialEntries: [path] }
  49. );
  50. };
  51. beforeEach(() => {
  52. (useAppDispatch as jest.Mock).mockImplementation(() => () => ({
  53. unwrap: unwrapMock,
  54. }));
  55. });
  56. it('at least one replica was rendered', () => {
  57. renderComponent();
  58. expect(screen.getByLabelText('replica-info')).toBeInTheDocument();
  59. });
  60. it('renders replica cell with props', () => {
  61. render(<Replica leader />);
  62. const element = screen.getByLabelText('replica-info');
  63. expect(element).toBeInTheDocument();
  64. expect(element).toHaveStyleRule(
  65. 'color',
  66. theme.topicMetaData.liderReplica.color
  67. );
  68. });
  69. describe('when replicas out of sync', () => {
  70. it('should be the appropriate color', () => {
  71. render(<Replica outOfSync />);
  72. const element = screen.getByLabelText('replica-info');
  73. expect(element).toBeInTheDocument();
  74. expect(element).toHaveStyleRule(
  75. 'color',
  76. theme.topicMetaData.outOfSync.color
  77. );
  78. expect(element).toHaveStyleRule('font-weight', '500');
  79. });
  80. });
  81. describe('when it has internal flag', () => {
  82. it('renders the Action button for Topic', () => {
  83. renderComponent({
  84. ...externalTopicPayload,
  85. cleanUpPolicy: CleanUpPolicy.DELETE,
  86. });
  87. expect(screen.getAllByLabelText('Dropdown Toggle').length).toEqual(1);
  88. });
  89. it('does not render Partitions', () => {
  90. renderComponent({ ...externalTopicPayload, partitions: [] });
  91. expect(screen.getByText('No Partitions found')).toBeInTheDocument();
  92. });
  93. });
  94. describe('should render circular alert', () => {
  95. it('should be in document', () => {
  96. renderComponent();
  97. const circles = screen.getAllByRole('circle');
  98. expect(circles.length).toEqual(2);
  99. });
  100. it('should be the appropriate color', () => {
  101. renderComponent({
  102. ...externalTopicPayload,
  103. underReplicatedPartitions: 0,
  104. inSyncReplicas: 1,
  105. replicas: 2,
  106. });
  107. const circles = screen.getAllByRole('circle');
  108. expect(circles[0]).toHaveStyle(
  109. `fill: ${theme.circularAlert.color.success}`
  110. );
  111. expect(circles[1]).toHaveStyle(
  112. `fill: ${theme.circularAlert.color.error}`
  113. );
  114. });
  115. });
  116. describe('when Clear Messages is clicked', () => {
  117. it('should when Clear Messages is clicked', async () => {
  118. renderComponent({
  119. ...externalTopicPayload,
  120. cleanUpPolicy: CleanUpPolicy.DELETE,
  121. });
  122. const clearMessagesButton = screen.getByText('Clear Messages');
  123. await userEvent.click(clearMessagesButton);
  124. expect(unwrapMock).toHaveBeenCalledTimes(1);
  125. });
  126. });
  127. describe('when the table partition dropdown appearance', () => {
  128. it('should check if the dropdown is disabled when it is readOnly', () => {
  129. renderComponent(
  130. {
  131. ...externalTopicPayload,
  132. },
  133. { ...defaultContextValues, isReadOnly: true }
  134. );
  135. expect(screen.getByLabelText('Dropdown Toggle')).toBeDisabled();
  136. });
  137. it('should check if the dropdown is disabled when it is internal', () => {
  138. renderComponent({
  139. ...internalTopicPayload,
  140. });
  141. expect(screen.getByLabelText('Dropdown Toggle')).toBeDisabled();
  142. });
  143. it('should check if the dropdown is disabled when cleanUpPolicy is not DELETE', () => {
  144. renderComponent({
  145. ...externalTopicPayload,
  146. cleanUpPolicy: CleanUpPolicy.COMPACT,
  147. });
  148. expect(screen.getByLabelText('Dropdown Toggle')).toBeDisabled();
  149. });
  150. });
  151. });