7c481abf0c
# 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
54 lines
1.2 KiB
TypeScript
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;
|
|
}, {});
|
|
};
|