1- import { mockUseJwt , renderHookWithApplicationContext } from '@/utils/testing.utils'
1+ import {
2+ mockUseCurrentRoute ,
3+ mockUseJwt ,
4+ renderHookWithApplicationContext ,
5+ } from '@/utils/testing.utils'
26import useGetConsumerPurposesActions from '../useGetConsumerPurposesActions'
37import { createMockPurpose } from '@/../__mocks__/data/purpose.mocks'
48import { rest } from 'msw'
@@ -111,6 +115,7 @@ describe('check if useGetConsumerPurposesActions returns the correct actions bas
111115 it ( 'should return the suspend action if the purpose is active' , ( ) => {
112116 const purposeMock = createMockPurpose ( {
113117 currentVersion : { state : 'ACTIVE' } ,
118+ rulesetExpiration : '2099-01-01T00:00:00Z' ,
114119 } )
115120 const { result } = renderUseGetConsumerPurposesActionsHook ( purposeMock )
116121 expect ( result . current . actions . length ) . toBeGreaterThanOrEqual ( 1 )
@@ -126,6 +131,7 @@ describe('check if useGetConsumerPurposesActions returns the correct actions bas
126131 const purposeMock = createMockPurpose ( {
127132 currentVersion : { state : 'SUSPENDED' } ,
128133 suspendedByConsumer : false ,
134+ rulesetExpiration : '2099-01-01T00:00:00Z' ,
129135 } )
130136 const { result } = renderUseGetConsumerPurposesActionsHook ( purposeMock )
131137 expect ( result . current . actions . length ) . toBeGreaterThanOrEqual ( 1 )
@@ -141,6 +147,7 @@ describe('check if useGetConsumerPurposesActions returns the correct actions bas
141147 const purposeMock = createMockPurpose ( {
142148 currentVersion : { state : 'SUSPENDED' } ,
143149 suspendedByConsumer : true ,
150+ rulesetExpiration : '2099-01-01T00:00:00Z' ,
144151 } )
145152 const { result } = renderUseGetConsumerPurposesActionsHook ( purposeMock )
146153 expect ( result . current . actions . length ) . toBeGreaterThanOrEqual ( 1 )
@@ -175,4 +182,103 @@ describe('check if useGetConsumerPurposesActions returns the correct actions bas
175182 expect ( history . location . pathname ) . toBe ( '/it/fruizione/finalita/test-purpose-id/riepilogo' )
176183 } )
177184 } )
185+ it ( 'should return only clone action when purpose is rejected in DELIVER mode' , ( ) => {
186+ const purposeMock = createMockPurpose ( {
187+ currentVersion : undefined ,
188+ rejectedVersion : {
189+ id : 'rejected-id' ,
190+ state : 'REJECTED' ,
191+ dailyCalls : 1 ,
192+ } ,
193+ eservice : { mode : 'DELIVER' } ,
194+ } )
195+
196+ const { result } = renderUseGetConsumerPurposesActionsHook ( purposeMock )
197+
198+ expect ( result . current . actions ) . toHaveLength ( 1 )
199+ expect ( result . current . actions [ 0 ] . label ) . toBe ( 'clone' )
200+ } )
201+
202+ it ( 'should not return clone action if isRulesetExpired and routeKey is SUBSCRIBE_PURPOSE_LIST' , ( ) => {
203+ mockUseCurrentRoute ( { routeKey : 'SUBSCRIBE_PURPOSE_LIST' } )
204+ const purposeMock = createMockPurpose ( {
205+ eservice : { mode : 'DELIVER' } ,
206+ rulesetExpiration : '2023-01-01T00:00:00Z' ,
207+ currentVersion : { state : 'ACTIVE' } ,
208+ } )
209+
210+ const { result } = renderUseGetConsumerPurposesActionsHook ( purposeMock )
211+
212+ const cloneAction = result . current . actions . find ( ( a ) => a . label === 'clone' )
213+ expect ( cloneAction ) . toBeUndefined ( )
214+ } )
215+
216+ it ( 'should return clone action if isRulesetExpired is not and routeKey is SUBSCRIBE_PURPOSE_LIST' , ( ) => {
217+ mockUseCurrentRoute ( { routeKey : 'SUBSCRIBE_PURPOSE_LIST' } )
218+ const purposeMock = createMockPurpose ( {
219+ eservice : { mode : 'DELIVER' } ,
220+ rulesetExpiration : '2099-01-01T00:00:00Z' ,
221+ currentVersion : { state : 'ACTIVE' } ,
222+ } )
223+
224+ const { result } = renderUseGetConsumerPurposesActionsHook ( purposeMock )
225+
226+ const cloneAction = result . current . actions . find ( ( a ) => a . label === 'clone' )
227+ expect ( cloneAction ) . toBeDefined ( )
228+ } )
229+
230+ it ( 'should return clone action if routeKey is not SUBSCRIBE_PURPOSE_LIST' , ( ) => {
231+ mockUseCurrentRoute ( { routeKey : 'SUBSCRIBE_PURPOSE_DETAILS' } )
232+ const purposeMock = createMockPurpose ( {
233+ eservice : { mode : 'DELIVER' } ,
234+ rulesetExpiration : '2099-01-01T00:00:00Z' ,
235+ currentVersion : { state : 'ACTIVE' } ,
236+ } )
237+
238+ const { result } = renderUseGetConsumerPurposesActionsHook ( purposeMock )
239+
240+ const cloneAction = result . current . actions . find ( ( a ) => a . label === 'clone' )
241+ expect ( cloneAction ) . toBeDefined ( )
242+ } )
243+
244+ it ( 'should not return clone action for archived purpose in non-DELIVER mode' , ( ) => {
245+ const purposeMock = createMockPurpose ( {
246+ currentVersion : { state : 'ARCHIVED' } ,
247+ eservice : { mode : 'RECEIVE' } ,
248+ } )
249+
250+ const { result } = renderUseGetConsumerPurposesActionsHook ( purposeMock )
251+
252+ const cloneAction = result . current . actions . find ( ( a ) => a . label === 'clone' )
253+ expect ( cloneAction ) . toBeUndefined ( )
254+ } )
255+
256+ describe ( 'clone action button' , ( ) => {
257+ it ( 'should have tooltip when ruleset is expired and the button is disabled' , ( ) => {
258+ mockUseCurrentRoute ( { routeKey : 'SUBSCRIBE_PURPOSE_DETAILS' } )
259+ const purposeMock = createMockPurpose ( {
260+ eservice : { mode : 'DELIVER' } ,
261+ rulesetExpiration : '2023-01-01T00:00:00Z' ,
262+ } )
263+ const { result } = renderUseGetConsumerPurposesActionsHook ( purposeMock )
264+
265+ const cloneAction = result . current . actions . find ( ( action ) => action . label === 'clone' )
266+
267+ expect ( cloneAction ?. tooltip ) . toBeDefined ( )
268+ expect ( cloneAction ?. disabled ) . toBe ( true )
269+ } )
270+
271+ it ( 'should not have tooltip when ruleset is not expired and the button is enabled' , ( ) => {
272+ const purposeMock = createMockPurpose ( {
273+ eservice : { mode : 'DELIVER' } ,
274+ rulesetExpiration : '2099-01-01T00:00:00Z' ,
275+ } )
276+ const { result } = renderUseGetConsumerPurposesActionsHook ( purposeMock )
277+
278+ const cloneAction = result . current . actions . find ( ( action ) => action . label === 'clone' )
279+
280+ expect ( cloneAction ?. tooltip ) . toBeUndefined ( )
281+ expect ( cloneAction ?. disabled ) . toBe ( false )
282+ } )
283+ } )
178284} )
0 commit comments