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');
});
});

View File

@ -0,0 +1,21 @@
import { expect } from '@storybook/test';
import { OperationType } from '@/apollo/types/operation-type';
import formatTitle from '../format-title';
describe('formatTitle', () => {
it('should correctly format the title', () => {
const res = formatTitle(
OperationType.Query,
'default',
'GetCurrentUser',
1000,
);
const title = res[0];
expect(title).toBe(
'%c apollo %cquery %cdefault::%cGetCurrentUser %c(in 1000 ms)',
);
});
});

View File

@ -0,0 +1,4 @@
// More work needed here
describe.skip('loggerLink', () => {
it('should log the correct message', () => {});
});

View File

@ -0,0 +1,121 @@
import { CurrentWorkspace } from '@/auth/states/currentWorkspaceState';
import { WorkspaceMember } from '@/workspace-member/types/WorkspaceMember';
import { getOnboardingStatus } from '../getOnboardingStatus';
describe('getOnboardingStatus', () => {
it('should return the correct status', () => {
const ongoingUserCreation = getOnboardingStatus({
isLoggedIn: false,
currentWorkspaceMember: null,
currentWorkspace: null,
});
const unknownStatus = getOnboardingStatus({
isLoggedIn: true,
currentWorkspaceMember: null,
currentWorkspace: null,
});
const ongoingWorkspaceCreation = getOnboardingStatus({
isLoggedIn: true,
currentWorkspaceMember: {
id: '1',
name: {
firstName: 'John',
lastName: 'Doe',
},
} as WorkspaceMember,
currentWorkspace: {
id: '1',
displayName: null,
} as CurrentWorkspace,
});
const ongoingProfileCreation = getOnboardingStatus({
isLoggedIn: true,
currentWorkspaceMember: {
id: '1',
name: {},
} as WorkspaceMember,
currentWorkspace: {
id: '1',
displayName: 'My Workspace',
} as CurrentWorkspace,
});
const completed = getOnboardingStatus({
isLoggedIn: true,
currentWorkspaceMember: {
id: '1',
name: {
firstName: 'John',
lastName: 'Doe',
},
} as WorkspaceMember,
currentWorkspace: {
id: '1',
displayName: 'My Workspace',
} as CurrentWorkspace,
});
const incomplete = getOnboardingStatus({
isLoggedIn: true,
currentWorkspaceMember: {
id: '1',
name: {
firstName: 'John',
lastName: 'Doe',
},
} as WorkspaceMember,
currentWorkspace: {
id: '1',
displayName: 'My Workspace',
subscriptionStatus: 'incomplete',
} as CurrentWorkspace,
isBillingEnabled: true,
});
const incompleteButBillingDisabled = getOnboardingStatus({
isLoggedIn: true,
currentWorkspaceMember: {
id: '1',
name: {
firstName: 'John',
lastName: 'Doe',
},
} as WorkspaceMember,
currentWorkspace: {
id: '1',
displayName: 'My Workspace',
subscriptionStatus: 'incomplete',
} as CurrentWorkspace,
});
const canceled = getOnboardingStatus({
isLoggedIn: true,
currentWorkspaceMember: {
id: '1',
name: {
firstName: 'John',
lastName: 'Doe',
},
} as WorkspaceMember,
currentWorkspace: {
id: '1',
displayName: 'My Workspace',
subscriptionStatus: 'canceled',
} as CurrentWorkspace,
isBillingEnabled: true,
});
expect(ongoingUserCreation).toBe('ongoing_user_creation');
expect(unknownStatus).toBe(undefined);
expect(ongoingWorkspaceCreation).toBe('ongoing_workspace_creation');
expect(ongoingProfileCreation).toBe('ongoing_profile_creation');
expect(completed).toBe('completed');
expect(incomplete).toBe('incomplete');
expect(canceled).toBe('canceled');
expect(incompleteButBillingDisabled).toBe('completed');
});
});

View File

@ -0,0 +1,49 @@
import { mapBoardFieldDefinitionsToViewFields } from '@/companies/utils/mapBoardFieldDefinitionsToViewFields';
import { FieldMetadata } from '@/object-record/field/types/FieldMetadata';
import { BoardFieldDefinition } from '@/object-record/record-board/types/BoardFieldDefinition';
describe('mapBoardFieldDefinitionsToViewFields', () => {
it('should map board field definitions to view fields', () => {
const fieldDefinitions: BoardFieldDefinition<FieldMetadata>[] = [
{
fieldMetadataId: 'fieldMetadataId',
label: 'label',
iconName: 'iconName',
type: 'BOOLEAN',
metadata: {
objectMetadataNameSingular: 'objectMetadataNameSingular',
fieldName: 'fieldName',
},
position: 0,
isVisible: true,
viewFieldId: 'viewFieldId',
},
{
fieldMetadataId: 'fieldMetadataId1',
label: 'label1',
iconName: 'iconName1',
type: 'NUMBER',
metadata: {
objectMetadataNameSingular: 'objectMetadataNameSingular1',
fieldName: 'fieldName1',
placeHolder: 'placeHolder1',
isPositive: true,
},
position: 1,
isVisible: false,
viewFieldId: 'viewFieldId1',
},
];
const viewFields = mapBoardFieldDefinitionsToViewFields(fieldDefinitions);
expect(viewFields).toHaveLength(2);
expect(viewFields[0]).toHaveProperty('id');
expect(viewFields[0]).toHaveProperty('size');
expect(viewFields[0]).toHaveProperty('position');
expect(viewFields[0]).toHaveProperty('isVisible');
expect(viewFields[0].definition).toEqual(fieldDefinitions[0]);
expect(viewFields[1].definition).toEqual(fieldDefinitions[1]);
});
});

View File

@ -0,0 +1,50 @@
import { mapFavorites } from '../mapFavorites';
describe('mapFavorites', () => {
it('should return the correct value', () => {
const favorites = [
{
id: '1',
person: {
id: '2',
name: {
firstName: 'John',
lastName: 'Doe',
},
avatarUrl: 'https://example.com/avatar.png',
},
},
{
id: '3',
company: {
id: '4',
name: 'My Company',
domainName: 'example.com',
},
position: 1,
},
];
const res = mapFavorites(favorites);
expect(res).toHaveLength(2);
// Person
expect(res[0].id).toBe('1');
expect(res[0].labelIdentifier).toBe('John Doe');
expect(res[0].avatarUrl).toBe('https://example.com/avatar.png');
expect(res[0].avatarType).toBe('rounded');
expect(res[0].link).toBe('/object/person/2');
expect(res[0].recordId).toBe('2');
expect(res[0].position).toBeUndefined();
// Company
expect(res[1].id).toBe('3');
expect(res[1].labelIdentifier).toBe('My Company');
expect(res[1].avatarUrl).toBe('https://favicon.twenty.com/example.com');
expect(res[1].avatarType).toBe('squared');
expect(res[1].link).toBe('/object/company/4');
expect(res[1].recordId).toBe('4');
expect(res[1].position).toBe(1);
});
});