Files
twenty/packages/twenty-front/scripts/mock-data/generate-metadata.ts
T
Charles BochetandGitHub 8a7a19f312 Improve test tooling (#18259)
## Summary

Unifies test mocking tooling across Jest and Storybook, replaces
handcrafted mock data with auto-generated server-fetched data, and
restructures the mock data generation script for maintainability.

### Mock data generation

- Split `generate-mock-data.ts` into three focused modules under
`scripts/mock-data/`:
  - `utils.ts` — shared authentication, GraphQL client, and file writer
  - `generate-metadata.ts` — fetches object metadata from `/metadata`
- `generate-record-data.ts` — fetches record data from `/graphql` using
metadata-driven dynamic queries
- The orchestrator (`generate-mock-data.ts`) authenticates once and
passes the token to both generators
- Company records are now fetched from the actual server (limited to 10
records) instead of being handcrafted
- Generated files are organized under `generated/metadata/objects/` and
`generated/data/companies/`

### Unified test utilities

- Consolidated Jest and MSW mocking into shared utilities that compose
production code (`prefillRecord`, `getRecordNodeFromRecord`,
`getRecordConnectionFromRecords`) with mock metadata
- Renamed `generateEmptyJestRecordNode` → `generateMockRecordNode` and
moved to `testing/utils/`
- Extracted `generateMockRecordConnection` into its own file
- Removed `sanitizeInputForPrefill` workaround (no longer needed with
correctly shaped generated data)
2026-02-26 14:31:54 +01:00

161 lines
3.7 KiB
TypeScript

/* eslint-disable no-console */
import { graphqlRequest, writeGeneratedFile } from './utils.js';
// Apollo Client automatically adds __typename to every object level;
// raw fetch does not, so we include it explicitly here.
const METADATA_QUERY = `
query ObjectMetadataItems {
objects(paging: { first: 1000 }) {
__typename
edges {
__typename
node {
__typename
id
universalIdentifier
nameSingular
namePlural
labelSingular
labelPlural
description
icon
isCustom
isRemote
isActive
isSystem
isUIReadOnly
createdAt
updatedAt
labelIdentifierFieldMetadataId
imageIdentifierFieldMetadataId
applicationId
shortcut
isLabelSyncedWithName
isSearchable
duplicateCriteria
indexMetadataList {
__typename
id
createdAt
updatedAt
name
indexWhereClause
indexType
isUnique
isCustom
indexFieldMetadataList {
__typename
id
fieldMetadataId
createdAt
updatedAt
order
}
}
fieldsList {
__typename
id
universalIdentifier
type
name
label
description
icon
isCustom
isActive
isSystem
isUIReadOnly
isNullable
isUnique
createdAt
updatedAt
defaultValue
options
settings
isLabelSyncedWithName
morphId
applicationId
relation {
__typename
type
sourceObjectMetadata {
__typename
id
nameSingular
namePlural
}
targetObjectMetadata {
__typename
id
nameSingular
namePlural
}
sourceFieldMetadata {
__typename
id
name
}
targetFieldMetadata {
__typename
id
name
}
}
morphRelations {
__typename
type
sourceObjectMetadata {
__typename
id
nameSingular
namePlural
}
targetObjectMetadata {
__typename
id
nameSingular
namePlural
}
sourceFieldMetadata {
__typename
id
name
}
targetFieldMetadata {
__typename
id
name
}
}
}
}
}
pageInfo {
__typename
hasNextPage
hasPreviousPage
startCursor
endCursor
}
}
}
`;
export const generateMetadata = async (token: string) => {
console.log('Fetching object metadata from /metadata ...');
const metadata = await graphqlRequest('/metadata', METADATA_QUERY, token);
writeGeneratedFile(
'metadata/objects/mock-objects-metadata.ts',
'mockedStandardObjectMetadataQueryResult',
'ObjectMetadataItemsQuery',
"import { ObjectMetadataItemsQuery } from '~/generated-metadata/graphql';",
metadata,
);
return metadata as {
objects: { edges: { node: Record<string, unknown> }[] };
};
};