Skip to content

The open alternative to Salesforce, designed for AI.

Refactor File Download Logic and Streamline Test Suite (#9281)

Samyak Piya2b82347fdc73
diff --git a/packages/twenty-front/src/modules/activities/files/utils/downloadFile.ts b/packages/twenty-front/src/modules/activities/files/utils/downloadFile.ts
index 5dd7e6c0015..af64fed4e22 100644
--- a/packages/twenty-front/src/modules/activities/files/utils/downloadFile.ts
+++ b/packages/twenty-front/src/modules/activities/files/utils/downloadFile.ts
@@ -1,3 +1,4 @@
+import { saveAs } from 'file-saver';
import { getFileAbsoluteURI } from '~/utils/file/getFileAbsoluteURI';
export const downloadFile = (fullPath: string, fileName: string) => {
@@ -8,13 +9,6 @@ export const downloadFile = (fullPath: string, fileName: string) => {
: Promise.reject('Failed downloading file'),
)
.then((blob) => {
- const url = window.URL.createObjectURL(blob);
- const a = document.createElement('a');
- a.style.display = 'none';
- a.href = url;
- a.download = fileName;
- document.body.appendChild(a);
- a.click();
- window.URL.revokeObjectURL(url);
+ saveAs(blob, fileName);
});
};
diff --git a/packages/twenty-front/src/modules/object-record/record-index/export/hooks/__tests__/useExportRecords.test.ts b/packages/twenty-front/src/modules/object-record/record-index/export/hooks/__tests__/useExportRecords.test.ts
index 9c0c17294ac..2f1bf0e3780 100644
--- a/packages/twenty-front/src/modules/object-record/record-index/export/hooks/__tests__/useExportRecords.test.ts
+++ b/packages/twenty-front/src/modules/object-record/record-index/export/hooks/__tests__/useExportRecords.test.ts
@@ -2,32 +2,10 @@ import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata'
import { ColumnDefinition } from '@/object-record/record-table/types/ColumnDefinition';
import { RelationDefinitionType } from '~/generated-metadata/graphql';
-import {
- csvDownloader,
- displayedExportProgress,
- download,
- generateCsv,
-} from '../useExportRecords';
+import { displayedExportProgress, generateCsv } from '../useExportRecords';
jest.useFakeTimers();
-describe('download', () => {
- it('creates a download link and clicks it', () => {
- const link = document.createElement('a');
- document.createElement = jest.fn().mockReturnValue(link);
- const appendChild = jest.spyOn(document.body, 'appendChild');
- const click = jest.spyOn(link, 'click');
-
- URL.createObjectURL = jest.fn().mockReturnValue('fake-url');
- download(new Blob(['test'], { type: 'text/plain' }), 'test.txt');
-
- expect(appendChild).toHaveBeenCalledWith(link);
- expect(link.href).toEqual('http://localhost/fake-url');
- expect(link.getAttribute('download')).toEqual('test.txt');
- expect(click).toHaveBeenCalledTimes(1);
- });
-});
-
describe('generateCsv', () => {
it('generates a csv with formatted headers', async () => {
const columns = [
@@ -58,32 +36,6 @@ describe('generateCsv', () => {
});
});
-describe('csvDownloader', () => {
- it('downloads a csv', () => {
- const filename = 'test.csv';
- const data = {
- rows: [
- { id: 1, name: 'John' },
- { id: 2, name: 'Alice' },
- ],
- columns: [],
- objectNameSingular: '',
- };
-
- const link = document.createElement('a');
- document.createElement = jest.fn().mockReturnValue(link);
- const createObjectURL = jest.spyOn(URL, 'createObjectURL');
-
- csvDownloader(filename, data);
-
- expect(link.getAttribute('download')).toEqual('test.csv');
- expect(createObjectURL).toHaveBeenCalledWith(expect.any(Blob));
- expect(createObjectURL).toHaveBeenCalledWith(
- expect.objectContaining({ type: 'text/csv' }),
- );
- });
-});
-
describe('displayedExportProgress', () => {
it.each([
[undefined, undefined, 'percentage', 'Export'],
diff --git a/packages/twenty-front/src/modules/object-record/record-index/export/hooks/useExportRecords.ts b/packages/twenty-front/src/modules/object-record/record-index/export/hooks/useExportRecords.ts
index 8053db0c004..4094adb1d86 100644
--- a/packages/twenty-front/src/modules/object-record/record-index/export/hooks/useExportRecords.ts
+++ b/packages/twenty-front/src/modules/object-record/record-index/export/hooks/useExportRecords.ts
@@ -10,21 +10,12 @@ import {
} from '@/object-record/record-index/export/hooks/useExportFetchRecords';
import { ColumnDefinition } from '@/object-record/record-table/types/ColumnDefinition';
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
+import { saveAs } from 'file-saver';
import { RelationDefinitionType } from '~/generated-metadata/graphql';
import { FieldMetadataType } from '~/generated/graphql';
import { isDefined } from '~/utils/isDefined';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
-export const download = (blob: Blob, filename: string) => {
- const url = URL.createObjectURL(blob);
- const link = document.createElement('a');
- link.href = url;
- link.setAttribute('download', filename);
- document.body.appendChild(link);
- link.click();
- link.parentNode?.removeChild(link);
-};
-
type GenerateExportOptions = {
columns: ColumnDefinition<FieldMetadata>[];
rows: object[];
@@ -128,7 +119,7 @@ export const displayedExportProgress = (progress?: ExportProgress): string => {
const downloader = (mimeType: string, generator: GenerateExport) => {
return (filename: string, data: GenerateExportOptions) => {
const blob = new Blob([generator(data)], { type: mimeType });
- download(blob, filename);
+ saveAs(blob, filename);
};
};