@@ -11,6 +11,10 @@ import { mockTemplateInvocationResponse } from './fixtures';
1111jest . spyOn ( api , 'get' ) ;
1212jest . mock ( '../JobInvocationSelectors' ) ;
1313
14+ jest . mock ( 'foremanReact/components/ToastsList' , ( ) => ( {
15+ addToast : jest . fn ( payload => ( { type : 'ADD_TOAST' , payload } ) ) ,
16+ } ) ) ;
17+
1418const mockStore = configureMockStore ( [ ] ) ;
1519const store = mockStore ( {
1620 HOSTS_API : {
@@ -186,4 +190,60 @@ describe('TemplateInvocation', () => {
186190 await screen . findByText ( 'Successfully copied to clipboard!' )
187191 ) . toBeInTheDocument ( ) ;
188192 } ) ;
193+
194+ describe ( 'Cancel/Abort task buttons API calls' , ( ) => {
195+ const responseWithCancellableTask = {
196+ ...mockTemplateInvocationResponse ,
197+ task : { id : 'task-123' , cancellable : true } ,
198+ permissions : {
199+ view_foreman_tasks : true ,
200+ cancel_job_invocations : true ,
201+ execute_jobs : true ,
202+ } ,
203+ } ;
204+
205+ beforeEach ( ( ) => {
206+ selectors . selectTemplateInvocationStatus . mockImplementation ( ( ) => ( ) =>
207+ 'RESOLVED'
208+ ) ;
209+ selectors . selectTemplateInvocation . mockImplementation ( ( ) => ( ) =>
210+ responseWithCancellableTask
211+ ) ;
212+ jest . spyOn ( api . APIActions , 'post' ) . mockReturnValue ( { type : 'MOCK_POST' } ) ;
213+ } ) ;
214+
215+ test ( 'clicking the `Cancel Task` button calls API with cancel param' , ( ) => {
216+ render (
217+ < Provider store = { store } >
218+ < TemplateInvocation { ...mockProps } />
219+ </ Provider >
220+ ) ;
221+ fireEvent . click ( screen . getByText ( 'Cancel Task' ) ) ;
222+
223+ const postCall = api . APIActions . post . mock . calls . find (
224+ call => call [ 0 ] . key === 'CANCEL_TASK'
225+ ) ?. [ 0 ] ;
226+ expect ( postCall . url ) . toBe (
227+ `/foreman_tasks/tasks/${ responseWithCancellableTask . task . id } /cancel`
228+ ) ;
229+ expect ( postCall . key ) . toBe ( 'CANCEL_TASK' ) ;
230+ } ) ;
231+
232+ test ( 'clicking the `Abort Task` button calls API with abort param' , ( ) => {
233+ render (
234+ < Provider store = { store } >
235+ < TemplateInvocation { ...mockProps } />
236+ </ Provider >
237+ ) ;
238+ fireEvent . click ( screen . getByText ( 'Abort task' ) ) ;
239+
240+ const postCall = api . APIActions . post . mock . calls . find (
241+ call => call [ 0 ] . key === 'ABORT_TASK'
242+ ) ?. [ 0 ] ;
243+ expect ( postCall . url ) . toBe (
244+ `/foreman_tasks/tasks/${ responseWithCancellableTask . task . id } /abort`
245+ ) ;
246+ expect ( postCall . key ) . toBe ( 'ABORT_TASK' ) ;
247+ } ) ;
248+ } ) ;
189249} ) ;
0 commit comments