-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from nhatphongdo/fix/override-decorator-does-…
…not-work-properly fix: override decorator does not work properly because of wrong method name
- Loading branch information
Showing
3 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
spec/override-decorator/override.create.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { HttpStatus, INestApplication, forwardRef } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import request from 'supertest'; | ||
|
||
import { OverrideCreateController } from './override.create.controller'; | ||
import { BaseEntity } from '../base/base.entity'; | ||
import { BaseService } from '../base/base.service'; | ||
import { TestHelper } from '../test.helper'; | ||
|
||
describe('OverrideDecorator for CREATE', () => { | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [forwardRef(() => TestHelper.getTypeOrmMysqlModule([BaseEntity])), TypeOrmModule.forFeature([BaseEntity])], | ||
controllers: [OverrideCreateController], | ||
providers: [BaseService], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
|
||
await app.init(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await TestHelper.dropTypeOrmEntityTables(); | ||
await app?.close(); | ||
}); | ||
|
||
it('should invoke override method', async () => { | ||
const routerPathList = TestHelper.getRoutePath(app.getHttpServer()); | ||
expect(routerPathList.post).toEqual(expect.arrayContaining(['/test'])); | ||
|
||
const response = await request(app.getHttpServer()).post('/test').send({}); | ||
|
||
expect(response.statusCode).toEqual(HttpStatus.CREATED); | ||
expect(response.body.result).toEqual('createOne'); | ||
}); | ||
|
||
it('should not invoke non override method', async () => { | ||
const routerPathList = TestHelper.getRoutePath(app.getHttpServer()); | ||
expect(routerPathList.post).toEqual(expect.arrayContaining(['/test/search'])); | ||
|
||
const response = await request(app.getHttpServer()).post('/test/search').send({}); | ||
|
||
expect(response.statusCode).toEqual(HttpStatus.OK); | ||
expect(response.body.result).not.toBeDefined(); | ||
expect(response.body.data).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Controller } from '@nestjs/common'; | ||
|
||
import { Crud } from '../../src/lib/crud.decorator'; | ||
import { CrudController } from '../../src/lib/interface'; | ||
import { Override } from '../../src/lib/override.decorator'; | ||
import { BaseEntity } from '../base/base.entity'; | ||
import { BaseService } from '../base/base.service'; | ||
|
||
@Crud({ | ||
entity: BaseEntity, | ||
}) | ||
@Controller('test') | ||
export class OverrideCreateController implements CrudController<BaseEntity> { | ||
constructor(public readonly crudService: BaseService) {} | ||
|
||
@Override('CREATE') | ||
overrideCreate() { | ||
return { result: 'createOne' }; | ||
} | ||
|
||
search() { | ||
return { result: 'search' }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters