Created
April 10, 2019 09:03
-
-
Save Diluka/de5e623ac3af7f934bbdc8e84c92811c to your computer and use it in GitHub Desktop.
test if module options is effect on body transform in @nestjsx/crud
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
import { Controller, INestApplication, Injectable } from '@nestjs/common'; | |
import { RepositoryService } from '../../src/typeorm'; | |
import { Company, ormConfig, User, UserProfile } from '../../integration/typeorm/e2e'; | |
import { Crud, Override, ParsedBody, ParsedParams } from '../../src/decorators'; | |
import { InjectRepository, TypeOrmModule } from '@nestjs/typeorm'; | |
import { CrudController, FilterParamParsed } from '../../src/interfaces'; | |
import { Test } from '@nestjs/testing'; | |
import * as supertest from 'supertest'; | |
import { CrudModule } from '../../src/module'; | |
import { CrudConfigService } from '../../src/module/crud-config.service'; | |
import * as _ from 'lodash'; | |
@Injectable() | |
class CompaniesService extends RepositoryService<Company> { | |
constructor(@InjectRepository(Company) repo) { | |
super(repo); | |
} | |
} | |
@Crud(Company, { | |
options: { | |
filter: [{ field: 'id', operator: 'notnull' }], | |
}, | |
}) | |
@Controller('companies') | |
class CompaniesController implements CrudController<CompaniesService, Company> { | |
constructor(public service: CompaniesService) { | |
} | |
get base(): CrudController<CompaniesService, Company> { | |
return this; | |
} | |
@Override() | |
async createOne(@ParsedParams() parsedParams: FilterParamParsed[], @ParsedBody() dto: Company): Promise<any> { | |
return { | |
check: dto instanceof Company, | |
fieldCheck: dto.establishedAt instanceof Date, | |
}; | |
} | |
} | |
describe('body transform test', () => { | |
let app: INestApplication; | |
let server: any; | |
let $: supertest.SuperTest<supertest.Test>; | |
beforeAll(async () => { | |
CrudModule.loadConfig({ | |
validation: { | |
transform: true, | |
}, | |
}); | |
const fixture = await Test.createTestingModule({ | |
imports: [ | |
TypeOrmModule.forRoot(ormConfig), | |
TypeOrmModule.forFeature([UserProfile, User, Company]), | |
], | |
providers: [CompaniesService], | |
controllers: [CompaniesController], | |
}).compile(); | |
app = fixture.createNestApplication(); | |
await app.init(); | |
server = app.getHttpServer(); | |
$ = supertest(server); | |
}); | |
afterAll(async () => { | |
await app.close(); | |
}); | |
it('should module options not be empty', () => { | |
expect(_.isEmpty(CrudConfigService.config)).toBeFalsy(); | |
}); | |
it('should be transformed', async () => { | |
const res = await $.post('/companies') | |
.send({ | |
name: `e2eNameX${Date.now()}`, | |
domain: `e2eDomainX`, | |
establishedAt: '2019-04-10T04:33:53+0800', | |
}) | |
.expect(201); | |
console.log(res.body); | |
expect(res.body.check).toBeTruthy(); | |
expect(res.body.fieldCheck).toBeTruthy(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment