database.mock.ts49 lines, 1.2 KB
| 1 | import { AppDataSource } from "../../config/database"; |
| 2 | |
| 3 | // Create mock functions for database operations |
| 4 | export const mockDatabaseConnection = () => { |
| 5 | const originalInitialize = AppDataSource.initialize; |
| 6 | |
| 7 | beforeAll(() => { |
| 8 | // Mock database initialization |
| 9 | AppDataSource.initialize = jest.fn().mockResolvedValue(AppDataSource); |
| 10 | }); |
| 11 | |
| 12 | afterAll(() => { |
| 13 | // Restore original database initialization |
| 14 | AppDataSource.initialize = originalInitialize; |
| 15 | }); |
| 16 | }; |
| 17 | |
| 18 | // Mock repository factory function |
| 19 | export const createMockRepository = () => { |
| 20 | const repository = { |
| 21 | find: jest.fn(), |
| 22 | findOne: jest.fn(), |
| 23 | findOneBy: jest.fn(), |
| 24 | save: jest.fn(), |
| 25 | update: jest.fn(), |
| 26 | delete: jest.fn(), |
| 27 | createQueryBuilder: jest.fn().mockReturnValue({ |
| 28 | where: jest.fn().mockReturnThis(), |
| 29 | andWhere: jest.fn().mockReturnThis(), |
| 30 | orderBy: jest.fn().mockReturnThis(), |
| 31 | getOne: jest.fn(), |
| 32 | getMany: jest.fn(), |
| 33 | execute: jest.fn(), |
| 34 | }), |
| 35 | }; |
| 36 | |
| 37 | return repository; |
| 38 | }; |
| 39 | |
| 40 | // Reset all mock repositories |
| 41 | export const resetMocks = (mockRepositories: any[]) => { |
| 42 | mockRepositories.forEach((repo) => { |
| 43 | Object.values(repo).forEach((method) => { |
| 44 | if (jest.isMockFunction(method)) { |
| 45 | method.mockReset(); |
| 46 | } |
| 47 | }); |
| 48 | }); |
| 49 | }; |