Skip to content

Commit 605df99

Browse files
committed
✅ add station facade unit test
1 parent 5a0ad56 commit 605df99

File tree

1 file changed

+213
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)