-
Notifications
You must be signed in to change notification settings - Fork 0
🧹 [code health] Refactor LayoutEditor tests to remove any and eslint-disable #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+24
−28
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,12 @@ import LayoutEditor from "../LayoutEditor"; | |||||||||||||||
| import { CardLayout } from "@/lib/types"; | ||||||||||||||||
| import "@testing-library/jest-dom"; | ||||||||||||||||
|
|
||||||||||||||||
| interface WindowWithDragState extends Window { | ||||||||||||||||
| triggerDragEnd?: (event: unknown) => void; | ||||||||||||||||
| mockIsOverId?: string; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| // Mock dnd-kit components | ||||||||||||||||
|
Comment on lines
+10
to
13
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/components/__tests__/LayoutEditor.test.tsx
Line: 10-13
Comment:
インターフェース定義の直後に空行が 2 行連続しています。1 行に統一するとスタイルが統一されます。
```suggestion
}
// Mock dnd-kit components
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||
| vi.mock("@dnd-kit/core", async (importOriginal) => { | ||||||||||||||||
| const actual = await importOriginal<typeof import("@dnd-kit/core")>(); | ||||||||||||||||
|
|
@@ -13,7 +19,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 WindowWithDragState).triggerDragEnd = onDragEnd; | ||||||||||||||||
| }}> | ||||||||||||||||
| {children} | ||||||||||||||||
| </div> | ||||||||||||||||
|
|
@@ -22,8 +28,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 WindowWithDragState).mockIsOverId === id, | ||||||||||||||||
| }), | ||||||||||||||||
| PointerSensor: vi.fn(), | ||||||||||||||||
| KeyboardSensor: vi.fn(), | ||||||||||||||||
|
|
@@ -64,10 +69,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 WindowWithDragState).triggerDragEnd = undefined; | ||||||||||||||||
| (window as unknown as WindowWithDragState).mockIsOverId = undefined; | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| it("renders blocks in their respective columns", () => { | ||||||||||||||||
|
|
@@ -119,12 +122,11 @@ 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 WindowWithDragState).triggerDragEnd; | ||||||||||||||||
| expect(triggerDragEnd).toBeDefined(); | ||||||||||||||||
|
|
||||||||||||||||
| // Drag 'avatar' to 'right' column | ||||||||||||||||
| triggerDragEnd({ | ||||||||||||||||
| triggerDragEnd!({ | ||||||||||||||||
| active: { id: "avatar" }, | ||||||||||||||||
| over: { id: "right" }, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
@@ -149,11 +151,10 @@ 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 WindowWithDragState).triggerDragEnd; | ||||||||||||||||
|
|
||||||||||||||||
| // Drag 'avatar' over 'topLanguages' | ||||||||||||||||
| triggerDragEnd({ | ||||||||||||||||
| triggerDragEnd!({ | ||||||||||||||||
| active: { id: "avatar" }, | ||||||||||||||||
| over: { id: "topLanguages" }, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
@@ -178,9 +179,8 @@ 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; | ||||||||||||||||
| triggerDragEnd({ | ||||||||||||||||
| const triggerDragEnd = (window as unknown as WindowWithDragState).triggerDragEnd; | ||||||||||||||||
| triggerDragEnd!({ | ||||||||||||||||
| active: { id: "avatar" }, | ||||||||||||||||
| over: null, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
@@ -200,9 +200,8 @@ 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; | ||||||||||||||||
| triggerDragEnd({ | ||||||||||||||||
| const triggerDragEnd = (window as unknown as WindowWithDragState).triggerDragEnd; | ||||||||||||||||
| triggerDragEnd!({ | ||||||||||||||||
| active: { id: "avatar" }, | ||||||||||||||||
| over: { id: "avatar" }, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
@@ -222,9 +221,8 @@ 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; | ||||||||||||||||
| triggerDragEnd({ | ||||||||||||||||
| const triggerDragEnd = (window as unknown as WindowWithDragState).triggerDragEnd; | ||||||||||||||||
| triggerDragEnd!({ | ||||||||||||||||
| active: { id: "avatar" }, | ||||||||||||||||
| over: { id: "non-existent-block" }, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
@@ -250,11 +248,10 @@ 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 WindowWithDragState).triggerDragEnd; | ||||||||||||||||
|
|
||||||||||||||||
| // Drag 'avatar' over 'stats' (downwards) | ||||||||||||||||
| triggerDragEnd({ | ||||||||||||||||
| triggerDragEnd!({ | ||||||||||||||||
| active: { id: "avatar" }, | ||||||||||||||||
| over: { id: "stats" }, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
@@ -278,11 +275,10 @@ 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 WindowWithDragState).triggerDragEnd; | ||||||||||||||||
|
|
||||||||||||||||
| // Drag 'avatar' to empty column 'right' | ||||||||||||||||
| triggerDragEnd({ | ||||||||||||||||
| triggerDragEnd!({ | ||||||||||||||||
| active: { id: "avatar" }, | ||||||||||||||||
| over: { id: "right" }, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
triggerDragEndがオプショナル(?)で宣言されているため、取得した変数の型は((event: unknown) => void) | undefinedになります。expect(...).toBeDefined()はランタイムチェックであり TypeScript の型を絞り込まないため、strictNullChecksが有効な環境ではtriggerDragEnd({...})の呼び出しが型エラー(TS2722)になります。非オプショナルに変更するか、取得箇所で!非 null アサーションを使うと完全に型安全になります。Prompt To Fix With AI