Skip to content

Commit 1f5a6a5

Browse files
authored
refactor(http-sample): generic types and test cases (#2006)
- generic type refactor and removed any annotations - test cases fixed for data types
2 parents 90db1c6 + 3243a8d commit 1f5a6a5

File tree

4 files changed

+57
-49
lines changed

4 files changed

+57
-49
lines changed

apps/http-sample/src/app.controller.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('AppController', () => {
1111
providers: [AppService],
1212
}).compile();
1313

14-
appController = app.get<AppController>(AppController);
14+
appController = app.get(AppController);
1515
});
1616

1717
describe('root', () => {

apps/http-sample/src/cats/cats.controller.spec.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { Test, TestingModule } from '@nestjs/testing';
1+
import { Test } from '@nestjs/testing';
22
import { of } from 'rxjs';
33
import { CatsController } from './cats.controller';
44
import { CatsService } from './cats.service';
5+
import { UpdateCatDto } from './dto/update-cat.dto';
56

6-
const testCat = { id: 1, name: 'Test cat', age: 5, breed: 'Russian Blue' };
7-
const testCatUpdate = {
7+
const testCat: UpdateCatDto = {
8+
id: 1,
9+
name: 'Test cat',
10+
age: 5,
11+
breed: 'Russian Blue',
12+
};
13+
14+
const testCatUpdate: UpdateCatDto = {
815
id: 1,
916
name: 'Test cat Update',
1017
age: 5,
@@ -13,10 +20,9 @@ const testCatUpdate = {
1320

1421
describe('CatsController', () => {
1522
let controller: CatsController;
16-
let service: CatsService;
1723

1824
beforeEach(async () => {
19-
const module: TestingModule = await Test.createTestingModule({
25+
const module = await Test.createTestingModule({
2026
controllers: [CatsController],
2127
providers: [
2228
{
@@ -32,8 +38,7 @@ describe('CatsController', () => {
3238
],
3339
}).compile();
3440

35-
controller = module.get<CatsController>(CatsController);
36-
service = module.get<CatsService>(CatsService);
41+
controller = module.get(CatsController);
3742
});
3843

3944
it('should be defined', () => {

apps/http-sample/src/cats/cats.service.spec.ts

+42-41
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
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';
53
import { AxiosResponse } from 'axios';
4+
import { of } from 'rxjs';
5+
import { CatsService } from './cats.service';
6+
import { CreateCatDto } from './dto/create-cat.dto';
67
import { UpdateCatDto } from './dto/update-cat.dto';
8+
import { TCat } from './entities/cat.entity';
79

810
describe('CatsService', () => {
911
let service: CatsService;
1012
let httpService: HttpService;
1113

1214
beforeEach(async () => {
13-
const module: TestingModule = await Test.createTestingModule({
15+
const module = await Test.createTestingModule({
1416
providers: [
1517
CatsService,
1618
{
@@ -26,31 +28,31 @@ describe('CatsService', () => {
2628
],
2729
}).compile();
2830

29-
service = module.get<CatsService>(CatsService);
30-
httpService = module.get<HttpService>(HttpService);
31+
service = module.get(CatsService);
32+
httpService = module.get(HttpService);
3133
});
3234

3335
it('should be defined', () => {
3436
expect(service).toBeDefined();
3537
});
3638

3739
it('should return all cats', () => {
38-
const data = [
40+
const data: TCat[] = [
3941
{
4042
name: 'cat #1',
41-
age: '10',
43+
age: 10,
4244
breed: 'Russian',
4345
id: 1,
4446
},
4547
{
4648
name: 'cat #2',
47-
age: '5',
49+
age: 5,
4850
breed: 'Russian',
4951
id: 2,
5052
},
5153
];
5254

53-
const response: AxiosResponse<any> = {
55+
const response: AxiosResponse<TCat[]> = {
5456
data,
5557
headers: {},
5658
config: { url: 'http://localhost:3000/mockUrl' },
@@ -63,13 +65,13 @@ describe('CatsService', () => {
6365
data: [
6466
{
6567
name: 'cat #1',
66-
age: '10',
68+
age: 10,
6769
breed: 'Russian',
6870
id: 1,
6971
},
7072
{
7173
name: 'cat #2',
72-
age: '5',
74+
age: 5,
7375
breed: 'Russian',
7476
id: 2,
7577
},
@@ -78,7 +80,7 @@ describe('CatsService', () => {
7880
config: { url: 'http://localhost:3000/mockUrl' },
7981
status: 200,
8082
statusText: 'OK',
81-
} as any),
83+
}),
8284
);
8385

8486
service.findAll().subscribe((res) => {
@@ -87,14 +89,14 @@ describe('CatsService', () => {
8789
});
8890

8991
it('should return one cat', () => {
90-
const data = {
92+
const data: TCat = {
9193
name: 'cat #1',
92-
age: '10',
94+
age: 10,
9395
breed: 'Russian',
9496
id: 5,
9597
};
9698

97-
const response: AxiosResponse<any> = {
99+
const response: AxiosResponse<TCat> = {
98100
data,
99101
headers: {},
100102
config: { url: 'http://localhost:3000/mockUrl/1' },
@@ -106,15 +108,15 @@ describe('CatsService', () => {
106108
of({
107109
data: {
108110
name: 'cat #1',
109-
age: '10',
111+
age: 10,
110112
breed: 'Russian',
111113
id: 5,
112114
},
113115
headers: {},
114116
config: { url: 'http://localhost:3000/mockUrl/1' },
115117
status: 200,
116118
statusText: 'OK',
117-
}) as any,
119+
}),
118120
);
119121

120122
service.findOne(5).subscribe((res) => {
@@ -123,20 +125,20 @@ describe('CatsService', () => {
123125
});
124126

125127
it('should return a new cat', () => {
126-
const data = {
128+
const data: TCat = {
127129
name: 'cat #1',
128130
age: 10,
129131
breed: 'Russian',
130132
id: 5,
131133
};
132134

133-
let createCatDto: any = {
135+
let createCatDto: CreateCatDto = {
134136
name: 'cat #1',
135137
age: 10,
136138
breed: 'Russian',
137139
};
138140

139-
const response: AxiosResponse<any> = {
141+
const response: AxiosResponse<TCat> = {
140142
data,
141143
headers: {},
142144
config: { url: 'http://localhost:3000/mockUrl' },
@@ -167,28 +169,27 @@ describe('CatsService', () => {
167169
id: 5,
168170
};
169171

170-
const response: AxiosResponse<any> = {
172+
const response: AxiosResponse<UpdateCatDto> = {
171173
data,
172174
headers: {},
173175
config: { url: 'http://localhost:3000/mockUrl/5' },
174176
status: 200,
175177
statusText: 'OK',
176178
};
177179

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+
}),
192193
);
193194

194195
service.update(5, data).subscribe((res) => {
@@ -197,14 +198,14 @@ describe('CatsService', () => {
197198
});
198199

199200
it('should return remove a cat', () => {
200-
const data = {
201+
const data: TCat = {
201202
name: 'cat #1',
202-
age: '10',
203+
age: 10,
203204
breed: 'Russian',
204205
id: 5,
205206
};
206207

207-
const response: AxiosResponse<any> = {
208+
const response: AxiosResponse<TCat> = {
208209
data,
209210
headers: {},
210211
config: { url: 'http://localhost:3000/mockUrl/5' },
@@ -216,15 +217,15 @@ describe('CatsService', () => {
216217
of({
217218
data: {
218219
name: 'cat #1',
219-
age: '10',
220+
age: 10,
220221
breed: 'Russian',
221222
id: 5,
222223
},
223224
headers: {},
224225
config: { url: 'http://localhost:3000/mockUrl/5' },
225226
status: 204,
226227
statusText: 'OK',
227-
}) as any,
228+
}),
228229
);
229230

230231
service.remove(5).subscribe((res) => {

apps/http-sample/src/cats/entities/cat.entity.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export class Cat {
33
age: number;
44
breed: string;
55
}
6+
7+
export type TCat = Cat & { id: number };

0 commit comments

Comments
 (0)