Skip to content

Commit

Permalink
Merge pull request #8402 from Tony133/test/sample-14-mongoose-base
Browse files Browse the repository at this point in the history
test(sample-14): added unit test and other improvements
  • Loading branch information
kamilmysliwiec authored Oct 29, 2021
2 parents fb5ee1d + ea25c7d commit 1394305
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 9 deletions.
2 changes: 1 addition & 1 deletion sample/14-mongoose-base/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Mongoose base sample
## Mongoose base sample

### Installation

Expand Down
10 changes: 10 additions & 0 deletions sample/14-mongoose-base/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 23 additions & 5 deletions sample/14-mongoose-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,38 @@
"@nestjs/schematics": "8.0.4",
"@nestjs/testing": "8.1.2",
"@types/express": "4.17.13",
"@types/jest": "27.0.2",
"@types/node": "16.11.6",
"@types/supertest": "2.0.11",
"@typescript-eslint/eslint-plugin": "4.33.0",
"@typescript-eslint/parser": "4.33.0",
"eslint": "7.32.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-import": "2.25.2",
"jest": "27.3.1",
"prettier": "2.4.1",
"supertest": "6.1.6",
"ts-jest": "27.0.7",
"ts-loader": "9.2.6",
"ts-node": "10.4.0",
"tsconfig-paths": "3.11.0",
"@typescript-eslint/eslint-plugin": "4.33.0",
"@typescript-eslint/parser": "4.33.0",
"eslint": "7.32.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-import": "2.25.2",
"typescript": "4.3.5"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
88 changes: 88 additions & 0 deletions sample/14-mongoose-base/src/cats/cats.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CatsController } from './cats.controller';
import { CreateCatDto } from './dto/create-cat.dto';
import { CatsService } from './cats.service';

describe('CatsController', () => {
let controller: CatsController;
let service: CatsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CatsController],
providers: [
{
provide: CatsService,
useValue: {
findAll: jest.fn().mockResolvedValue([
{
name: 'Cat #1',
breed: 'Bread #1',
age: 4,
},
{
name: 'Cat #2',
breed: 'Breed #2',
age: 3,
},
{
name: 'Cat #3',
breed: 'Breed #3',
age: 2,
},
]),
create: jest
.fn()
.mockImplementation((createCatDto: CreateCatDto) =>
Promise.resolve({ _id: '1', ...createCatDto }),
),
},
},
],
}).compile();

controller = module.get(CatsController);
service = module.get(CatsService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('create()', () => {
it('should create a new cat', async () => {
const createCatDto: CreateCatDto = {
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
};

expect(controller.create(createCatDto)).resolves.toEqual({
_id: '1',
...createCatDto,
});
});
});

describe('findAll()', () => {
it('should get an array of cats', () => {
expect(controller.findAll()).resolves.toEqual([
{
name: 'Cat #1',
breed: 'Bread #1',
age: 4,
},
{
name: 'Cat #2',
breed: 'Breed #2',
age: 3,
},
{
name: 'Cat #3',
breed: 'Breed #3',
age: 2,
},
]);
});
});
});
2 changes: 1 addition & 1 deletion sample/14-mongoose-base/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class CatsController {

@Post()
async create(@Body() createCatDto: CreateCatDto) {
this.catsService.create(createCatDto);
return this.catsService.create(createCatDto);
}

@Get()
Expand Down
78 changes: 78 additions & 0 deletions sample/14-mongoose-base/src/cats/cats.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CatsService } from './cats.service';
import { Cat } from './interfaces/cat.interface';
import { Model } from 'mongoose';

const mockCat = {
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
};

const catsArray = [
{
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
},
{
name: 'Cat #2',
breed: 'Breed #2',
age: 2,
},
];

describe('CatService', () => {
let service: CatsService;
let model: Model<Cat>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
CatsService,
{
provide: 'CAT_MODEL',
useValue: {
new: jest.fn().mockResolvedValue(mockCat),
constructor: jest.fn().mockResolvedValue(mockCat),
find: jest.fn(),
create: jest.fn(),
save: jest.fn(),
exec: jest.fn(),
},
},
],
}).compile();

service = module.get(CatsService);
model = module.get<Model<Cat>>('CAT_MODEL');
});

it('should be defined', () => {
expect(service).toBeDefined();
});

it('should return all cats', async () => {
jest.spyOn(model, 'find').mockReturnValue({
exec: jest.fn().mockResolvedValueOnce(catsArray),
} as any);
const cats = await service.findAll();
expect(cats).toEqual(catsArray);
});

it('should insert a new cat', async () => {
jest.spyOn(model, 'create').mockImplementationOnce(() =>
Promise.resolve({
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
}),
);
const newCat = await service.create({
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
});
expect(newCat).toEqual(mockCat);
});
});
4 changes: 2 additions & 2 deletions sample/14-mongoose-base/src/cats/cats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export class CatsService {
constructor(@Inject('CAT_MODEL') private readonly catModel: Model<Cat>) {}

async create(createCatDto: CreateCatDto): Promise<Cat> {
const createdCat = new this.catModel(createCatDto);
return createdCat.save();
const createdCat = this.catModel.create(createCatDto);
return createdCat;
}

async findAll(): Promise<Cat[]> {
Expand Down

0 comments on commit 1394305

Please sign in to comment.