Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for sample 06-mongoose #13887

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions sample/06-mongoose/package-lock.json

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

2 changes: 1 addition & 1 deletion sample/06-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"@nestjs/common": "10.3.2",
"@nestjs/core": "10.3.2",
"@nestjs/mongoose": "10.0.2",
"@nestjs/mongoose": "10.0.3",
"@nestjs/platform-express": "10.3.8",
"mongoose": "8.0.1",
"reflect-metadata": "0.2.1",
Expand Down
140 changes: 98 additions & 42 deletions sample/06-mongoose/src/cats/cats.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,144 @@ import { Test, TestingModule } from '@nestjs/testing';
import { CatsController } from './cats.controller';
import { CreateCatDto } from './dto/create-cat.dto';
import { CatsService } from './cats.service';
import { Types } from 'mongoose';

const catsServiceMock = {
create: jest.fn(),
findAll: jest.fn(),
findOne: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
};

describe('Cats Controller', () => {
let controller: CatsController;
let service: CatsService;
const createCatDto: CreateCatDto = {
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
};

const mockCat = {
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
_id: 'a id',
};
let service: jest.Mocked<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().mockResolvedValue(createCatDto),
},
useValue: catsServiceMock,
},
],
}).compile();

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

describe('create()', () => {
it('should create a new cat', async () => {
const createSpy = jest
.spyOn(service, 'create')
.mockResolvedValueOnce(mockCat);
const mockedCat = {
_id: new Types.ObjectId(),
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
};
service.create.mockResolvedValueOnce(mockedCat);

const createCatDto: CreateCatDto = {
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
};
const result = await controller.create(createCatDto);

await controller.create(createCatDto);
expect(createSpy).toHaveBeenCalledWith(createCatDto);
expect(result).toEqual(mockedCat);
expect(service.create).toHaveBeenCalledWith(createCatDto);
});
});

describe('findAll()', () => {
it('should return an array of cats', async () => {
expect(controller.findAll()).resolves.toEqual([
const mockedCats = [
{
_id: new Types.ObjectId(),
name: 'Cat #1',
breed: 'Bread #1',
age: 4,
},
{
_id: new Types.ObjectId(),
name: 'Cat #2',
breed: 'Breed #2',
age: 3,
},
{
_id: new Types.ObjectId(),
name: 'Cat #3',
breed: 'Breed #3',
age: 2,
},
]);
];
service.findAll.mockResolvedValueOnce(mockedCats);

const result = await controller.findAll();

expect(result).toEqual(mockedCats);
expect(service.findAll).toHaveBeenCalled();
});
});

describe('findOne()', () => {
it('should return a single cat', async () => {
const mockedCat = {
_id: new Types.ObjectId(),
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
};
service.findOne.mockResolvedValueOnce(mockedCat);

const id = new Types.ObjectId().toString();
const result = await controller.findOne(id);

expect(result).toEqual(mockedCat);
expect(service.findOne).toHaveBeenCalledWith(id);
});
});

describe('update()', () => {
it('should update a single cat', async () => {
const mockedCat = {
_id: new Types.ObjectId(),
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
};
service.update.mockResolvedValueOnce(mockedCat);

const id = new Types.ObjectId().toString();
const updateCatDto: CreateCatDto = {
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
};
const result = await controller.update(id, updateCatDto);

expect(result).toEqual(mockedCat);
expect(service.update).toHaveBeenCalledWith(id, updateCatDto);
});
});

describe('delete()', () => {
it('should delete a single cat', async () => {
const mockedCat = {
_id: new Types.ObjectId(),
name: 'Cat #1',
breed: 'Breed #1',
age: 4,
};
service.delete.mockResolvedValueOnce(mockedCat);

const id = new Types.ObjectId().toString();
const result = await controller.delete(id);

expect(result).toEqual(mockedCat);
expect(service.delete).toHaveBeenCalledWith(id);
});
});
});
8 changes: 7 additions & 1 deletion sample/06-mongoose/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CreateCatDto } from './dto/create-cat.dto';
import { Cat } from './schemas/cat.schema';
import { UpdateCatDto } from './dto/update-cat.dto';

@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}

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

@Get()
Expand All @@ -22,6 +23,11 @@ export class CatsController {
return this.catsService.findOne(id);
}

@Post(':id')
async update(@Param('id') id: string, @Body() updateCatDto: UpdateCatDto) {
return this.catsService.update(id, updateCatDto);
}

@Delete(':id')
async delete(@Param('id') id: string) {
return this.catsService.delete(id);
Expand Down
Loading