feat(auth): create selectors for authStore; cover them with tests
This commit is contained in:
2
src/features/auth/model/selectors/index.ts
Normal file
2
src/features/auth/model/selectors/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from "./selectFormValid/selectFormValid";
|
||||||
|
export * from "./selectAuthData/selectAuthData";
|
||||||
@@ -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,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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,
|
||||||
|
});
|
||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import type { AuthStore } from "../../types/store";
|
||||||
|
|
||||||
|
export const selectFormValid = (state: AuthStore) =>
|
||||||
|
Object.values(state.formData).every((field) => field.valid);
|
||||||
Reference in New Issue
Block a user