@@ -293,228 +293,187 @@ describe('utils', () => {
293293
294294 expect ( result ) . toBeNull ( ) ;
295295 } ) ;
296- describe ( 'applySortByToOptions' , ( ) => {
297- it ( 'should not modify options when sortBy is empty' , ( ) => {
298- const options = { } ;
299- const sortBy = [ ] ;
300- const apiFieldsForColumnAccessor = { someColumn : { key : 'column_name' } } ;
301-
302- applySortByToOptions ( sortBy , apiFieldsForColumnAccessor , options ) ;
303-
304- expect ( options ) . toEqual ( { } ) ;
305- } ) ;
306-
307- it ( 'should correctly apply sorting options when sortBy is provided' , ( ) => {
308- const options = { } ;
309- const sortBy = [
310- { id : 'someColumn' , desc : false } ,
311- { id : 'anotherColumn' , desc : true } ,
312- ] ;
313- const apiFieldsForColumnAccessor = {
314- someColumn : { key : 'column_name' } ,
315- anotherColumn : { key : 'another_column' } ,
316- } ;
317-
318- applySortByToOptions ( sortBy , apiFieldsForColumnAccessor , options ) ;
319-
320- expect ( options ) . toEqual ( {
321- ordering : 'column_name,-another_column' , // Ensure ordering matches the expected format
322- } ) ;
323- } ) ;
324-
325- it ( 'should handle missing keys in apiFieldsForColumnAccessor gracefully' , ( ) => {
326- const options = { } ;
327- const sortBy = [
328- { id : 'nonExistentColumn' , desc : false } ,
329- { id : 'someColumn' , desc : true } ,
330- ] ;
331- const apiFieldsForColumnAccessor = {
332- someColumn : { key : 'column_name' } ,
333- } ;
334-
335- applySortByToOptions ( sortBy , apiFieldsForColumnAccessor , options ) ;
336-
337- expect ( options ) . toEqual ( {
338- ordering : '-column_name' , // Only column_name should be included in ordering
339- } ) ;
340- } ) ;
341-
342- it ( 'should return undefined if sortBy is not provided' , ( ) => {
343- const options = { } ;
344- const sortBy = null ;
345- const apiFieldsForColumnAccessor = { someColumn : { key : 'column_name' } } ;
346-
347- applySortByToOptions ( sortBy , apiFieldsForColumnAccessor , options ) ;
348-
349- expect ( options ) . toEqual ( { } ) ;
350- } ) ;
351- } ) ;
352- describe ( 'trackDataTableEvent' , ( ) => {
353- beforeEach ( ( ) => {
354- jest . clearAllMocks ( ) ;
355- } ) ;
356-
357- it ( 'sends track event when shouldTrackRef.current is true' , ( ) => {
358- const shouldTrackRef = { current : true } ;
359- const enterpriseId = 'test-enterprise-id' ;
360- const eventName = 'test-event-name' ;
361- const tableId = 'test-table-id' ;
362- const options = { page : 1 , pageSize : 10 } ;
363-
364- const result = trackDataTableEvent ( {
365- shouldTrackRef,
366- enterpriseId,
367- eventName,
368- tableId,
369- options,
370- } ) ;
371-
372- expect ( sendEnterpriseTrackEvent ) . toHaveBeenCalledWith (
373- enterpriseId ,
374- eventName ,
375- {
376- tableId,
377- ...options ,
378- } ,
379- ) ;
380- expect ( result ) . toBe ( true ) ;
381- expect ( shouldTrackRef . current ) . toBe ( true ) ;
382- } ) ;
383-
384- it ( 'does not send track event but sets shouldTrackRef to true when initial value is false' , ( ) => {
385- const shouldTrackRef = { current : false } ;
386- const enterpriseId = 'test-enterprise-id' ;
387- const eventName = 'test-event-name' ;
388- const tableId = 'test-table-id' ;
389- const options = { page : 1 , pageSize : 10 } ;
390-
391- const result = trackDataTableEvent ( {
392- shouldTrackRef,
393- enterpriseId,
394- eventName,
395- tableId,
396- options,
397- } ) ;
398-
399- expect ( sendEnterpriseTrackEvent ) . not . toHaveBeenCalled ( ) ;
400- expect ( result ) . toBe ( true ) ;
401- expect ( shouldTrackRef . current ) . toBe ( true ) ;
402- } ) ;
403-
404- it ( 'handles undefined options properly' , ( ) => {
405- const shouldTrackRef = { current : true } ;
406- const enterpriseId = 'test-enterprise-id' ;
407- const eventName = 'test-event-name' ;
408- const tableId = 'test-table-id' ;
409-
410- trackDataTableEvent ( {
411- shouldTrackRef,
412- enterpriseId,
413- eventName,
414- tableId,
415- } ) ;
416-
417- expect ( sendEnterpriseTrackEvent ) . toHaveBeenCalledWith (
418- enterpriseId ,
419- eventName ,
420- {
421- tableId,
422- } ,
423- ) ;
424- } ) ;
425- } ) ;
426- describe ( 'updateUrlWithPageNumber' , ( ) => {
427- const mockNavigate = jest . fn ( ) ;
428- const createMockLocation = ( search = '' ) => ( {
429- pathname : '/test-path' ,
430- search,
431- } ) ;
432-
433- beforeEach ( ( ) => {
434- jest . clearAllMocks ( ) ;
435- } ) ;
296+ } ) ;
297+ } ) ;
298+ describe ( 'applySortByToOptions' , ( ) => {
299+ it ( 'should not modify options when sortBy is empty' , ( ) => {
300+ const options = { } ;
301+ const sortBy = [ ] ;
302+ const apiFieldsForColumnAccessor = { someColumn : { key : 'column_name' } } ;
436303
437- it ( 'adds page number to URL when pageNumber is not 1' , ( ) => {
438- const location = createMockLocation ( ) ;
439- const pageNumber = 3 ;
440- const id = 'test-table' ;
304+ applySortByToOptions ( sortBy , apiFieldsForColumnAccessor , options ) ;
441305
442- updateUrlWithPageNumber ( id , pageNumber , location , mockNavigate ) ;
306+ expect ( options ) . toEqual ( { } ) ;
307+ } ) ;
443308
444- expect ( mockNavigate ) . toHaveBeenCalledWith ( '/test-path?test-table-page=3' , { replace : true } ) ;
445- } ) ;
309+ it ( 'should correctly apply sorting options when sortBy is provided' , ( ) => {
310+ const options = { } ;
311+ const sortBy = [
312+ { id : 'someColumn' , desc : false } ,
313+ { id : 'anotherColumn' , desc : true } ,
314+ ] ;
315+ const apiFieldsForColumnAccessor = {
316+ someColumn : { key : 'column_name' } ,
317+ anotherColumn : { key : 'another_column' } ,
318+ } ;
446319
447- it ( 'removes page number from URL when pageNumber is 1' , ( ) => {
448- const location = createMockLocation ( '?test-table-page=3&filter=active' ) ;
449- const pageNumber = 1 ;
450- const id = 'test-table' ;
320+ applySortByToOptions ( sortBy , apiFieldsForColumnAccessor , options ) ;
451321
452- updateUrlWithPageNumber ( id , pageNumber , location , mockNavigate ) ;
322+ expect ( options ) . toEqual ( {
323+ ordering : 'column_name,-another_column' , // Ensure ordering matches the expected format
324+ } ) ;
325+ } ) ;
453326
454- expect ( mockNavigate ) . toHaveBeenCalledWith ( '/test-path?filter=active' , { replace : true } ) ;
455- } ) ;
327+ it ( 'should handle missing keys in apiFieldsForColumnAccessor gracefully' , ( ) => {
328+ const options = { } ;
329+ const sortBy = [
330+ { id : 'nonExistentColumn' , desc : false } ,
331+ { id : 'someColumn' , desc : true } ,
332+ ] ;
333+ const apiFieldsForColumnAccessor = {
334+ someColumn : { key : 'column_name' } ,
335+ } ;
456336
457- it ( 'respects existing query parameters when adding page number' , ( ) => {
458- const location = createMockLocation ( '?filter=active&sort=name' ) ;
459- const pageNumber = 2 ;
460- const id = 'test-table' ;
337+ applySortByToOptions ( sortBy , apiFieldsForColumnAccessor , options ) ;
461338
462- updateUrlWithPageNumber ( id , pageNumber , location , mockNavigate ) ;
339+ expect ( options ) . toEqual ( {
340+ ordering : '-column_name' , // Only column_name should be included in ordering
341+ } ) ;
342+ } ) ;
463343
464- expect ( mockNavigate ) . toHaveBeenCalledWith ( '/test-path?filter=active&sort=name&test-table-page=2' , { replace : true } ) ;
465- } ) ;
344+ it ( 'should return undefined if sortBy is not provided' , ( ) => {
345+ const options = { } ;
346+ const sortBy = null ;
347+ const apiFieldsForColumnAccessor = { someColumn : { key : 'column_name' } } ;
466348
467- it ( 'uses replace=false when specified' , ( ) => {
468- const location = createMockLocation ( ) ;
469- const pageNumber = 2 ;
470- const id = 'test-table' ;
349+ applySortByToOptions ( sortBy , apiFieldsForColumnAccessor , options ) ;
471350
472- updateUrlWithPageNumber ( id , pageNumber , location , mockNavigate , false ) ;
351+ expect ( options ) . toEqual ( { } ) ;
352+ } ) ;
353+ } ) ;
354+ describe ( 'trackDataTableEvent' , ( ) => {
355+ beforeEach ( ( ) => {
356+ jest . clearAllMocks ( ) ;
357+ } ) ;
473358
474- expect ( mockNavigate ) . toHaveBeenCalledWith ( '/test-path?test-table-page=2' , { replace : false } ) ;
475- } ) ;
359+ it ( 'sends track event when shouldTrackRef.current is true' , ( ) => {
360+ const shouldTrackRef = { current : true } ;
361+ const enterpriseId = 'test-enterprise-id' ;
362+ const eventName = 'test-event-name' ;
363+ const tableId = 'test-table-id' ;
364+ const options = { page : 1 , pageSize : 10 } ;
365+
366+ const result = trackDataTableEvent ( {
367+ shouldTrackRef,
368+ enterpriseId,
369+ eventName,
370+ tableId,
371+ options,
476372 } ) ;
477- describe ( 'saveToLocalStorage' , ( ) => {
478- it ( 'saves string value to localStorage' , ( ) => {
479- saveToLocalStorage ( 'testKey' , 'testValue' ) ;
480- expect ( global . localStorage . setItem ) . toHaveBeenCalledWith ( 'testKey' , '"testValue"' ) ;
481- } ) ;
482-
483- it ( 'saves object value to localStorage' , ( ) => {
484- const value = { foo : 'bar' } ;
485- saveToLocalStorage ( 'testObject' , value ) ;
486- expect ( global . localStorage . setItem ) . toHaveBeenCalledWith ( 'testObject' , JSON . stringify ( value ) ) ;
487- } ) ;
373+
374+ expect ( sendEnterpriseTrackEvent ) . toHaveBeenCalledWith (
375+ enterpriseId ,
376+ eventName ,
377+ {
378+ tableId,
379+ ...options ,
380+ } ,
381+ ) ;
382+ expect ( result ) . toBe ( true ) ;
383+ expect ( shouldTrackRef . current ) . toBe ( true ) ;
384+ } ) ;
385+
386+ it ( 'does not send track event but sets shouldTrackRef to true when initial value is false' , ( ) => {
387+ const shouldTrackRef = { current : false } ;
388+ const enterpriseId = 'test-enterprise-id' ;
389+ const eventName = 'test-event-name' ;
390+ const tableId = 'test-table-id' ;
391+ const options = { page : 1 , pageSize : 10 } ;
392+
393+ const result = trackDataTableEvent ( {
394+ shouldTrackRef,
395+ enterpriseId,
396+ eventName,
397+ tableId,
398+ options,
488399 } ) ;
489400
490- describe ( 'getFromLocalStorage' , ( ) => {
491- it ( 'retrieves and parses stored string value' , ( ) => {
492- global . localStorage . getItem . mockReturnValue ( '"testValue"' ) ;
493- const result = getFromLocalStorage ( 'testKey' ) ;
494- expect ( global . localStorage . getItem ) . toHaveBeenCalledWith ( 'testKey' ) ;
495- expect ( result ) . toEqual ( 'testValue' ) ;
496- } ) ;
497-
498- it ( 'retrieves and parses stored object value' , ( ) => {
499- const storedValue = { foo : 'bar' } ;
500- global . localStorage . getItem . mockReturnValue ( JSON . stringify ( storedValue ) ) ;
501- const result = getFromLocalStorage ( 'testObject' ) ;
502- expect ( global . localStorage . getItem ) . toHaveBeenCalledWith ( 'testObject' ) ;
503- expect ( result ) . toEqual ( storedValue ) ;
504- } ) ;
505-
506- it ( 'returns null for non-existent key' , ( ) => {
507- global . localStorage . getItem . mockReturnValue ( null ) ;
508- const result = getFromLocalStorage ( 'nonExistentKey' ) ;
509- expect ( result ) . toBeNull ( ) ;
510- } ) ;
511-
512- it ( 'handles malformed JSON gracefully' , ( ) => {
513- global . localStorage . getItem . mockReturnValue ( 'not valid JSON' ) ;
514- const result = getFromLocalStorage ( 'badJSON' ) ;
515- expect ( result ) . toBeNull ( ) ;
516- } ) ;
401+ expect ( sendEnterpriseTrackEvent ) . not . toHaveBeenCalled ( ) ;
402+ expect ( result ) . toBe ( true ) ;
403+ expect ( shouldTrackRef . current ) . toBe ( true ) ;
404+ } ) ;
405+
406+ it ( 'handles undefined options properly' , ( ) => {
407+ const shouldTrackRef = { current : true } ;
408+ const enterpriseId = 'test-enterprise-id' ;
409+ const eventName = 'test-event-name' ;
410+ const tableId = 'test-table-id' ;
411+
412+ trackDataTableEvent ( {
413+ shouldTrackRef,
414+ enterpriseId,
415+ eventName,
416+ tableId,
517417 } ) ;
418+
419+ expect ( sendEnterpriseTrackEvent ) . toHaveBeenCalledWith (
420+ enterpriseId ,
421+ eventName ,
422+ {
423+ tableId,
424+ } ,
425+ ) ;
426+ } ) ;
427+ } ) ;
428+ describe ( 'updateUrlWithPageNumber' , ( ) => {
429+ const mockNavigate = jest . fn ( ) ;
430+ const createMockLocation = ( search = '' ) => ( {
431+ pathname : '/test-path' ,
432+ search,
433+ } ) ;
434+
435+ beforeEach ( ( ) => {
436+ jest . clearAllMocks ( ) ;
437+ } ) ;
438+
439+ it ( 'adds page number to URL when pageNumber is not 1' , ( ) => {
440+ const location = createMockLocation ( ) ;
441+ const pageNumber = 3 ;
442+ const id = 'test-table' ;
443+
444+ updateUrlWithPageNumber ( id , pageNumber , location , mockNavigate ) ;
445+
446+ expect ( mockNavigate ) . toHaveBeenCalledWith ( '/test-path?test-table-page=3' , { replace : true } ) ;
447+ } ) ;
448+
449+ it ( 'removes page number from URL when pageNumber is 1' , ( ) => {
450+ const location = createMockLocation ( '?test-table-page=3&filter=active' ) ;
451+ const pageNumber = 1 ;
452+ const id = 'test-table' ;
453+
454+ updateUrlWithPageNumber ( id , pageNumber , location , mockNavigate ) ;
455+
456+ expect ( mockNavigate ) . toHaveBeenCalledWith ( '/test-path?filter=active' , { replace : true } ) ;
457+ } ) ;
458+
459+ it ( 'respects existing query parameters when adding page number' , ( ) => {
460+ const location = createMockLocation ( '?filter=active&sort=name' ) ;
461+ const pageNumber = 2 ;
462+ const id = 'test-table' ;
463+
464+ updateUrlWithPageNumber ( id , pageNumber , location , mockNavigate ) ;
465+
466+ expect ( mockNavigate ) . toHaveBeenCalledWith ( '/test-path?filter=active&sort=name&test-table-page=2' , { replace : true } ) ;
467+ } ) ;
468+
469+ it ( 'uses replace=false when specified' , ( ) => {
470+ const location = createMockLocation ( ) ;
471+ const pageNumber = 2 ;
472+ const id = 'test-table' ;
473+
474+ updateUrlWithPageNumber ( id , pageNumber , location , mockNavigate , false ) ;
475+
476+ expect ( mockNavigate ) . toHaveBeenCalledWith ( '/test-path?test-table-page=2' , { replace : false } ) ;
518477 } ) ;
519478 } ) ;
520479} ) ;
0 commit comments