1+
2+ import { getIronSession } from "iron-session" ;
3+ import { beforeEach , describe , expect , it , vi } from "vitest" ;
4+ import { SavedJobsController } from "../../../../src/modules/savedJobs/savedJobs.controller" ;
5+
6+ vi . mock ( "iron-session" , ( ) => ( {
7+ getIronSession : vi . fn ( ) ,
8+ } ) ) ;
9+
10+ const mockService = {
11+ getAll : vi . fn ( ) ,
12+ getById : vi . fn ( ) ,
13+ create : vi . fn ( ) ,
14+ update : vi . fn ( ) ,
15+ delete : vi . fn ( ) ,
16+ } ;
17+
18+ function createMockResponse ( ) {
19+ const res = { } as any ;
20+
21+ res . status = vi . fn ( ) . mockReturnValue ( res ) ;
22+ res . json = vi . fn ( ) . mockReturnValue ( res ) ;
23+ res . send = vi . fn ( ) . mockReturnValue ( res ) ;
24+
25+ return res ;
26+ }
27+
28+ describe ( "SavedJobsController" , ( ) => {
29+ let controller : SavedJobsController ;
30+
31+ beforeEach ( ( ) => {
32+ vi . clearAllMocks ( ) ;
33+ controller = new SavedJobsController ( mockService as any ) ;
34+ } ) ;
35+
36+ describe ( "getAll" , ( ) => {
37+ it ( "retorna 401 quando não autenticado" , async ( ) => {
38+ ( getIronSession as any ) . mockResolvedValue ( { } ) ;
39+
40+ const req = { } as any ;
41+ const res = createMockResponse ( ) ;
42+
43+ await controller . getAll ( req , res ) ;
44+
45+ expect ( res . status ) . toHaveBeenCalledWith ( 401 ) ;
46+ } ) ;
47+
48+ it ( "retorna vagas" , async ( ) => {
49+ ( getIronSession as any ) . mockResolvedValue ( {
50+ userId : "user-1" ,
51+ } ) ;
52+
53+ mockService . getAll . mockResolvedValue ( [ { id : "1" } ] ) ;
54+
55+ const req = { } as any ;
56+ const res = createMockResponse ( ) ;
57+
58+ await controller . getAll ( req , res ) ;
59+
60+ expect ( mockService . getAll ) . toHaveBeenCalledWith ( "user-1" ) ;
61+ expect ( res . json ) . toHaveBeenCalledWith ( [ { id : "1" } ] ) ;
62+ } ) ;
63+
64+ it ( "retorna erro 500" , async ( ) => {
65+ ( getIronSession as any ) . mockResolvedValue ( {
66+ userId : "user-1" ,
67+ } ) ;
68+
69+ mockService . getAll . mockRejectedValue (
70+ new Error ( "database error" ) ,
71+ ) ;
72+
73+ const req = { } as any ;
74+ const res = createMockResponse ( ) ;
75+
76+ await controller . getAll ( req , res ) ;
77+
78+ expect ( res . status ) . toHaveBeenCalledWith ( 500 ) ;
79+ } ) ;
80+ } ) ;
81+
82+ describe ( "getById" , ( ) => {
83+ it ( "retorna 401" , async ( ) => {
84+ ( getIronSession as any ) . mockResolvedValue ( { } ) ;
85+
86+ const req = {
87+ params : { id : "1" } ,
88+ } as any ;
89+
90+ const res = createMockResponse ( ) ;
91+
92+ await controller . getById ( req , res ) ;
93+
94+ expect ( res . status ) . toHaveBeenCalledWith ( 401 ) ;
95+ } ) ;
96+
97+ it ( "retorna 404" , async ( ) => {
98+ ( getIronSession as any ) . mockResolvedValue ( {
99+ userId : "user-1" ,
100+ } ) ;
101+
102+ mockService . getById . mockResolvedValue ( null ) ;
103+
104+ const req = {
105+ params : { id : "1" } ,
106+ } as any ;
107+
108+ const res = createMockResponse ( ) ;
109+
110+ await controller . getById ( req , res ) ;
111+
112+ expect ( res . status ) . toHaveBeenCalledWith ( 404 ) ;
113+ } ) ;
114+
115+ it ( "retorna vaga" , async ( ) => {
116+ ( getIronSession as any ) . mockResolvedValue ( {
117+ userId : "user-1" ,
118+ } ) ;
119+
120+ mockService . getById . mockResolvedValue ( {
121+ id : "1" ,
122+ } ) ;
123+
124+ const req = {
125+ params : { id : "1" } ,
126+ } as any ;
127+
128+ const res = createMockResponse ( ) ;
129+
130+ await controller . getById ( req , res ) ;
131+
132+ expect ( res . json ) . toHaveBeenCalled ( ) ;
133+ } ) ;
134+ } ) ;
135+
136+ describe ( "create" , ( ) => {
137+ it ( "retorna 401" , async ( ) => {
138+ ( getIronSession as any ) . mockResolvedValue ( { } ) ;
139+
140+ const req = {
141+ body : { } ,
142+ } as any ;
143+
144+ const res = createMockResponse ( ) ;
145+
146+ await controller . create ( req , res ) ;
147+
148+ expect ( res . status ) . toHaveBeenCalledWith ( 401 ) ;
149+ } ) ;
150+
151+ it ( "retorna 400 quando body inválido" , async ( ) => {
152+ ( getIronSession as any ) . mockResolvedValue ( {
153+ userId : "user-1" ,
154+ } ) ;
155+
156+ const req = {
157+ body : {
158+ jobLink : "link-invalido" ,
159+ } ,
160+ } as any ;
161+
162+ const res = createMockResponse ( ) ;
163+
164+ await controller . create ( req , res ) ;
165+
166+ expect ( res . status ) . toHaveBeenCalledWith ( 400 ) ;
167+ } ) ;
168+
169+ it ( "cria vaga" , async ( ) => {
170+ ( getIronSession as any ) . mockResolvedValue ( {
171+ userId : "user-1" ,
172+ } ) ;
173+
174+ mockService . create . mockResolvedValue ( {
175+ id : "1" ,
176+ } ) ;
177+
178+ const req = {
179+ body : {
180+ jobLink : "https://google.com" ,
181+ } ,
182+ } as any ;
183+
184+ const res = createMockResponse ( ) ;
185+
186+ await controller . create ( req , res ) ;
187+
188+ expect ( res . status ) . toHaveBeenCalledWith ( 201 ) ;
189+ } ) ;
190+
191+ it ( "retorna 409 quando vaga já existe" , async ( ) => {
192+ ( getIronSession as any ) . mockResolvedValue ( {
193+ userId : "user-1" ,
194+ } ) ;
195+
196+ mockService . create . mockRejectedValue (
197+ new Error ( "Vaga já salva." ) ,
198+ ) ;
199+
200+ const req = {
201+ body : {
202+ jobLink : "https://google.com" ,
203+ } ,
204+ } as any ;
205+
206+ const res = createMockResponse ( ) ;
207+
208+ await controller . create ( req , res ) ;
209+
210+ expect ( res . status ) . toHaveBeenCalledWith ( 409 ) ;
211+ } ) ;
212+ } ) ;
213+
214+ describe ( "update" , ( ) => {
215+ it ( "retorna 401" , async ( ) => {
216+ ( getIronSession as any ) . mockResolvedValue ( { } ) ;
217+
218+ const req = {
219+ params : { id : "1" } ,
220+ } as any ;
221+
222+ const res = createMockResponse ( ) ;
223+
224+ await controller . update ( req , res ) ;
225+
226+ expect ( res . status ) . toHaveBeenCalledWith ( 401 ) ;
227+ } ) ;
228+
229+ it ( "retorna 400 para payload inválido" , async ( ) => {
230+ ( getIronSession as any ) . mockResolvedValue ( {
231+ userId : "user-1" ,
232+ } ) ;
233+
234+ const req = {
235+ params : { id : "1" } ,
236+ body : {
237+ jobLink : "url inválida" ,
238+ } ,
239+ } as any ;
240+
241+ const res = createMockResponse ( ) ;
242+
243+ await controller . update ( req , res ) ;
244+
245+ expect ( res . status ) . toHaveBeenCalledWith ( 400 ) ;
246+ } ) ;
247+
248+ it ( "atualiza vaga" , async ( ) => {
249+ ( getIronSession as any ) . mockResolvedValue ( {
250+ userId : "user-1" ,
251+ } ) ;
252+
253+ mockService . update . mockResolvedValue ( {
254+ id : "1" ,
255+ } ) ;
256+
257+ const req = {
258+ params : { id : "1" } ,
259+ body : {
260+ notes : "teste" ,
261+ } ,
262+ } as any ;
263+
264+ const res = createMockResponse ( ) ;
265+
266+ await controller . update ( req , res ) ;
267+
268+ expect ( res . json ) . toHaveBeenCalled ( ) ;
269+ } ) ;
270+ } ) ;
271+
272+ describe ( "delete" , ( ) => {
273+ it ( "retorna 401" , async ( ) => {
274+ ( getIronSession as any ) . mockResolvedValue ( { } ) ;
275+
276+ const req = {
277+ params : { id : "1" } ,
278+ } as any ;
279+
280+ const res = createMockResponse ( ) ;
281+
282+ await controller . delete ( req , res ) ;
283+
284+ expect ( res . status ) . toHaveBeenCalledWith ( 401 ) ;
285+ } ) ;
286+
287+ it ( "remove vaga" , async ( ) => {
288+ ( getIronSession as any ) . mockResolvedValue ( {
289+ userId : "user-1" ,
290+ } ) ;
291+
292+ mockService . delete . mockResolvedValue ( undefined ) ;
293+
294+ const req = {
295+ params : { id : "1" } ,
296+ } as any ;
297+
298+ const res = createMockResponse ( ) ;
299+
300+ await controller . delete ( req , res ) ;
301+
302+ expect ( res . status ) . toHaveBeenCalledWith ( 204 ) ;
303+ expect ( res . send ) . toHaveBeenCalled ( ) ;
304+ } ) ;
305+
306+ it ( "retorna erro desconhecido" , async ( ) => {
307+ ( getIronSession as any ) . mockResolvedValue ( {
308+ userId : "user-1" ,
309+ } ) ;
310+
311+ mockService . delete . mockRejectedValue ( "erro" ) ;
312+
313+ const req = {
314+ params : { id : "1" } ,
315+ } as any ;
316+
317+ const res = createMockResponse ( ) ;
318+
319+ await controller . delete ( req , res ) ;
320+
321+ expect ( res . status ) . toHaveBeenCalledWith ( 500 ) ;
322+ expect ( res . json ) . toHaveBeenCalledWith ( {
323+ error : "Erro desconhecido" ,
324+ } ) ;
325+ } ) ;
326+ } ) ;
327+ } ) ;
0 commit comments