Skip to content

Commit

Permalink
Merge branch 'test/sample-01-cats-app' of https://github.com/Yansb/nest
Browse files Browse the repository at this point in the history
… into Yansb-test/sample-01-cats-app
  • Loading branch information
kamilmysliwiec committed Nov 20, 2024
2 parents 71e8ace + c032e83 commit f3ec5f2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
22 changes: 21 additions & 1 deletion sample/01-cats-app/src/cats/cats.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,29 @@ describe('CatsController', () => {
name: 'Pixel',
},
];
jest.spyOn(catsService, 'findAll').mockImplementation(() => result);
//@ts-ignore
catsService.cats = result;

expect(await catsController.findAll()).toBe(result);
});
});

describe('create', () => {
it('should add a new cat', async () => {
const cat: Cat = {
age: 2,
breed: 'Bombay',
name: 'Pixel',
};
const expectedCatArray = [cat];

//@ts-ignore
expect(catsService.cats).toStrictEqual([]);

await catsController.create(cat);

//@ts-ignore
expect(catsService.cats).toStrictEqual(expectedCatArray);
});
});
});
48 changes: 48 additions & 0 deletions sample/01-cats-app/src/cats/cats.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Test } from '@nestjs/testing';
import { CatsService } from './cats.service';
import { Cat } from './interfaces/cat.interface';

describe('CatsService', () => {
let catsService: CatsService;

beforeEach(async () => {
const moduleRef = await Test.createTestingModule({
providers: [CatsService],
}).compile();

catsService = moduleRef.get<CatsService>(CatsService);
});

describe('findAll', () => {
it('should return an array of cats', async () => {
const result = [
{
name: 'Frajola',
age: 2,
breed: 'Stray',
},
];
//@ts-ignore
catsService.cats = result;

await expect(catsService.findAll()).resolves.toBe(result);
});
});

describe('create', () => {
it('should add a new cat', async () => {
const cat: Cat = {
name: 'Frajola',
age: 2,
breed: 'Stray',
};
const expectedCatArray = [cat];
//@ts-ignore
expect(catsService.cats).toStrictEqual([]);

await catsService.create(cat);
//@ts-ignore
expect(catsService.cats).toStrictEqual(expectedCatArray);
});
});
});
4 changes: 2 additions & 2 deletions sample/01-cats-app/src/cats/cats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class CatsService {
this.cats.push(cat);
}

findAll(): Cat[] {
return this.cats;
findAll(): Promise<Cat[]> {
return Promise.resolve(this.cats);
}
}

0 comments on commit f3ec5f2

Please sign in to comment.