Files
twenty/packages/twenty-server/test/integration/graphql/suites/view/view-group/successful-create-many-view-groups-v2.integration-spec.ts
T
Paul Rastoin 8dd43be9a2 Create many view groups (#15591)
# Introduction
Same as https://github.com/twentyhq/twenty/pull/15576 but for view
groups creation

When creating a kanban view with v2 flag activated in production result
in race condition due to request being slow and //.
That's why we're introducing a batch create on view group here
closing https://github.com/twentyhq/core-team-issues/issues/1847

## In v2
- batch create view group endpoint is available
- frontend will target the new endpoint

## In v1
- batch create view group endpoint is not available
- frontend will stick to old fake batch view group creation loop


## Gallery
### v2
<img width="1796" height="486" alt="image"
src="https://github.com/user-attachments/assets/932cfe9f-85f1-41cc-a1c4-72a4b5d5a256"
/>

### v1
<img width="1852" height="966" alt="image"
src="https://github.com/user-attachments/assets/8aa9df11-cdea-4b12-ae60-118cdf5e257b"
/>
2025-11-04 15:17:55 +01:00

266 lines
7.4 KiB
TypeScript

import { createOneFieldMetadata } from 'test/integration/metadata/suites/field-metadata/utils/create-one-field-metadata.util';
import { createOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/create-one-object-metadata.util';
import { deleteOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/delete-one-object-metadata.util';
import { updateOneObjectMetadata } from 'test/integration/metadata/suites/object-metadata/utils/update-one-object-metadata.util';
import { createManyCoreViewGroups } from 'test/integration/metadata/suites/view-group/utils/create-many-core-view-groups.util';
import { deleteOneCoreViewGroup } from 'test/integration/metadata/suites/view-group/utils/delete-one-core-view-group.util';
import { destroyOneCoreViewGroup } from 'test/integration/metadata/suites/view-group/utils/destroy-one-core-view-group.util';
import { createOneCoreView } from 'test/integration/metadata/suites/view/utils/create-one-core-view.util';
import { FieldMetadataType } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import { type CreateViewGroupInput } from 'src/engine/metadata-modules/view-group/dtos/inputs/create-view-group.input';
describe('View Group Resolver - Successful Create Many Operations - v2', () => {
let testSetup: {
testViewId: string;
testObjectMetadataId: string;
firstTestFieldMetadataId: string;
secondTestFieldMetadataId: string;
thirdTestFieldMetadataId: string;
};
let createdViewGroupIds: string[] = [];
beforeAll(async () => {
const {
data: {
createOneObject: { id: objectMetadataId },
},
} = await createOneObjectMetadata({
expectToFail: false,
input: {
nameSingular: 'myGroupTestObjectV2',
namePlural: 'myGroupTestObjectsV2',
labelSingular: 'My Group Test Object v2',
labelPlural: 'My Group Test Objects v2',
icon: 'Icon123',
},
});
const {
data: {
createOneField: { id: firstTestFieldMetadataId },
},
} = await createOneFieldMetadata({
expectToFail: false,
input: {
name: 'testField',
label: 'Test Field',
type: FieldMetadataType.TEXT,
objectMetadataId,
isLabelSyncedWithName: true,
},
gqlFields: `
id
name
label
isLabelSyncedWithName
`,
});
const {
data: {
createOneField: { id: secondTestFieldMetadataId },
},
} = await createOneFieldMetadata({
expectToFail: false,
input: {
name: 'secondTestField',
label: 'Test Field',
type: FieldMetadataType.TEXT,
objectMetadataId,
isLabelSyncedWithName: false,
},
gqlFields: `
id
name
label
isLabelSyncedWithName
`,
});
const {
data: {
createOneField: { id: thirdTestFieldMetadataId },
},
} = await createOneFieldMetadata({
expectToFail: false,
input: {
name: 'thirdTestField',
label: 'Test Field',
type: FieldMetadataType.TEXT,
objectMetadataId,
isLabelSyncedWithName: false,
},
gqlFields: `
id
name
label
isLabelSyncedWithName
`,
});
const {
data: {
createCoreView: { id: testViewId },
},
} = await createOneCoreView({
input: {
icon: 'icon123',
objectMetadataId,
name: 'TestViewForGroups',
},
expectToFail: false,
});
testSetup = {
testViewId,
testObjectMetadataId: objectMetadataId,
firstTestFieldMetadataId,
secondTestFieldMetadataId,
thirdTestFieldMetadataId,
};
});
afterAll(async () => {
await updateOneObjectMetadata({
input: {
idToUpdate: testSetup.testObjectMetadataId,
updatePayload: {
isActive: false,
},
},
});
await deleteOneObjectMetadata({
expectToFail: false,
input: { idToDelete: testSetup.testObjectMetadataId },
});
});
afterEach(async () => {
for (const viewGroupId of createdViewGroupIds) {
if (isDefined(viewGroupId)) {
const {
data: { deleteCoreViewGroup },
} = await deleteOneCoreViewGroup({
expectToFail: false,
input: {
id: viewGroupId,
},
});
expect(deleteCoreViewGroup.deletedAt).not.toBeNull();
await destroyOneCoreViewGroup({
expectToFail: false,
input: {
id: viewGroupId,
},
});
}
}
createdViewGroupIds = [];
});
it('should successfully create multiple view groups in batch', async () => {
const inputs: CreateViewGroupInput[] = [
{
fieldMetadataId: testSetup.firstTestFieldMetadataId,
viewId: testSetup.testViewId,
position: 0,
isVisible: true,
fieldValue: 'Group A',
},
{
fieldMetadataId: testSetup.secondTestFieldMetadataId,
viewId: testSetup.testViewId,
position: 1,
isVisible: false,
fieldValue: 'Group B',
},
{
fieldMetadataId: testSetup.thirdTestFieldMetadataId,
viewId: testSetup.testViewId,
position: 2,
isVisible: true,
fieldValue: 'Group C',
},
];
const {
data: { createManyCoreViewGroups: createdViewGroups },
errors,
} = await createManyCoreViewGroups({
inputs,
expectToFail: false,
});
expect(errors).toBeUndefined();
expect(createdViewGroups).toBeDefined();
expect(createdViewGroups).toHaveLength(3);
createdViewGroups.forEach((viewGroup, index) => {
expect(viewGroup).toMatchObject({
fieldMetadataId: inputs[index].fieldMetadataId,
viewId: testSetup.testViewId,
position: inputs[index].position,
isVisible: inputs[index].isVisible,
fieldValue: inputs[index].fieldValue,
});
createdViewGroupIds.push(viewGroup.id);
});
});
it('should successfully create single view group using batch endpoint', async () => {
const inputs: CreateViewGroupInput[] = [
{
fieldMetadataId: testSetup.firstTestFieldMetadataId,
viewId: testSetup.testViewId,
position: 5,
isVisible: true,
fieldValue: 'Single Group',
},
];
const {
data: { createManyCoreViewGroups: createdViewGroups },
errors,
} = await createManyCoreViewGroups({
inputs,
expectToFail: false,
});
expect(errors).toBeUndefined();
expect(createdViewGroups).toBeDefined();
expect(createdViewGroups).toHaveLength(1);
const viewGroup = createdViewGroups[0];
expect(viewGroup).toMatchObject({
fieldMetadataId: testSetup.firstTestFieldMetadataId,
viewId: testSetup.testViewId,
position: 5,
isVisible: true,
fieldValue: 'Single Group',
});
createdViewGroupIds.push(viewGroup.id);
});
it('should return empty array when creating zero view groups', async () => {
const inputs: CreateViewGroupInput[] = [];
const {
data: { createManyCoreViewGroups: createdViewGroups },
errors,
} = await createManyCoreViewGroups({
inputs,
expectToFail: false,
});
expect(errors).toBeUndefined();
expect(createdViewGroups).toBeDefined();
expect(createdViewGroups).toHaveLength(0);
});
});