Skip to content

Commit 3fb4a05

Browse files
authored
Merge pull request #13 from SecJS/feat/len-factories-base-service
feat: Add factory class and base service impl in templates
2 parents efbeca7 + 6c33c56 commit 3fb4a05

31 files changed

+235
-490
lines changed

app/templates/nestjsMongoose/E2E/__name__/delete.spec.ts.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import * as request from 'supertest'
33
import { Debug } from '@secjs/logger'
44
import { AppModule } from 'app/AppModule'
55
import { App, Database } from 'test/Utils'
6-
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'
6+
import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory'
77

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

1212
const status = 200
1313
const method = 'DELETE'
14-
const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}`
14+
const path = `/<%= nameCamel %>s/${<%= nameCamel %>.id}`
1515

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

3131
const { body } = await request(app.server.getHttpServer())
3232
.delete(path)
@@ -35,10 +35,11 @@ describe('\n[E2E] <%= namePascal %> ❌', () => {
3535
expect(body.method).toBe(method)
3636
expect(body.status).toBe(status)
3737
expect(body.error).toEqual({
38+
isSecJsException: true,
3839
name: 'NotFoundException',
3940
message: {
40-
error: 'Not Found',
41-
message: 'NOT_FOUND_<%= nameUp %>',
41+
error: 'Not Found Error',
42+
message: 'NOT_FOUND_PROJECT',
4243
statusCode: status,
4344
},
4445
})
@@ -47,15 +48,15 @@ describe('\n[E2E] <%= namePascal %> ❌', () => {
4748

4849
let app: App
4950
let database: Database
50-
let <%= nameCamel %>Seeder: <%= namePascal %>Seeder
51+
let <%= nameCamel %>Factory: <%= namePascal %>Factory
5152

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

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

58-
<%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder)
59+
<%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory)
5960
})
6061

6162
afterEach(async () => {

app/templates/nestjsMongoose/E2E/__name__/index.spec.ts.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as request from 'supertest'
33
import { Debug } from '@secjs/logger'
44
import { AppModule } from 'app/AppModule'
55
import { App, Database } from 'test/Utils'
6-
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'
6+
import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory'
77

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

17-
await <%= nameCamel %>Seeder.seedMany(2)
18-
await <%= nameCamel %>Seeder.seedManyDeleted(2)
17+
await <%= nameCamel %>Factory.count(2).create()
18+
await <%= nameCamel %>Factory.count(2).deleted().create()
1919

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

8181
let app: App
8282
let database: Database
83-
let <%= nameCamel %>Seeder: <%= namePascal %>Seeder
83+
let <%= nameCamel %>Factory: <%= namePascal %>Factory
8484

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

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

91-
<%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder)
91+
<%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory)
9292
})
9393

9494
afterEach(async () => {

app/templates/nestjsMongoose/E2E/__name__/show.spec.ts.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import * as request from 'supertest'
33
import { Debug } from '@secjs/logger'
44
import { AppModule } from 'app/AppModule'
55
import { App, Database } from 'test/Utils'
6-
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'
6+
import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory'
77

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

1212
const status = 200
1313
const method = 'GET'
@@ -34,9 +34,10 @@ describe('\n[E2E] <%= namePascal %> 🔍', () => {
3434
expect(body.method).toBe(method)
3535
expect(body.status).toBe(status)
3636
expect(body.error).toEqual({
37+
isSecJsException: true,
3738
name: 'NotFoundException',
3839
message: {
39-
error: 'Not Found',
40+
error: 'Not Found Error',
4041
message: 'NOT_FOUND_<%= nameUp %>',
4142
statusCode: status,
4243
},
@@ -90,15 +91,15 @@ describe('\n[E2E] <%= namePascal %> 🔍', () => {
9091

9192
let app: App
9293
let database: Database
93-
let <%= nameCamel %>Seeder: <%= namePascal %>Seeder
94+
let <%= nameCamel %>Factory: <%= namePascal %>Factory
9495

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

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

101-
<%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder)
102+
<%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory)
102103
})
103104

104105
afterEach(async () => {

app/templates/nestjsMongoose/E2E/__name__/store.spec.ts.txt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,32 @@ import * as request from 'supertest'
33
import { Debug } from '@secjs/logger'
44
import { AppModule } from 'app/AppModule'
55
import { App, Database } from 'test/Utils'
6-
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'
76

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

14-
const storeBlueprint = <%= nameCamel %>Seeder.storeBlueprint()
13+
const payload = {
14+
name: 'test'
15+
}
1516

1617
const { body } = await request(app.server.getHttpServer())
1718
.post(path)
18-
.send(storeBlueprint)
19+
.send(payload)
1920
.expect(status)
2021

2122
expect(body.method).toBe(method)
2223
expect(body.status).toBe(status)
23-
expect(body.data.name).toBe(storeBlueprint.name)
24+
expect(body.data.name).toBe(payload.name)
2425
})
2526

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

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

4748
let app: App
4849
let database: Database
49-
let <%= nameCamel %>Seeder: <%= namePascal %>Seeder
5050

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

5454
app = await new App([AppModule]).initApp()
5555
database = new Database(app)
56-
57-
<%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder)
5856
})
5957

6058
afterEach(async () => {

app/templates/nestjsMongoose/E2E/__name__/update.spec.ts.txt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,48 @@ import * as request from 'supertest'
33
import { Debug } from '@secjs/logger'
44
import { AppModule } from 'app/AppModule'
55
import { App, Database } from 'test/Utils'
6-
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'
6+
import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory'
77

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

1212
const status = 200
1313
const method = 'PUT'
14-
const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}`
14+
const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}?apiKey=${apiKey}`
1515

16-
const updateBlueprint = <%= nameCamel %>Seeder.updateBlueprint()
16+
const payload = {
17+
name: 'test',
18+
}
1719

1820
const { body } = await request(app.server.getHttpServer())
1921
.put(path)
20-
.send(updateBlueprint)
22+
.send(payload)
2123
.expect(status)
2224

2325
expect(body.method).toBe(method)
2426
expect(body.status).toBe(status)
2527
expect(body.data.id).toBe(<%= nameCamel %>.id)
28+
expect(body.data.name).toBe(payload.name)
2629
})
2730

2831
it('should throw a not found error when can not find <%= nameCamel %>', async () => {
2932
const status = 404
3033
const method = 'PUT'
31-
const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011`
32-
33-
const updateBlueprint = <%= nameCamel %>Seeder.blueprint()
34+
const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011?apiKey=${apiKey}`
3435

3536
const { body } = await request(app.server.getHttpServer())
3637
.put(path)
37-
.send(updateBlueprint)
38+
.send({ name: 'test' })
3839
.expect(status)
3940

4041
expect(body.method).toBe(method)
4142
expect(body.status).toBe(status)
4243
expect(body.error).toEqual({
44+
isSecJsException: true,
4345
name: 'NotFoundException',
4446
message: {
45-
error: 'Not Found',
47+
error: 'Not Found Error',
4648
message: 'NOT_FOUND_<%= nameUp %>',
4749
statusCode: status,
4850
},
@@ -53,7 +55,7 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => {
5355
// And create tests to validate if the schema validator are working well.
5456

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

7375
let app: App
7476
let database: Database
75-
let <%= nameCamel %>Seeder: <%= namePascal %>Seeder
77+
let <%= nameCamel %>Factory: <%= namePascal %>Factory
7678

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

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

83-
<%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder)
85+
<%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory)
8486
})
8587

8688
afterEach(async () => {
@@ -90,4 +92,3 @@ afterEach(async () => {
9092
await database.truncateCache()
9193
await app.closeApp()
9294
})
93-

app/templates/nestjsMongoose/__name__Controller.ts.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export class <%= namePascal %>Controller {
3030
private <%= nameCamel %>Service: <%= namePascal %>Service
3131

3232
@Get()
33-
@ApiQuery({ name: 'limit', allowEmptyValue: true })
3433
@ApiQuery({ name: 'page', allowEmptyValue: true })
34+
@ApiQuery({ name: 'limit', allowEmptyValue: true })
3535
async index(
3636
@Query(QueryParamsPipe) queries: any,
3737
@Pagination() paginate: PaginationContract,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Inject, Injectable } from '@nestjs/common'
2+
import { Factory } from '@secjs/base/database/Factory'
3+
import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder'
4+
5+
@Injectable()
6+
export class <%= namePascal %>Factory extends Factory<any> {
7+
@Inject(<%= namePascal %>Seeder) protected seeder: <%= namePascal %>Seeder
8+
9+
blueprint = () => {
10+
return {
11+
example: this.faker.name.findName(),
12+
}
13+
}
14+
15+
fakeBlueprint = () => {
16+
return {
17+
id: this.faker.datatype.uuid(),
18+
name: this.faker.name.findName(),
19+
status: 'pendent',
20+
createdAt: new Date(),
21+
updatedAt: new Date(),
22+
deletedAt: null,
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)