feature/login-and-register-forms #2
@@ -22,6 +22,20 @@ describe("authStore", () => {
|
|||||||
});
|
});
|
||||||
afterAll(() => server.close());
|
afterAll(() => server.close());
|
||||||
|
|
||||||
|
describe("setEmail", () => {
|
||||||
|
it("should set the email in formData", () => {
|
||||||
|
useAuthStore.getState().setEmail(MOCK_NEW_EMAIL);
|
||||||
|
expect(useAuthStore.getState().formData.email).toBe(MOCK_NEW_EMAIL);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("setPassword", () => {
|
||||||
|
it("should set the password in formData", () => {
|
||||||
|
useAuthStore.getState().setPassword(MOCK_PASSWORD);
|
||||||
|
expect(useAuthStore.getState().formData.password).toBe(MOCK_PASSWORD);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("reset", () => {
|
describe("reset", () => {
|
||||||
it("should reset the store to default state", () => {
|
it("should reset the store to default state", () => {
|
||||||
useAuthStore.getState().reset();
|
useAuthStore.getState().reset();
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ import { callApi } from "shared/utils";
|
|||||||
import { UNEXPECTED_ERROR_MESSAGE } from "shared/api";
|
import { UNEXPECTED_ERROR_MESSAGE } from "shared/api";
|
||||||
|
|
||||||
export const defaultStoreState: Readonly<AuthStoreState> = {
|
export const defaultStoreState: Readonly<AuthStoreState> = {
|
||||||
|
formData: {
|
||||||
|
email: "",
|
||||||
|
password: "",
|
||||||
|
},
|
||||||
user: undefined,
|
user: undefined,
|
||||||
status: "idle",
|
status: "idle",
|
||||||
error: null,
|
error: null,
|
||||||
@@ -12,7 +16,17 @@ export const defaultStoreState: Readonly<AuthStoreState> = {
|
|||||||
|
|
||||||
export const useAuthStore = create<AuthStore>()((set) => ({
|
export const useAuthStore = create<AuthStore>()((set) => ({
|
||||||
...defaultStoreState,
|
...defaultStoreState,
|
||||||
reset: () => set(defaultStoreState),
|
|
||||||
|
setEmail: (email: string) => {
|
||||||
|
set((state) => ({ formData: { ...state.formData, email } }));
|
||||||
|
},
|
||||||
|
setPassword: (password: string) => {
|
||||||
|
set((state) => ({ formData: { ...state.formData, password } }));
|
||||||
|
},
|
||||||
|
reset: () => {
|
||||||
|
set(defaultStoreState);
|
||||||
|
},
|
||||||
|
|
||||||
login: async (loginData) => {
|
login: async (loginData) => {
|
||||||
set({ status: "loading" });
|
set({ status: "loading" });
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -3,6 +3,10 @@ import type { AuthData, AuthStatus } from "./service";
|
|||||||
import type { ApiError } from "shared/utils";
|
import type { ApiError } from "shared/utils";
|
||||||
|
|
||||||
export interface AuthStoreState {
|
export interface AuthStoreState {
|
||||||
|
/**
|
||||||
|
* Form data for login/register forms
|
||||||
|
*/
|
||||||
|
formData: AuthData;
|
||||||
/**
|
/**
|
||||||
* User's credentials
|
* User's credentials
|
||||||
*/
|
*/
|
||||||
@@ -17,16 +21,14 @@ export interface AuthStoreState {
|
|||||||
error: ApiError | Error | null;
|
error: ApiError | Error | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ResetAction = () => void;
|
|
||||||
export type LoginAction = (data: AuthData) => Promise<void>;
|
|
||||||
export type RegisterAction = (data: AuthData) => Promise<void>;
|
|
||||||
export type LogoutAction = () => Promise<void>;
|
|
||||||
|
|
||||||
export interface AuthStoreActions {
|
export interface AuthStoreActions {
|
||||||
reset: ResetAction;
|
setEmail: (email: string) => void;
|
||||||
login: LoginAction;
|
setPassword: (password: string) => void;
|
||||||
register: RegisterAction;
|
reset: () => void;
|
||||||
logout: LogoutAction;
|
|
||||||
|
login: (data: AuthData) => Promise<void>;
|
||||||
|
register: (data: AuthData) => Promise<void>;
|
||||||
|
logout: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AuthStore = AuthStoreState & AuthStoreActions;
|
export type AuthStore = AuthStoreState & AuthStoreActions;
|
||||||
|
|||||||
Reference in New Issue
Block a user