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 extends Window {
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 creating a separate MockWindow interface and casting window repeatedly, you can use TypeScript's interface augmentation to add these properties directly to the global Window object. This approach is cleaner as it allows you to access these properties on window directly without any casting throughout the test file, which aligns better with the goal of improving code health and maintainability.

Suggested change
interface MockWindow extends Window {
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

With the global interface augmentation suggested above, you can remove the redundant double casting and access the property directly on window.

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

With the global interface augmentation, this cast is no longer necessary.

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

With the global interface augmentation, these casts are no longer necessary.

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

With the global interface augmentation, this cast is no longer necessary.

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
Contributor

Choose a reason for hiding this comment

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

P2 非 null アサーションのみで .toBeDefined() チェックが省略されている

最初のドラッグテスト(line 124)では triggerDragEnd! の直後に expect(triggerDragEnd).toBeDefined() が置かれていますが、2 番目以降のテスト(ここを含む計 6 箇所)では同ガードが省略されています。! は TypeScript コンパイル時のみ有効で実行時には何も保証しないため、fireEvent.click(dndContext) が意図通りに機能しなかった場合、triggerDragEnd(...) の呼び出し時に「TypeError: triggerDragEnd is not a function」が発生し、どのアサーションが失敗したか特定しにくくなります。今回の ! 追加に合わせて他テストにも .toBeDefined() ガードを追加しておくと、診断しやすくなります。

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/components/__tests__/LayoutEditor.test.tsx
Line: 153

Comment:
**非 null アサーションのみで `.toBeDefined()` チェックが省略されている**

最初のドラッグテスト(line 124)では `triggerDragEnd!` の直後に `expect(triggerDragEnd).toBeDefined()` が置かれていますが、2 番目以降のテスト(ここを含む計 6 箇所)では同ガードが省略されています。`!` は TypeScript コンパイル時のみ有効で実行時には何も保証しないため、`fireEvent.click(dndContext)` が意図通りに機能しなかった場合、`triggerDragEnd(...)` の呼び出し時に「TypeError: triggerDragEnd is not a function」が発生し、どのアサーションが失敗したか特定しにくくなります。今回の `!` 追加に合わせて他テストにも `.toBeDefined()` ガードを追加しておくと、診断しやすくなります。

How can I resolve this? If you propose a fix, please make it concise.

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

With the global interface augmentation, this cast is no longer necessary.

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

With the global interface augmentation, this cast is no longer necessary.

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

With the global interface augmentation, this cast is no longer necessary.

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

With the global interface augmentation, this cast is no longer necessary.

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

With the global interface augmentation, this cast is no longer necessary.

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

With the global interface augmentation, this cast is no longer necessary.

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


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