Skip to content

Commit

Permalink
Merge pull request #13 from SecJS/feat/len-factories-base-service
Browse files Browse the repository at this point in the history
feat: Add factory class and base service impl in templates
  • Loading branch information
jlenon7 authored Sep 28, 2021
2 parents efbeca7 + 6c33c56 commit 3fb4a05
Show file tree
Hide file tree
Showing 31 changed files with 235 additions and 490 deletions.
17 changes: 9 additions & 8 deletions app/templates/nestjsMongoose/E2E/__name__/delete.spec.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import * as request from 'supertest'
import { Debug } from '@secjs/logger'
import { AppModule } from 'app/AppModule'
import { App, Database } from 'test/Utils'
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'
import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory'

describe('\n[E2E] <%= namePascal %> ❌', () => {
it('should delete one <%= nameCamel %>', async () => {
const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne()
const <%= nameCamel %> = await <%= nameCamel %>Factory.create()

const status = 200
const method = 'DELETE'
const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}`
const path = `/<%= nameCamel %>s/${<%= nameCamel %>.id}`

const { body } = await request(app.server.getHttpServer())
.delete(path)
Expand All @@ -26,7 +26,7 @@ describe('\n[E2E] <%= namePascal %> ❌', () => {
it('should throw a not found error when can not find <%= nameCamel %>', async () => {
const status = 404
const method = 'DELETE'
const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011`
const path = `/<%= nameCamel %>s/507f1f77bcf86cd799439011`

const { body } = await request(app.server.getHttpServer())
.delete(path)
Expand All @@ -35,10 +35,11 @@ describe('\n[E2E] <%= namePascal %> ❌', () => {
expect(body.method).toBe(method)
expect(body.status).toBe(status)
expect(body.error).toEqual({
isSecJsException: true,
name: 'NotFoundException',
message: {
error: 'Not Found',
message: 'NOT_FOUND_<%= nameUp %>',
error: 'Not Found Error',
message: 'NOT_FOUND_PROJECT',
statusCode: status,
},
})
Expand All @@ -47,15 +48,15 @@ describe('\n[E2E] <%= namePascal %> ❌', () => {

let app: App
let database: Database
let <%= nameCamel %>Seeder: <%= namePascal %>Seeder
let <%= nameCamel %>Factory: <%= namePascal %>Factory

beforeEach(async () => {
Debug(`Executing ${beforeEach.name}`, 'api:test')

app = await new App([AppModule]).initApp()
database = new Database(app)

<%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder)
<%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory)
})

afterEach(async () => {
Expand Down
10 changes: 5 additions & 5 deletions app/templates/nestjsMongoose/E2E/__name__/index.spec.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as request from 'supertest'
import { Debug } from '@secjs/logger'
import { AppModule } from 'app/AppModule'
import { App, Database } from 'test/Utils'
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'
import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory'

describe('\n[E2E] <%= namePascal %> 🗒', () => {
it('should return all <%= namePluralCamel %> paginated and filtered', async () => {
Expand All @@ -14,8 +14,8 @@ describe('\n[E2E] <%= namePascal %> 🗒', () => {
const method = 'GET'
const path = `/<%= namePluralCamel %>?page=${page}&limit=${limit}&*deletedAt=null`

await <%= nameCamel %>Seeder.seedMany(2)
await <%= nameCamel %>Seeder.seedManyDeleted(2)
await <%= nameCamel %>Factory.count(2).create()
await <%= nameCamel %>Factory.count(2).deleted().create()

const { body } = await request(app.server.getHttpServer())
.get(path)
Expand Down Expand Up @@ -80,15 +80,15 @@ describe('\n[E2E] <%= namePascal %> 🗒', () => {

let app: App
let database: Database
let <%= nameCamel %>Seeder: <%= namePascal %>Seeder
let <%= nameCamel %>Factory: <%= namePascal %>Factory

beforeEach(async () => {
Debug(`Executing ${beforeEach.name}`, 'api:test')

app = await new App([AppModule]).initApp()
database = new Database(app)

<%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder)
<%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory)
})

afterEach(async () => {
Expand Down
11 changes: 6 additions & 5 deletions app/templates/nestjsMongoose/E2E/__name__/show.spec.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import * as request from 'supertest'
import { Debug } from '@secjs/logger'
import { AppModule } from 'app/AppModule'
import { App, Database } from 'test/Utils'
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'
import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory'

describe('\n[E2E] <%= namePascal %> 🔍', () => {
it('should return one <%= nameCamel %>', async () => {
const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne()
const <%= nameCamel %> = await <%= nameCamel %>Factory.create()

const status = 200
const method = 'GET'
Expand All @@ -34,9 +34,10 @@ describe('\n[E2E] <%= namePascal %> 🔍', () => {
expect(body.method).toBe(method)
expect(body.status).toBe(status)
expect(body.error).toEqual({
isSecJsException: true,
name: 'NotFoundException',
message: {
error: 'Not Found',
error: 'Not Found Error',
message: 'NOT_FOUND_<%= nameUp %>',
statusCode: status,
},
Expand Down Expand Up @@ -90,15 +91,15 @@ describe('\n[E2E] <%= namePascal %> 🔍', () => {

let app: App
let database: Database
let <%= nameCamel %>Seeder: <%= namePascal %>Seeder
let <%= nameCamel %>Factory: <%= namePascal %>Factory

beforeEach(async () => {
Debug(`Executing ${beforeEach.name}`, 'api:test')

app = await new App([AppModule]).initApp()
database = new Database(app)

<%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder)
<%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory)
})

afterEach(async () => {
Expand Down
14 changes: 6 additions & 8 deletions app/templates/nestjsMongoose/E2E/__name__/store.spec.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@ import * as request from 'supertest'
import { Debug } from '@secjs/logger'
import { AppModule } from 'app/AppModule'
import { App, Database } from 'test/Utils'
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'

describe('\n[E2E] <%= namePascal %> 🧱', () => {
it('should create a new <%= nameCamel %>', async () => {
const status = 201
const method = 'POST'
const path = `/<%= namePluralCamel %>`

const storeBlueprint = <%= nameCamel %>Seeder.storeBlueprint()
const payload = {
name: 'test'
}

const { body } = await request(app.server.getHttpServer())
.post(path)
.send(storeBlueprint)
.send(payload)
.expect(status)

expect(body.method).toBe(method)
expect(body.status).toBe(status)
expect(body.data.name).toBe(storeBlueprint.name)
expect(body.data.name).toBe(payload.name)
})

// Implement all properties in app/Contracts/Dtos/<%= namePascal %>Dto.ts
// And create tests to validate if the schema validator are working well.

// it('should throw validation errors when trying to create a <%= nameCamel %> with validation errors', async () => {
// const status = 400
// const status = 422
// const path = `/<%= namePluralCamel %>`
//
// const { body } = await request(app.server.getHttpServer())
Expand All @@ -46,15 +47,12 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => {

let app: App
let database: Database
let <%= nameCamel %>Seeder: <%= namePascal %>Seeder

beforeEach(async () => {
Debug(`Executing ${beforeEach.name}`, 'api:test')

app = await new App([AppModule]).initApp()
database = new Database(app)

<%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder)
})

afterEach(async () => {
Expand Down
29 changes: 15 additions & 14 deletions app/templates/nestjsMongoose/E2E/__name__/update.spec.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,48 @@ import * as request from 'supertest'
import { Debug } from '@secjs/logger'
import { AppModule } from 'app/AppModule'
import { App, Database } from 'test/Utils'
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'
import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory'

describe('\n[E2E] <%= namePascal %> 🛠', () => {
it('should update one <%= nameCamel %>', async () => {
const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne()
const <%= nameCamel %> = await <%= nameCamel %>Factory.create()

const status = 200
const method = 'PUT'
const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}`
const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}?apiKey=${apiKey}`

const updateBlueprint = <%= nameCamel %>Seeder.updateBlueprint()
const payload = {
name: 'test',
}

const { body } = await request(app.server.getHttpServer())
.put(path)
.send(updateBlueprint)
.send(payload)
.expect(status)

expect(body.method).toBe(method)
expect(body.status).toBe(status)
expect(body.data.id).toBe(<%= nameCamel %>.id)
expect(body.data.name).toBe(payload.name)
})

it('should throw a not found error when can not find <%= nameCamel %>', async () => {
const status = 404
const method = 'PUT'
const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011`

const updateBlueprint = <%= nameCamel %>Seeder.blueprint()
const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011?apiKey=${apiKey}`

const { body } = await request(app.server.getHttpServer())
.put(path)
.send(updateBlueprint)
.send({ name: 'test' })
.expect(status)

expect(body.method).toBe(method)
expect(body.status).toBe(status)
expect(body.error).toEqual({
isSecJsException: true,
name: 'NotFoundException',
message: {
error: 'Not Found',
error: 'Not Found Error',
message: 'NOT_FOUND_<%= nameUp %>',
statusCode: status,
},
Expand All @@ -53,7 +55,7 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => {
// And create tests to validate if the schema validator are working well.

// it('should throw validation errors when trying to update a <%= nameCamel %> with validation errors', async () => {
// const status = 400
// const status = 422
// const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011`
//
// const { body } = await request(app.server.getHttpServer())
Expand All @@ -72,15 +74,15 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => {

let app: App
let database: Database
let <%= nameCamel %>Seeder: <%= namePascal %>Seeder
let <%= nameCamel %>Factory: <%= namePascal %>Factory

beforeEach(async () => {
Debug(`Executing ${beforeEach.name}`, 'api:test')

app = await new App([AppModule]).initApp()
database = new Database(app)

<%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder)
<%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory)
})

afterEach(async () => {
Expand All @@ -90,4 +92,3 @@ afterEach(async () => {
await database.truncateCache()
await app.closeApp()
})

2 changes: 1 addition & 1 deletion app/templates/nestjsMongoose/__name__Controller.ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export class <%= namePascal %>Controller {
private <%= nameCamel %>Service: <%= namePascal %>Service

@Get()
@ApiQuery({ name: 'limit', allowEmptyValue: true })
@ApiQuery({ name: 'page', allowEmptyValue: true })
@ApiQuery({ name: 'limit', allowEmptyValue: true })
async index(
@Query(QueryParamsPipe) queries: any,
@Pagination() paginate: PaginationContract,
Expand Down
25 changes: 25 additions & 0 deletions app/templates/nestjsMongoose/__name__Factory.ts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Inject, Injectable } from '@nestjs/common'
import { Factory } from '@secjs/base/database/Factory'
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'

@Injectable()
export class <%= namePascal %>Factory extends Factory<any> {
@Inject(<%= namePascal %>Seeder) protected seeder: <%= namePascal %>Seeder

blueprint = () => {
return {
example: this.faker.name.findName(),
}
}

fakeBlueprint = () => {
return {
id: this.faker.datatype.uuid(),
name: this.faker.name.findName(),
status: 'pendent',
createdAt: new Date(),
updatedAt: new Date(),
deletedAt: null,
}
}
}
Loading

0 comments on commit 3fb4a05

Please sign in to comment.