@@ -203,13 +203,14 @@ describe('Datagrid', () => {
203203 component . enableEdit = true ;
204204 component . startEdit ( 0 ) ;
205205 const emitSpy = jest . spyOn ( component . rowSave , 'emit' ) ;
206+ const originalRow = component . data [ 0 ] ;
206207
207208 component . editForm . patchValue ( { name : 'Alice Cooper' } ) ;
208209 component . saveEdit ( 0 ) ;
209210
210211 expect ( emitSpy ) . toHaveBeenCalledWith ( {
211- original : component . data [ 0 ] ,
212- updated : { ...component . data [ 0 ] , name : 'Alice Cooper' } ,
212+ original : originalRow ,
213+ updated : { ...originalRow , name : 'Alice Cooper' } ,
213214 index : 0
214215 } ) ;
215216 expect ( component . editingIndex ) . toBeNull ( ) ;
@@ -243,10 +244,75 @@ describe('Datagrid', () => {
243244 component . enableDelete = true ;
244245 component . pageSize = 3 ; // ensure paged contains third entry
245246 const emitSpy = jest . spyOn ( component . rowDelete , 'emit' ) ;
247+ const deleted = component . data [ 2 ] ;
246248
247249 component . deleteRow ( 2 ) ;
248250
249- expect ( emitSpy ) . toHaveBeenCalledWith ( { row : component . data [ 2 ] , index : 2 } ) ;
251+ expect ( emitSpy ) . toHaveBeenCalledWith ( { row : deleted , index : 2 } ) ;
252+ expect ( component . data . find ( r => r . id === deleted . id ) ) . toBeDefined ( ) ;
253+ } ) ;
254+
255+ it ( 'uses trackBy callback when provided' , ( ) => {
256+ const row = component . data [ 0 ] ;
257+ expect ( component . trackRow ( 0 , row ) ) . toBe ( 0 ) ;
258+
259+ component . trackBy = ( _i , r ) => ( r as any ) . id ;
260+ expect ( component . trackRow ( 0 , row ) ) . toBe ( 1 ) ;
261+ } ) ;
262+
263+ it ( 'uses the custom editService when provided' , ( ) => {
264+ const assignValues = jest . fn ( ( row : any , patch : any ) => ( { ...row , ...patch } ) ) ;
265+ const create = jest . fn ( ( ) => [ ] ) ;
266+ const update = jest . fn ( ( ) => [ ] ) ;
267+ const remove = jest . fn ( ( ) => [ ] ) ;
268+ const saveChanges = jest . fn ( ( ) => [ ] ) ;
269+ const cancelChanges = jest . fn ( ( ) => [ ] ) ;
270+
271+ component . editService = {
272+ create,
273+ update,
274+ remove,
275+ assignValues,
276+ isNew : ( ) => false ,
277+ hasChanges : ( ) => false ,
278+ saveChanges,
279+ cancelChanges,
280+ } as any ;
281+
282+ component . enableAdd = true ;
283+ component . startAdd ( ) ;
284+ expect ( create ) . toHaveBeenCalled ( ) ;
285+ component . addForm . setValue ( {
286+ id : 4 ,
287+ name : 'Dana' ,
288+ email : 'dana@example.com' ,
289+ score : 50 ,
290+ active : true ,
291+ created : '2024-04-01'
292+ } ) ;
293+ component . saveAdd ( ) ;
294+ expect ( saveChanges ) . toHaveBeenCalled ( ) ;
295+ component . startAdd ( ) ;
296+ component . cancelAdd ( ) ;
297+ expect ( cancelChanges ) . toHaveBeenCalled ( ) ;
298+
299+ component . enableEdit = true ;
300+ component . startEdit ( 0 ) ;
301+ component . editForm . patchValue ( { name : 'Alice Cooper' } ) ;
302+ component . saveEdit ( 0 ) ;
303+
304+ expect ( assignValues ) . toHaveBeenCalled ( ) ;
305+ expect ( update ) . toHaveBeenCalled ( ) ;
306+ expect ( saveChanges ) . toHaveBeenCalled ( ) ;
307+
308+ component . startEdit ( 0 ) ;
309+ component . cancelEdit ( 0 ) ;
310+ expect ( cancelChanges ) . toHaveBeenCalled ( ) ;
311+
312+ component . enableDelete = true ;
313+ component . pageSize = 3 ;
314+ component . deleteRow ( 2 ) ;
315+ expect ( remove ) . toHaveBeenCalled ( ) ;
250316 } ) ;
251317
252318 it ( 'cycles sort direction and emits sortChange' , ( ) => {
@@ -317,11 +383,13 @@ describe('Datagrid', () => {
317383 it ( 'starts editing when row clicked outside interactive targets' , ( ) => {
318384 component . enableEdit = true ;
319385 component . editOnRowClick = true ;
386+ const editSpy = jest . spyOn ( component , 'startEdit' ) ;
320387 const target = document . createElement ( 'div' ) ;
321388 const event = createEventForTarget ( target ) ;
322389
323390 component . onRowClick ( event , 0 ) ;
324391
392+ expect ( editSpy ) . toHaveBeenCalledWith ( 0 ) ;
325393 expect ( component . editingIndex ) . toBe ( 0 ) ;
326394 } ) ;
327395
@@ -353,6 +421,16 @@ describe('Datagrid', () => {
353421 expect ( component . isResponsiveEnabled ( ) ) . toBe ( false ) ;
354422 } ) ;
355423
424+ it ( 'validates email input with a safe check (guards extremely long values)' , ( ) => {
425+ const emailCol = component . columns . find ( c => c . field === 'email' ) ! ;
426+ const draft : any = { email : '!@!.' + '!.' . repeat ( 20000 ) } ;
427+ const errors : any = { } ;
428+
429+ component . validateInto ( emailCol , draft , errors ) ;
430+
431+ expect ( errors . email ) . toBe ( 'Invalid email' ) ;
432+ } ) ;
433+
356434 it ( 'delegates export via triggerExport' , ( ) => {
357435 component . exportOptions . enabled = true ;
358436 const exportSpy = jest . spyOn ( component , 'export' ) . mockResolvedValue ( ) ;
0 commit comments