feat(LoginForm): add isLoading check to LoginForm, add new test cases

This commit is contained in:
Ilia Mashkov
2026-03-24 20:03:34 +03:00
parent c006a94c4d
commit 70bcf7fdcf
2 changed files with 22 additions and 3 deletions

View File

@@ -21,9 +21,20 @@ describe("LoginForm", () => {
expect(screen.getByRole("button", { name: /login/i })).toBeDisabled(); expect(screen.getByRole("button", { name: /login/i })).toBeDisabled();
}); });
it("enables submit button when form is valid", () => { it("disables submit button when auth store status equals loading", () => {
useAuthStore.getState().setEmail(MOCK_EMAIL); useAuthStore.getState().setEmail(MOCK_EMAIL);
useAuthStore.getState().setPassword(MOCK_PASSWORD); useAuthStore.getState().setPassword(MOCK_PASSWORD);
useAuthStore.setState({ status: "loading" });
render(<LoginForm />);
expect(screen.getByRole("button", { name: /login/i })).toBeDisabled();
});
it("enables submit button when form is valid and auth store status isn't equal to loading", () => {
useAuthStore.getState().setEmail(MOCK_EMAIL);
useAuthStore.getState().setPassword(MOCK_PASSWORD);
useAuthStore.setState({ status: "idle" });
render(<LoginForm />); render(<LoginForm />);
expect(screen.getByRole("button", { name: /login/i })).toBeEnabled(); expect(screen.getByRole("button", { name: /login/i })).toBeEnabled();
}); });
@@ -49,6 +60,7 @@ describe("LoginForm", () => {
useAuthStore.getState().setEmail(MOCK_EMAIL); useAuthStore.getState().setEmail(MOCK_EMAIL);
useAuthStore.getState().setPassword(MOCK_PASSWORD); useAuthStore.getState().setPassword(MOCK_PASSWORD);
useAuthStore.setState({ status: "idle" });
render(<LoginForm />); render(<LoginForm />);
const submitButton = screen.getByRole("button", { name: /login/i }); const submitButton = screen.getByRole("button", { name: /login/i });

View File

@@ -1,4 +1,8 @@
import { selectFormValid, useAuthStore } from "../../model"; import {
selectFormValid,
selectStatusIsLoading,
useAuthStore,
} from "../../model";
import type { SubmitEvent, ChangeEvent } from "react"; import type { SubmitEvent, ChangeEvent } from "react";
/** /**
@@ -8,6 +12,9 @@ export function LoginForm() {
const { formData, setEmail, setPassword, login } = useAuthStore(); const { formData, setEmail, setPassword, login } = useAuthStore();
const formValid = useAuthStore(selectFormValid); const formValid = useAuthStore(selectFormValid);
const isLoading = useAuthStore(selectStatusIsLoading);
const disabled = !formValid || isLoading;
const handleEmailChange = (e: ChangeEvent<HTMLInputElement>) => { const handleEmailChange = (e: ChangeEvent<HTMLInputElement>) => {
setEmail(e.target.value); setEmail(e.target.value);
@@ -36,7 +43,7 @@ export function LoginForm() {
value={formData?.password?.value ?? ""} value={formData?.password?.value ?? ""}
onChange={handlePasswordChange} onChange={handlePasswordChange}
/> />
<button type="submit" disabled={!formValid}> <button type="submit" disabled={disabled}>
Login Login
</button> </button>
</form> </form>