diff --git a/tests/e2e/discriminator.spec.ts b/tests/e2e/discriminator.spec.ts index 0677cc19..0cffb60c 100644 --- a/tests/e2e/discriminator.spec.ts +++ b/tests/e2e/discriminator.spec.ts @@ -1,16 +1,58 @@ -import { HttpStatus, INestApplication } from '@nestjs/common'; +import { HttpStatus, INestApplication, DynamicModule } from '@nestjs/common'; import { Test } from '@nestjs/testing'; import { Server } from 'http'; import * as request from 'supertest'; -import { AppModule } from '../src/app.module'; +import { MongooseModule } from '../../lib'; +import { EventModule } from '../src/event/event.module'; +import { Event, EventSchema } from '../src/event/schemas/event.schema'; +import { + ClieckLinkEvent, + ClieckLinkEventSchema, +} from '../src/event/schemas/click-link-event.schema'; +import { + SignUpEvent, + SignUpEventSchema, +} from '../src/event/schemas/sign-up-event.schema'; -describe('Discriminator', () => { +const testCase: [string, DynamicModule][] = [ + [ + 'forFeature', + MongooseModule.forFeature([ + { + name: Event.name, + schema: EventSchema, + discriminators: [ + { name: ClieckLinkEvent.name, schema: ClieckLinkEventSchema }, + { name: SignUpEvent.name, schema: SignUpEventSchema }, + ], + }, + ]), + ], + [ + 'forFeatureAsync', + MongooseModule.forFeatureAsync([ + { + name: Event.name, + useFactory: async () => EventSchema, + discriminators: [ + { name: ClieckLinkEvent.name, schema: ClieckLinkEventSchema }, + { name: SignUpEvent.name, schema: SignUpEventSchema }, + ], + }, + ]), + ], +]; + +describe.each(testCase)('Discriminator - %s', (_, features) => { let server: Server; let app: INestApplication; beforeEach(async () => { const module = await Test.createTestingModule({ - imports: [AppModule], + imports: [ + MongooseModule.forRoot('mongodb://localhost:27017/test'), + EventModule.forFeature(features), + ], }).compile(); app = module.createNestApplication(); diff --git a/tests/src/app.module.ts b/tests/src/app.module.ts index f31a6d8b..e7b799f7 100644 --- a/tests/src/app.module.ts +++ b/tests/src/app.module.ts @@ -1,13 +1,11 @@ import { Module } from '@nestjs/common'; import { MongooseModule } from '../../lib'; import { CatsModule } from './cats/cats.module'; -import { EventModule } from './event/event.module'; @Module({ imports: [ MongooseModule.forRoot('mongodb://localhost:27017/test'), CatsModule, - EventModule, ], }) export class AppModule {} diff --git a/tests/src/event/event.module.ts b/tests/src/event/event.module.ts index 8cbac0ea..1a1ecbb7 100644 --- a/tests/src/event/event.module.ts +++ b/tests/src/event/event.module.ts @@ -1,28 +1,15 @@ -import { Module } from '@nestjs/common'; -import { MongooseModule } from '../../../lib'; +import { DynamicModule, Module } from '@nestjs/common'; import { EventService } from './event.service'; import { EventController } from './event.controller'; -import { Event, EventSchema } from './schemas/event.schema'; -import { - ClieckLinkEvent, - ClieckLinkEventSchema, -} from './schemas/click-link-event.schema'; -import { SignUpEvent, SignUpEventSchema } from './schemas/sign-up-event.schema'; -@Module({ - imports: [ - MongooseModule.forFeature([ - { - name: Event.name, - schema: EventSchema, - discriminators: [ - { name: ClieckLinkEvent.name, schema: ClieckLinkEventSchema }, - { name: SignUpEvent.name, schema: SignUpEventSchema }, - ], - }, - ]), - ], - controllers: [EventController], - providers: [EventService], -}) -export class EventModule {} +@Module({}) +export class EventModule { + static forFeature(module: DynamicModule): DynamicModule { + return { + imports: [module], + module: EventModule, + controllers: [EventController], + providers: [EventService], + }; + } +} diff --git a/tests/src/event/event.service.ts b/tests/src/event/event.service.ts index a0f97356..b995fe53 100644 --- a/tests/src/event/event.service.ts +++ b/tests/src/event/event.service.ts @@ -4,12 +4,20 @@ import { InjectModel } from '../../../lib'; import { CreateClickLinkEventDto } from './dto/create-click-link-event.dto'; import { CreateSignUpEventDto } from './dto/create-sign-up-event.dto'; import { Event } from './schemas/event.schema'; +import { ClieckLinkEvent } from './schemas/click-link-event.schema'; +import { SignUpEvent } from './schemas/sign-up-event.schema'; @Injectable() export class EventService { constructor( @InjectModel(Event.name) private readonly eventModel: Model, + + @InjectModel(ClieckLinkEvent.name) + private readonly clientEventModel: Model, + + @InjectModel(SignUpEvent.name) + private readonly signUpEventModel: Model, ) {} async create(