diff --git a/client/src/app/features/auth/apis/auth-api.test.ts b/client/src/app/features/auth/apis/auth-api.test.ts index 7caf468..3bb5b9c 100644 --- a/client/src/app/features/auth/apis/auth-api.test.ts +++ b/client/src/app/features/auth/apis/auth-api.test.ts @@ -46,7 +46,10 @@ describe("createAuthApi", () => { describe("login", () => { it("posts credentials and returns the bearer token from the envelope", async () => { fetchSpy.mockResolvedValue( - makeOkResponse({ status: "success", data: { token: TOKEN, token_type: "Bearer" } }) as Response, + makeOkResponse({ + status: "success", + data: { token: TOKEN, token_type: "Bearer" }, + }) as Response, ); const result = await api.login({ email: "taro@example.com", password: "password123" }); @@ -64,9 +67,9 @@ describe("createAuthApi", () => { it("throws on HTTP error", async () => { fetchSpy.mockResolvedValue(makeNgResponse(422) as Response); - await expect( - api.login({ email: "taro@example.com", password: "wrong" }), - ).rejects.toThrow("HTTP 422"); + await expect(api.login({ email: "taro@example.com", password: "wrong" })).rejects.toThrow( + "HTTP 422", + ); }); }); @@ -117,4 +120,4 @@ describe("createAuthApi", () => { await expect(api.getCurrentUser(TOKEN)).rejects.toThrow("HTTP 500"); }); }); -}); \ No newline at end of file +}); diff --git a/client/src/app/features/auth/mapper/__tests__/to-login-request.test.ts b/client/src/app/features/auth/mapper/__tests__/to-login-request.test.ts new file mode 100644 index 0000000..2145379 --- /dev/null +++ b/client/src/app/features/auth/mapper/__tests__/to-login-request.test.ts @@ -0,0 +1,32 @@ +import { describe, it, expect } from "@jest/globals"; +import { toLoginRequest } from "../to-login-request"; +import type { LoginFormValues } from "../../types"; + +const makeFormValues = (overrides: Partial = {}): LoginFormValues => ({ + email: "taro@example.com", + password: "password123", + ...overrides, +}); + +describe("toLoginRequest", () => { + it("maps form values to the login request", () => { + const request = toLoginRequest(makeFormValues()); + + expect(request).toEqual({ + email: "taro@example.com", + password: "password123", + }); + }); + + it("trims email", () => { + const request = toLoginRequest(makeFormValues({ email: " taro@example.com " })); + + expect(request.email).toBe("taro@example.com"); + }); + + it("does not trim password", () => { + const request = toLoginRequest(makeFormValues({ password: " spaced out " })); + + expect(request.password).toBe(" spaced out "); + }); +}); diff --git a/client/src/app/features/auth/mapper/to-login-request.ts b/client/src/app/features/auth/mapper/to-login-request.ts new file mode 100644 index 0000000..d8cfc45 --- /dev/null +++ b/client/src/app/features/auth/mapper/to-login-request.ts @@ -0,0 +1,7 @@ +import type { LoginFormValues } from "../types"; +import type { LoginRequest } from "../apis/auth-api"; + +export const toLoginRequest = (values: LoginFormValues): LoginRequest => ({ + email: values.email.trim(), + password: values.password, +});