1
1
import { HttpService } from '@nestjs/axios' ;
2
- import { Test , TestingModule } from '@nestjs/testing' ;
3
- import { CatsService } from './cats.service' ;
4
- import { of } from 'rxjs' ;
2
+ import { Test } from '@nestjs/testing' ;
5
3
import { AxiosResponse } from 'axios' ;
4
+ import { of } from 'rxjs' ;
5
+ import { CatsService } from './cats.service' ;
6
+ import { CreateCatDto } from './dto/create-cat.dto' ;
6
7
import { UpdateCatDto } from './dto/update-cat.dto' ;
8
+ import { TCat } from './entities/cat.entity' ;
7
9
8
10
describe ( 'CatsService' , ( ) => {
9
11
let service : CatsService ;
10
12
let httpService : HttpService ;
11
13
12
14
beforeEach ( async ( ) => {
13
- const module : TestingModule = await Test . createTestingModule ( {
15
+ const module = await Test . createTestingModule ( {
14
16
providers : [
15
17
CatsService ,
16
18
{
@@ -26,31 +28,31 @@ describe('CatsService', () => {
26
28
] ,
27
29
} ) . compile ( ) ;
28
30
29
- service = module . get < CatsService > ( CatsService ) ;
30
- httpService = module . get < HttpService > ( HttpService ) ;
31
+ service = module . get ( CatsService ) ;
32
+ httpService = module . get ( HttpService ) ;
31
33
} ) ;
32
34
33
35
it ( 'should be defined' , ( ) => {
34
36
expect ( service ) . toBeDefined ( ) ;
35
37
} ) ;
36
38
37
39
it ( 'should return all cats' , ( ) => {
38
- const data = [
40
+ const data : TCat [ ] = [
39
41
{
40
42
name : 'cat #1' ,
41
- age : '10' ,
43
+ age : 10 ,
42
44
breed : 'Russian' ,
43
45
id : 1 ,
44
46
} ,
45
47
{
46
48
name : 'cat #2' ,
47
- age : '5' ,
49
+ age : 5 ,
48
50
breed : 'Russian' ,
49
51
id : 2 ,
50
52
} ,
51
53
] ;
52
54
53
- const response : AxiosResponse < any > = {
55
+ const response : AxiosResponse < TCat [ ] > = {
54
56
data,
55
57
headers : { } ,
56
58
config : { url : 'http://localhost:3000/mockUrl' } ,
@@ -63,13 +65,13 @@ describe('CatsService', () => {
63
65
data : [
64
66
{
65
67
name : 'cat #1' ,
66
- age : '10' ,
68
+ age : 10 ,
67
69
breed : 'Russian' ,
68
70
id : 1 ,
69
71
} ,
70
72
{
71
73
name : 'cat #2' ,
72
- age : '5' ,
74
+ age : 5 ,
73
75
breed : 'Russian' ,
74
76
id : 2 ,
75
77
} ,
@@ -78,7 +80,7 @@ describe('CatsService', () => {
78
80
config : { url : 'http://localhost:3000/mockUrl' } ,
79
81
status : 200 ,
80
82
statusText : 'OK' ,
81
- } as any ) ,
83
+ } ) ,
82
84
) ;
83
85
84
86
service . findAll ( ) . subscribe ( ( res ) => {
@@ -87,14 +89,14 @@ describe('CatsService', () => {
87
89
} ) ;
88
90
89
91
it ( 'should return one cat' , ( ) => {
90
- const data = {
92
+ const data : TCat = {
91
93
name : 'cat #1' ,
92
- age : '10' ,
94
+ age : 10 ,
93
95
breed : 'Russian' ,
94
96
id : 5 ,
95
97
} ;
96
98
97
- const response : AxiosResponse < any > = {
99
+ const response : AxiosResponse < TCat > = {
98
100
data,
99
101
headers : { } ,
100
102
config : { url : 'http://localhost:3000/mockUrl/1' } ,
@@ -106,15 +108,15 @@ describe('CatsService', () => {
106
108
of ( {
107
109
data : {
108
110
name : 'cat #1' ,
109
- age : '10' ,
111
+ age : 10 ,
110
112
breed : 'Russian' ,
111
113
id : 5 ,
112
114
} ,
113
115
headers : { } ,
114
116
config : { url : 'http://localhost:3000/mockUrl/1' } ,
115
117
status : 200 ,
116
118
statusText : 'OK' ,
117
- } ) as any ,
119
+ } ) ,
118
120
) ;
119
121
120
122
service . findOne ( 5 ) . subscribe ( ( res ) => {
@@ -123,20 +125,20 @@ describe('CatsService', () => {
123
125
} ) ;
124
126
125
127
it ( 'should return a new cat' , ( ) => {
126
- const data = {
128
+ const data : TCat = {
127
129
name : 'cat #1' ,
128
130
age : 10 ,
129
131
breed : 'Russian' ,
130
132
id : 5 ,
131
133
} ;
132
134
133
- let createCatDto : any = {
135
+ let createCatDto : CreateCatDto = {
134
136
name : 'cat #1' ,
135
137
age : 10 ,
136
138
breed : 'Russian' ,
137
139
} ;
138
140
139
- const response : AxiosResponse < any > = {
141
+ const response : AxiosResponse < TCat > = {
140
142
data,
141
143
headers : { } ,
142
144
config : { url : 'http://localhost:3000/mockUrl' } ,
@@ -167,28 +169,27 @@ describe('CatsService', () => {
167
169
id : 5 ,
168
170
} ;
169
171
170
- const response : AxiosResponse < any > = {
172
+ const response : AxiosResponse < UpdateCatDto > = {
171
173
data,
172
174
headers : { } ,
173
175
config : { url : 'http://localhost:3000/mockUrl/5' } ,
174
176
status : 200 ,
175
177
statusText : 'OK' ,
176
178
} ;
177
179
178
- jest . spyOn ( httpService , 'put' ) . mockImplementation (
179
- ( ) =>
180
- of ( {
181
- data : {
182
- name : 'cat #1' ,
183
- age : 10 ,
184
- breed : 'Russian' ,
185
- id : 5 ,
186
- } ,
187
- headers : { } ,
188
- config : { url : 'http://localhost:3000/mockUrl/5' } ,
189
- status : 200 ,
190
- statusText : 'OK' ,
191
- } ) as any ,
180
+ jest . spyOn ( httpService , 'put' ) . mockImplementation ( ( ) =>
181
+ of ( {
182
+ data : {
183
+ name : 'cat #1' ,
184
+ age : 10 ,
185
+ breed : 'Russian' ,
186
+ id : 5 ,
187
+ } ,
188
+ headers : { } ,
189
+ config : { url : 'http://localhost:3000/mockUrl/5' } ,
190
+ status : 200 ,
191
+ statusText : 'OK' ,
192
+ } ) ,
192
193
) ;
193
194
194
195
service . update ( 5 , data ) . subscribe ( ( res ) => {
@@ -197,14 +198,14 @@ describe('CatsService', () => {
197
198
} ) ;
198
199
199
200
it ( 'should return remove a cat' , ( ) => {
200
- const data = {
201
+ const data : TCat = {
201
202
name : 'cat #1' ,
202
- age : '10' ,
203
+ age : 10 ,
203
204
breed : 'Russian' ,
204
205
id : 5 ,
205
206
} ;
206
207
207
- const response : AxiosResponse < any > = {
208
+ const response : AxiosResponse < TCat > = {
208
209
data,
209
210
headers : { } ,
210
211
config : { url : 'http://localhost:3000/mockUrl/5' } ,
@@ -216,15 +217,15 @@ describe('CatsService', () => {
216
217
of ( {
217
218
data : {
218
219
name : 'cat #1' ,
219
- age : '10' ,
220
+ age : 10 ,
220
221
breed : 'Russian' ,
221
222
id : 5 ,
222
223
} ,
223
224
headers : { } ,
224
225
config : { url : 'http://localhost:3000/mockUrl/5' } ,
225
226
status : 204 ,
226
227
statusText : 'OK' ,
227
- } ) as any ,
228
+ } ) ,
228
229
) ;
229
230
230
231
service . remove ( 5 ) . subscribe ( ( res ) => {
0 commit comments