|
| 1 | +--- |
| 2 | +to: src/<%= h.changeCase.paramCase(name) %>/<%= h.changeCase.paramCase(name) %>.controller.integration.spec.ts |
| 3 | +--- |
| 4 | +import { Test, TestingModule } from '@nestjs/testing' |
| 5 | +import { <%= h.changeCase.pascalCase(name) %>Controller } from './<%= h.changeCase.paramCase(name) %>.controller' |
| 6 | +import { AppModule } from '../app.module' |
| 7 | +import { HttpStatus, INestApplication } from '@nestjs/common' |
| 8 | +import { Model } from 'mongoose' |
| 9 | +import supertest from 'supertest' |
| 10 | +import { <%= h.inflection.singularize(h.changeCase.pascalCase(name)) %> } from './<%= h.inflection.singularize(name) %>.schema' |
| 11 | +import { getModelToken } from '@nestjs/mongoose' |
| 12 | +import { <%= h.changeCase.pascalCase(name) %>ControllerCreateRequestBodyDto } from './dto/<%= h.changeCase.paramCase(name) %>.create.dto' |
| 13 | +import { createTest<%= h.inflection.singularize(h.changeCase.pascalCase(name)) %> } from './<%= h.inflection.singularize(name) %>.mocks' |
| 14 | + |
| 15 | +let nestApplication: INestApplication |
| 16 | +let <%= h.inflection.singularize(h.changeCase.camelCase(name)) %>Model: Model<<%= h.inflection.singularize(h.changeCase.pascalCase(name)) %>> |
| 17 | + |
| 18 | +const first<%= h.inflection.singularize(h.changeCase.pascalCase(name)) %> = createTest<%= h.inflection.singularize(h.changeCase.pascalCase(name)) %>() |
| 19 | +const second<%= h.inflection.singularize(h.changeCase.pascalCase(name)) %> = createTest<%= h.inflection.singularize(h.changeCase.pascalCase(name)) %>() |
| 20 | + |
| 21 | +beforeAll(async () => { |
| 22 | + const testingModule: TestingModule = await Test.createTestingModule({ |
| 23 | + imports: [AppModule], |
| 24 | + }).compile() |
| 25 | + |
| 26 | + nestApplication = testingModule.createNestApplication() |
| 27 | + await nestApplication.init() |
| 28 | + <%= h.inflection.singularize(h.changeCase.camelCase(name)) %>Model = nestApplication.get(getModelToken(<%= h.inflection.singularize(h.changeCase.pascalCase(name)) %>.name)) |
| 29 | +}) |
| 30 | + |
| 31 | +beforeEach(async () => { |
| 32 | + await <%= h.inflection.singularize(h.changeCase.camelCase(name)) %>Model.create(first<%= h.inflection.singularize(h.changeCase.pascalCase(name)) %>) |
| 33 | + await <%= h.inflection.singularize(h.changeCase.camelCase(name)) %>Model.create(second<%= h.inflection.singularize(h.changeCase.pascalCase(name)) %>) |
| 34 | +}) |
| 35 | + |
| 36 | +afterEach(async () => { |
| 37 | + await <%= h.inflection.singularize(h.changeCase.camelCase(name)) %>Model.deleteMany({}) |
| 38 | +}) |
| 39 | + |
| 40 | +afterAll(async () => { |
| 41 | + await nestApplication.close() |
| 42 | +}) |
| 43 | + |
| 44 | +describe('<%= h.changeCase.pascalCase(name) %>Controller', () => { |
| 45 | + describe('create <%= h.changeCase.lowerCase(h.changeCase.sentenceCase(h.inflection.singularize(name))) %>', () => { |
| 46 | + it('should create <%= h.changeCase.lowerCase(h.changeCase.sentenceCase(h.inflection.singularize(name))) %>', async () => { |
| 47 | + const body: <%= h.changeCase.pascalCase(name) %>ControllerCreateRequestBodyDto = {} |
| 48 | + |
| 49 | + const response = await supertest(nestApplication.getHttpServer()).post('/<%= h.changeCase.paramCase(name) %>').send(body) |
| 50 | + |
| 51 | + expect(response.body).toMatchSnapshot({ |
| 52 | + _id: expect.any(String), |
| 53 | + createdAt: expect.any(String), |
| 54 | + updatedAt: expect.any(String), |
| 55 | + }) |
| 56 | + expect(response.status).toBe(HttpStatus.CREATED) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + describe('get <%= h.changeCase.lowerCase(h.changeCase.sentenceCase(h.inflection.singularize(name))) %> by id', () => { |
| 61 | + it('should throw error when <%= h.changeCase.lowerCase(h.changeCase.sentenceCase(h.inflection.singularize(name))) %> does not exist', async () => { |
| 62 | + const response = await supertest(nestApplication.getHttpServer()) |
| 63 | + .get(`/<%= h.changeCase.paramCase(name) %>/62c3261b4bc318d862a90a71`) |
| 64 | + .send() |
| 65 | + |
| 66 | + expect(response.body).toMatchSnapshot() |
| 67 | + expect(response.status).toBe(HttpStatus.NOT_FOUND) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should get <%= h.changeCase.lowerCase(h.changeCase.sentenceCase(h.inflection.singularize(name))) %> by id', async () => { |
| 71 | + const response = await supertest(nestApplication.getHttpServer()) |
| 72 | + .get(`/<%= h.changeCase.paramCase(name) %>/${first<%= h.inflection.singularize(h.changeCase.pascalCase(name)) %>._id}`) |
| 73 | + .send() |
| 74 | + |
| 75 | + expect(response.body).toMatchSnapshot({ |
| 76 | + _id: expect.any(String), |
| 77 | + createdAt: expect.any(String), |
| 78 | + updatedAt: expect.any(String), |
| 79 | + }) |
| 80 | + expect(response.status).toBe(HttpStatus.OK) |
| 81 | + }) |
| 82 | + }) |
| 83 | +}) |
0 commit comments