fix(auth): Correct API paths and resolve build/type errors in tests and config
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
import { describe, it, expect, vi, beforeEach, type MockInstance } from "vitest"
|
||||
import axios from "axios"
|
||||
import { AuthService } from "./auth"
|
||||
|
||||
vi.mock("axios")
|
||||
const mockedAxios = axios as jest.Mocked<typeof axios>
|
||||
const mockedAxios = axios as unknown as {
|
||||
post: MockInstance<typeof axios.post>
|
||||
}
|
||||
|
||||
describe("AuthService", () => {
|
||||
beforeEach(() => {
|
||||
@@ -22,9 +24,16 @@ describe("AuthService", () => {
|
||||
|
||||
const result = await AuthService.login("test@example.com", "password123")
|
||||
|
||||
expect(mockedAxios.post).toHaveBeenCalledWith("/api/auth/login", expect.any(FormData))
|
||||
expect(mockedAxios.post).toHaveBeenCalledWith("/auth/login", expect.any(FormData))
|
||||
|
||||
// Validate FormData content
|
||||
const formData = (mockedAxios.post.mock.calls[0][1] as FormData)
|
||||
expect(formData.get("username")).toBe("test@example.com")
|
||||
expect(formData.get("password")).toBe("password123")
|
||||
|
||||
expect(result.access_token).toBe("fake-jwt-token")
|
||||
expect(localStorage.getItem("token")).toBe("fake-jwt-token")
|
||||
expect(AuthService.isAuthenticated()).toBe(true)
|
||||
})
|
||||
|
||||
it("handles login failure", async () => {
|
||||
@@ -32,6 +41,7 @@ describe("AuthService", () => {
|
||||
|
||||
await expect(AuthService.login("test@example.com", "wrong")).rejects.toThrow("Invalid credentials")
|
||||
expect(localStorage.getItem("token")).toBeNull()
|
||||
expect(AuthService.isAuthenticated()).toBe(false)
|
||||
})
|
||||
|
||||
it("successfully registers a user", async () => {
|
||||
@@ -45,7 +55,7 @@ describe("AuthService", () => {
|
||||
|
||||
const result = await AuthService.register("test@example.com", "password123")
|
||||
|
||||
expect(mockedAxios.post).toHaveBeenCalledWith("/api/auth/register", {
|
||||
expect(mockedAxios.post).toHaveBeenCalledWith("/auth/register", {
|
||||
email: "test@example.com",
|
||||
password: "password123",
|
||||
})
|
||||
@@ -54,7 +64,9 @@ describe("AuthService", () => {
|
||||
|
||||
it("logs out and clears the token", () => {
|
||||
localStorage.setItem("token", "some-token")
|
||||
expect(AuthService.isAuthenticated()).toBe(true)
|
||||
AuthService.logout()
|
||||
expect(localStorage.getItem("token")).toBeNull()
|
||||
expect(AuthService.isAuthenticated()).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user