Skip to content

Commit

Permalink
fix: make discriminator test better
Browse files Browse the repository at this point in the history
  • Loading branch information
Pong420 committed Nov 23, 2020
1 parent 86b0066 commit 766edd4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 31 deletions.
50 changes: 46 additions & 4 deletions tests/e2e/discriminator.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
2 changes: 0 additions & 2 deletions tests/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
37 changes: 12 additions & 25 deletions tests/src/event/event.module.ts
Original file line number Diff line number Diff line change
@@ -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],
};
}
}
8 changes: 8 additions & 0 deletions tests/src/event/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event & Document>,

@InjectModel(ClieckLinkEvent.name)
private readonly clientEventModel: Model<Event & Document>,

@InjectModel(SignUpEvent.name)
private readonly signUpEventModel: Model<Event & Document>,
) {}

async create(
Expand Down

0 comments on commit 766edd4

Please sign in to comment.