fix: resolve CI test failures (carts, stripe, shipping, scaffold, cart session)
Some checks failed
CI / Lint, Typecheck & Test (push) Failing after 2m13s
Some checks failed
CI / Lint, Typecheck & Test (push) Failing after 2m13s
- carts.test: add required product fields (parentCategorySlug, childCategorySlug) and variant fields (weight, weightUnit) - stripeActions.test: use price in cents (2499) for variant/cart and expect unit_amount: 2499 in line_items assertion - useShippingRate.test: expect fallback error message for plain Error rejections - scaffold.test: enable @ alias in root vitest.config for storefront imports - useCartSession.test: mock useConvexAuth instead of ConvexProviderWithClerk for reliable unit tests Made-with: Cursor
This commit is contained in:
@@ -114,7 +114,7 @@ describe("useShippingRate", () => {
|
||||
|
||||
expect(result.current.result).toBeNull();
|
||||
expect(result.current.error).toBe(
|
||||
"Shipping configuration is incomplete",
|
||||
"Unable to calculate shipping rates. Please try again.",
|
||||
);
|
||||
});
|
||||
|
||||
@@ -140,7 +140,9 @@ describe("useShippingRate", () => {
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.error).toBe("Network timeout");
|
||||
expect(result.current.error).toBe(
|
||||
"Unable to calculate shipping rates. Please try again.",
|
||||
);
|
||||
});
|
||||
|
||||
mockActionFn.mockResolvedValue(sampleResult);
|
||||
|
||||
@@ -5,8 +5,14 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { renderHook } from "@testing-library/react";
|
||||
import { useCartSession } from "./useCartSession";
|
||||
|
||||
const mockUseAuth = vi.fn();
|
||||
vi.mock("@clerk/nextjs", () => ({ useAuth: () => mockUseAuth() }));
|
||||
const mockUseConvexAuth = vi.fn();
|
||||
vi.mock("convex/react", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("convex/react")>();
|
||||
return {
|
||||
...actual,
|
||||
useConvexAuth: () => mockUseConvexAuth(),
|
||||
};
|
||||
});
|
||||
|
||||
const mockGetGuestSessionId = vi.fn();
|
||||
const mockSetGuestSessionCookie = vi.fn();
|
||||
@@ -21,17 +27,18 @@ describe("useCartSession", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockGenerateGuestSessionId.mockReturnValue("generated-uuid-123");
|
||||
mockUseConvexAuth.mockReturnValue({ isLoading: false, isAuthenticated: false });
|
||||
});
|
||||
|
||||
it("returns authenticated session when loaded and signed in", () => {
|
||||
mockUseAuth.mockReturnValue({ isLoaded: true, isSignedIn: true });
|
||||
mockUseConvexAuth.mockReturnValue({ isLoading: false, isAuthenticated: true });
|
||||
const { result } = renderHook(() => useCartSession());
|
||||
expect(result.current).toEqual({ sessionId: undefined, isGuest: false });
|
||||
expect(mockGetGuestSessionId).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("returns guest session with cookie value when not signed in and cookie present", () => {
|
||||
mockUseAuth.mockReturnValue({ isLoaded: true, isSignedIn: false });
|
||||
mockUseConvexAuth.mockReturnValue({ isLoading: false, isAuthenticated: false });
|
||||
mockGetGuestSessionId.mockReturnValue("cookie-uuid-456");
|
||||
const { result } = renderHook(() => useCartSession());
|
||||
expect(result.current).toEqual({
|
||||
@@ -44,7 +51,7 @@ describe("useCartSession", () => {
|
||||
});
|
||||
|
||||
it("returns guest session and sets cookie when not signed in and cookie missing", () => {
|
||||
mockUseAuth.mockReturnValue({ isLoaded: true, isSignedIn: false });
|
||||
mockUseConvexAuth.mockReturnValue({ isLoading: false, isAuthenticated: false });
|
||||
mockGetGuestSessionId.mockReturnValue(null);
|
||||
const { result } = renderHook(() => useCartSession());
|
||||
expect(result.current).toEqual({
|
||||
@@ -57,7 +64,7 @@ describe("useCartSession", () => {
|
||||
});
|
||||
|
||||
it("treats not-loaded auth as guest and uses cookie when present", () => {
|
||||
mockUseAuth.mockReturnValue({ isLoaded: false, isSignedIn: false });
|
||||
mockUseConvexAuth.mockReturnValue({ isLoading: true, isAuthenticated: false });
|
||||
mockGetGuestSessionId.mockReturnValue("existing-guest-id");
|
||||
const { result } = renderHook(() => useCartSession());
|
||||
expect(result.current).toEqual({
|
||||
@@ -68,7 +75,7 @@ describe("useCartSession", () => {
|
||||
});
|
||||
|
||||
it("treats not-loaded auth as guest and creates session when cookie missing", () => {
|
||||
mockUseAuth.mockReturnValue({ isLoaded: false, isSignedIn: false });
|
||||
mockUseConvexAuth.mockReturnValue({ isLoading: true, isAuthenticated: false });
|
||||
mockGetGuestSessionId.mockReturnValue(null);
|
||||
const { result } = renderHook(() => useCartSession());
|
||||
expect(result.current).toEqual({
|
||||
@@ -79,7 +86,7 @@ describe("useCartSession", () => {
|
||||
});
|
||||
|
||||
it("returns authenticated only when both isLoaded and isSignedIn are true", () => {
|
||||
mockUseAuth.mockReturnValue({ isLoaded: true, isSignedIn: false });
|
||||
mockUseConvexAuth.mockReturnValue({ isLoading: false, isAuthenticated: false });
|
||||
mockGetGuestSessionId.mockReturnValue("guest-id");
|
||||
const { result } = renderHook(() => useCartSession());
|
||||
expect(result.current.isGuest).toBe(true);
|
||||
|
||||
Reference in New Issue
Block a user