-
Notifications
You must be signed in to change notification settings - Fork 0
🧹 [code health improvement] Refactor LayoutEditor tests to use proper typing instead of any #293
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,8 +22,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 { mockIsOverId?: string }).mockIsOverId === id, | ||||||
| }), | ||||||
| PointerSensor: vi.fn(), | ||||||
| KeyboardSensor: vi.fn(), | ||||||
|
|
@@ -58,16 +57,16 @@ const defaultLayout: CardLayout = { | |||||
| }; | ||||||
|
|
||||||
| describe("LayoutEditor", () => { | ||||||
| type MockWindow = typeof window & { triggerDragEnd?: (event: unknown) => void; mockIsOverId?: string; }; | ||||||
| const mockWindow = window as unknown as MockWindow; | ||||||
| let mockOnLayoutChange: ReturnType<typeof vi.fn>; | ||||||
| let mockOnToggleVisibility: ReturnType<typeof vi.fn>; | ||||||
|
|
||||||
| 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; | ||||||
| mockWindow.triggerDragEnd = undefined; | ||||||
| mockWindow.mockIsOverId = undefined; | ||||||
| }); | ||||||
|
|
||||||
| it("renders blocks in their respective columns", () => { | ||||||
|
|
@@ -119,12 +118,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 = mockWindow.triggerDragEnd; | ||||||
|
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. Since
Suggested change
|
||||||
| expect(triggerDragEnd).toBeDefined(); | ||||||
|
|
||||||
| // Drag 'avatar' to 'right' column | ||||||
| triggerDragEnd({ | ||||||
| triggerDragEnd!({ | ||||||
| active: { id: "avatar" }, | ||||||
| over: { id: "right" }, | ||||||
| }); | ||||||
|
|
@@ -149,11 +147,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 = mockWindow.triggerDragEnd; | ||||||
|
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. |
||||||
|
|
||||||
| // Drag 'avatar' over 'topLanguages' | ||||||
| triggerDragEnd({ | ||||||
| triggerDragEnd!({ | ||||||
| active: { id: "avatar" }, | ||||||
| over: { id: "topLanguages" }, | ||||||
| }); | ||||||
|
|
@@ -178,9 +175,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 = mockWindow.triggerDragEnd; | ||||||
|
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. |
||||||
| triggerDragEnd!({ | ||||||
| active: { id: "avatar" }, | ||||||
| over: null, | ||||||
| }); | ||||||
|
|
@@ -200,9 +196,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 = mockWindow.triggerDragEnd; | ||||||
|
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. |
||||||
| triggerDragEnd!({ | ||||||
| active: { id: "avatar" }, | ||||||
| over: { id: "avatar" }, | ||||||
| }); | ||||||
|
|
@@ -222,9 +217,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 = mockWindow.triggerDragEnd; | ||||||
|
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. |
||||||
| triggerDragEnd!({ | ||||||
| active: { id: "avatar" }, | ||||||
| over: { id: "non-existent-block" }, | ||||||
| }); | ||||||
|
|
@@ -250,11 +244,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 = mockWindow.triggerDragEnd; | ||||||
|
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. |
||||||
|
|
||||||
| // Drag 'avatar' over 'stats' (downwards) | ||||||
| triggerDragEnd({ | ||||||
| triggerDragEnd!({ | ||||||
| active: { id: "avatar" }, | ||||||
| over: { id: "stats" }, | ||||||
| }); | ||||||
|
|
@@ -278,11 +271,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 = mockWindow.triggerDragEnd; | ||||||
|
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. |
||||||
|
|
||||||
| // Drag 'avatar' to empty column 'right' | ||||||
| triggerDragEnd({ | ||||||
| triggerDragEnd!({ | ||||||
| active: { id: "avatar" }, | ||||||
| over: { id: "right" }, | ||||||
| }); | ||||||
|
|
||||||
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.
MockWindow型のスコープが限定的MockWindow型がdescribeブロック内で定義されているため、ファイル先頭のvi.mockファクトリ(行16)では利用できません。そのため、ファクトリ内では別のインライン型キャスト(window as unknown as { triggerDragEnd: (event: unknown) => void })が引き続き使用されています。MockWindowをモジュールトップレベルに移動すれば、ファクトリ内でも同じ型を再利用でき、完全な一貫性が確保されます。Prompt To Fix With AI