@kenniy/godeye-data-contracts
Version:
Enterprise-grade base repository architecture for GOD-EYE microservices with zero overhead and maximum code reuse
144 lines (143 loc) • 6.24 kB
TypeScript
/**
* Mongoose Mock Utilities
* Provides comprehensive mocking for Mongoose models, queries, and operations
*/
export interface MockMongooseQuery<T = any> {
populate: jest.MockedFunction<(arg: any) => MockMongooseQuery<T>>;
select: jest.MockedFunction<(arg: any) => MockMongooseQuery<T>>;
sort: jest.MockedFunction<(arg: any) => MockMongooseQuery<T>>;
limit: jest.MockedFunction<(arg: any) => MockMongooseQuery<T>>;
skip: jest.MockedFunction<(arg: any) => MockMongooseQuery<T>>;
lean: jest.MockedFunction<(arg?: boolean) => MockMongooseQuery<T>>;
exec: jest.MockedFunction<() => Promise<T>>;
}
export interface MockMongooseDocument {
_id: string;
id?: string;
save: jest.MockedFunction<() => Promise<any>>;
toObject?: jest.MockedFunction<() => any>;
toJSON?: jest.MockedFunction<() => any>;
[key: string]: any;
}
export interface MockMongooseModel<T = any> {
collection: {
name: string;
};
modelName: string;
schema: MockMongooseSchema;
db: MockMongooseDb;
findOne: jest.MockedFunction<(filter?: any) => MockMongooseQuery<T | null>>;
find: jest.MockedFunction<(filter?: any) => MockMongooseQuery<T[]>>;
findById: jest.MockedFunction<(id: string) => MockMongooseQuery<T | null>>;
findByIdAndUpdate: jest.MockedFunction<(id: string, update: any, options?: any) => MockMongooseQuery<T | null>>;
findByIdAndDelete: jest.MockedFunction<(id: string) => MockMongooseQuery<T | null>>;
findOneAndUpdate: jest.MockedFunction<(filter: any, update: any, options?: any) => MockMongooseQuery<T | null>>;
findOneAndDelete: jest.MockedFunction<(filter: any) => MockMongooseQuery<T | null>>;
updateMany: jest.MockedFunction<(filter: any, update: any, options?: any) => MockMongooseQuery<any>>;
deleteMany: jest.MockedFunction<(filter: any) => MockMongooseQuery<any>>;
insertMany: jest.MockedFunction<(docs: any[], options?: any) => Promise<T[]>>;
bulkWrite: jest.MockedFunction<(operations: any[]) => Promise<any>>;
countDocuments: jest.MockedFunction<(filter?: any) => MockMongooseQuery<number>>;
estimatedDocumentCount: jest.MockedFunction<() => MockMongooseQuery<number>>;
aggregate: jest.MockedFunction<(pipeline: any[]) => MockMongooseAggregate>;
createIndexes: jest.MockedFunction<() => Promise<any>>;
ensureIndexes: jest.MockedFunction<() => Promise<any>>;
validate: jest.MockedFunction<(doc: any) => Promise<any>>;
new (doc?: any): MockMongooseDocument;
(doc?: any): MockMongooseDocument;
}
export interface MockMongooseSchema {
eachPath: jest.MockedFunction<(callback: (pathname: string, schemaType: any) => void) => void>;
paths: {
[path: string]: any;
};
}
export interface MockMongooseDb {
startSession: jest.MockedFunction<() => Promise<MockMongooseSession>>;
transaction: jest.MockedFunction<(callback: (session: MockMongooseSession) => Promise<any>) => Promise<any>>;
}
export interface MockMongooseSession {
startTransaction: jest.MockedFunction<() => void>;
commitTransaction: jest.MockedFunction<() => Promise<void>>;
abortTransaction: jest.MockedFunction<() => Promise<void>>;
endSession: jest.MockedFunction<() => Promise<void>>;
}
export interface MockMongooseAggregate {
exec: jest.MockedFunction<() => Promise<any[]>>;
allowDiskUse: jest.MockedFunction<(allow: boolean) => MockMongooseAggregate>;
maxTimeMS: jest.MockedFunction<(ms: number) => MockMongooseAggregate>;
append: jest.MockedFunction<(...stages: any[]) => MockMongooseAggregate>;
}
/**
* Mongoose Mock Factory - Creates comprehensive Mongoose mocks
*/
export declare class MongooseMockFactory {
/**
* Creates a mock query with chainable methods
*/
static createMockQuery<T>(returnValue: T): MockMongooseQuery<T>;
/**
* Creates a mock document with save functionality
*/
static createMockDocument(data?: any): MockMongooseDocument;
/**
* Creates a mock session for transaction testing
*/
static createMockSession(): MockMongooseSession;
/**
* Creates a mock aggregate query
*/
static createMockAggregate(returnValue?: any[]): MockMongooseAggregate;
/**
* Creates a comprehensive mock model with all methods
*/
static createMockModel<T = any>(modelName?: string, collectionName?: string, schemaPaths?: string[]): MockMongooseModel<T>;
/**
* Creates schema type information for a given path
*/
private static createSchemaType;
/**
* Creates a repository-specific mock model with common schema
*/
static createUserModel(): MockMongooseModel;
static createFileModel(): MockMongooseModel;
static createProfileModel(): MockMongooseModel;
static createBusinessModel(): MockMongooseModel;
}
/**
* Mock Repository Builder - Fluent interface for building repository mocks
*/
export declare class MockRepositoryBuilder<T = any> {
private model;
private returnValues;
constructor(modelName?: string, collectionName?: string);
static create<T = any>(modelName?: string, collectionName?: string): MockRepositoryBuilder<T>;
/**
* Configure return values for query methods
*/
withFindOneResult(result: T | null): MockRepositoryBuilder<T>;
withFindResult(results: T[]): MockRepositoryBuilder<T>;
withFindByIdResult(result: T | null): MockRepositoryBuilder<T>;
withCountResult(count: number): MockRepositoryBuilder<T>;
withAggregateResult(results: any[]): MockRepositoryBuilder<T>;
withInsertManyResult(results: T[]): MockRepositoryBuilder<T>;
withUpdateResult(result: any): MockRepositoryBuilder<T>;
withDeleteResult(success?: boolean): MockRepositoryBuilder<T>;
/**
* Configure error scenarios
*/
withFindOneError(error: Error): MockRepositoryBuilder<T>;
withSaveError(error: Error): MockRepositoryBuilder<T>;
/**
* Configure complex aggregation results (for pagination)
*/
withPaginationResult(items: T[], total: number): MockRepositoryBuilder<T>;
/**
* Reset all mocks to default state
*/
reset(): MockRepositoryBuilder<T>;
/**
* Get the built mock model
*/
build(): MockMongooseModel<T>;
}