Files
twenty/packages/twenty-server/test/utils/extract-record-ids-and-dates-as-expect-any.ts
T
Paul Rastoin 7c481abf0c Handle input transpilation morph relation v2 (#14124)
# Introduction
From `morphRelationCreationPayload` to `flatFieldMetadatas`

close https://github.com/twentyhq/core-team-issues/issues/1406

## Unit testing
I'm not a fan of covering things this way, where we could have some
strong integration testing tests in the first place
But I wanted to freeze the optimistic path computation, also covered the
exception because it was cheap, doesn't mean I won't cover them through
integration tests later anw
2025-08-28 17:22:03 +02:00

54 lines
1.2 KiB
TypeScript

import { isDefined } from 'twenty-shared/utils';
export const extractRecordIdsAndDatesAsExpectAny = (
record: Record<string, unknown> | Array<Record<string, unknown>>,
): any => {
if (Array.isArray(record)) {
return record.map(extractRecordIdsAndDatesAsExpectAny);
}
if (typeof record !== 'object') {
throw new Error(
'extractRecordIdsAndDatesAsExpectAny should be called with an array or a record only',
);
}
return Object.entries(record).reduce((acc, [key, value]) => {
if (!isDefined(value)) {
return acc;
}
if (value instanceof Date) {
return {
...acc,
[key]: expect.any(Date),
};
}
if (
key.endsWith('Id') ||
key === 'uniqueIdentifier' ||
key === 'id' ||
key === 'updatedAt' ||
key === 'deletedAt' ||
key === 'createdAt'
) {
return {
...acc,
[key]: expect.any(String),
};
}
if (typeof value === 'object' || Array.isArray(value)) {
return {
...acc,
[key]: extractRecordIdsAndDatesAsExpectAny(
value as Record<string, unknown>,
),
};
}
return acc;
}, {});
};