Jelajahi Sumber

test: harden logs page mode assertions

ding113 1 Minggu lalu
induk
melakukan
072aed3c2d
1 mengubah file dengan 46 tambahan dan 6 penghapusan
  1. 46 6
      tests/unit/dashboard/logs/page.test.tsx

+ 46 - 6
tests/unit/dashboard/logs/page.test.tsx

@@ -1,5 +1,5 @@
-import { Children, isValidElement, type ReactNode } from "react";
-import { describe, expect, it, vi } from "vitest";
+import { Children, isValidElement, type ReactElement, type ReactNode } from "react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
 
 const authMocks = vi.hoisted(() => ({
   getSession: vi.fn(),
@@ -29,11 +29,48 @@ vi.mock("@/app/[locale]/dashboard/logs/_components/usage-logs-skeleton", () => (
   UsageLogsSkeleton: () => null,
 }));
 
+function MockUsageLogsActiveSessionsSection() {
+  return null;
+}
+
+MockUsageLogsActiveSessionsSection.displayName = "UsageLogsActiveSessionsSection";
+
+function MockUsageLogsDataSection() {
+  return null;
+}
+
+MockUsageLogsDataSection.displayName = "UsageLogsDataSection";
+
 vi.mock("@/app/[locale]/dashboard/logs/_components/usage-logs-sections", () => ({
-  UsageLogsActiveSessionsSection: () => null,
-  UsageLogsDataSection: () => null,
+  UsageLogsActiveSessionsSection: MockUsageLogsActiveSessionsSection,
+  UsageLogsDataSection: MockUsageLogsDataSection,
 }));
 
+beforeEach(() => {
+  vi.clearAllMocks();
+});
+
+function getChildComponentNames(element: ReactElement<{ children?: ReactNode }>) {
+  return Children.toArray(element.props.children)
+    .filter(isValidElement)
+    .map((child) => {
+      const innerChild = child.props?.children;
+      if (isValidElement(innerChild)) {
+        if (typeof innerChild.type === "string") {
+          return innerChild.type;
+        }
+
+        return innerChild.type.displayName ?? innerChild.type.name ?? "unknown";
+      }
+
+      if (typeof child.type === "string") {
+        return child.type;
+      }
+
+      return child.type.displayName ?? child.type.name ?? "unknown";
+    });
+}
+
 describe("UsageLogsPage", () => {
   it("only renders the logs data section in high concurrency mode", async () => {
     authMocks.getSession.mockResolvedValue({
@@ -56,7 +93,7 @@ describe("UsageLogsPage", () => {
     if (!isValidElement<{ children?: ReactNode }>(element)) {
       throw new Error("UsageLogsPage should return a React element");
     }
-    expect(Children.toArray(element.props.children)).toHaveLength(1);
+    expect(getChildComponentNames(element)).toEqual(["UsageLogsDataSection"]);
   });
 
   it("renders active sessions section and logs data section in normal mode", async () => {
@@ -80,6 +117,9 @@ describe("UsageLogsPage", () => {
     if (!isValidElement<{ children?: ReactNode }>(element)) {
       throw new Error("UsageLogsPage should return a React element");
     }
-    expect(Children.toArray(element.props.children)).toHaveLength(2);
+    expect(getChildComponentNames(element)).toEqual([
+      "UsageLogsActiveSessionsSection",
+      "UsageLogsDataSection",
+    ]);
   });
 });