2023-07-13 01:14:48 -07:00
|
|
|
import { Container } from 'typedi';
|
|
|
|
import { mock } from 'jest-mock-extended';
|
|
|
|
import type { DeepPartial } from 'ts-essentials';
|
2024-04-05 04:47:49 -07:00
|
|
|
import { DataSource, EntityManager, type EntityMetadata } from '@n8n/typeorm';
|
2023-12-27 02:50:43 -08:00
|
|
|
import type { Class } from 'n8n-core';
|
2023-07-13 01:14:48 -07:00
|
|
|
|
|
|
|
export const mockInstance = <T>(
|
2023-12-27 02:50:43 -08:00
|
|
|
serviceClass: Class<T>,
|
2023-07-13 01:14:48 -07:00
|
|
|
data: DeepPartial<T> | undefined = undefined,
|
|
|
|
) => {
|
|
|
|
const instance = mock<T>(data);
|
2023-12-27 02:50:43 -08:00
|
|
|
Container.set(serviceClass, instance);
|
2023-07-13 01:14:48 -07:00
|
|
|
return instance;
|
|
|
|
};
|
2024-04-05 04:47:49 -07:00
|
|
|
|
|
|
|
export const mockEntityManager = (entityClass: Class) => {
|
|
|
|
const entityManager = mockInstance(EntityManager);
|
|
|
|
const dataSource = mockInstance(DataSource, {
|
|
|
|
manager: entityManager,
|
|
|
|
getMetadata: () => mock<EntityMetadata>({ target: entityClass }),
|
|
|
|
});
|
|
|
|
Object.assign(entityManager, { connection: dataSource });
|
|
|
|
return entityManager;
|
|
|
|
};
|