feat(auth): create selectors for authStore; cover them with tests

This commit is contained in:
Ilia Mashkov
2026-03-24 13:02:58 +03:00
parent b871bf27de
commit 8cea93220b
5 changed files with 52 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
export * from "./selectFormValid/selectFormValid";
export * from "./selectAuthData/selectAuthData";

View File

@@ -0,0 +1,14 @@
import { useAuthStore } from "../../stores/authStore/authStore";
import { selectAuthData } from "./selectAuthData";
import { MOCK_EMAIL, MOCK_PASSWORD } from "../../../api/calls/mocks";
describe("selectAuthData", () => {
it("should return the correct auth data", () => {
useAuthStore.getState().setEmail(MOCK_EMAIL);
useAuthStore.getState().setPassword(MOCK_PASSWORD);
expect(selectAuthData(useAuthStore.getState())).toEqual({
email: MOCK_EMAIL,
password: MOCK_PASSWORD,
});
});
});

View File

@@ -0,0 +1,7 @@
import type { AuthData } from "../../types/service";
import type { AuthStore } from "../../types/store";
export const selectAuthData = (state: AuthStore): AuthData => ({
email: state.formData.email.value,
password: state.formData.password.value,
});

View File

@@ -0,0 +1,25 @@
import { MOCK_EMAIL, MOCK_PASSWORD } from "../../../api/calls/mocks";
import { useAuthStore } from "../../stores/authStore/authStore";
import { selectFormValid } from "./selectFormValid";
describe("selectFormValid", () => {
afterEach(() => {
useAuthStore.getState().reset();
});
it("should be false when email is invalid", () => {
useAuthStore.getState().setEmail("");
expect(selectFormValid(useAuthStore.getState())).toBe(false);
});
it("should be false when password is invalid", () => {
useAuthStore.getState().setPassword("");
expect(selectFormValid(useAuthStore.getState())).toBe(false);
});
it("should be true when email and password are valid", () => {
useAuthStore.getState().setEmail(MOCK_EMAIL);
useAuthStore.getState().setPassword(MOCK_PASSWORD);
expect(selectFormValid(useAuthStore.getState())).toBe(true);
});
});

View File

@@ -0,0 +1,4 @@
import type { AuthStore } from "../../types/store";
export const selectFormValid = (state: AuthStore) =>
Object.values(state.formData).every((field) => field.valid);