-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2645 from HHS/mb/TTAHUB-3787/export-functionality
[TTAHUB-3787] Enhance communication log CSV export functionality
- Loading branch information
Showing
15 changed files
with
299 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* eslint-disable max-len */ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import useAsyncWidgetExport from '../useAsyncWidgetExport'; | ||
import { blobToCsvDownload } from '../../utils'; | ||
import { IS, NOOP } from '../../Constants'; | ||
|
||
jest.mock('../../utils', () => ({ | ||
blobToCsvDownload: jest.fn(), | ||
})); | ||
|
||
describe('useAsyncWidgetExport', () => { | ||
const mockFetcher = jest.fn(); | ||
const mockCheckboxes = { 1: true, 2: true, 3: false }; | ||
const mockExportName = 'test-export'; | ||
const mockSortConfig = { sortBy: 'name', direction: 'asc' }; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call fetcher with correct parameters when exportType is "selected"', async () => { | ||
const { result } = renderHook(() => useAsyncWidgetExport(mockCheckboxes, mockExportName, mockSortConfig, mockFetcher)); | ||
|
||
await act(async () => { | ||
await result.current.exportRows('selected'); | ||
}); | ||
|
||
expect(mockFetcher).toHaveBeenCalledWith( | ||
'name', | ||
'asc', | ||
0, | ||
false, | ||
[ | ||
{ | ||
topic: 'id', | ||
condition: IS, | ||
query: '1', | ||
}, | ||
{ | ||
topic: 'id', | ||
condition: IS, | ||
query: '2', | ||
}, | ||
], | ||
'csv', | ||
); | ||
expect(blobToCsvDownload).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call fetcher with correct parameters when exportType is not "selected"', async () => { | ||
const { result } = renderHook(() => useAsyncWidgetExport(mockCheckboxes, mockExportName, mockSortConfig, mockFetcher)); | ||
|
||
await act(async () => { | ||
await result.current.exportRows('all'); | ||
}); | ||
|
||
expect(mockFetcher).toHaveBeenCalledWith( | ||
'name', | ||
'asc', | ||
0, | ||
false, | ||
[], | ||
'csv', | ||
); | ||
expect(blobToCsvDownload).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should handle fetcher error', async () => { | ||
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(NOOP); | ||
mockFetcher.mockRejectedValue(new Error('Fetch error')); | ||
|
||
const { result } = renderHook(() => useAsyncWidgetExport(mockCheckboxes, mockExportName, mockSortConfig, mockFetcher)); | ||
|
||
await act(async () => { | ||
await result.current.exportRows('all'); | ||
}); | ||
|
||
expect(consoleErrorSpy).toHaveBeenCalledWith(new Error('Fetch error')); | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useCallback } from 'react'; | ||
import { IS } from '../Constants'; | ||
import { blobToCsvDownload } from '../utils'; | ||
|
||
export default function useAsyncWidgetExport( | ||
checkboxes, | ||
exportName, | ||
sortConfig, | ||
fetcher, | ||
filters = [], | ||
) { | ||
const exportRows = useCallback(async (exportType) => { | ||
// Clone the filters to avoid mutating the original array | ||
const fs = filters.map((filter) => ({ ...filter })); | ||
|
||
if (exportType === 'selected') { | ||
const selectedRowsIds = Object.keys(checkboxes).filter((key) => checkboxes[key]); | ||
selectedRowsIds.forEach((id) => { | ||
fs.push({ | ||
topic: 'id', | ||
condition: IS, | ||
query: id, | ||
}); | ||
}); | ||
} | ||
|
||
try { | ||
const blob = await fetcher( | ||
sortConfig.sortBy, | ||
sortConfig.direction, | ||
0, | ||
false, | ||
fs, | ||
'csv', | ||
); | ||
blobToCsvDownload(blob, exportName); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
} | ||
}, [checkboxes, exportName, fetcher, filters, sortConfig.direction, sortConfig.sortBy]); | ||
|
||
return { | ||
exportRows, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.