Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions src/components/__tests__/LayoutEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import LayoutEditor from "../LayoutEditor";
import { CardLayout } from "@/lib/types";
import "@testing-library/jest-dom";

interface MockWindow {
triggerDragEnd?: (event: unknown) => void;
mockIsOverId?: string;
}
Comment on lines +7 to +10
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of defining a local interface and using repetitive double-casting (as unknown as MockWindow), you can use global augmentation to extend the Window interface. This is more idiomatic in TypeScript for test files and significantly improves code readability by allowing direct access to these properties on the window object.

Suggested change
interface MockWindow {
triggerDragEnd?: (event: unknown) => void;
mockIsOverId?: string;
}
declare global {
interface Window {
triggerDragEnd?: (event: unknown) => void;
mockIsOverId?: string;
}
}


// Mock dnd-kit components
vi.mock("@dnd-kit/core", async (importOriginal) => {
const actual = await importOriginal<typeof import("@dnd-kit/core")>();
Expand All @@ -13,7 +18,7 @@ vi.mock("@dnd-kit/core", async (importOriginal) => {
<div data-testid="dnd-context" onClick={() => {
// Expose a way to trigger onDragEnd via a synthetic event or global for testing
// We'll attach it to window for easy triggering
(window as unknown as { triggerDragEnd: (event: unknown) => void }).triggerDragEnd = onDragEnd;
(window as unknown as MockWindow).triggerDragEnd = onDragEnd;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the redundant cast now that the Window interface is augmented.

Suggested change
(window as unknown as MockWindow).triggerDragEnd = onDragEnd;
window.triggerDragEnd = onDragEnd;

}}>
{children}
</div>
Expand All @@ -22,8 +27,7 @@ vi.mock("@dnd-kit/core", async (importOriginal) => {
useSensor: vi.fn(() => ({})),
useDroppable: ({ id }: { id: string }) => ({
setNodeRef: vi.fn(),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
isOver: (window as any).mockIsOverId === id,
isOver: (window as unknown as MockWindow).mockIsOverId === id,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the redundant cast now that the Window interface is augmented.

Suggested change
isOver: (window as unknown as MockWindow).mockIsOverId === id,
isOver: window.mockIsOverId === id,

}),
PointerSensor: vi.fn(),
KeyboardSensor: vi.fn(),
Expand Down Expand Up @@ -64,10 +68,8 @@ describe("LayoutEditor", () => {
beforeEach(() => {
mockOnLayoutChange = vi.fn();
mockOnToggleVisibility = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).triggerDragEnd = undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).mockIsOverId = undefined;
(window as unknown as MockWindow).triggerDragEnd = undefined;
(window as unknown as MockWindow).mockIsOverId = undefined;
Comment on lines +71 to +72
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the redundant casts now that the Window interface is augmented.

Suggested change
(window as unknown as MockWindow).triggerDragEnd = undefined;
(window as unknown as MockWindow).mockIsOverId = undefined;
window.triggerDragEnd = undefined;
window.mockIsOverId = undefined;

});

it("renders blocks in their respective columns", () => {
Expand Down Expand Up @@ -119,8 +121,7 @@ describe("LayoutEditor", () => {
const dndContext = screen.getByTestId("dnd-context");
fireEvent.click(dndContext);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const triggerDragEnd = (window as any).triggerDragEnd;
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the redundant cast now that the Window interface is augmented.

Suggested change
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
const triggerDragEnd = window.triggerDragEnd!;

expect(triggerDragEnd).toBeDefined();

// Drag 'avatar' to 'right' column
Expand Down Expand Up @@ -149,8 +150,7 @@ describe("LayoutEditor", () => {
const dndContext = screen.getByTestId("dnd-context");
fireEvent.click(dndContext);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const triggerDragEnd = (window as any).triggerDragEnd;
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the redundant cast now that the Window interface is augmented.

Suggested change
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
const triggerDragEnd = window.triggerDragEnd!;


// Drag 'avatar' over 'topLanguages'
triggerDragEnd({
Expand Down Expand Up @@ -178,8 +178,7 @@ describe("LayoutEditor", () => {
const dndContext = screen.getByTestId("dnd-context");
fireEvent.click(dndContext);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const triggerDragEnd = (window as any).triggerDragEnd;
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the redundant cast now that the Window interface is augmented.

Suggested change
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
const triggerDragEnd = window.triggerDragEnd!;

triggerDragEnd({
active: { id: "avatar" },
over: null,
Expand All @@ -200,8 +199,7 @@ describe("LayoutEditor", () => {
const dndContext = screen.getByTestId("dnd-context");
fireEvent.click(dndContext);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const triggerDragEnd = (window as any).triggerDragEnd;
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the redundant cast now that the Window interface is augmented.

Suggested change
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
const triggerDragEnd = window.triggerDragEnd!;

triggerDragEnd({
active: { id: "avatar" },
over: { id: "avatar" },
Expand All @@ -222,8 +220,7 @@ describe("LayoutEditor", () => {
const dndContext = screen.getByTestId("dnd-context");
fireEvent.click(dndContext);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const triggerDragEnd = (window as any).triggerDragEnd;
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the redundant cast now that the Window interface is augmented.

Suggested change
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
const triggerDragEnd = window.triggerDragEnd!;

triggerDragEnd({
active: { id: "avatar" },
over: { id: "non-existent-block" },
Expand All @@ -250,8 +247,7 @@ describe("LayoutEditor", () => {
const dndContext = screen.getByTestId("dnd-context");
fireEvent.click(dndContext);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const triggerDragEnd = (window as any).triggerDragEnd;
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the redundant cast now that the Window interface is augmented.

Suggested change
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
const triggerDragEnd = window.triggerDragEnd!;


// Drag 'avatar' over 'stats' (downwards)
triggerDragEnd({
Expand All @@ -278,8 +274,7 @@ describe("LayoutEditor", () => {
const dndContext = screen.getByTestId("dnd-context");
fireEvent.click(dndContext);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const triggerDragEnd = (window as any).triggerDragEnd;
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the redundant cast now that the Window interface is augmented.

Suggested change
const triggerDragEnd = (window as unknown as MockWindow).triggerDragEnd!;
const triggerDragEnd = window.triggerDragEnd!;


// Drag 'avatar' to empty column 'right'
triggerDragEnd({
Expand Down
Loading