@@ -56,6 +56,7 @@ describe("authService", () => {
5656 credentials : "include" ,
5757 } )
5858 ) ;
59+
5960 expect ( result . token ) . toBe ( "abc" ) ;
6061 expect ( result . user . email ) . toBe ( "test@test.com" ) ;
6162 } ) ;
@@ -87,60 +88,6 @@ describe("authService", () => {
8788 auth . login ( { email : "x@x.com" , password : "wrong" } )
8889 ) . rejects . toThrow ( "Falha ao fazer login." ) ;
8990 } ) ;
90-
91- it ( "should handle text response fallback" , async ( ) => {
92- fetchMock . mockResolvedValueOnce (
93- mockResponse ( {
94- contentType : "text/plain" ,
95- textData : "Server error occurred" ,
96- } )
97- ) ;
98-
99- const result = await auth . login ( {
100- email : "test@test.com" ,
101- password : "123456" ,
102- } ) ;
103-
104- expect ( result ) . toEqual ( { message : "Server error occurred" } ) ;
105- } ) ;
106-
107- it ( "should handle empty text response" , async ( ) => {
108- fetchMock . mockResolvedValueOnce (
109- mockResponse ( {
110- contentType : "text/plain" ,
111- textData : "" ,
112- } )
113- ) ;
114-
115- const result = await auth . login ( {
116- email : "test@test.com" ,
117- password : "123456" ,
118- } ) ;
119-
120- expect ( result ) . toEqual ( { } ) ;
121- } ) ;
122-
123- it ( "should handle invalid JSON response" , async ( ) => {
124- const mockResponseObj = {
125- ok : true ,
126- headers : {
127- get : ( key : string ) => {
128- if ( key === "content-type" ) return "application/json" ;
129- return null ;
130- } ,
131- } ,
132- json : vi . fn ( ) . mockRejectedValue ( new Error ( "Invalid JSON" ) ) ,
133- text : vi . fn ( ) . mockResolvedValue ( "" ) ,
134- } ;
135- fetchMock . mockResolvedValueOnce ( mockResponseObj as any ) ;
136-
137- const result = await auth . login ( {
138- email : "test@test.com" ,
139- password : "123456" ,
140- } ) ;
141-
142- expect ( result ) . toEqual ( { } ) ;
143- } ) ;
14491 } ) ;
14592
14693 describe ( "register" , ( ) => {
@@ -161,13 +108,10 @@ describe("authService", () => {
161108 expect . stringContaining ( "/api/auth/register" ) ,
162109 expect . objectContaining ( {
163110 method : "POST" ,
164- body : JSON . stringify ( {
165- email : "new@test.com" ,
166- password : "123456" ,
167- name : "New User" ,
168- } ) ,
111+ headers : { "Content-Type" : "application/json" } ,
169112 } )
170113 ) ;
114+
171115 expect ( result . user . id ) . toBe ( 1 ) ;
172116 } ) ;
173117
@@ -187,11 +131,15 @@ describe("authService", () => {
187131 } ) ;
188132
189133 expect ( result . user . id ) . toBe ( 1 ) ;
134+
190135 const callArgs = fetchMock . mock . calls [ 0 ] [ 1 ] ;
136+
191137 expect ( JSON . parse ( callArgs . body ) ) . toEqual ( {
192138 email : "new@test.com" ,
193139 password : "123456" ,
194140 name : "New User" ,
141+ phone : "+5511999999999" ,
142+ cpf : "12345678901" ,
195143 } ) ;
196144 } ) ;
197145
@@ -228,45 +176,11 @@ describe("authService", () => {
228176 password : "123456" ,
229177 name : "Test" ,
230178 } )
231- ) . rejects . toThrow ( "Falha ao cadastrar: 500 OK" ) ;
232- } ) ;
233-
234- it ( "should handle text response on register" , async ( ) => {
235- fetchMock . mockResolvedValueOnce (
236- mockResponse ( {
237- ok : true ,
238- contentType : "text/plain" ,
239- textData : "Registration successful" ,
240- } )
241- ) ;
242-
243- const result = await auth . register ( {
244- email : "test@test.com" ,
245- password : "123456" ,
246- name : "Test User" ,
247- } ) ;
248-
249- expect ( result ) . toEqual ( { message : "Registration successful" } ) ;
179+ ) . rejects . toThrow ( "Falha ao cadastrar." ) ;
250180 } ) ;
251181 } ) ;
252182
253183 describe ( "logout" , ( ) => {
254- it ( "should logout successfully" , async ( ) => {
255- fetchMock . mockResolvedValueOnce (
256- mockResponse ( {
257- jsonData : { } ,
258- } )
259- ) ;
260-
261- await expect ( auth . logout ( ) ) . resolves . toBeUndefined ( ) ;
262- expect ( fetchMock ) . toHaveBeenCalledWith (
263- expect . stringContaining ( "/api/auth/logout" ) ,
264- expect . objectContaining ( {
265- method : "POST" ,
266- credentials : "include" ,
267- } )
268- ) ;
269- } ) ;
270184
271185 it ( "should throw on logout failure with message" , async ( ) => {
272186 fetchMock . mockResolvedValueOnce (
@@ -302,8 +216,10 @@ describe("authService", () => {
302216 ) ;
303217
304218 const user = await auth . getCurrentUser ( ) ;
219+
305220 expect ( user . name ) . toBe ( "Bene" ) ;
306221 expect ( user . email ) . toBe ( "bene@test.com" ) ;
222+
307223 expect ( fetchMock ) . toHaveBeenCalledWith (
308224 expect . stringContaining ( "/api/auth/me" ) ,
309225 { credentials : "include" }
@@ -331,29 +247,9 @@ describe("authService", () => {
331247 } )
332248 ) ;
333249
334- await expect ( auth . getCurrentUser ( ) ) . rejects . toThrow ( "Falha ao carregar usuário." ) ;
335- } ) ;
336- } ) ;
337-
338- describe ( "buildApiUrl with base URL" , ( ) => {
339- it ( "should use VITE_API_BASE_URL when configured" , async ( ) => {
340- const originalEnv = import . meta. env . VITE_API_BASE_URL ;
341- import . meta. env . VITE_API_BASE_URL = "https://api.example.com" ;
342-
343- fetchMock . mockResolvedValueOnce (
344- mockResponse ( {
345- jsonData : { token : "abc" } ,
346- } )
347- ) ;
348-
349- await auth . login ( { email : "test@test.com" , password : "123456" } ) ;
350-
351- expect ( fetchMock ) . toHaveBeenCalledWith (
352- "https://api.example.com/api/auth/login" ,
353- expect . any ( Object )
250+ await expect ( auth . getCurrentUser ( ) ) . rejects . toThrow (
251+ "Falha ao carregar usuário."
354252 ) ;
355-
356- import . meta. env . VITE_API_BASE_URL = originalEnv ;
357253 } ) ;
358254 } ) ;
359255} ) ;
0 commit comments