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