Compare commits
13 Commits
db42c87489
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6751c7fc04 | |||
|
|
83fbf83bae | ||
|
|
576665f32b | ||
|
|
4d854d08a3 | ||
|
|
70bcf7fdcf | ||
|
|
c006a94c4d | ||
|
|
e4630a7fcb | ||
|
|
c41f02f505 | ||
|
|
683443673e | ||
|
|
1f20f11852 | ||
|
|
c378f7c83a | ||
|
|
8cea93220b | ||
|
|
b871bf27de |
@@ -1,3 +1,6 @@
|
||||
// For more info, see https://github.com/storybookjs/eslint-plugin-storybook#configuration-flat-config-format
|
||||
import storybook from "eslint-plugin-storybook";
|
||||
|
||||
import js from '@eslint/js'
|
||||
import globals from 'globals'
|
||||
import reactHooks from 'eslint-plugin-react-hooks'
|
||||
@@ -5,9 +8,7 @@ import reactRefresh from 'eslint-plugin-react-refresh'
|
||||
import tseslint from 'typescript-eslint'
|
||||
import { defineConfig, globalIgnores } from 'eslint/config'
|
||||
|
||||
export default defineConfig([
|
||||
globalIgnores(['dist']),
|
||||
{
|
||||
export default defineConfig([globalIgnores(['dist']), {
|
||||
files: ['**/*.{ts,tsx}'],
|
||||
extends: [
|
||||
js.configs.recommended,
|
||||
@@ -19,5 +20,4 @@ export default defineConfig([
|
||||
ecmaVersion: 2020,
|
||||
globals: globals.browser,
|
||||
},
|
||||
},
|
||||
])
|
||||
}, ...storybook.configs["flat/recommended"]])
|
||||
|
||||
13
package.json
13
package.json
@@ -10,7 +10,9 @@
|
||||
"preview": "vite preview",
|
||||
"test:unit": "vitest",
|
||||
"test:run": "vitest run",
|
||||
"test:coverage": "vitest run --coverage"
|
||||
"test:coverage": "vitest run --coverage",
|
||||
"storybook": "storybook dev -p 6006",
|
||||
"build-storybook": "storybook build"
|
||||
},
|
||||
"dependencies": {
|
||||
"ky": "^1.14.3",
|
||||
@@ -19,8 +21,13 @@
|
||||
"zustand": "^5.0.12"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@chromatic-com/storybook": "^5.0.2",
|
||||
"@eslint/js": "^9.39.1",
|
||||
"@module-federation/vite": "^1.12.3",
|
||||
"@storybook/addon-a11y": "^10.3.3",
|
||||
"@storybook/addon-docs": "^10.3.3",
|
||||
"@storybook/addon-vitest": "^10.3.3",
|
||||
"@storybook/react-vite": "^10.3.3",
|
||||
"@testing-library/dom": "^10.4.1",
|
||||
"@testing-library/jest-dom": "^6.9.1",
|
||||
"@testing-library/react": "^16.3.2",
|
||||
@@ -29,14 +36,18 @@
|
||||
"@types/react": "^19.2.7",
|
||||
"@types/react-dom": "^19.2.3",
|
||||
"@vitejs/plugin-react": "^5.1.4",
|
||||
"@vitest/browser-playwright": "4.1.0",
|
||||
"@vitest/coverage-v8": "^4.1.0",
|
||||
"babel-plugin-react-compiler": "^1.0.0",
|
||||
"eslint": "^9.39.1",
|
||||
"eslint-plugin-react-hooks": "^7.0.1",
|
||||
"eslint-plugin-react-refresh": "^0.4.24",
|
||||
"eslint-plugin-storybook": "^10.3.3",
|
||||
"globals": "^16.5.0",
|
||||
"jsdom": "^29.0.0",
|
||||
"msw": "^2.12.11",
|
||||
"playwright": "^1.58.2",
|
||||
"storybook": "^10.3.3",
|
||||
"typescript": "~5.9.3",
|
||||
"typescript-eslint": "^8.48.0",
|
||||
"vite": "^7.3.1",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export * from "./login";
|
||||
export * from "./register";
|
||||
export * from "./logout";
|
||||
export * from "../../../../shared/api/calls/refresh";
|
||||
export * from "./mocks";
|
||||
|
||||
@@ -3,4 +3,7 @@ export * from "./types/service";
|
||||
export * from "./types/store";
|
||||
|
||||
// Stores
|
||||
export * from "./stores/authStore/authStore";
|
||||
export * from "./stores";
|
||||
|
||||
// Selectors
|
||||
export * from "./selectors";
|
||||
|
||||
3
src/features/auth/model/selectors/index.ts
Normal file
3
src/features/auth/model/selectors/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./selectFormValid/selectFormValid";
|
||||
export * from "./selectAuthData/selectAuthData";
|
||||
export * from "./selectStatusIsLoading/selectStatusIsLoading";
|
||||
@@ -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);
|
||||
@@ -0,0 +1,18 @@
|
||||
import { useAuthStore } from "../../stores";
|
||||
import { selectStatusIsLoading } from "./selectStatusIsLoading";
|
||||
|
||||
describe("selectStatusIsLoading", () => {
|
||||
afterEach(() => {
|
||||
useAuthStore.getState().reset();
|
||||
});
|
||||
|
||||
it("should return true when status is 'loading'", () => {
|
||||
useAuthStore.setState({ status: "loading" });
|
||||
expect(selectStatusIsLoading(useAuthStore.getState())).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false when status is not 'loading'", () => {
|
||||
useAuthStore.setState({ status: "idle" });
|
||||
expect(selectStatusIsLoading(useAuthStore.getState())).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
import type { AuthStore } from "../../types/store";
|
||||
|
||||
export const selectStatusIsLoading = (state: AuthStore) =>
|
||||
state.status === "loading";
|
||||
@@ -1,10 +1,5 @@
|
||||
import { setupServer } from "msw/node";
|
||||
import {
|
||||
loginMock,
|
||||
registerMock,
|
||||
logoutMock,
|
||||
refreshMock,
|
||||
} from "../../../api/calls";
|
||||
import * as apiCalls from "../../../api/calls";
|
||||
import { defaultStoreState, useAuthStore } from "./authStore";
|
||||
import {
|
||||
MOCK_EMAIL,
|
||||
@@ -12,7 +7,15 @@ import {
|
||||
MOCK_PASSWORD,
|
||||
} from "../../../api/calls/mocks";
|
||||
|
||||
const server = setupServer(loginMock, registerMock, logoutMock, refreshMock);
|
||||
const server = setupServer(
|
||||
apiCalls.loginMock,
|
||||
apiCalls.registerMock,
|
||||
apiCalls.logoutMock,
|
||||
);
|
||||
|
||||
const loginSpy = vi.spyOn(apiCalls, "login");
|
||||
const registerSpy = vi.spyOn(apiCalls, "register");
|
||||
const logoutSpy = vi.spyOn(apiCalls, "logout");
|
||||
|
||||
describe("authStore", () => {
|
||||
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
|
||||
@@ -22,6 +25,24 @@ 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).toMatchObject({
|
||||
value: MOCK_NEW_EMAIL,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("setPassword", () => {
|
||||
it("should set the password in formData", () => {
|
||||
useAuthStore.getState().setPassword(MOCK_PASSWORD);
|
||||
expect(useAuthStore.getState().formData.password).toMatchObject({
|
||||
value: MOCK_PASSWORD,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("reset", () => {
|
||||
it("should reset the store to default state", () => {
|
||||
useAuthStore.getState().reset();
|
||||
@@ -30,10 +51,33 @@ describe("authStore", () => {
|
||||
});
|
||||
|
||||
describe("login", () => {
|
||||
it("should not do api call if status is loading", async () => {
|
||||
useAuthStore.getState().setEmail(MOCK_EMAIL);
|
||||
useAuthStore.getState().setPassword(MOCK_PASSWORD);
|
||||
useAuthStore.setState({ status: "loading" });
|
||||
|
||||
await useAuthStore.getState().login();
|
||||
|
||||
expect(loginSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not do api call if form is invalid", async () => {
|
||||
useAuthStore.getState().setEmail("");
|
||||
useAuthStore.getState().setPassword("");
|
||||
|
||||
await useAuthStore.getState().login();
|
||||
|
||||
const { status } = useAuthStore.getState();
|
||||
|
||||
expect(loginSpy).not.toHaveBeenCalled();
|
||||
expect(status).toBe("idle");
|
||||
});
|
||||
|
||||
it("should set access token, user data, and update status after successful login", async () => {
|
||||
await useAuthStore
|
||||
.getState()
|
||||
.login({ email: MOCK_EMAIL, password: MOCK_PASSWORD });
|
||||
useAuthStore.getState().setEmail(MOCK_EMAIL);
|
||||
useAuthStore.getState().setPassword(MOCK_PASSWORD);
|
||||
|
||||
await useAuthStore.getState().login();
|
||||
|
||||
const { user, status, error } = useAuthStore.getState();
|
||||
|
||||
@@ -43,9 +87,10 @@ describe("authStore", () => {
|
||||
});
|
||||
|
||||
it("should set error and update status if login fails", async () => {
|
||||
await useAuthStore
|
||||
.getState()
|
||||
.login({ email: "wrong@test.com", password: "wrongPassword" });
|
||||
useAuthStore.getState().setEmail("wrong@test.com");
|
||||
useAuthStore.getState().setPassword("wrongPassword");
|
||||
|
||||
await useAuthStore.getState().login();
|
||||
|
||||
const { status, error } = useAuthStore.getState();
|
||||
|
||||
@@ -55,10 +100,33 @@ describe("authStore", () => {
|
||||
});
|
||||
|
||||
describe("register", () => {
|
||||
it("should not do api call if status is loading", async () => {
|
||||
useAuthStore.getState().setEmail(MOCK_EMAIL);
|
||||
useAuthStore.getState().setPassword(MOCK_PASSWORD);
|
||||
useAuthStore.setState({ status: "loading" });
|
||||
|
||||
await useAuthStore.getState().register();
|
||||
|
||||
expect(registerSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not do api call if form is invalid", async () => {
|
||||
useAuthStore.getState().setEmail("");
|
||||
useAuthStore.getState().setPassword("");
|
||||
|
||||
await useAuthStore.getState().register();
|
||||
|
||||
const { status } = useAuthStore.getState();
|
||||
|
||||
expect(registerSpy).not.toHaveBeenCalled();
|
||||
expect(status).toBe("idle");
|
||||
});
|
||||
|
||||
it("should set access token, user data, and update status after successful registration", async () => {
|
||||
await useAuthStore
|
||||
.getState()
|
||||
.register({ email: MOCK_NEW_EMAIL, password: MOCK_PASSWORD });
|
||||
useAuthStore.getState().setEmail(MOCK_NEW_EMAIL);
|
||||
useAuthStore.getState().setPassword(MOCK_PASSWORD);
|
||||
|
||||
await useAuthStore.getState().register();
|
||||
|
||||
const { user, status, error } = useAuthStore.getState();
|
||||
|
||||
@@ -68,9 +136,10 @@ describe("authStore", () => {
|
||||
});
|
||||
|
||||
it("should set error and update status if registration fails", async () => {
|
||||
await useAuthStore
|
||||
.getState()
|
||||
.register({ email: MOCK_EMAIL, password: MOCK_PASSWORD });
|
||||
useAuthStore.getState().setEmail(MOCK_EMAIL);
|
||||
useAuthStore.getState().setPassword(MOCK_PASSWORD);
|
||||
|
||||
await useAuthStore.getState().register();
|
||||
|
||||
const { status, error } = useAuthStore.getState();
|
||||
|
||||
@@ -80,6 +149,14 @@ describe("authStore", () => {
|
||||
});
|
||||
|
||||
describe("logout", () => {
|
||||
it("should not do api call if status is loading", async () => {
|
||||
useAuthStore.setState({ status: "loading" });
|
||||
|
||||
await useAuthStore.getState().logout();
|
||||
|
||||
expect(logoutSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should clear access token, user data, and update status after logout", async () => {
|
||||
await useAuthStore.getState().logout();
|
||||
|
||||
|
||||
@@ -3,19 +3,66 @@ import type { AuthStore, AuthStoreState } from "../../types/store";
|
||||
import { login, logout, register } from "../../../api";
|
||||
import { callApi } from "shared/utils";
|
||||
import { UNEXPECTED_ERROR_MESSAGE } from "shared/api";
|
||||
import { selectAuthData, selectFormValid } from "../../selectors";
|
||||
|
||||
export const defaultStoreState: Readonly<AuthStoreState> = {
|
||||
formData: {
|
||||
email: { value: "", valid: false },
|
||||
password: { value: "", valid: false },
|
||||
},
|
||||
user: undefined,
|
||||
status: "idle",
|
||||
error: null,
|
||||
};
|
||||
|
||||
export const useAuthStore = create<AuthStore>()((set) => ({
|
||||
function validateEmail(email: string): boolean {
|
||||
return Boolean(email);
|
||||
}
|
||||
|
||||
function validatePassword(password: string): boolean {
|
||||
return Boolean(password);
|
||||
}
|
||||
|
||||
export const useAuthStore = create<AuthStore>()((set, get) => ({
|
||||
...defaultStoreState,
|
||||
reset: () => set(defaultStoreState),
|
||||
login: async (loginData) => {
|
||||
|
||||
setEmail: (email: string) => {
|
||||
const isValid = validateEmail(email);
|
||||
set((state) => ({
|
||||
formData: { ...state.formData, email: { value: email, valid: isValid } },
|
||||
}));
|
||||
},
|
||||
setPassword: (password: string) => {
|
||||
const isValid = validatePassword(password);
|
||||
set((state) => ({
|
||||
formData: {
|
||||
...state.formData,
|
||||
password: { value: password, valid: isValid },
|
||||
},
|
||||
}));
|
||||
},
|
||||
reset: () => {
|
||||
set(defaultStoreState);
|
||||
},
|
||||
|
||||
login: async () => {
|
||||
const { status } = get();
|
||||
|
||||
if (status === "loading") {
|
||||
return;
|
||||
}
|
||||
|
||||
set({ status: "loading" });
|
||||
|
||||
const formValid = selectFormValid(get());
|
||||
|
||||
if (!formValid) {
|
||||
set({ status: "idle" });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const loginData = selectAuthData(get());
|
||||
const [responseData, loginError] = await callApi(() => login(loginData));
|
||||
|
||||
if (loginError) {
|
||||
@@ -28,8 +75,6 @@ export const useAuthStore = create<AuthStore>()((set) => ({
|
||||
user: responseData?.user,
|
||||
error: null,
|
||||
});
|
||||
|
||||
// useTokenStore.setState({ accessToken: responseData?.accessToken });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
set({
|
||||
@@ -38,8 +83,24 @@ export const useAuthStore = create<AuthStore>()((set) => ({
|
||||
});
|
||||
}
|
||||
},
|
||||
register: async (registerData) => {
|
||||
register: async () => {
|
||||
const { status } = get();
|
||||
|
||||
if (status === "loading") {
|
||||
return;
|
||||
}
|
||||
|
||||
set({ status: "loading" });
|
||||
|
||||
const formValid = selectFormValid(get());
|
||||
|
||||
if (!formValid) {
|
||||
set({ status: "idle" });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const registerData = selectAuthData(get());
|
||||
const [responseData, registerError] = await callApi(() =>
|
||||
register(registerData),
|
||||
);
|
||||
@@ -54,8 +115,6 @@ export const useAuthStore = create<AuthStore>()((set) => ({
|
||||
user: responseData?.user,
|
||||
error: null,
|
||||
});
|
||||
|
||||
// useTokenStore.setState({ accessToken: responseData?.accessToken });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
set({
|
||||
@@ -65,12 +124,18 @@ export const useAuthStore = create<AuthStore>()((set) => ({
|
||||
}
|
||||
},
|
||||
logout: async () => {
|
||||
const prevStatus = get().status;
|
||||
|
||||
if (prevStatus === "loading") {
|
||||
return;
|
||||
}
|
||||
|
||||
set({ status: "loading" });
|
||||
try {
|
||||
const [, logoutError] = await callApi(() => logout());
|
||||
|
||||
if (logoutError) {
|
||||
set({ error: logoutError });
|
||||
set({ error: logoutError, status: prevStatus });
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
1
src/features/auth/model/stores/index.ts
Normal file
1
src/features/auth/model/stores/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { useAuthStore } from "./authStore/authStore";
|
||||
@@ -2,9 +2,20 @@ import type { User } from "entities/User";
|
||||
import type { AuthData, AuthStatus } from "./service";
|
||||
import type { ApiError } from "shared/utils";
|
||||
|
||||
export type AuthFormData = {
|
||||
[K in keyof AuthData]: {
|
||||
value: AuthData[K];
|
||||
valid: boolean;
|
||||
};
|
||||
};
|
||||
|
||||
export interface AuthStoreState {
|
||||
/**
|
||||
* User's credentials
|
||||
* Form data for login/register forms
|
||||
*/
|
||||
formData: AuthFormData;
|
||||
/**
|
||||
* Current user
|
||||
*/
|
||||
user?: User;
|
||||
/**
|
||||
@@ -17,16 +28,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: () => Promise<void>;
|
||||
register: () => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
}
|
||||
|
||||
export type AuthStore = AuthStoreState & AuthStoreActions;
|
||||
|
||||
89
src/features/auth/ui/LoginForm/LoginForm.spec.tsx
Normal file
89
src/features/auth/ui/LoginForm/LoginForm.spec.tsx
Normal file
@@ -0,0 +1,89 @@
|
||||
import { selectAuthData, useAuthStore } from "../../model";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { LoginForm } from "./LoginForm";
|
||||
import { MOCK_EMAIL, MOCK_PASSWORD } from "../../api";
|
||||
|
||||
describe("LoginForm", () => {
|
||||
afterEach(() => {
|
||||
useAuthStore.getState().reset();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("should render form", () => {
|
||||
render(<LoginForm />);
|
||||
const form = screen.getByRole("form");
|
||||
|
||||
expect(form).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("disables submit button when form is invalid", () => {
|
||||
render(<LoginForm />);
|
||||
const loginButton = screen.getByRole("button", { name: /login/i });
|
||||
|
||||
expect(loginButton).toBeDisabled();
|
||||
});
|
||||
|
||||
it("disables submit button when auth store status equals loading", async () => {
|
||||
useAuthStore.setState({ status: "loading" });
|
||||
|
||||
render(<LoginForm />);
|
||||
|
||||
const emailInput = screen.getByRole("textbox", { name: /email/i });
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const loginButton = screen.getByRole("button", { name: /login/i });
|
||||
await userEvent.type(emailInput, MOCK_EMAIL);
|
||||
await userEvent.type(passwordInput, MOCK_PASSWORD);
|
||||
|
||||
expect(loginButton).toBeDisabled();
|
||||
});
|
||||
|
||||
it("enables submit button when form is valid and auth store status isn't equal to loading", async () => {
|
||||
useAuthStore.setState({ status: "idle" });
|
||||
|
||||
render(<LoginForm />);
|
||||
|
||||
const emailInput = screen.getByRole("textbox", { name: /email/i });
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const loginButton = screen.getByRole("button", { name: /login/i });
|
||||
await userEvent.type(emailInput, MOCK_EMAIL);
|
||||
await userEvent.type(passwordInput, MOCK_PASSWORD);
|
||||
|
||||
expect(loginButton).toBeEnabled();
|
||||
});
|
||||
|
||||
it("updates email value in auth store when user types", async () => {
|
||||
render(<LoginForm />);
|
||||
const emailInput = screen.getByRole("textbox", { name: /email/i });
|
||||
await userEvent.type(emailInput, MOCK_EMAIL);
|
||||
|
||||
const storeEmailValue = selectAuthData(useAuthStore.getState()).email;
|
||||
expect(storeEmailValue).toBe(MOCK_EMAIL);
|
||||
});
|
||||
|
||||
it("updates password value in auth store when user types", async () => {
|
||||
render(<LoginForm />);
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
await userEvent.type(passwordInput, MOCK_PASSWORD);
|
||||
|
||||
const storePasswordValue = selectAuthData(useAuthStore.getState()).password;
|
||||
expect(storePasswordValue).toBe(MOCK_PASSWORD);
|
||||
});
|
||||
|
||||
it("calls login when submit form is valid and user clicks submit", async () => {
|
||||
const loginSpy = vi.spyOn(useAuthStore.getState(), "login");
|
||||
useAuthStore.setState({ status: "idle" });
|
||||
|
||||
render(<LoginForm />);
|
||||
|
||||
const emailInput = screen.getByRole("textbox", { name: /email/i });
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const submitButton = screen.getByRole("button", { name: /login/i });
|
||||
|
||||
await userEvent.type(emailInput, MOCK_EMAIL);
|
||||
await userEvent.type(passwordInput, MOCK_PASSWORD);
|
||||
await userEvent.click(submitButton);
|
||||
|
||||
expect(loginSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
12
src/features/auth/ui/LoginForm/LoginForm.stories.ts
Normal file
12
src/features/auth/ui/LoginForm/LoginForm.stories.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { Meta, StoryObj } from "@storybook/react-vite";
|
||||
import { LoginForm } from "./LoginForm";
|
||||
|
||||
const meta: Meta<typeof LoginForm> = {
|
||||
component: LoginForm,
|
||||
title: "features/auth/LoginForm",
|
||||
};
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof meta>;
|
||||
|
||||
export const Default: Story = {};
|
||||
@@ -1,5 +1,51 @@
|
||||
import {
|
||||
selectFormValid,
|
||||
selectStatusIsLoading,
|
||||
useAuthStore,
|
||||
} from "../../model";
|
||||
import type { SubmitEvent, ChangeEvent } from "react";
|
||||
|
||||
/**
|
||||
* Login form component
|
||||
*/
|
||||
export function LoginForm() {
|
||||
const { formData, setEmail, setPassword, login } = useAuthStore();
|
||||
|
||||
const formValid = useAuthStore(selectFormValid);
|
||||
const isLoading = useAuthStore(selectStatusIsLoading);
|
||||
|
||||
const disabled = !formValid || isLoading;
|
||||
|
||||
const handleEmailChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
setEmail(e.target.value);
|
||||
};
|
||||
|
||||
const handlePasswordChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
setPassword(e.target.value);
|
||||
};
|
||||
|
||||
const handleSubmit = (e: SubmitEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
login();
|
||||
};
|
||||
|
||||
return (
|
||||
<form>Login Form</form>
|
||||
<form aria-label="Login form" onSubmit={handleSubmit}>
|
||||
<input
|
||||
type="email"
|
||||
aria-label="Email"
|
||||
value={formData?.email?.value ?? ""}
|
||||
onChange={handleEmailChange}
|
||||
/>
|
||||
<input
|
||||
type="password"
|
||||
aria-label="Password"
|
||||
value={formData?.password?.value ?? ""}
|
||||
onChange={handlePasswordChange}
|
||||
/>
|
||||
<button type="submit" disabled={disabled}>
|
||||
Login
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
115
src/features/auth/ui/RegisterForm/RegisterForm.spec.tsx
Normal file
115
src/features/auth/ui/RegisterForm/RegisterForm.spec.tsx
Normal file
@@ -0,0 +1,115 @@
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { RegisterForm } from "./RegisterForm";
|
||||
import { selectAuthData, useAuthStore } from "../../model";
|
||||
import { MOCK_NEW_EMAIL, MOCK_PASSWORD } from "../../api";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
|
||||
describe("RegisterForm", () => {
|
||||
afterEach(() => {
|
||||
useAuthStore.getState().reset();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("should render form", () => {
|
||||
render(<RegisterForm />);
|
||||
|
||||
const form = screen.getByRole("form");
|
||||
expect(form).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("should disable button when form is invalid", async () => {
|
||||
render(<RegisterForm />);
|
||||
|
||||
const registerButton = screen.getByRole("button", { name: /register/i });
|
||||
|
||||
expect(registerButton).toBeDisabled();
|
||||
});
|
||||
|
||||
it("should disable button when auth store status equals loading", async () => {
|
||||
useAuthStore.setState({ status: "loading" });
|
||||
|
||||
render(<RegisterForm />);
|
||||
|
||||
const emailInput = screen.getByRole("textbox", { name: /email/i });
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const confirmPasswordInput = screen.getByLabelText(/confirm/i);
|
||||
const registerButton = screen.getByRole("button", { name: /register/i });
|
||||
|
||||
await userEvent.type(emailInput, MOCK_NEW_EMAIL);
|
||||
await userEvent.type(passwordInput, MOCK_PASSWORD);
|
||||
await userEvent.type(confirmPasswordInput, MOCK_PASSWORD);
|
||||
|
||||
expect(registerButton).toBeDisabled();
|
||||
});
|
||||
|
||||
it("should disable button when password and confirm password do not match", async () => {
|
||||
render(<RegisterForm />);
|
||||
|
||||
const emailInput = screen.getByRole("textbox", { name: /email/i });
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const confirmPasswordInput = screen.getByLabelText(/confirm/i);
|
||||
const registerButton = screen.getByRole("button", { name: /register/i });
|
||||
|
||||
await userEvent.type(emailInput, MOCK_NEW_EMAIL);
|
||||
await userEvent.type(passwordInput, MOCK_PASSWORD);
|
||||
await userEvent.type(confirmPasswordInput, MOCK_PASSWORD + "1");
|
||||
|
||||
expect(registerButton).toBeDisabled();
|
||||
});
|
||||
|
||||
it("should enable button when password and confirm password match and auth store status isn't equal to loading", async () => {
|
||||
useAuthStore.setState({ status: "idle" });
|
||||
|
||||
render(<RegisterForm />);
|
||||
|
||||
const emailInput = screen.getByRole("textbox", { name: /email/i });
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const confirmPasswordInput = screen.getByLabelText(/confirm/i);
|
||||
const registerButton = screen.getByRole("button", { name: /register/i });
|
||||
|
||||
await userEvent.type(emailInput, MOCK_NEW_EMAIL);
|
||||
await userEvent.type(passwordInput, MOCK_PASSWORD);
|
||||
await userEvent.type(confirmPasswordInput, MOCK_PASSWORD);
|
||||
|
||||
expect(registerButton).not.toBeDisabled();
|
||||
});
|
||||
|
||||
it("should change email value in auth store when user types", async () => {
|
||||
render(<RegisterForm />);
|
||||
const emailInput = screen.getByRole("textbox", { name: /email/i });
|
||||
await userEvent.type(emailInput, MOCK_NEW_EMAIL);
|
||||
|
||||
const storeEmailValue = selectAuthData(useAuthStore.getState()).email;
|
||||
|
||||
expect(storeEmailValue).toBe(MOCK_NEW_EMAIL);
|
||||
});
|
||||
|
||||
it("should change password value in auth store when user types", async () => {
|
||||
render(<RegisterForm />);
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
await userEvent.type(passwordInput, MOCK_PASSWORD);
|
||||
|
||||
const storePasswordValue = selectAuthData(useAuthStore.getState()).password;
|
||||
|
||||
expect(storePasswordValue).toBe(MOCK_PASSWORD);
|
||||
});
|
||||
|
||||
it("should perform register api call when user clicks register button", async () => {
|
||||
const registerSpy = vi.spyOn(useAuthStore.getState(), "register");
|
||||
useAuthStore.setState({ status: "idle" });
|
||||
|
||||
render(<RegisterForm />);
|
||||
|
||||
const emailInput = screen.getByRole("textbox", { name: /email/i });
|
||||
const passwordInput = screen.getByLabelText(/password/i);
|
||||
const confirmPasswordInput = screen.getByLabelText(/confirm/i);
|
||||
const registerButton = screen.getByRole("button", { name: /register/i });
|
||||
|
||||
await userEvent.type(emailInput, MOCK_NEW_EMAIL);
|
||||
await userEvent.type(passwordInput, MOCK_PASSWORD);
|
||||
await userEvent.type(confirmPasswordInput, MOCK_PASSWORD);
|
||||
await userEvent.click(registerButton);
|
||||
|
||||
expect(registerSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
12
src/features/auth/ui/RegisterForm/RegisterForm.stories.ts
Normal file
12
src/features/auth/ui/RegisterForm/RegisterForm.stories.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { Meta, StoryObj } from "@storybook/react-vite";
|
||||
import { RegisterForm } from "./RegisterForm";
|
||||
|
||||
const meta: Meta<typeof RegisterForm> = {
|
||||
component: RegisterForm,
|
||||
title: "features/auth/RegisterForm",
|
||||
};
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof meta>;
|
||||
|
||||
export const Default: Story = {};
|
||||
@@ -1,5 +1,64 @@
|
||||
import { useState, type ChangeEvent, type SubmitEvent } from "react";
|
||||
import {
|
||||
selectFormValid,
|
||||
selectStatusIsLoading,
|
||||
useAuthStore,
|
||||
} from "../../model";
|
||||
|
||||
/**
|
||||
* Register form component
|
||||
*/
|
||||
export function RegisterForm() {
|
||||
const { formData, setEmail, setPassword, register } = useAuthStore();
|
||||
|
||||
const [confirmedPassword, setConfirmedPassword] = useState("");
|
||||
|
||||
const formValid = useAuthStore(selectFormValid);
|
||||
const isLoading = useAuthStore(selectStatusIsLoading);
|
||||
|
||||
const passwordMatch = formData?.password?.value === confirmedPassword;
|
||||
const disabled = isLoading || !formValid || !passwordMatch;
|
||||
|
||||
const handleEmailChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
setEmail(e.target.value);
|
||||
};
|
||||
|
||||
const handlePasswordChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
setPassword(e.target.value);
|
||||
};
|
||||
|
||||
const handleConfirmChange = (e: ChangeEvent<HTMLInputElement>) => {
|
||||
setConfirmedPassword(e.target.value);
|
||||
};
|
||||
|
||||
const handleSubmit = (e: SubmitEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
register();
|
||||
};
|
||||
|
||||
return (
|
||||
<form>Register Form</form>
|
||||
<form aria-label="Register Form" onSubmit={handleSubmit}>
|
||||
<input
|
||||
type="email"
|
||||
aria-label="Email"
|
||||
value={formData?.email?.value ?? ""}
|
||||
onChange={handleEmailChange}
|
||||
/>
|
||||
<input
|
||||
type="password"
|
||||
aria-label="Password"
|
||||
value={formData?.password?.value ?? ""}
|
||||
onChange={handlePasswordChange}
|
||||
/>
|
||||
<input
|
||||
type="password"
|
||||
aria-label="Confirm"
|
||||
value={confirmedPassword}
|
||||
onChange={handleConfirmChange}
|
||||
/>
|
||||
<button type="submit" aria-label="Register" disabled={disabled}>
|
||||
Register
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,32 +1,59 @@
|
||||
import { defineConfig } from "vitest/config";
|
||||
import react from "@vitejs/plugin-react";
|
||||
import path from "path";
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { storybookTest } from '@storybook/addon-vitest/vitest-plugin';
|
||||
import { playwright } from '@vitest/browser-playwright';
|
||||
const dirname = typeof __dirname !== 'undefined' ? __dirname : path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
// More info at: https://storybook.js.org/docs/next/writing-tests/integrations/vitest-addon
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
react({
|
||||
plugins: [react({
|
||||
babel: {
|
||||
plugins: [["babel-plugin-react-compiler", {}]],
|
||||
},
|
||||
}),
|
||||
],
|
||||
plugins: [["babel-plugin-react-compiler", {}]]
|
||||
}
|
||||
})],
|
||||
resolve: {
|
||||
alias: {
|
||||
shared: path.resolve(__dirname, "src/shared"),
|
||||
entities: path.resolve(__dirname, "src/entities"),
|
||||
features: path.resolve(__dirname, "src/features"),
|
||||
widgets: path.resolve(__dirname, "src/widgets"),
|
||||
app: path.resolve(__dirname, "src/app"),
|
||||
},
|
||||
app: path.resolve(__dirname, "src/app")
|
||||
}
|
||||
},
|
||||
test: {
|
||||
globals: true,
|
||||
environment: "jsdom",
|
||||
setupFiles: ["./src/shared/config/test/setup.ts"],
|
||||
coverage: {
|
||||
provider: "v8",
|
||||
reporter: ["text", "json", "html"],
|
||||
exclude: ["node_modules/", "src/test/"],
|
||||
},
|
||||
exclude: ["node_modules/", "src/test/"]
|
||||
},
|
||||
projects: [{
|
||||
extends: true,
|
||||
test: {
|
||||
globals: true,
|
||||
environment: "jsdom",
|
||||
setupFiles: ["./src/shared/config/test/setup.ts"]
|
||||
}
|
||||
}, {
|
||||
extends: true,
|
||||
plugins: [
|
||||
// The plugin will run tests for the stories defined in your Storybook config
|
||||
// See options at: https://storybook.js.org/docs/next/writing-tests/integrations/vitest-addon#storybooktest
|
||||
storybookTest({
|
||||
configDir: path.join(dirname, '.storybook')
|
||||
})],
|
||||
test: {
|
||||
name: 'storybook',
|
||||
browser: {
|
||||
enabled: true,
|
||||
headless: true,
|
||||
provider: playwright({}),
|
||||
instances: [{
|
||||
browser: 'chromium'
|
||||
}]
|
||||
}
|
||||
}
|
||||
}]
|
||||
}
|
||||
});
|
||||
1
vitest.shims.d.ts
vendored
Normal file
1
vitest.shims.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/// <reference types="@vitest/browser-playwright" />
|
||||
Reference in New Issue
Block a user