Files
twenty/server/src/tenant/utils/__tests__/deduce-relation-direction.spec.ts
Charles Bochet 54d7acd518 Split components into object-metadata and object-record (#2425)
* Split components into object-metadata and object-record

* Fix seed
2023-11-10 15:54:32 +01:00

70 lines
2.3 KiB
TypeScript

import { RelationMetadataInterface } from 'src/tenant/schema-builder/interfaces/relation-metadata.interface';
import { RelationMetadataType } from 'src/metadata/relation-metadata/relation-metadata.entity';
import {
deduceRelationDirection,
RelationDirection,
} from 'src/tenant/utils/deduce-relation-direction.util';
describe('deduceRelationDirection', () => {
it('should return FROM when the current object Metadata ID matches fromObjectMetadataId', () => {
const currentObjectId = 'from_object_id';
const relationMetadata = {
id: 'relation_id',
fromObjectMetadataId: currentObjectId,
toObjectMetadataId: 'to_object_id',
fromFieldMetadataId: 'from_field_id',
toFieldMetadataId: 'to_field_id',
relationType: RelationMetadataType.ONE_TO_ONE,
};
const result = deduceRelationDirection(
currentObjectId,
relationMetadata as RelationMetadataInterface,
);
expect(result).toBe(RelationDirection.FROM);
});
it('should return TO when the current object Metadata ID matches toObjectMetadataId', () => {
// Arrange
const currentObjectId = 'to_object_id';
const relationMetadata = {
id: 'relation_id',
fromObjectMetadataId: 'from_object_id',
toObjectMetadataId: currentObjectId,
fromFieldMetadataId: 'from_field_id',
toFieldMetadataId: 'to_field_id',
relationType: RelationMetadataType.ONE_TO_ONE,
};
const result = deduceRelationDirection(
currentObjectId,
relationMetadata as RelationMetadataInterface,
);
expect(result).toBe(RelationDirection.TO);
});
it('should throw an error when the current object Metadata ID does not match any object metadata ID', () => {
const currentObjectId = 'unrelated_object_id';
const relationMetadata = {
id: 'relation_id',
fromObjectMetadataId: 'from_object_id',
toObjectMetadataId: 'to_object_id',
fromFieldMetadataId: 'from_field_id',
toFieldMetadataId: 'to_field_id',
relationType: RelationMetadataType.ONE_TO_ONE,
};
expect(() =>
deduceRelationDirection(
currentObjectId,
relationMetadata as RelationMetadataInterface,
),
).toThrow(
`Relation metadata ${relationMetadata.id} is not related to object ${currentObjectId}`,
);
});
});