Skip to content

Commit aad0d8e

Browse files
committed
✅ add controllers unit tests
1 parent 9416906 commit aad0d8e

7 files changed

+453
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { CardController } from './card.controller';
2+
3+
const mockFacade = {
4+
add: vi.fn(),
5+
update: vi.fn(),
6+
findTransactionsByCardId: vi.fn(),
7+
};
8+
9+
const makeSut = (facade = mockFacade) => {
10+
return new CardController(facade);
11+
};
12+
13+
describe('CardController', () => {
14+
describe('Add', () => {
15+
it('should add card', async () => {
16+
const sut = makeSut();
17+
18+
await sut.add('any_name', 100);
19+
20+
expect(mockFacade.add).toHaveBeenCalledWith({
21+
name: 'any_name',
22+
balance: 100,
23+
});
24+
});
25+
26+
it('should throw when use case throws', async () => {
27+
const facade = {
28+
add: vi.fn().mockRejectedValueOnce(new Error()),
29+
update: vi.fn(),
30+
findTransactionsByCardId: vi.fn(),
31+
};
32+
33+
const sut = makeSut(facade);
34+
35+
await expect(
36+
async () => await sut.add('any_name', 100),
37+
).rejects.toThrow();
38+
});
39+
});
40+
41+
describe('Update', () => {
42+
it('should update card', async () => {
43+
const sut = makeSut();
44+
45+
await sut.update(1, 'any_name', 100);
46+
47+
expect(mockFacade.update).toHaveBeenCalledWith({
48+
id: 1,
49+
name: 'any_name',
50+
balance: 100,
51+
});
52+
});
53+
54+
it('should throw when use case throws', async () => {
55+
const facade = {
56+
add: vi.fn(),
57+
update: vi.fn().mockRejectedValueOnce(new Error()),
58+
findTransactionsByCardId: vi.fn(),
59+
};
60+
61+
const sut = makeSut(facade);
62+
63+
await expect(
64+
async () => await sut.update(1, 'any_name', 100),
65+
).rejects.toThrow();
66+
});
67+
});
68+
69+
describe('FindTransactionsByCardId', () => {
70+
it('should find transactions by card id', async () => {
71+
const sut = makeSut();
72+
73+
await sut.findTransactionsByCardId(1);
74+
75+
expect(mockFacade.findTransactionsByCardId).toHaveBeenCalledWith({
76+
cardId: 1,
77+
});
78+
});
79+
80+
it('should throw when use case throws', async () => {
81+
const facade = {
82+
add: vi.fn(),
83+
update: vi.fn(),
84+
findTransactionsByCardId: vi.fn().mockRejectedValueOnce(new Error()),
85+
};
86+
87+
const sut = makeSut(facade);
88+
89+
await expect(
90+
async () => await sut.findTransactionsByCardId(1),
91+
).rejects.toThrow();
92+
});
93+
});
94+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { HelloController } from './hello.controller';
2+
3+
const makeSut = () => {
4+
return new HelloController();
5+
};
6+
7+
describe('HelloController', () => {
8+
it('should return hello world', () => {
9+
const sut = makeSut();
10+
11+
expect(sut.get()).toEqual({ hello: 'world' });
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
import { StationController } from './station.controller';
2+
3+
const mockFacade = {
4+
add: vi.fn(),
5+
findAll: vi.fn(),
6+
findById: vi.fn(),
7+
update: vi.fn(),
8+
remove: vi.fn(),
9+
removeAll: vi.fn(),
10+
restoreAll: vi.fn(),
11+
};
12+
13+
const makeSut = (facade = mockFacade) => {
14+
return new StationController(facade);
15+
};
16+
17+
describe('StationController', () => {
18+
describe('Add', () => {
19+
it('should add card', async () => {
20+
const sut = makeSut();
21+
22+
await sut.add('any_name', 'any_line');
23+
24+
expect(mockFacade.add).toHaveBeenCalledWith({
25+
name: 'any_name',
26+
line: 'any_line',
27+
});
28+
});
29+
30+
it('should throw when use case throws', async () => {
31+
const facade = {
32+
add: vi.fn().mockRejectedValueOnce(new Error()),
33+
findAll: vi.fn(),
34+
findById: vi.fn(),
35+
update: vi.fn(),
36+
remove: vi.fn(),
37+
removeAll: vi.fn(),
38+
restoreAll: vi.fn(),
39+
};
40+
41+
const sut = makeSut(facade);
42+
43+
await expect(
44+
async () => await sut.add('any_name', 'any_line'),
45+
).rejects.toThrow();
46+
});
47+
});
48+
49+
describe('FindAll', () => {
50+
it('should find all stations', async () => {
51+
const sut = makeSut();
52+
53+
await sut.findAll();
54+
55+
expect(mockFacade.findAll).toHaveBeenCalled();
56+
});
57+
58+
it('should throw when use case throws', async () => {
59+
const facade = {
60+
add: vi.fn(),
61+
findAll: vi.fn().mockRejectedValueOnce(new Error()),
62+
findById: vi.fn(),
63+
update: vi.fn(),
64+
remove: vi.fn(),
65+
removeAll: vi.fn(),
66+
restoreAll: vi.fn(),
67+
};
68+
69+
const sut = makeSut(facade);
70+
71+
await expect(async () => await sut.findAll()).rejects.toThrow();
72+
});
73+
});
74+
75+
describe('FindById', () => {
76+
it('should find station by id', async () => {
77+
const sut = makeSut();
78+
79+
await sut.findById(1);
80+
81+
expect(mockFacade.findById).toHaveBeenCalledWith({
82+
id: 1,
83+
});
84+
});
85+
86+
it('should throw when use case throws', async () => {
87+
const facade = {
88+
add: vi.fn(),
89+
findAll: vi.fn(),
90+
findById: vi.fn().mockRejectedValueOnce(new Error()),
91+
update: vi.fn(),
92+
remove: vi.fn(),
93+
removeAll: vi.fn(),
94+
restoreAll: vi.fn(),
95+
};
96+
97+
const sut = makeSut(facade);
98+
99+
await expect(async () => await sut.findById(1)).rejects.toThrow();
100+
});
101+
});
102+
103+
describe('Update', () => {
104+
it('should update card', async () => {
105+
const sut = makeSut();
106+
107+
await sut.update(1, 'any_name', 'any_line');
108+
109+
expect(mockFacade.update).toHaveBeenCalledWith({
110+
id: 1,
111+
name: 'any_name',
112+
line: 'any_line',
113+
});
114+
});
115+
116+
it('should throw when use case throws', async () => {
117+
const facade = {
118+
add: vi.fn(),
119+
findAll: vi.fn(),
120+
findById: vi.fn(),
121+
update: vi.fn().mockRejectedValueOnce(new Error()),
122+
remove: vi.fn(),
123+
removeAll: vi.fn(),
124+
restoreAll: vi.fn(),
125+
};
126+
127+
const sut = makeSut(facade);
128+
129+
await expect(
130+
async () => await sut.update(1, 'any_name', 'any_line'),
131+
).rejects.toThrow();
132+
});
133+
});
134+
135+
describe('Remove', () => {
136+
it('should remove card', async () => {
137+
const sut = makeSut();
138+
139+
await sut.remove(1);
140+
141+
expect(mockFacade.remove).toHaveBeenCalledWith({
142+
id: 1,
143+
});
144+
});
145+
146+
it('should throw when use case throws', async () => {
147+
const facade = {
148+
add: vi.fn(),
149+
findAll: vi.fn(),
150+
findById: vi.fn(),
151+
update: vi.fn(),
152+
remove: vi.fn().mockRejectedValueOnce(new Error()),
153+
removeAll: vi.fn(),
154+
restoreAll: vi.fn(),
155+
};
156+
157+
const sut = makeSut(facade);
158+
159+
await expect(async () => await sut.remove(1)).rejects.toThrow();
160+
});
161+
});
162+
163+
describe('RemoveAll', () => {
164+
it('should remove all cards', async () => {
165+
const sut = makeSut();
166+
167+
await sut.removeAll();
168+
169+
expect(mockFacade.removeAll).toHaveBeenCalled();
170+
});
171+
172+
it('should throw when use case throws', async () => {
173+
const facade = {
174+
add: vi.fn(),
175+
findAll: vi.fn(),
176+
findById: vi.fn(),
177+
update: vi.fn(),
178+
remove: vi.fn(),
179+
removeAll: vi.fn().mockRejectedValueOnce(new Error()),
180+
restoreAll: vi.fn(),
181+
};
182+
183+
const sut = makeSut(facade);
184+
185+
await expect(async () => await sut.removeAll()).rejects.toThrow();
186+
});
187+
});
188+
189+
describe('RestoreAll', () => {
190+
it('should restore all cards', async () => {
191+
const sut = makeSut();
192+
193+
await sut.restoreAll();
194+
195+
expect(mockFacade.restoreAll).toHaveBeenCalled();
196+
});
197+
198+
it('should throw when use case throws', async () => {
199+
const facade = {
200+
add: vi.fn(),
201+
findAll: vi.fn(),
202+
findById: vi.fn(),
203+
update: vi.fn(),
204+
remove: vi.fn(),
205+
removeAll: vi.fn(),
206+
restoreAll: vi.fn().mockRejectedValueOnce(new Error()),
207+
};
208+
209+
const sut = makeSut(facade);
210+
211+
await expect(async () => await sut.restoreAll()).rejects.toThrow();
212+
});
213+
});
214+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { AddCardValidator } from './add-card.validator';
2+
3+
describe('AddCardValidator', () => {
4+
it('should validate', async () => {
5+
await expect(async () => {
6+
await AddCardValidator.validate('any_name', 0);
7+
}).not.toThrow();
8+
9+
await expect(async () => {
10+
await AddCardValidator.validate('any_name');
11+
}).not.toThrow();
12+
13+
await expect(async () => {
14+
await AddCardValidator.validate('');
15+
}).rejects.toThrow('name is a required field');
16+
17+
await expect(async () => {
18+
await AddCardValidator.validate(' ');
19+
}).rejects.toThrow('name is a required field');
20+
21+
await expect(async () => {
22+
await AddCardValidator.validate('an');
23+
}).rejects.toThrow('name must be at least 3 characters');
24+
25+
await expect(async () => {
26+
await AddCardValidator.validate('a'.repeat(33));
27+
}).rejects.toThrow('name must be at most 32 characters');
28+
});
29+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { AddStationValidator } from './add-station.validator';
2+
3+
describe('AddStationValidator', () => {
4+
it('should validate', async () => {
5+
await expect(async () => {
6+
await AddStationValidator.validate('any_name', 'any_line');
7+
}).not.toThrow();
8+
9+
await expect(async () => {
10+
await AddStationValidator.validate('', 'any_line');
11+
}).rejects.toThrow('name is a required field');
12+
13+
await expect(async () => {
14+
await AddStationValidator.validate(' ', 'any_line');
15+
}).rejects.toThrow('name is a required field');
16+
17+
await expect(async () => {
18+
await AddStationValidator.validate('any_name', '');
19+
}).rejects.toThrow('line is a required field');
20+
21+
await expect(async () => {
22+
await AddStationValidator.validate('any_name', ' ');
23+
}).rejects.toThrow('line is a required field');
24+
25+
await expect(async () => {
26+
await AddStationValidator.validate('an', 'any_line');
27+
}).rejects.toThrow('name must be at least 3 characters');
28+
29+
await expect(async () => {
30+
await AddStationValidator.validate('any_name', 'an');
31+
}).rejects.toThrow('line must be at least 3 characters');
32+
33+
await expect(async () => {
34+
await AddStationValidator.validate('a'.repeat(33), 'an_line');
35+
}).rejects.toThrow('name must be at most 32 characters');
36+
37+
await expect(async () => {
38+
await AddStationValidator.validate('any_name', 'a'.repeat(33));
39+
}).rejects.toThrow('line must be at most 32 characters');
40+
});
41+
});

0 commit comments

Comments
 (0)