hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB7122d71Burak Boduroğlu4 months ago419 commits
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { postLogin } from "../../auth"; |
| 3 | import { postLogout } from "../../auth"; |
| 4 | import { postRegister } from "../../auth"; |
| 5 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 6 | |
| 7 | describe("auth API", () => { |
| 8 | afterEach(() => { |
| 9 | vi.restoreAllMocks(); |
| 10 | }); |
| 11 | |
| 12 | it("postLogin calls /auth/login with credentials", async () => { |
| 13 | const mockUser = { id: "u1", username: "test" }; |
| 14 | mockFetchSuccess(mockUser); |
| 15 | |
| 16 | const result = await postLogin({ username: "test", password: "password" }); |
| 17 | expect(result).toEqual(mockUser); |
| 18 | |
| 19 | const [url, init] = lastFetchCall(); |
| 20 | expect(url).toBe("/api/auth/login"); |
| 21 | expect(init?.method).toBe("POST"); |
| 22 | expect(init?.body).toBe(JSON.stringify({ username: "test", password: "password" })); |
| 23 | }); |
| 24 | |
| 25 | it("postLogout calls /auth/logout", async () => { |
| 26 | mockFetch204(); |
| 27 | |
| 28 | await postLogout(); |
| 29 | |
| 30 | const [url, init] = lastFetchCall(); |
| 31 | expect(url).toBe("/api/auth/logout"); |
| 32 | expect(init?.method).toBe("POST"); |
| 33 | }); |
| 34 | |
| 35 | it("postRegister calls /auth/register with data", async () => { |
| 36 | const mockUser = { id: "u2", username: "newuser" }; |
| 37 | mockFetchSuccess(mockUser); |
| 38 | |
| 39 | const data = { username: "newuser", password: "password", email: "test@test.com" }; |
| 40 | const result = await postRegister(data); |
| 41 | expect(result).toEqual(mockUser); |
| 42 | |
| 43 | const [url, init] = lastFetchCall(); |
| 44 | expect(url).toBe("/api/auth/register"); |
| 45 | expect(init?.method).toBe("POST"); |
| 46 | expect(init?.body).toBe(JSON.stringify(data)); |
| 47 | }); |
| 48 | }); |