-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8402 from Tony133/test/sample-14-mongoose-base
test(sample-14): added unit test and other improvements
- Loading branch information
Showing
7 changed files
with
203 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
### Mongoose base sample | ||
## Mongoose base sample | ||
|
||
### Installation | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters