@@ -5,16 +5,34 @@ import ClusterContext from 'components/contexts/ClusterContext';
55import Details from 'components/Topics/Topic/Details/Details' ;
66import { internalTopicPayload } from 'redux/reducers/topics/__test__/fixtures' ;
77import { 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
1016describe ( '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 ( / C l e a r m e s s a g e s / 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 ( / R e c r e a t e t o p i c / 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 ( / R e c r e a t e t o p i c / i) ;
101191 userEvent . click ( recreateTopicButton ) ;
102192 const confirmBtn = screen . getByRole ( 'button' , { name : / s u b m i t / 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 ( / R e c r e a t e t o p i c / i) ;
112200 userEvent . click ( recreateTopicButton ) ;
113201 const cancelBtn = screen . getByRole ( 'button' , { name : / c a n c e l / i } ) ;
0 commit comments