@@ -2,14 +2,9 @@ import { Test, TestingModule } from '@nestjs/testing';
2
2
import { VehicleBrandsService } from './vehicle-brands.service' ;
3
3
import { getRepositoryToken } from '@nestjs/typeorm' ;
4
4
import { Repository } from 'typeorm' ;
5
- import { VehicleBrand } from './entities/vehicle-brand.entity' ;
6
5
import { HttpException , HttpStatus } from '@nestjs/common' ;
7
-
8
- const mockVehicleBrandRepository = {
9
- find : jest . fn ( ) ,
10
- findOneBy : jest . fn ( ) ,
11
- save : jest . fn ( ) ,
12
- } ;
6
+ import { UpdateVehicleBrandDto } from './dto/update-vehicle-brand.dto' ;
7
+ import { VehicleBrand } from './entities/vehicle-brand.entity' ;
13
8
14
9
describe ( 'VehicleBrandsService' , ( ) => {
15
10
let service : VehicleBrandsService ;
@@ -21,74 +16,77 @@ describe('VehicleBrandsService', () => {
21
16
VehicleBrandsService ,
22
17
{
23
18
provide : getRepositoryToken ( VehicleBrand ) ,
24
- useValue : mockVehicleBrandRepository ,
19
+ useClass : Repository ,
25
20
} ,
26
21
] ,
27
22
} ) . compile ( ) ;
28
23
29
24
service = module . get < VehicleBrandsService > ( VehicleBrandsService ) ;
30
- repository = module . get < Repository < VehicleBrand > > (
31
- getRepositoryToken ( VehicleBrand )
32
- ) ;
25
+ repository = module . get < Repository < VehicleBrand > > ( getRepositoryToken ( VehicleBrand ) ) ;
33
26
} ) ;
34
27
35
- it ( 'should be defined' , ( ) => {
36
- expect ( service ) . toBeDefined ( ) ;
37
- } ) ;
38
-
39
- describe ( 'create' , ( ) => {
40
- it ( 'should throw an error if vehicle brand already exists' , async ( ) => {
41
- const createVehicleBrandDto = { name : 'Toyota' } ;
42
- jest
43
- . spyOn ( service , 'findByName' )
44
- . mockResolvedValue ( { id : 1 , name : 'Toyota' } as VehicleBrand ) ;
28
+ describe ( 'update' , ( ) => {
29
+ it ( 'should throw conflict exception if vehicle brand name already exists' , async ( ) => {
30
+ const updateVehicleBrandDto : UpdateVehicleBrandDto = { name : 'Toyota' } ;
31
+ jest . spyOn ( service , 'findByName' ) . mockResolvedValue ( new VehicleBrand ( ) ) ;
45
32
46
- await expect ( service . create ( createVehicleBrandDto ) ) . rejects . toThrow (
33
+ await expect ( service . update ( 1 , updateVehicleBrandDto ) ) . rejects . toThrow (
47
34
new HttpException (
48
- {
49
- status : HttpStatus . CONFLICT ,
50
- error : 'Vehicle brand already exists' ,
51
- } ,
35
+ { status : HttpStatus . CONFLICT , error : 'Vehicle brand already exists' } ,
52
36
HttpStatus . CONFLICT
53
37
)
54
38
) ;
55
39
} ) ;
56
40
57
- it ( 'should save a new vehicle brand' , async ( ) => {
58
- const createVehicleBrandDto = { name : 'Toyota' } ;
59
- jest . spyOn ( service , 'findByName' ) . mockResolvedValue ( null ) ;
60
- mockVehicleBrandRepository . save . mockResolvedValue ( createVehicleBrandDto ) ;
41
+ it ( 'should throw not found exception if vehicle brand is not found by ID ' , async ( ) => {
42
+ const updateVehicleBrandDto : UpdateVehicleBrandDto = { name : 'Toyota' } ;
43
+ jest . spyOn ( service , 'findByName' ) . mockRejectedValue ( { status : HttpStatus . NOT_FOUND } ) ;
44
+ jest . spyOn ( repository , 'findOneBy' ) . mockResolvedValue ( null ) ;
61
45
62
- expect ( await service . create ( createVehicleBrandDto ) ) . toEqual (
63
- createVehicleBrandDto
64
- ) ;
65
- expect ( mockVehicleBrandRepository . save ) . toHaveBeenCalledWith (
66
- createVehicleBrandDto
46
+ await expect ( service . update ( 1 , updateVehicleBrandDto ) ) . rejects . toThrow (
47
+ new HttpException (
48
+ { status : HttpStatus . NOT_FOUND , error : 'Vehicle brand not found' } ,
49
+ HttpStatus . NOT_FOUND
50
+ )
67
51
) ;
68
52
} ) ;
69
- } ) ;
70
53
71
- describe ( 'findAll' , ( ) => {
72
- it ( 'should return an array of vehicle brands' , async ( ) => {
73
- const result = [ { id : 1 , name : 'Toyota' } ] ;
74
- mockVehicleBrandRepository . find . mockResolvedValue ( result ) ;
54
+ it ( 'should update and return the vehicle brand' , async ( ) => {
55
+ const updateVehicleBrandDto : UpdateVehicleBrandDto = { name : 'Toyota' } ;
56
+ const vehicleBrand = new VehicleBrand ( ) ;
57
+ jest . spyOn ( service , 'findByName' ) . mockRejectedValue ( { status : HttpStatus . NOT_FOUND } ) ;
58
+ jest . spyOn ( repository , 'findOneBy' ) . mockResolvedValue ( vehicleBrand ) ;
59
+ jest . spyOn ( repository , 'save' ) . mockResolvedValue ( vehicleBrand ) ;
75
60
76
- expect ( await service . findAll ( ) ) . toEqual ( result ) ;
61
+ const result = await service . update ( 1 , updateVehicleBrandDto ) ;
62
+ expect ( result ) . toBe ( vehicleBrand ) ;
63
+ expect ( repository . save ) . toHaveBeenCalledWith ( { id : 1 , name : 'Toyota' } ) ;
64
+ expect ( repository . findOneBy ) . toHaveBeenCalledWith ( { id : 1 } ) ;
77
65
} ) ;
78
66
} ) ;
79
67
80
- describe ( 'findOne' , ( ) => {
81
- it ( 'should return a vehicle brand by id' , async ( ) => {
82
- const result = { id : 1 , name : 'Toyota' } ;
83
- mockVehicleBrandRepository . findOneBy . mockResolvedValue ( result ) ;
68
+ describe ( 'remove' , ( ) => {
69
+ it ( 'should throw not found exception if vehicle brand is not found by ID' , async ( ) => {
70
+ jest . spyOn ( repository , 'findOneBy' ) . mockResolvedValue ( null ) ;
84
71
85
- expect ( await service . findOne ( 1 ) ) . toEqual ( result ) ;
72
+ await expect ( service . remove ( 1 ) ) . rejects . toThrow (
73
+ new HttpException (
74
+ { status : HttpStatus . NOT_FOUND , error : 'Vehicle brand not found' } ,
75
+ HttpStatus . NOT_FOUND
76
+ )
77
+ ) ;
86
78
} ) ;
87
79
88
- it ( 'should return null if vehicle brand not found' , async ( ) => {
89
- mockVehicleBrandRepository . findOneBy . mockResolvedValue ( null ) ;
80
+ it ( 'should remove the vehicle brand' , async ( ) => {
81
+ const vehicleBrand = new VehicleBrand ( ) ;
82
+ const deleteResult = { affected : 1 , raw : [ ] } ;
83
+ jest . spyOn ( repository , 'findOneBy' ) . mockResolvedValue ( vehicleBrand ) ;
84
+ jest . spyOn ( repository , 'delete' ) . mockResolvedValue ( deleteResult ) ;
90
85
91
- expect ( await service . findOne ( 1 ) ) . toBeNull ( ) ;
86
+ const result = await service . remove ( 1 ) ;
87
+ expect ( result ) . toBe ( deleteResult ) ;
88
+ expect ( repository . findOneBy ) . toHaveBeenCalledWith ( { id : 1 } ) ;
89
+ expect ( repository . delete ) . toHaveBeenCalledWith ( 1 ) ;
92
90
} ) ;
93
91
} ) ;
94
- } ) ;
92
+ } ) ;
0 commit comments