hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { getRegisterSchools } from "../../auth"; |
| 3 | import { postLogin } from "../../auth"; |
| 4 | import { postLogout } from "../../auth"; |
| 5 | import { postRegister } from "../../auth"; |
| 6 | import { postSelectSchool } from "../../auth"; |
| 7 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 8 | |
| 9 | describe("auth API", () => { |
| 10 | afterEach(() => { |
| 11 | vi.restoreAllMocks(); |
| 12 | }); |
| 13 | |
| 14 | it("postLogin calls /auth/login with credentials", async () => { |
| 15 | const mockUser = { id: "u1", username: "test" }; |
| 16 | mockFetchSuccess(mockUser); |
| 17 | |
| 18 | const result = await postLogin({ username: "test", password: "password" }); |
| 19 | expect(result).toEqual(mockUser); |
| 20 | |
| 21 | const [url, init] = lastFetchCall(); |
| 22 | expect(url).toBe("/api/auth/login"); |
| 23 | expect(init?.method).toBe("POST"); |
| 24 | expect(init?.body).toBe(JSON.stringify({ username: "test", password: "password" })); |
| 25 | }); |
| 26 | |
| 27 | it("postSelectSchool binds the person session to a school", async () => { |
| 28 | const mockUser = { id: "u1", username: "test" }; |
| 29 | mockFetchSuccess(mockUser); |
| 30 | |
| 31 | const school = "019732e3-7b00-7000-8000-00000000dead"; |
| 32 | const result = await postSelectSchool({ school }); |
| 33 | expect(result).toEqual(mockUser); |
| 34 | |
| 35 | const [url, init] = lastFetchCall(); |
| 36 | expect(url).toBe("/api/auth/school"); |
| 37 | expect(init?.method).toBe("POST"); |
| 38 | expect(init?.body).toBe(JSON.stringify({ school })); |
| 39 | }); |
| 40 | |
| 41 | it("postLogout calls /auth/logout", async () => { |
| 42 | mockFetch204(); |
| 43 | |
| 44 | await postLogout(); |
| 45 | |
| 46 | const [url, init] = lastFetchCall(); |
| 47 | expect(url).toBe("/api/auth/logout"); |
| 48 | expect(init?.method).toBe("POST"); |
| 49 | }); |
| 50 | |
| 51 | it("postRegister calls /auth/register with data", async () => { |
| 52 | const mockResponse = { username: "newuser", role: "student" }; |
| 53 | mockFetchSuccess(mockResponse); |
| 54 | |
| 55 | const data = { school: "019732e3-7b00-7000-8000-00000000dead", username: "newuser", password: "password" }; |
| 56 | const result = await postRegister(data); |
| 57 | expect(result).toEqual(mockResponse); |
| 58 | |
| 59 | const [url, init] = lastFetchCall(); |
| 60 | expect(url).toBe("/api/auth/register"); |
| 61 | expect(init?.method).toBe("POST"); |
| 62 | expect(init?.body).toBe(JSON.stringify(data)); |
| 63 | }); |
| 64 | |
| 65 | it("getRegisterSchools reads active schools for the register picker", async () => { |
| 66 | const schools = [{ id: "019732e3-7b00-7000-8000-00000000dead", name: "Hezarfen" }]; |
| 67 | mockFetchSuccess(schools); |
| 68 | |
| 69 | const result = await getRegisterSchools(); |
| 70 | expect(result).toEqual(schools); |
| 71 | |
| 72 | const [url, init] = lastFetchCall(); |
| 73 | expect(url).toBe("/api/auth/schools"); |
| 74 | expect(init?.method).toBe("GET"); |
| 75 | }); |
| 76 | }); |