forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
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 nestjs#8037 from Tony133/test/sample-13-unit-test
tests(sample-13): added unit tests
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
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
68 changes: 68 additions & 0 deletions
68
sample/13-mongo-typeorm/src/photo/photo.controller.spec.ts
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,68 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { PhotoController } from './photo.controller'; | ||
import { PhotoService } from './photo.service'; | ||
|
||
describe('Photo Controller', () => { | ||
let controller: PhotoController; | ||
let service: PhotoService; | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [PhotoController], | ||
providers: [ | ||
{ | ||
provide: PhotoService, | ||
useValue: { | ||
findAll: jest.fn().mockResolvedValue([ | ||
{ | ||
name: 'Photo #1', | ||
description: 'Description #1', | ||
filename: 'Filename #1', | ||
isPublish: true, | ||
}, | ||
{ | ||
name: 'Photo #2', | ||
description: 'Description #2', | ||
filename: 'Filename #2', | ||
isPublish: true, | ||
}, | ||
{ | ||
name: 'Photo #3', | ||
description: 'Description #3', | ||
filename: 'Filename #3', | ||
isPublish: false, | ||
}, | ||
]), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
controller = module.get<PhotoController>(PhotoController); | ||
service = module.get<PhotoService>(PhotoService); | ||
}); | ||
|
||
describe('findAll()', () => { | ||
it('should return an array of photos', () => { | ||
expect(controller.findAll()).resolves.toEqual([ | ||
{ | ||
name: 'Photo #1', | ||
description: 'Description #1', | ||
filename: 'Filename #1', | ||
isPublish: true, | ||
}, | ||
{ | ||
name: 'Photo #2', | ||
description: 'Description #2', | ||
filename: 'Filename #2', | ||
isPublish: true, | ||
}, | ||
{ | ||
name: 'Photo #3', | ||
description: 'Description #3', | ||
filename: 'Filename #3', | ||
isPublish: false, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
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,51 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { Photo } from './photo.entity'; | ||
import { PhotoService } from './photo.service'; | ||
|
||
describe('CatService', () => { | ||
let service: PhotoService; | ||
let repository: Repository<Photo>; | ||
|
||
const photosArray = [ | ||
{ | ||
name: 'Photo #1', | ||
description: 'Description #1', | ||
filename: 'Filename #1', | ||
isPublish: true, | ||
}, | ||
{ | ||
name: 'Photo #2', | ||
description: 'Description #2', | ||
filename: 'Filename #2', | ||
isPublish: true, | ||
}, | ||
]; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
PhotoService, | ||
{ | ||
provide: getRepositoryToken(Photo), | ||
useValue: { | ||
find: jest.fn().mockResolvedValue(photosArray), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<PhotoService>(PhotoService); | ||
repository = module.get<Repository<Photo>>(getRepositoryToken(Photo)); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
it('should return an array of photos', async () => { | ||
const photos = await service.findAll(); | ||
expect(photos).toEqual(photosArray); | ||
}); | ||
}); |