-
-
Notifications
You must be signed in to change notification settings - Fork 551
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #132 Co-authored-by: bashleigh <[email protected]>
- Loading branch information
1 parent
883cef9
commit c189bab
Showing
11 changed files
with
204 additions
and
26 deletions.
There are no files selected for viewing
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
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
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
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
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
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,5 @@ | ||
export interface DtoOptions { | ||
create?: any; | ||
update?: any; | ||
replace?: any; | ||
} |
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
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,2 @@ | ||
export * from './test-create.dto'; | ||
export * from './test-update.dto'; |
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,22 @@ | ||
import { | ||
IsString, | ||
IsEmail, | ||
IsNumber, | ||
IsOptional, | ||
IsNotEmpty, | ||
IsEmpty, | ||
} from 'class-validator'; | ||
|
||
export class TestCreateDto { | ||
@IsString() | ||
firstName: string; | ||
|
||
@IsString() | ||
lastName: string; | ||
|
||
@IsEmail({ require_tld: false }) | ||
email: string; | ||
|
||
@IsNumber() | ||
age: number; | ||
} |
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,26 @@ | ||
import { | ||
IsString, | ||
IsEmail, | ||
IsNumber, | ||
IsOptional, | ||
IsNotEmpty, | ||
IsEmpty, | ||
} from 'class-validator'; | ||
|
||
export class TestUpdateDto { | ||
@IsOptional() | ||
@IsString() | ||
firstName?: string; | ||
|
||
@IsOptional() | ||
@IsString() | ||
lastName?: string; | ||
|
||
@IsOptional() | ||
@IsEmail({ require_tld: false }) | ||
email?: string; | ||
|
||
@IsOptional() | ||
@IsNumber() | ||
age?: number; | ||
} |
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,108 @@ | ||
import * as request from 'supertest'; | ||
import { Test } from '@nestjs/testing'; | ||
import { Controller, INestApplication } from '@nestjs/common'; | ||
import { APP_FILTER } from '@nestjs/core'; | ||
|
||
import { Crud } from '../src/decorators/crud.decorator'; | ||
import { HttpExceptionFilter } from './__fixture__/exception.filter'; | ||
import { TestModel } from './__fixture__/test.model'; | ||
import { TestCreateDto, TestUpdateDto } from './__fixture__/dto'; | ||
import { TestService } from './__fixture__/test.service'; | ||
|
||
describe('#crud', () => { | ||
describe('#dto options', () => { | ||
let app: INestApplication; | ||
let server: any; | ||
|
||
@Crud({ | ||
model: { | ||
type: TestModel, | ||
}, | ||
dto: { | ||
create: TestCreateDto, | ||
update: TestUpdateDto, | ||
}, | ||
}) | ||
@Controller('test') | ||
class TestController { | ||
constructor(public service: TestService<TestModel>) {} | ||
} | ||
|
||
beforeAll(async () => { | ||
const fixture = await Test.createTestingModule({ | ||
controllers: [TestController], | ||
providers: [{ provide: APP_FILTER, useClass: HttpExceptionFilter }, TestService], | ||
}).compile(); | ||
|
||
app = fixture.createNestApplication(); | ||
|
||
await app.init(); | ||
server = app.getHttpServer(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
describe('#createOneBase', () => { | ||
it('should return status 201', () => { | ||
const send: TestCreateDto = { | ||
firstName: 'firstName', | ||
lastName: 'lastName', | ||
email: '[email protected]', | ||
age: 15, | ||
}; | ||
return request(server) | ||
.post('/test') | ||
.send(send) | ||
.expect(201); | ||
}); | ||
it('should return status 400', (done) => { | ||
const send: TestModel = { | ||
firstName: 'firstName', | ||
lastName: 'lastName', | ||
email: '[email protected]', | ||
}; | ||
return request(server) | ||
.post('/test') | ||
.send(send) | ||
.expect(400) | ||
.end((_, res) => { | ||
expect(res.body.message[0].property).toBe('age'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#updateOneBase', () => { | ||
it('should return status 200', () => { | ||
const send: TestModel = { | ||
id: 1, | ||
firstName: 'firstName', | ||
lastName: 'lastName', | ||
email: '[email protected]', | ||
age: 15, | ||
}; | ||
return request(server) | ||
.patch('/test/1') | ||
.send(send) | ||
.expect(200); | ||
}); | ||
it('should return status 400', (done) => { | ||
const send: TestModel = { | ||
firstName: 'firstName', | ||
lastName: 'lastName', | ||
email: 'foo', | ||
}; | ||
return request(server) | ||
.patch('/test/1') | ||
.send(send) | ||
.expect(400) | ||
.end((_, res) => { | ||
expect(res.body.message[0].property).toBe('email'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |