test(ux-28): add integration test verifying singleton authmodal and cartdrawer in shell layout
This commit is contained in:
1 parent
23724df4e4
commit
c4d95762f8
1 file changed
+194
@@ -0,0 +1,194 @@
|
||||
import React from "react";
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { render, screen, cleanup } from "@testing-library/react";
|
||||
|
||||
// Mock next/navigation
|
||||
let currentPathname = "/";
|
||||
const mockRouter = {
|
||||
push: vi.fn(),
|
||||
replace: vi.fn(),
|
||||
back: vi.fn(),
|
||||
prefetch: vi.fn(),
|
||||
};
|
||||
|
||||
vi.mock("next/navigation", () => ({
|
||||
usePathname: () => currentPathname,
|
||||
useRouter: () => mockRouter,
|
||||
useSearchParams: () => new URLSearchParams(),
|
||||
}));
|
||||
|
||||
// Mock dynamic components to render test identifiers
|
||||
vi.mock("next/dynamic", () => ({
|
||||
default: (loader: any) => {
|
||||
const str = loader.toString();
|
||||
const isCart = str.includes("CartDrawer");
|
||||
const isAuth = str.includes("AuthModal");
|
||||
const isLogin = str.includes("LoginModal");
|
||||
return function MockDynamic(props: any) {
|
||||
if (props.isOpen === false) return null;
|
||||
if (isCart) return <div data-testid="shell-cart-drawer" data-open={String(props.isOpen)} />;
|
||||
if (isAuth) return <div data-testid="shell-auth-modal" data-open={String(props.isOpen)} />;
|
||||
if (isLogin) return <div data-testid="shell-login-modal" data-open={String(props.isOpen)} />;
|
||||
return <div data-testid="shell-dynamic-other" />;
|
||||
};
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../components/Header", () => ({
|
||||
default: () => <header data-testid="shell-header">Header</header>,
|
||||
}));
|
||||
|
||||
vi.mock("../components/Footer", () => ({
|
||||
default: () => <footer data-testid="shell-footer">Footer</footer>,
|
||||
}));
|
||||
|
||||
vi.mock("../components/NetworkBanner", () => ({
|
||||
NetworkBanner: () => null,
|
||||
}));
|
||||
|
||||
vi.mock("../components/NavigationProgressBar", () => ({
|
||||
default: () => null,
|
||||
}));
|
||||
|
||||
vi.mock("../components/MobileBottomNav", () => ({
|
||||
default: () => null,
|
||||
}));
|
||||
|
||||
vi.mock("../components/PrescriptionUploadModal", () => ({
|
||||
default: () => null,
|
||||
}));
|
||||
|
||||
vi.mock("sonner", () => ({
|
||||
Toaster: () => null,
|
||||
toast: { success: vi.fn(), error: vi.fn() },
|
||||
}));
|
||||
|
||||
// Mock real components used inside page routes
|
||||
vi.mock("../components/AuthModal", () => ({
|
||||
default: (props: any) => (
|
||||
<div data-testid="page-auth-modal" data-isopen={String(props.isOpen)} />
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock("../components/CartDrawer", () => ({
|
||||
default: (props: any) => (
|
||||
<div data-testid="page-cart-drawer" data-isopen={String(props.isOpen)} />
|
||||
),
|
||||
}));
|
||||
|
||||
// Mock stores
|
||||
const mockUserStoreState: any = {
|
||||
isAuthModalOpen: false,
|
||||
setAuthModalOpen: vi.fn(),
|
||||
fetchProfile: vi.fn().mockResolvedValue({}),
|
||||
user: null,
|
||||
};
|
||||
|
||||
vi.mock("../lib/store/userStore", () => {
|
||||
const store = (selector?: any) => (typeof selector === "function" ? selector(mockUserStoreState) : mockUserStoreState);
|
||||
store.getState = () => mockUserStoreState;
|
||||
return { useUserStore: store };
|
||||
});
|
||||
|
||||
const mockSettingsStoreState: any = {
|
||||
fetchSettings: vi.fn().mockResolvedValue({}),
|
||||
getText: vi.fn().mockReturnValue(""),
|
||||
getBoolean: vi.fn().mockReturnValue(false),
|
||||
};
|
||||
|
||||
vi.mock("../lib/store/settingsStore", () => {
|
||||
const store = (selector?: any) => (typeof selector === "function" ? selector(mockSettingsStoreState) : mockSettingsStoreState);
|
||||
store.getState = () => mockSettingsStoreState;
|
||||
return { useSettingsStore: store };
|
||||
});
|
||||
|
||||
const mockUIStoreState: any = {
|
||||
isCartOpen: false,
|
||||
isLoginModalOpen: false,
|
||||
isB2BPortalOpen: false,
|
||||
advisorData: null,
|
||||
setCartOpen: vi.fn(),
|
||||
setLoginModalOpen: vi.fn(),
|
||||
setB2BPortalOpen: vi.fn(),
|
||||
clearAdvisorData: vi.fn(),
|
||||
};
|
||||
|
||||
vi.mock("../lib/store/uiStore", () => {
|
||||
const store = (selector?: any) => (typeof selector === "function" ? selector(mockUIStoreState) : mockUIStoreState);
|
||||
store.getState = () => mockUIStoreState;
|
||||
return { useUIStore: store };
|
||||
});
|
||||
|
||||
import ClientLayout from "../app/ClientLayout";
|
||||
import LoginPage from "../app/login/page";
|
||||
import CartPage from "../app/cart/page";
|
||||
|
||||
describe("ClientLayout Shell & Singleton Modals Integration (UX-28)", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockUserStoreState.isAuthModalOpen = false;
|
||||
mockUIStoreState.isCartOpen = false;
|
||||
currentPathname = "/";
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
});
|
||||
|
||||
it("renders singleton CartDrawer and AuthModal in ClientLayout on normal routes when opened", () => {
|
||||
mockUIStoreState.isCartOpen = true;
|
||||
mockUserStoreState.isAuthModalOpen = true;
|
||||
|
||||
render(
|
||||
<ClientLayout>
|
||||
<div>Content</div>
|
||||
</ClientLayout>
|
||||
);
|
||||
|
||||
// On normal route, Header, Footer, and active drawers are rendered
|
||||
expect(screen.getByTestId("shell-header")).toBeTruthy();
|
||||
expect(screen.getByTestId("shell-footer")).toBeTruthy();
|
||||
expect(screen.getByTestId("shell-cart-drawer")).toBeTruthy();
|
||||
expect(screen.getByTestId("shell-auth-modal")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("ensures no duplicate CartDrawer exists when user visits /cart utility route", () => {
|
||||
currentPathname = "/cart";
|
||||
mockUIStoreState.isCartOpen = true; // Even if UI store flag was left open
|
||||
|
||||
const { container } = render(
|
||||
<ClientLayout>
|
||||
<CartPage />
|
||||
</ClientLayout>
|
||||
);
|
||||
|
||||
// Footer and shell-level CartDrawer must be suppressed on /cart
|
||||
expect(screen.queryByTestId("shell-footer")).toBeNull();
|
||||
|
||||
// Page level cart drawer is present
|
||||
expect(screen.getByTestId("page-cart-drawer")).toBeTruthy();
|
||||
|
||||
// Shell level dynamic cart drawer is NOT rendered because isUtilityRoute is true
|
||||
expect(screen.queryByTestId("shell-cart-drawer")).toBeNull();
|
||||
});
|
||||
|
||||
it("ensures no duplicate AuthModal exists when user visits /login utility route", () => {
|
||||
currentPathname = "/login";
|
||||
mockUserStoreState.isAuthModalOpen = true; // Even if userStore auth flag was left open
|
||||
|
||||
const { container } = render(
|
||||
<ClientLayout>
|
||||
<LoginPage />
|
||||
</ClientLayout>
|
||||
);
|
||||
|
||||
// Footer and shell-level AuthModal must be suppressed on /login
|
||||
expect(screen.queryByTestId("shell-footer")).toBeNull();
|
||||
|
||||
// Page level auth modal is present
|
||||
expect(screen.getByTestId("page-auth-modal")).toBeTruthy();
|
||||
|
||||
// Shell level dynamic auth modal is NOT rendered because isUtilityRoute is true
|
||||
expect(screen.queryByTestId("shell-auth-modal")).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user