Add jest tests for twenty-front (#2983)

* Add jest tests for twenty-front

Co-authored-by: v1b3m <vibenjamin6@gmail.com>

* Fix tests

---------

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
gitstart-twenty
2023-12-15 17:53:20 +08:00
committed by GitHub
parent af9d3fb217
commit 5f7442cf23
27 changed files with 693 additions and 2 deletions

View File

@ -3,6 +3,7 @@ import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { ActivityTargetableEntity } from '../../types/ActivityTargetableEntity';
// do we need to test this?
export const useAttachments = (entity: ActivityTargetableEntity) => {
const { records: attachments } = useFindManyRecords({
objectNameSingular: 'attachment',

View File

@ -0,0 +1,43 @@
import { downloadFile } from '../downloadFile';
// Mock fetch
global.fetch = jest.fn(() =>
Promise.resolve({
status: 200,
blob: jest.fn(),
} as unknown as Response),
);
window.URL.createObjectURL = jest.fn(() => 'mock-url');
window.URL.revokeObjectURL = jest.fn();
// FIXME: jest is behaving weirdly here, it's not finding the element
// Also the document's innerHTML is empty
// `global.fetch` and `window.fetch` are also undefined
describe.skip('downloadFile', () => {
it('should download a file', () => {
// Call downloadFile
downloadFile('path/to/file.pdf', 'file.pdf');
// Assert on fetch
expect(fetch).toHaveBeenCalledWith(
process.env.REACT_APP_SERVER_BASE_URL + '/files/path/to/file.pdf',
);
// Assert on element creation
const link = document.querySelector(
'a[href="mock-url"][download="file.pdf"]',
);
console.log(document.body.innerHTML, link);
expect(link).not.toBeNull();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(link?.style?.display).toBe('none');
// Assert on element click
expect(link).toHaveBeenCalledTimes(1);
// Clean up mocks
jest.clearAllMocks();
});
});

View File

@ -0,0 +1,13 @@
import { getFileType } from '../getFileType';
describe('getFileType', () => {
it('should return the correct file type for a given file name', () => {
expect(getFileType('test.doc')).toBe('TextDocument');
expect(getFileType('test.xls')).toBe('Spreadsheet');
expect(getFileType('test.ppt')).toBe('Presentation');
expect(getFileType('test.png')).toBe('Image');
expect(getFileType('test.mp4')).toBe('Video');
expect(getFileType('test.mp3')).toBe('Audio');
expect(getFileType('test.zip')).toBe('Archive');
});
});

View File

@ -0,0 +1,22 @@
import { ActivityForDrawer } from '@/activities/types/ActivityForDrawer';
import { mockedActivities } from '~/testing/mock-data/activities';
import { groupActivitiesByMonth } from '../groupActivitiesByMonth';
describe('groupActivitiesByMonth', () => {
it('should group activities by month', () => {
const grouped = groupActivitiesByMonth(
mockedActivities as unknown as ActivityForDrawer[],
);
expect(grouped).toHaveLength(2);
expect(grouped[0].items).toHaveLength(1);
expect(grouped[1].items).toHaveLength(1);
expect(grouped[0].year).toBe(2023);
expect(grouped[1].year).toBe(2023);
expect(grouped[0].month).toBe(11);
expect(grouped[1].month).toBe(3);
});
});

View File

@ -0,0 +1,27 @@
import { EntityForSelect } from '@/object-record/relation-picker/types/EntityForSelect';
import { flatMapAndSortEntityForSelectArrayOfArrayByName } from '../flatMapAndSortEntityForSelectArrayByName';
describe('flatMapAndSortEntityForSelectArrayOfArrayByName', () => {
it('should return the correct value', () => {
const entityForSelectArray = [
[
{ id: 1, name: 'xRya' },
{ id: 2, name: 'BrcA' },
],
[
{ id: 3, name: 'aCxd' },
{ id: 4, name: 'kp7u' },
],
] as unknown as EntityForSelect[][];
const res =
flatMapAndSortEntityForSelectArrayOfArrayByName(entityForSelectArray);
expect(res).toHaveLength(4);
expect(res[0].id).toBe(3);
expect(res[1].id).toBe(2);
expect(res[2].id).toBe(4);
expect(res[3].id).toBe(1);
});
});

View File

@ -0,0 +1,47 @@
import { ActivityTargetableEntity } from '@/activities/types/ActivityTargetableEntity';
import { getTargetableEntitiesWithParents } from '@/activities/utils/getTargetableEntitiesWithParents';
describe('getTargetableEntitiesWithParents', () => {
it('should return the correct value', () => {
const entities: ActivityTargetableEntity[] = [
{
id: '1',
type: 'Person',
relatedEntities: [
{
id: '2',
type: 'Company',
},
],
},
{
id: '4',
type: 'Company',
},
{
id: '3',
type: 'Custom',
relatedEntities: [
{
id: '6',
type: 'Person',
},
{
id: '5',
type: 'Company',
},
],
},
];
const res = getTargetableEntitiesWithParents(entities);
expect(res).toHaveLength(6);
expect(res[0].id).toBe('1');
expect(res[1].id).toBe('2');
expect(res[2].id).toBe('4');
expect(res[3].id).toBe('3');
expect(res[4].id).toBe('6');
expect(res[5].id).toBe('5');
});
});