88import { renderHook , act } from '@testing-library/react' ;
99import { HttpSetup } from '@kbn/core-http-browser' ;
1010import { useQuickPromptUpdater } from './use_quick_prompt_updater' ;
11- import { FindPromptsResponse , PromptResponse } from '@kbn/elastic-assistant-common' ;
12-
11+ import { FindPromptsResponse , PromptResponse , PromptTypeEnum } from '@kbn/elastic-assistant-common' ;
12+ import { bulkUpdatePrompts } from '../../../..' ;
13+ import { IToasts } from '@kbn/core-notifications-browser' ;
1314const mockHttp = { } as HttpSetup ;
15+ jest . mock ( '../../../..' ) ;
16+ jest . mock ( '../../quick_prompts/quick_prompt_settings/helpers' , ( ) => {
17+ return {
18+ getRandomEuiColor : jest . fn ( ( ) => '#61A2FF' ) ,
19+ } ;
20+ } ) ;
21+ const mockBulkUpdatePrompts = bulkUpdatePrompts as jest . Mock ;
1422const quickPrompt : PromptResponse = {
1523 timestamp : '2025-02-24T18:13:51.851Z' ,
1624 users : [ { id : 'u_mGBROF_q5bmFCATbLXAcCwKa0k8JvONAwSruelyKA5E_0' , name : 'elastic' } ] ,
@@ -57,11 +65,19 @@ const mockAllPrompts: FindPromptsResponse = {
5765 } ,
5866 ] ,
5967} ;
68+ const mockToasts = {
69+ addSuccess : jest . fn ( ) ,
70+ addDanger : jest . fn ( ) ,
71+ } as unknown as IToasts ;
6072
6173describe ( 'useQuickPromptUpdater' , ( ) => {
74+ beforeEach ( ( ) => {
75+ jest . clearAllMocks ( ) ;
76+ } ) ;
6277 it ( 'should initialize with quick prompts' , ( ) => {
6378 const { result } = renderHook ( ( ) =>
6479 useQuickPromptUpdater ( {
80+ toasts : mockToasts ,
6581 allPrompts : mockAllPrompts ,
6682 currentAppId : 'securitySolutionUI' ,
6783 http : mockHttp ,
@@ -76,6 +92,7 @@ describe('useQuickPromptUpdater', () => {
7692 it ( 'should select a quick prompt by id' , ( ) => {
7793 const { result } = renderHook ( ( ) =>
7894 useQuickPromptUpdater ( {
95+ toasts : mockToasts ,
7996 allPrompts : mockAllPrompts ,
8097 currentAppId : 'securitySolutionUI' ,
8198 http : mockHttp ,
@@ -93,6 +110,7 @@ describe('useQuickPromptUpdater', () => {
93110 it ( 'should add a new quick prompt when selecting by name' , ( ) => {
94111 const { result } = renderHook ( ( ) =>
95112 useQuickPromptUpdater ( {
113+ toasts : mockToasts ,
96114 allPrompts : mockAllPrompts ,
97115 currentAppId : 'securitySolutionUI' ,
98116 http : mockHttp ,
@@ -104,13 +122,21 @@ describe('useQuickPromptUpdater', () => {
104122 result . current . onQuickPromptSelect ( 'New Quick Prompt' ) ;
105123 } ) ;
106124
107- expect ( result . current . quickPromptSettings ) . toHaveLength ( 3 ) ;
108- expect ( result . current . quickPromptSettings [ 2 ] . name ) . toBe ( 'New Quick Prompt' ) ;
125+ expect ( result . current . selectedQuickPrompt ) . toEqual ( {
126+ name : 'New Quick Prompt' ,
127+ id : '' ,
128+ content : '' ,
129+ color : '#61A2FF' ,
130+ categories : [ ] ,
131+ promptType : PromptTypeEnum . quick ,
132+ consumer : 'securitySolutionUI' ,
133+ } ) ;
109134 } ) ;
110135
111136 it ( 'should change the content of a selected quick prompt' , ( ) => {
112137 const { result } = renderHook ( ( ) =>
113138 useQuickPromptUpdater ( {
139+ toasts : mockToasts ,
114140 allPrompts : mockAllPrompts ,
115141 currentAppId : 'securitySolutionUI' ,
116142 http : mockHttp ,
@@ -132,6 +158,7 @@ describe('useQuickPromptUpdater', () => {
132158 it ( 'should update prompt color' , ( ) => {
133159 const { result } = renderHook ( ( ) =>
134160 useQuickPromptUpdater ( {
161+ toasts : mockToasts ,
135162 allPrompts : mockAllPrompts ,
136163 currentAppId : 'securitySolutionUI' ,
137164 http : mockHttp ,
@@ -156,6 +183,7 @@ describe('useQuickPromptUpdater', () => {
156183 it ( 'should reset quick prompt settings' , ( ) => {
157184 const { result } = renderHook ( ( ) =>
158185 useQuickPromptUpdater ( {
186+ toasts : mockToasts ,
159187 allPrompts : mockAllPrompts ,
160188 currentAppId : 'securitySolutionUI' ,
161189 http : mockHttp ,
@@ -167,12 +195,101 @@ describe('useQuickPromptUpdater', () => {
167195 result . current . onQuickPromptSelect ( 'New Quick Prompt' ) ;
168196 } ) ;
169197
170- expect ( result . current . quickPromptSettings ) . toHaveLength ( 3 ) ;
198+ expect ( result . current . selectedQuickPrompt ?. name ) . toEqual ( 'New Quick Prompt' ) ;
171199
172200 act ( ( ) => {
173201 result . current . resetQuickPromptSettings ( ) ;
174202 } ) ;
203+ expect ( result . current . selectedQuickPrompt ) . toEqual ( undefined ) ;
204+ } ) ;
205+ it ( 'should delete a quick prompt by id' , async ( ) => {
206+ const { result } = renderHook ( ( ) =>
207+ useQuickPromptUpdater ( {
208+ toasts : mockToasts ,
209+ allPrompts : mockAllPrompts ,
210+ currentAppId : 'securitySolutionUI' ,
211+ http : mockHttp ,
212+ promptsLoaded : true ,
213+ } )
214+ ) ;
175215
176- expect ( result . current . quickPromptSettings ) . toHaveLength ( 2 ) ;
216+ act ( ( ) => {
217+ result . current . onQuickPromptDelete ( 'OZ4qOZUBqnYEVX-cWulv' ) ;
218+ } ) ;
219+ await act ( async ( ) => {
220+ await result . current . saveQuickPromptSettings ( ) ;
221+ } ) ;
222+
223+ expect ( mockBulkUpdatePrompts ) . toHaveBeenCalledWith (
224+ mockHttp ,
225+ {
226+ delete : { ids : [ 'OZ4qOZUBqnYEVX-cWulv' ] } ,
227+ } ,
228+ mockToasts
229+ ) ;
230+ } ) ;
231+
232+ it ( 'should change the context of a selected quick prompt' , ( ) => {
233+ const { result } = renderHook ( ( ) =>
234+ useQuickPromptUpdater ( {
235+ toasts : mockToasts ,
236+ allPrompts : mockAllPrompts ,
237+ currentAppId : 'securitySolutionUI' ,
238+ http : mockHttp ,
239+ promptsLoaded : true ,
240+ } )
241+ ) ;
242+
243+ act ( ( ) => {
244+ result . current . onQuickPromptSelect ( 'OZ4qOZUBqnYEVX-cWulv' ) ;
245+ } ) ;
246+
247+ act ( ( ) => {
248+ result . current . onQuickPromptContextChange ( [
249+ { category : 'new-category' , description : 'text' , tooltip : 'hi' } ,
250+ ] ) ;
251+ } ) ;
252+
253+ expect ( result . current . selectedQuickPrompt ?. categories ) . toEqual ( [ 'new-category' ] ) ;
254+ } ) ;
255+
256+ it ( 'should save quick prompt settings' , async ( ) => {
257+ const { result } = renderHook ( ( ) =>
258+ useQuickPromptUpdater ( {
259+ toasts : mockToasts ,
260+ allPrompts : mockAllPrompts ,
261+ currentAppId : 'securitySolutionUI' ,
262+ http : mockHttp ,
263+ promptsLoaded : true ,
264+ } )
265+ ) ;
266+
267+ act ( ( ) => {
268+ result . current . onQuickPromptSelect ( 'OZ4qOZUBqnYEVX-cWulv' ) ;
269+ } ) ;
270+
271+ act ( ( ) => {
272+ result . current . onPromptContentChange ( 'Updated content' ) ;
273+ } ) ;
274+ await act ( async ( ) => {
275+ await result . current . saveQuickPromptSettings ( ) ;
276+ } ) ;
277+ expect ( mockBulkUpdatePrompts ) . toHaveBeenCalledWith (
278+ mockHttp ,
279+ {
280+ create : [
281+ {
282+ categories : [ ] ,
283+ color : '#61A2FF' ,
284+ consumer : 'securitySolutionUI' ,
285+ content : 'Updated content' ,
286+ id : '' ,
287+ name : 'OZ4qOZUBqnYEVX-cWulv' ,
288+ promptType : 'quick' ,
289+ } ,
290+ ] ,
291+ } ,
292+ mockToasts
293+ ) ;
177294 } ) ;
178295} ) ;
0 commit comments