@@ -10,6 +10,7 @@ vi.mock('../../../utils', () => ({
1010 POST : vi . fn ( ) ,
1111 DELETE : vi . fn ( ) ,
1212 GET : vi . fn ( ) ,
13+ PUT : vi . fn ( ) ,
1314 } ,
1415} ) ) ;
1516
@@ -72,6 +73,8 @@ const defaultProps = {
7273 groupings : mockGroupings ,
7374 onClose : vi . fn ( ) ,
7475 onGroupingsUpdated : vi . fn ( ) ,
76+ encryptGroupingName : vi . fn ( ( name : string ) => Promise . resolve ( `encrypted_${ name } ` ) ) ,
77+ loadGroupings : vi . fn ( ) ,
7578} ;
7679
7780describe ( 'GroupingModal' , ( ) => {
@@ -178,50 +181,52 @@ describe('GroupingModal', () => {
178181 error : undefined ,
179182 } ) ;
180183
181- const mockGet = vi . mocked ( fetchClient . GET ) ;
182- mockGet . mockResolvedValue ( {
183- data : { groupings : [ ...mockGroupings , { id : 'new-group-id' , name : 'New Group' , device_ids : [ 'device1' ] } ] } ,
184- response : { status : 200 } as Response ,
185- error : undefined ,
186- } ) ;
184+ const mockLoadGroupings = vi . fn ( ) ;
187185
188- render ( < GroupingModal { ...defaultProps } /> ) ;
186+ render ( < GroupingModal { ...defaultProps } loadGroupings = { mockLoadGroupings } /> ) ;
189187
190188 const createButton = screen . getByRole ( 'button' , { name : / c r e a t e / i } ) ;
191189 fireEvent . click ( createButton ) ;
192190
193- await waitFor ( async ( ) => {
194- const nameInput = screen . getByPlaceholderText ( 'grouping_name_placeholder' ) ;
195- fireEvent . change ( nameInput , { target : { value : 'New Group' } } ) ;
191+ await waitFor ( ( ) => {
192+ expect ( screen . getByPlaceholderText ( 'grouping_name_placeholder' ) ) . toBeInTheDocument ( ) ;
193+ } ) ;
196194
197- const checkboxes = screen . getAllByRole ( 'checkbox ') ;
198- fireEvent . click ( checkboxes [ 0 ] ) ;
195+ const nameInput = screen . getByPlaceholderText ( 'grouping_name_placeholder ') ;
196+ fireEvent . change ( nameInput , { target : { value : 'New Group' } } ) ;
199197
200- const saveButton = screen . getByRole ( 'button' , { name : 'save' } ) ;
201- fireEvent . click ( saveButton ) ;
198+ const checkboxes = screen . getAllByRole ( 'checkbox' ) ;
199+ fireEvent . click ( checkboxes [ 0 ] ) ;
202200
203- await waitFor ( ( ) => {
204- expect ( mockPost ) . toHaveBeenCalledWith ( '/grouping/create' , expect . any ( Object ) ) ;
205- } ) ;
201+ const saveButton = screen . getByRole ( 'button' , { name : 'save' } ) ;
202+ fireEvent . click ( saveButton ) ;
203+
204+ await waitFor ( ( ) => {
205+ expect ( mockPost ) . toHaveBeenCalledWith ( '/grouping/create' , expect . objectContaining ( {
206+ body : expect . objectContaining ( { name : 'encrypted_New Group' } )
207+ } ) ) ;
208+ expect ( mockLoadGroupings ) . toHaveBeenCalled ( ) ;
206209 } ) ;
207210 } ) ;
208211
209212 it ( 'updates existing grouping when save is clicked after editing' , async ( ) => {
210213 const mockPost = vi . mocked ( fetchClient . POST ) ;
211214 mockPost . mockResolvedValue ( {
212- data : null ,
215+ data : undefined ,
213216 response : { status : 200 } as Response ,
214217 error : undefined ,
215218 } ) ;
216219
217- const mockGet = vi . mocked ( fetchClient . GET ) ;
218- mockGet . mockResolvedValue ( {
219- data : { groupings : mockGroupings } ,
220+ const mockPut = vi . mocked ( fetchClient . PUT ) ;
221+ mockPut . mockResolvedValue ( {
222+ data : undefined ,
220223 response : { status : 200 } as Response ,
221224 error : undefined ,
222225 } ) ;
223226
224- render ( < GroupingModal { ...defaultProps } /> ) ;
227+ const mockLoadGroupings = vi . fn ( ) ;
228+
229+ render ( < GroupingModal { ...defaultProps } loadGroupings = { mockLoadGroupings } /> ) ;
225230
226231 // Click edit on first grouping
227232 const editButtons = screen . getAllByRole ( 'button' , { name : '' } ) ;
@@ -238,8 +243,11 @@ describe('GroupingModal', () => {
238243 fireEvent . click ( saveButton ) ;
239244
240245 await waitFor ( ( ) => {
241- // Should call POST to add/remove devices
242- expect ( mockPost ) . toHaveBeenCalled ( ) ;
246+ // Should call PUT to update name
247+ expect ( mockPut ) . toHaveBeenCalledWith ( '/grouping/edit' , expect . objectContaining ( {
248+ body : expect . objectContaining ( { name : 'encrypted_Updated Group Name' } )
249+ } ) ) ;
250+ expect ( mockLoadGroupings ) . toHaveBeenCalled ( ) ;
243251 } ) ;
244252 } ) ;
245253 }
@@ -252,19 +260,14 @@ describe('GroupingModal', () => {
252260
253261 const mockDelete = vi . mocked ( fetchClient . DELETE ) ;
254262 mockDelete . mockResolvedValue ( {
255- data : null ,
263+ data : undefined ,
256264 response : { status : 200 } as Response ,
257265 error : undefined ,
258266 } ) ;
259267
260- const mockGet = vi . mocked ( fetchClient . GET ) ;
261- mockGet . mockResolvedValue ( {
262- data : { groupings : [ mockGroupings [ 1 ] ] } ,
263- response : { status : 200 } as Response ,
264- error : undefined ,
265- } ) ;
268+ const mockLoadGroupings = vi . fn ( ) ;
266269
267- render ( < GroupingModal { ...defaultProps } /> ) ;
270+ render ( < GroupingModal { ...defaultProps } loadGroupings = { mockLoadGroupings } /> ) ;
268271
269272 const deleteButtons = screen . getAllByRole ( 'button' , { name : '' } ) ;
270273 // Find delete button (should be the second icon button)
@@ -275,6 +278,7 @@ describe('GroupingModal', () => {
275278 await waitFor ( ( ) => {
276279 expect ( window . confirm ) . toHaveBeenCalled ( ) ;
277280 expect ( mockDelete ) . toHaveBeenCalledWith ( '/grouping/delete' , expect . any ( Object ) ) ;
281+ expect ( mockLoadGroupings ) . toHaveBeenCalled ( ) ;
278282 } ) ;
279283
280284 // Restore original confirm
@@ -394,32 +398,27 @@ describe('GroupingModal', () => {
394398 } ) ;
395399
396400 it ( 'calls onGroupingsUpdated after successful operations' , async ( ) => {
397- const mockGet = vi . mocked ( fetchClient . GET ) ;
398- mockGet . mockResolvedValue ( {
399- data : { groupings : mockGroupings } ,
400- response : { status : 200 } as Response ,
401- error : undefined ,
402- } ) ;
401+ const mockLoadGroupings = vi . fn ( ) ;
403402
404403 const originalConfirm = window . confirm ;
405404 window . confirm = vi . fn ( ( ) => true ) ;
406405
407406 const mockDelete = vi . mocked ( fetchClient . DELETE ) ;
408407 mockDelete . mockResolvedValue ( {
409- data : null ,
408+ data : undefined ,
410409 response : { status : 200 } as Response ,
411410 error : undefined ,
412411 } ) ;
413412
414- render ( < GroupingModal { ...defaultProps } /> ) ;
413+ render ( < GroupingModal { ...defaultProps } loadGroupings = { mockLoadGroupings } /> ) ;
415414
416415 const deleteButtons = screen . getAllByRole ( 'button' , { name : '' } ) ;
417416 const deleteButton = deleteButtons [ deleteButtons . length - 1 ] ;
418417
419418 fireEvent . click ( deleteButton ) ;
420419
421420 await waitFor ( ( ) => {
422- expect ( defaultProps . onGroupingsUpdated ) . toHaveBeenCalledWith ( mockGroupings ) ;
421+ expect ( mockLoadGroupings ) . toHaveBeenCalled ( ) ;
423422 } ) ;
424423
425424 window . confirm = originalConfirm ;
0 commit comments