Skip to content

Commit d15611e

Browse files
(#129) adiciona testes unitarios
1 parent 2060cf1 commit d15611e

File tree

4 files changed

+251
-2
lines changed

4 files changed

+251
-2
lines changed

src/app.controller.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('AppController', () => {
1919
});
2020

2121
it('should make health check', () => {
22-
expect(controller.heathCheck()).toEqual({
22+
expect(controller.healthCheck()).toEqual({
2323
message: 'GEROcuidadoApiUsuario health check Ok!',
2424
data: {},
2525
});

src/app.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('AppService', () => {
1616
});
1717

1818
it('should make health check', () => {
19-
expect(service.heathCheck()).toEqual({
19+
expect(service.healthCheck()).toEqual({
2020
message: 'GEROcuidadoApiUsuario health check Ok!',
2121
data: {},
2222
});
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { getRepositoryToken } from '@nestjs/typeorm';
3+
import { Filtering } from '../shared/decorators/filtrate.decorator';
4+
import { OrderParams, Ordering } from '../shared/decorators/ordenate.decorator';
5+
import {
6+
Pagination,
7+
PaginationParams,
8+
} from '../shared/decorators/paginate.decorator';
9+
import { Usuario } from './entities/usuario.entity';
10+
import { IUsuarioFilter } from './interfaces/usuario-filter.interface';
11+
import { UsuarioController } from './usuario.controller';
12+
import { UsuarioService } from './usuario.service';
13+
14+
describe('UsuarioController', () => {
15+
let controller: UsuarioController;
16+
let service: UsuarioService;
17+
18+
const userDto = {
19+
nome: 'Henrique',
20+
email: 'hacmelo@gmail.com',
21+
senha: '123',
22+
foto: '1',
23+
admin: false,
24+
};
25+
26+
const user = {
27+
...userDto,
28+
id: 1,
29+
foto: Buffer.from('1'),
30+
};
31+
32+
beforeEach(async () => {
33+
const module: TestingModule = await Test.createTestingModule({
34+
imports: [],
35+
controllers: [UsuarioController],
36+
providers: [
37+
{
38+
provide: UsuarioService,
39+
useValue: {
40+
create: jest.fn(),
41+
findOne: jest.fn(),
42+
remove: jest.fn(),
43+
update: jest.fn(),
44+
findAll: jest.fn(),
45+
},
46+
},
47+
{
48+
provide: getRepositoryToken(Usuario),
49+
useValue: {},
50+
},
51+
],
52+
}).compile();
53+
54+
controller = module.get<UsuarioController>(UsuarioController);
55+
service = module.get<UsuarioService>(UsuarioService);
56+
});
57+
58+
it('should be defined', () => {
59+
expect(controller).toBeDefined();
60+
});
61+
62+
it('should create Usuario', async () => {
63+
jest.spyOn(service, 'create').mockReturnValue(Promise.resolve(user));
64+
65+
const response = await controller.create(userDto);
66+
expect(response.data).toEqual(user);
67+
expect(response.message).toEqual('Salvo com sucesso!');
68+
});
69+
70+
it('should find Usuario', async () => {
71+
jest.spyOn(service, 'findOne').mockReturnValue(Promise.resolve(user));
72+
73+
const response = await controller.findOne({ id: 1 });
74+
expect(response).toEqual(user);
75+
});
76+
77+
it('should remove Usuario', async () => {
78+
jest.spyOn(service, 'remove').mockReturnValue(Promise.resolve(user));
79+
80+
const response = await controller.remove({ id: 1 });
81+
expect(response.data).toEqual(user);
82+
expect(response.message).toEqual('Excluído com sucesso!');
83+
});
84+
85+
it('should update Usuario', async () => {
86+
jest.spyOn(service, 'update').mockReturnValue(Promise.resolve(user));
87+
88+
const response = await controller.update({ id: 1 }, { nome: 'Henrique' });
89+
expect(response.data).toEqual(user);
90+
expect(response.message).toEqual('Atualizado com sucesso!');
91+
});
92+
93+
describe('findAll', () => {
94+
const filter: IUsuarioFilter = {
95+
nome: 'Henrique',
96+
id: 1,
97+
email: 'email@email.com',
98+
};
99+
const filtering = new Filtering<IUsuarioFilter>(JSON.stringify(filter));
100+
101+
const order: OrderParams = {
102+
column: 'id',
103+
dir: 'ASC',
104+
};
105+
const ordering: Ordering = new Ordering(JSON.stringify(order));
106+
107+
const paginate: PaginationParams = {
108+
limit: 10,
109+
offset: 0,
110+
};
111+
const pagination: Pagination = new Pagination(paginate);
112+
113+
it('should findAll Usuario', async () => {
114+
const expected = { data: [user], count: 1, pageSize: 1 };
115+
116+
jest.spyOn(service, 'findAll').mockReturnValue(Promise.resolve(expected));
117+
118+
const { data, count, pageSize } = await controller.findAll(
119+
filtering,
120+
pagination,
121+
ordering,
122+
);
123+
124+
expect(count).toEqual(1);
125+
expect(pageSize).toEqual(1);
126+
expect(data).toEqual([user]);
127+
});
128+
});
129+
});
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { getRepositoryToken } from '@nestjs/typeorm';
3+
import { Repository } from 'typeorm';
4+
import { OrderParams, Ordering } from '../shared/decorators/ordenate.decorator';
5+
import {
6+
Pagination,
7+
PaginationParams,
8+
} from '../shared/decorators/paginate.decorator';
9+
import { Usuario } from './entities/usuario.entity';
10+
import { UsuarioService } from './usuario.service';
11+
12+
describe('UsuarioService', () => {
13+
let service: UsuarioService;
14+
let repository: Repository<Usuario>;
15+
16+
const mockRepository = {
17+
save: jest.fn(),
18+
findOneOrFail: jest.fn(),
19+
remove: jest.fn(),
20+
createQueryBuilder: jest.fn(() => ({
21+
where: jest.fn().mockReturnThis(),
22+
limit: jest.fn().mockReturnThis(),
23+
offset: jest.fn().mockReturnThis(),
24+
orderBy: jest.fn().mockReturnThis(),
25+
getManyAndCount: jest.fn(),
26+
})),
27+
};
28+
29+
beforeEach(async () => {
30+
const module: TestingModule = await Test.createTestingModule({
31+
providers: [
32+
UsuarioService,
33+
{
34+
provide: getRepositoryToken(Usuario),
35+
useValue: mockRepository,
36+
},
37+
],
38+
}).compile();
39+
40+
service = module.get<UsuarioService>(UsuarioService);
41+
repository = module.get<Repository<Usuario>>(getRepositoryToken(Usuario));
42+
});
43+
44+
it('should be defined', () => {
45+
expect(service).toBeDefined();
46+
});
47+
48+
it('should create Usuario', async () => {
49+
const user = { nome: 'Henrique' } as any;
50+
jest.spyOn(repository, 'save').mockReturnValue({ id: 1 } as any);
51+
52+
const created = await service.create(user);
53+
expect(created.id).toEqual(1);
54+
});
55+
56+
it('should find Usuario', async () => {
57+
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ id: 1 } as any);
58+
59+
const found = await service.findOne(1);
60+
expect(found.id).toEqual(1);
61+
});
62+
63+
it('should remove Usuario', async () => {
64+
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ id: 1 } as any);
65+
jest.spyOn(repository, 'remove').mockReturnValue({ id: 1 } as any);
66+
67+
const removed = await service.remove(1);
68+
expect(removed.id).toEqual(1);
69+
});
70+
71+
it('should update Usuario', async () => {
72+
jest.spyOn(repository, 'findOneOrFail').mockReturnValue({ id: 1 } as any);
73+
jest
74+
.spyOn(repository, 'save')
75+
.mockReturnValue({ id: 1, nome: 'Henrique' } as any);
76+
77+
const found = await service.update(1, { nome: 'Henrique' });
78+
expect(found).toEqual({ id: 1, nome: 'Henrique' });
79+
});
80+
81+
describe('findAll', () => {
82+
const usuario = {
83+
id: 1,
84+
nome: 'Henrique',
85+
email: 'email@email.com',
86+
};
87+
88+
const order: OrderParams = {
89+
column: 'id',
90+
dir: 'ASC',
91+
};
92+
const ordering: Ordering = new Ordering(JSON.stringify(order));
93+
94+
const paginate: PaginationParams = {
95+
limit: 10,
96+
offset: 0,
97+
};
98+
const pagination: Pagination = new Pagination(paginate);
99+
100+
it('should findAll Usuario', async () => {
101+
jest.spyOn(repository, 'createQueryBuilder').mockReturnValue({
102+
where: () => ({
103+
limit: () => ({
104+
offset: () => ({
105+
orderBy: () => ({
106+
getManyAndCount: jest
107+
.fn()
108+
.mockResolvedValueOnce([[usuario], 1]),
109+
}),
110+
}),
111+
}),
112+
}),
113+
} as any);
114+
115+
const { data, count } = await service.findAll({}, ordering, pagination);
116+
expect(count).toEqual(1);
117+
expect((data as Usuario[])[0]).toEqual(usuario);
118+
});
119+
});
120+
});

0 commit comments

Comments
 (0)