Skip to content

Commit

Permalink
refactor(http-sample): generic types and test cases (#2006)
Browse files Browse the repository at this point in the history
- generic type refactor and removed any annotations
- test cases fixed for data types
  • Loading branch information
jmcdo29 committed Oct 14, 2023
2 parents 90db1c6 + 3243a8d commit 1f5a6a5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 49 deletions.
2 changes: 1 addition & 1 deletion apps/http-sample/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('AppController', () => {
providers: [AppService],
}).compile();

appController = app.get<AppController>(AppController);
appController = app.get(AppController);
});

describe('root', () => {
Expand Down
19 changes: 12 additions & 7 deletions apps/http-sample/src/cats/cats.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Test, TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { of } from 'rxjs';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import { UpdateCatDto } from './dto/update-cat.dto';

const testCat = { id: 1, name: 'Test cat', age: 5, breed: 'Russian Blue' };
const testCatUpdate = {
const testCat: UpdateCatDto = {
id: 1,
name: 'Test cat',
age: 5,
breed: 'Russian Blue',
};

const testCatUpdate: UpdateCatDto = {
id: 1,
name: 'Test cat Update',
age: 5,
Expand All @@ -13,10 +20,9 @@ const testCatUpdate = {

describe('CatsController', () => {
let controller: CatsController;
let service: CatsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
controllers: [CatsController],
providers: [
{
Expand All @@ -32,8 +38,7 @@ describe('CatsController', () => {
],
}).compile();

controller = module.get<CatsController>(CatsController);
service = module.get<CatsService>(CatsService);
controller = module.get(CatsController);
});

it('should be defined', () => {
Expand Down
83 changes: 42 additions & 41 deletions apps/http-sample/src/cats/cats.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { HttpService } from '@nestjs/axios';
import { Test, TestingModule } from '@nestjs/testing';
import { CatsService } from './cats.service';
import { of } from 'rxjs';
import { Test } from '@nestjs/testing';
import { AxiosResponse } from 'axios';
import { of } from 'rxjs';
import { CatsService } from './cats.service';
import { CreateCatDto } from './dto/create-cat.dto';
import { UpdateCatDto } from './dto/update-cat.dto';
import { TCat } from './entities/cat.entity';

describe('CatsService', () => {
let service: CatsService;
let httpService: HttpService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
const module = await Test.createTestingModule({
providers: [
CatsService,
{
Expand All @@ -26,31 +28,31 @@ describe('CatsService', () => {
],
}).compile();

service = module.get<CatsService>(CatsService);
httpService = module.get<HttpService>(HttpService);
service = module.get(CatsService);
httpService = module.get(HttpService);
});

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

it('should return all cats', () => {
const data = [
const data: TCat[] = [
{
name: 'cat #1',
age: '10',
age: 10,
breed: 'Russian',
id: 1,
},
{
name: 'cat #2',
age: '5',
age: 5,
breed: 'Russian',
id: 2,
},
];

const response: AxiosResponse<any> = {
const response: AxiosResponse<TCat[]> = {
data,
headers: {},
config: { url: 'http://localhost:3000/mockUrl' },
Expand All @@ -63,13 +65,13 @@ describe('CatsService', () => {
data: [
{
name: 'cat #1',
age: '10',
age: 10,
breed: 'Russian',
id: 1,
},
{
name: 'cat #2',
age: '5',
age: 5,
breed: 'Russian',
id: 2,
},
Expand All @@ -78,7 +80,7 @@ describe('CatsService', () => {
config: { url: 'http://localhost:3000/mockUrl' },
status: 200,
statusText: 'OK',
} as any),
}),
);

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

it('should return one cat', () => {
const data = {
const data: TCat = {
name: 'cat #1',
age: '10',
age: 10,
breed: 'Russian',
id: 5,
};

const response: AxiosResponse<any> = {
const response: AxiosResponse<TCat> = {
data,
headers: {},
config: { url: 'http://localhost:3000/mockUrl/1' },
Expand All @@ -106,15 +108,15 @@ describe('CatsService', () => {
of({
data: {
name: 'cat #1',
age: '10',
age: 10,
breed: 'Russian',
id: 5,
},
headers: {},
config: { url: 'http://localhost:3000/mockUrl/1' },
status: 200,
statusText: 'OK',
}) as any,
}),
);

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

it('should return a new cat', () => {
const data = {
const data: TCat = {
name: 'cat #1',
age: 10,
breed: 'Russian',
id: 5,
};

let createCatDto: any = {
let createCatDto: CreateCatDto = {
name: 'cat #1',
age: 10,
breed: 'Russian',
};

const response: AxiosResponse<any> = {
const response: AxiosResponse<TCat> = {
data,
headers: {},
config: { url: 'http://localhost:3000/mockUrl' },
Expand Down Expand Up @@ -167,28 +169,27 @@ describe('CatsService', () => {
id: 5,
};

const response: AxiosResponse<any> = {
const response: AxiosResponse<UpdateCatDto> = {
data,
headers: {},
config: { url: 'http://localhost:3000/mockUrl/5' },
status: 200,
statusText: 'OK',
};

jest.spyOn(httpService, 'put').mockImplementation(
() =>
of({
data: {
name: 'cat #1',
age: 10,
breed: 'Russian',
id: 5,
},
headers: {},
config: { url: 'http://localhost:3000/mockUrl/5' },
status: 200,
statusText: 'OK',
}) as any,
jest.spyOn(httpService, 'put').mockImplementation(() =>
of({
data: {
name: 'cat #1',
age: 10,
breed: 'Russian',
id: 5,
},
headers: {},
config: { url: 'http://localhost:3000/mockUrl/5' },
status: 200,
statusText: 'OK',
}),
);

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

it('should return remove a cat', () => {
const data = {
const data: TCat = {
name: 'cat #1',
age: '10',
age: 10,
breed: 'Russian',
id: 5,
};

const response: AxiosResponse<any> = {
const response: AxiosResponse<TCat> = {
data,
headers: {},
config: { url: 'http://localhost:3000/mockUrl/5' },
Expand All @@ -216,15 +217,15 @@ describe('CatsService', () => {
of({
data: {
name: 'cat #1',
age: '10',
age: 10,
breed: 'Russian',
id: 5,
},
headers: {},
config: { url: 'http://localhost:3000/mockUrl/5' },
status: 204,
statusText: 'OK',
}) as any,
}),
);

service.remove(5).subscribe((res) => {
Expand Down
2 changes: 2 additions & 0 deletions apps/http-sample/src/cats/entities/cat.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export class Cat {
age: number;
breed: string;
}

export type TCat = Cat & { id: number };

0 comments on commit 1f5a6a5

Please sign in to comment.