Skip to content

Commit b2a04a2

Browse files
authored
Improve tests coverage (#1864)
* improve tests coverage of topic details component * improve tests coverage on topic details overview component
1 parent deddf09 commit b2a04a2

File tree

2 files changed

+158
-59
lines changed

2 files changed

+158
-59
lines changed

kafka-ui-react-app/src/components/Topics/Topic/Details/Overview/__test__/Overview.spec.tsx

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Overview, {
66
} from 'components/Topics/Topic/Details/Overview/Overview';
77
import theme from 'theme/theme';
88
import { CleanUpPolicy } from 'generated-sources';
9+
import ClusterContext from 'components/contexts/ClusterContext';
10+
import userEvent from '@testing-library/user-event';
911

1012
describe('Overview', () => {
1113
const mockClusterName = 'local';
@@ -26,72 +28,67 @@ describe('Overview', () => {
2628
offsetMin: 0,
2729
},
2830
];
31+
const defaultContextValues = {
32+
isReadOnly: false,
33+
hasKafkaConnectConfigured: true,
34+
hasSchemaRegistryConfigured: true,
35+
isTopicDeletionAllowed: true,
36+
};
37+
const defaultProps: OverviewProps = {
38+
name: mockTopicName,
39+
partitions: [],
40+
internal: true,
41+
clusterName: mockClusterName,
42+
topicName: mockTopicName,
43+
clearTopicMessages: mockClearTopicMessages,
44+
};
2945

3046
const setupComponent = (
31-
props: OverviewProps,
47+
props = defaultProps,
48+
contextValues = defaultContextValues,
3249
underReplicatedPartitions?: number,
3350
inSyncReplicas?: number,
3451
replicas?: number
3552
) =>
3653
render(
37-
<Overview
38-
underReplicatedPartitions={underReplicatedPartitions}
39-
inSyncReplicas={inSyncReplicas}
40-
replicas={replicas}
41-
{...props}
42-
/>
54+
<ClusterContext.Provider value={contextValues}>
55+
<Overview
56+
underReplicatedPartitions={underReplicatedPartitions}
57+
inSyncReplicas={inSyncReplicas}
58+
replicas={replicas}
59+
{...props}
60+
/>
61+
</ClusterContext.Provider>
4362
);
4463

4564
describe('when it has internal flag', () => {
4665
it('does not render the Action button a Topic', () => {
4766
setupComponent({
48-
name: mockTopicName,
67+
...defaultProps,
4968
partitions: mockPartitions,
5069
internal: false,
51-
clusterName: mockClusterName,
52-
topicName: mockTopicName,
5370
cleanUpPolicy: CleanUpPolicy.DELETE,
54-
clearTopicMessages: mockClearTopicMessages,
5571
});
56-
expect(screen.getByRole('menu')).toBeInTheDocument();
72+
expect(screen.getAllByRole('menu')[0]).toBeInTheDocument();
5773
});
5874

5975
it('does not render Partitions', () => {
60-
setupComponent({
61-
name: mockTopicName,
62-
partitions: [],
63-
internal: true,
64-
clusterName: mockClusterName,
65-
topicName: mockTopicName,
66-
clearTopicMessages: mockClearTopicMessages,
67-
});
76+
setupComponent();
6877

6978
expect(screen.getByText('No Partitions found')).toBeInTheDocument();
7079
});
7180
});
7281

7382
describe('should render circular alert', () => {
7483
it('should be in document', () => {
75-
setupComponent({
76-
name: mockTopicName,
77-
partitions: [],
78-
internal: true,
79-
clusterName: mockClusterName,
80-
topicName: mockTopicName,
81-
clearTopicMessages: mockClearTopicMessages,
82-
});
84+
setupComponent();
8385
const circles = screen.getAllByRole('circle');
8486
expect(circles.length).toEqual(2);
8587
});
8688

8789
it('should be the appropriate color', () => {
8890
setupComponent({
89-
name: mockTopicName,
90-
partitions: [],
91-
internal: true,
92-
clusterName: mockClusterName,
93-
topicName: mockTopicName,
94-
clearTopicMessages: mockClearTopicMessages,
91+
...defaultProps,
9592
underReplicatedPartitions: 0,
9693
inSyncReplicas: 1,
9794
replicas: 2,
@@ -105,4 +102,18 @@ describe('Overview', () => {
105102
);
106103
});
107104
});
105+
106+
describe('when Clear Messages is clicked', () => {
107+
setupComponent({
108+
...defaultProps,
109+
partitions: mockPartitions,
110+
internal: false,
111+
cleanUpPolicy: CleanUpPolicy.DELETE,
112+
});
113+
114+
const clearMessagesButton = screen.getByText('Clear Messages');
115+
userEvent.click(clearMessagesButton);
116+
117+
expect(mockClearTopicMessages).toHaveBeenCalledTimes(1);
118+
});
108119
});

kafka-ui-react-app/src/components/Topics/Topic/Details/__test__/Details.spec.tsx

Lines changed: 113 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,34 @@ import ClusterContext from 'components/contexts/ClusterContext';
55
import Details from 'components/Topics/Topic/Details/Details';
66
import { internalTopicPayload } from 'redux/reducers/topics/__test__/fixtures';
77
import { render } from 'lib/testHelpers';
8-
import { clusterTopicPath } from 'lib/paths';
8+
import {
9+
clusterTopicEditPath,
10+
clusterTopicPath,
11+
clusterTopicsPath,
12+
} from 'lib/paths';
13+
import { Router } from 'react-router-dom';
14+
import { createMemoryHistory } from 'history';
915

1016
describe('Details', () => {
1117
const mockDelete = jest.fn();
1218
const mockClusterName = 'local';
1319
const mockClearTopicMessages = jest.fn();
1420
const mockInternalTopicPayload = internalTopicPayload.internal;
1521
const mockRecreateTopic = jest.fn();
22+
const defaultPathname = clusterTopicPath(
23+
mockClusterName,
24+
internalTopicPayload.name
25+
);
26+
const mockHistory = createMemoryHistory({
27+
initialEntries: [defaultPathname],
28+
});
29+
jest.spyOn(mockHistory, 'push');
1630

17-
const setupComponent = (pathname: string) =>
31+
const setupComponent = (
32+
pathname = defaultPathname,
33+
history = mockHistory,
34+
props = {}
35+
) =>
1836
render(
1937
<ClusterContext.Provider
2038
value={{
@@ -24,17 +42,20 @@ describe('Details', () => {
2442
isTopicDeletionAllowed: true,
2543
}}
2644
>
27-
<Details
28-
clusterName={mockClusterName}
29-
topicName={internalTopicPayload.name}
30-
name={internalTopicPayload.name}
31-
isInternal={false}
32-
deleteTopic={mockDelete}
33-
recreateTopic={mockRecreateTopic}
34-
clearTopicMessages={mockClearTopicMessages}
35-
isDeleted={false}
36-
isDeletePolicy
37-
/>
45+
<Router history={history}>
46+
<Details
47+
clusterName={mockClusterName}
48+
topicName={internalTopicPayload.name}
49+
name={internalTopicPayload.name}
50+
isInternal={false}
51+
deleteTopic={mockDelete}
52+
recreateTopic={mockRecreateTopic}
53+
clearTopicMessages={mockClearTopicMessages}
54+
isDeleted={false}
55+
isDeletePolicy
56+
{...props}
57+
/>
58+
</Router>
3859
</ClusterContext.Provider>,
3960
{ pathname }
4061
);
@@ -68,10 +89,83 @@ describe('Details', () => {
6889
});
6990
});
7091

92+
describe('when remove topic modal is open', () => {
93+
beforeEach(() => {
94+
setupComponent();
95+
96+
const openModalButton = screen.getAllByText('Remove topic')[0];
97+
userEvent.click(openModalButton);
98+
});
99+
100+
it('calls deleteTopic on confirm', () => {
101+
const submitButton = screen.getAllByText('Submit')[0];
102+
userEvent.click(submitButton);
103+
104+
expect(mockDelete).toHaveBeenCalledWith(
105+
mockClusterName,
106+
internalTopicPayload.name
107+
);
108+
});
109+
110+
it('closes the modal when cancel button is clicked', () => {
111+
const cancelButton = screen.getAllByText('Cancel')[0];
112+
userEvent.click(cancelButton);
113+
114+
expect(cancelButton).not.toBeInTheDocument();
115+
});
116+
});
117+
118+
describe('when clear messages modal is open', () => {
119+
beforeEach(() => {
120+
setupComponent();
121+
122+
const confirmButton = screen.getAllByText('Clear messages')[0];
123+
userEvent.click(confirmButton);
124+
});
125+
126+
it('it calls clearTopicMessages on confirm', () => {
127+
const submitButton = screen.getAllByText('Submit')[0];
128+
userEvent.click(submitButton);
129+
130+
expect(mockClearTopicMessages).toHaveBeenCalledWith(
131+
mockClusterName,
132+
internalTopicPayload.name
133+
);
134+
});
135+
136+
it('closes the modal when cancel button is clicked', () => {
137+
const cancelButton = screen.getAllByText('Cancel')[0];
138+
userEvent.click(cancelButton);
139+
140+
expect(cancelButton).not.toBeInTheDocument();
141+
});
142+
});
143+
144+
describe('when edit settings is clicked', () => {
145+
it('redirects to the edit page', () => {
146+
setupComponent();
147+
148+
const button = screen.getAllByText('Edit settings')[0];
149+
userEvent.click(button);
150+
151+
const redirectRoute = clusterTopicEditPath(
152+
mockClusterName,
153+
internalTopicPayload.name
154+
);
155+
156+
expect(mockHistory.push).toHaveBeenCalledWith(redirectRoute);
157+
});
158+
});
159+
160+
it('redirects to the correct route if topic is deleted', () => {
161+
setupComponent(defaultPathname, mockHistory, { isDeleted: true });
162+
const redirectRoute = clusterTopicsPath(mockClusterName);
163+
164+
expect(mockHistory.push).toHaveBeenCalledWith(redirectRoute);
165+
});
166+
71167
it('shows a confirmation popup on deleting topic messages', () => {
72-
setupComponent(
73-
clusterTopicPath(mockClusterName, internalTopicPayload.name)
74-
);
168+
setupComponent();
75169
const { getByText } = screen;
76170
const clearMessagesButton = getByText(/Clear messages/i);
77171
userEvent.click(clearMessagesButton);
@@ -82,9 +176,7 @@ describe('Details', () => {
82176
});
83177

84178
it('shows a confirmation popup on recreating topic', () => {
85-
setupComponent(
86-
clusterTopicPath(mockClusterName, internalTopicPayload.name)
87-
);
179+
setupComponent();
88180
const recreateTopicButton = screen.getByText(/Recreate topic/i);
89181
userEvent.click(recreateTopicButton);
90182

@@ -94,9 +186,7 @@ describe('Details', () => {
94186
});
95187

96188
it('calling recreation function after click on Submit button', () => {
97-
setupComponent(
98-
clusterTopicPath(mockClusterName, internalTopicPayload.name)
99-
);
189+
setupComponent();
100190
const recreateTopicButton = screen.getByText(/Recreate topic/i);
101191
userEvent.click(recreateTopicButton);
102192
const confirmBtn = screen.getByRole('button', { name: /submit/i });
@@ -105,9 +195,7 @@ describe('Details', () => {
105195
});
106196

107197
it('close popup confirmation window after click on Cancel button', () => {
108-
setupComponent(
109-
clusterTopicPath(mockClusterName, internalTopicPayload.name)
110-
);
198+
setupComponent();
111199
const recreateTopicButton = screen.getByText(/Recreate topic/i);
112200
userEvent.click(recreateTopicButton);
113201
const cancelBtn = screen.getByRole('button', { name: /cancel/i });

0 commit comments

Comments
 (0)