-
Notifications
You must be signed in to change notification settings - Fork 33
fix(onboarding): make folder picker dialog reliably open on top #2484
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mockGetFocusedWindow = vi.hoisted(() => vi.fn()); | ||
| const mockGetAllWindows = vi.hoisted(() => vi.fn()); | ||
| const mockShowOpenDialog = vi.hoisted(() => vi.fn()); | ||
| const mockShowMessageBox = vi.hoisted(() => vi.fn()); | ||
|
|
||
| vi.mock("inversify", () => ({ | ||
| injectable: () => (target: unknown) => target, | ||
| })); | ||
|
|
||
| vi.mock("electron", () => ({ | ||
| BrowserWindow: { | ||
| getFocusedWindow: mockGetFocusedWindow, | ||
| getAllWindows: mockGetAllWindows, | ||
| }, | ||
| dialog: { | ||
| showOpenDialog: mockShowOpenDialog, | ||
| showMessageBox: mockShowMessageBox, | ||
| }, | ||
| })); | ||
|
|
||
| import { ElectronDialog } from "./electron-dialog"; | ||
|
|
||
| function makeWindow(overrides: { visible?: boolean } = {}) { | ||
| return { | ||
| isVisible: vi.fn(() => overrides.visible ?? true), | ||
| focus: vi.fn(), | ||
| }; | ||
| } | ||
|
|
||
| describe("ElectronDialog.pickFile", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockShowOpenDialog.mockResolvedValue({ | ||
| canceled: false, | ||
| filePaths: ["/repo"], | ||
| }); | ||
| }); | ||
|
|
||
| it("parents the picker to the focused window when one exists", async () => { | ||
| const focused = makeWindow(); | ||
| mockGetFocusedWindow.mockReturnValue(focused); | ||
|
|
||
| const result = await new ElectronDialog().pickFile({ directories: true }); | ||
|
|
||
| expect(result).toEqual(["/repo"]); | ||
| expect(mockShowOpenDialog).toHaveBeenCalledWith( | ||
| focused, | ||
| expect.any(Object), | ||
| ); | ||
| // A focused window is already on top, so we never reach the fallback windows. | ||
| expect(mockGetAllWindows).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("falls back to a visible window and focuses it when nothing is focused", async () => { | ||
| mockGetFocusedWindow.mockReturnValue(null); | ||
| const hidden = makeWindow({ visible: false }); | ||
| const visible = makeWindow({ visible: true }); | ||
| mockGetAllWindows.mockReturnValue([hidden, visible]); | ||
|
|
||
| await new ElectronDialog().pickFile({ directories: true }); | ||
|
|
||
| expect(visible.focus).toHaveBeenCalledTimes(1); | ||
| // The dialog must be parented to the visible window, not opened unparented | ||
| // (which is what previously left it stuck behind/off-screen). | ||
| expect(mockShowOpenDialog).toHaveBeenCalledWith( | ||
| visible, | ||
| expect.any(Object), | ||
| ); | ||
| }); | ||
|
|
||
| it("opens unparented only when there are no windows at all", async () => { | ||
| mockGetFocusedWindow.mockReturnValue(null); | ||
| mockGetAllWindows.mockReturnValue([]); | ||
|
|
||
| await new ElectronDialog().pickFile({ directories: true }); | ||
|
|
||
| expect(mockShowOpenDialog).toHaveBeenCalledWith(expect.any(Object)); | ||
| expect(mockShowOpenDialog.mock.calls[0]).toHaveLength(1); | ||
| }); | ||
| }); | ||
|
Comment on lines
+32
to
+82
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.
The PR description explicitly calls out applying the same Prompt To Fix With AIThis is a comment left during a code review.
Path: apps/code/src/main/platform-adapters/electron-dialog.test.ts
Line: 32-82
Comment:
**Tests only cover `pickFile`, not `confirm()`**
The PR description explicitly calls out applying the same `resolveDialogParent()` fix to `confirm()`, but the test file has no coverage for that path. If the wiring in `confirm()` is later accidentally removed or regressed, nothing would catch it. A minimal test mirroring the focused-window case for `confirm()` (checking that `showMessageBox` receives the parent window) would close this gap.
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! |
||
|
|
||
| describe("ElectronDialog.confirm", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockShowMessageBox.mockResolvedValue({ response: 0 }); | ||
| }); | ||
|
|
||
| it("parents the message box to the focused window when one exists", async () => { | ||
| const focused = makeWindow(); | ||
| mockGetFocusedWindow.mockReturnValue(focused); | ||
|
|
||
| await new ElectronDialog().confirm({ | ||
| title: "Discard?", | ||
| message: "Discard changes?", | ||
| options: ["Cancel", "Discard"], | ||
| }); | ||
|
|
||
| expect(mockShowMessageBox).toHaveBeenCalledWith( | ||
| focused, | ||
| expect.any(Object), | ||
| ); | ||
| }); | ||
|
|
||
| it("falls back to a visible window and focuses it when nothing is focused", async () => { | ||
| mockGetFocusedWindow.mockReturnValue(null); | ||
| const hidden = makeWindow({ visible: false }); | ||
| const visible = makeWindow({ visible: true }); | ||
| mockGetAllWindows.mockReturnValue([hidden, visible]); | ||
|
|
||
| await new ElectronDialog().confirm({ | ||
| title: "Discard?", | ||
| message: "Discard changes?", | ||
| options: ["Cancel", "Discard"], | ||
| }); | ||
|
|
||
| expect(visible.focus).toHaveBeenCalledTimes(1); | ||
| expect(mockShowMessageBox).toHaveBeenCalledWith( | ||
| visible, | ||
| expect.any(Object), | ||
| ); | ||
| }); | ||
| }); | ||
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.
The three cases here test the same function entry point (
pickFile) with varying window configurations and differingshowOpenDialog/focusassertions — a good fit forit.each. The team's convention prefers parameterised tests for exactly this shape of variation.Prompt To Fix With AI
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!