hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 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 { postSelectSchool } from "../../auth"; |
| 6 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 7 | |
| 8 | describe("auth API", () => { |
| 9 | afterEach(() => { |
| 10 | vi.restoreAllMocks(); |
| 11 | }); |
| 12 | |
| 13 | it("postLogin calls /auth/login with credentials", async () => { |
| 14 | const mockUser = { id: "u1", username: "test" }; |
| 15 | mockFetchSuccess(mockUser); |
| 16 | |
| 17 | const result = await postLogin({ username: "test", password: "password" }); |
| 18 | expect(result).toEqual(mockUser); |
| 19 | |
| 20 | const [url, init] = lastFetchCall(); |
| 21 | expect(url).toBe("/api/auth/login"); |
| 22 | expect(init?.method).toBe("POST"); |
| 23 | expect(init?.body).toBe(JSON.stringify({ username: "test", password: "password" })); |
| 24 | }); |
| 25 | |
| 26 | it("postSelectSchool binds the person session to a school", async () => { |
| 27 | const mockUser = { id: "u1", username: "test" }; |
| 28 | mockFetchSuccess(mockUser); |
| 29 | |
| 30 | const result = await postSelectSchool({ school: "demo" }); |
| 31 | expect(result).toEqual(mockUser); |
| 32 | |
| 33 | const [url, init] = lastFetchCall(); |
| 34 | expect(url).toBe("/api/auth/school"); |
| 35 | expect(init?.method).toBe("POST"); |
| 36 | expect(init?.body).toBe(JSON.stringify({ school: "demo" })); |
| 37 | }); |
| 38 | |
| 39 | it("postLogout calls /auth/logout", async () => { |
| 40 | mockFetch204(); |
| 41 | |
| 42 | await postLogout(); |
| 43 | |
| 44 | const [url, init] = lastFetchCall(); |
| 45 | expect(url).toBe("/api/auth/logout"); |
| 46 | expect(init?.method).toBe("POST"); |
| 47 | }); |
| 48 | |
| 49 | it("postRegister calls /auth/register with data", async () => { |
| 50 | const mockResponse = { username: "newuser", role: "student" }; |
| 51 | mockFetchSuccess(mockResponse); |
| 52 | |
| 53 | const data = { school: "demo", username: "newuser", password: "password" }; |
| 54 | const result = await postRegister(data); |
| 55 | expect(result).toEqual(mockResponse); |
| 56 | |
| 57 | const [url, init] = lastFetchCall(); |
| 58 | expect(url).toBe("/api/auth/register"); |
| 59 | expect(init?.method).toBe("POST"); |
| 60 | expect(init?.body).toBe(JSON.stringify(data)); |
| 61 | }); |
| 62 | }); |