-
Notifications
You must be signed in to change notification settings - Fork 4
[PB-4263] Backup folders popup overlaps file explorer window #239
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,121 @@ | ||||||||||||||||||||||
| import { BrowserWindow, dialog } from 'electron'; | ||||||||||||||||||||||
| import { call } from 'tests/vitest/utils.helper'; | ||||||||||||||||||||||
| import { getPathFromDialog } from './get-path-from-dialog'; | ||||||||||||||||||||||
| import path from 'node:path'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const mockWindow = { | ||||||||||||||||||||||
| hide: vi.fn(), | ||||||||||||||||||||||
| show: vi.fn(), | ||||||||||||||||||||||
| isVisible: vi.fn().mockReturnValue(true), | ||||||||||||||||||||||
| isDestroyed: vi.fn().mockReturnValue(false), | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| vi.mock('electron', () => ({ | ||||||||||||||||||||||
| dialog: { | ||||||||||||||||||||||
| showOpenDialog: vi.fn(), | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| BrowserWindow: { | ||||||||||||||||||||||
| getFocusedWindow: vi.fn(), | ||||||||||||||||||||||
| getAllWindows: vi.fn(), | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| })); | ||||||||||||||||||||||
|
Comment on lines
+13
to
+21
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. These functions can be added to the same object in vitest.setup.main.ts to avoid duplication. |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| describe('getPathFromDialog', () => { | ||||||||||||||||||||||
| const mockedDialog = vi.mocked(dialog.showOpenDialog); | ||||||||||||||||||||||
| const mockedGetFocusedWindow = vi.mocked(BrowserWindow.getFocusedWindow); | ||||||||||||||||||||||
| const mockedGetAllWindows = vi.mocked(BrowserWindow.getAllWindows); | ||||||||||||||||||||||
|
Comment on lines
+24
to
+26
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. Use partialSpyOn helper intead of vi.mocked |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||
| mockedGetFocusedWindow.mockReturnValue(mockWindow as unknown as BrowserWindow); | ||||||||||||||||||||||
| mockedGetAllWindows.mockReturnValue([mockWindow] as unknown as BrowserWindow[]); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
Comment on lines
+28
to
+31
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
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('should hide the focused window before opening the dialog', async () => { | ||||||||||||||||||||||
| mockedDialog.mockResolvedValue({ canceled: true, filePaths: [] }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| await getPathFromDialog(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expect(mockWindow.hide).toHaveBeenCalled(); | ||||||||||||||||||||||
|
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. Use toBeCalled instead toHaveBeenCalled |
||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('should show the window after the dialog closes', async () => { | ||||||||||||||||||||||
| mockedDialog.mockResolvedValue({ canceled: true, filePaths: [] }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| await getPathFromDialog(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expect(mockWindow.show).toHaveBeenCalled(); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('should not show the window if it was destroyed', async () => { | ||||||||||||||||||||||
| mockWindow.isDestroyed.mockReturnValue(true); | ||||||||||||||||||||||
| mockedDialog.mockResolvedValue({ canceled: true, filePaths: [] }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| await getPathFromDialog(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expect(mockWindow.show).not.toHaveBeenCalled(); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('should use a visible window when no focused window exists', async () => { | ||||||||||||||||||||||
| mockedGetFocusedWindow.mockReturnValue(null); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| mockedDialog.mockResolvedValue({ canceled: true, filePaths: [] }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| await getPathFromDialog(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expect(mockWindow.hide).toHaveBeenCalled(); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('should return null when the dialog is canceled', async () => { | ||||||||||||||||||||||
| mockedDialog.mockResolvedValue({ canceled: true, filePaths: [] }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const result = await getPathFromDialog(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expect(result).toBe(null); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('should return the path with a trailing separator and the item name', async () => { | ||||||||||||||||||||||
| mockedDialog.mockResolvedValue({ canceled: false, filePaths: ['/home/user/Documents'] }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const result = await getPathFromDialog(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expect(result).toStrictEqual({ | ||||||||||||||||||||||
| path: `/home/user/Documents${path.sep}`, | ||||||||||||||||||||||
| itemName: 'Documents', | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('should not duplicate the separator if the path already ends with one', async () => { | ||||||||||||||||||||||
| mockedDialog.mockResolvedValue({ canceled: false, filePaths: [`/home/user/Documents${path.sep}`] }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const result = await getPathFromDialog(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expect(result).toStrictEqual({ | ||||||||||||||||||||||
| path: `/home/user/Documents${path.sep}`, | ||||||||||||||||||||||
| itemName: 'Documents', | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('should open the dialog with openDirectory property', async () => { | ||||||||||||||||||||||
| mockedDialog.mockResolvedValue({ canceled: true, filePaths: [] }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| await getPathFromDialog(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| call(mockedDialog).toMatchObject({ properties: ['openDirectory'] }); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| it('should skip hide and show and still return the selected path when no BackupFolderSelector window exists', async () => { | ||||||||||||||||||||||
| mockedGetFocusedWindow.mockReturnValue(null); | ||||||||||||||||||||||
| mockedGetAllWindows.mockReturnValue([]); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| mockedDialog.mockResolvedValue({ canceled: false, filePaths: ['/home/user/folder'] }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const result = await getPathFromDialog(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| expect(mockWindow.hide).not.toHaveBeenCalled(); | ||||||||||||||||||||||
| expect(mockWindow.show).not.toHaveBeenCalled(); | ||||||||||||||||||||||
| expect(result).toStrictEqual({ | ||||||||||||||||||||||
| path: `/home/user/folder${path.sep}`, | ||||||||||||||||||||||
| itemName: 'folder', | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { BrowserWindow, dialog } from 'electron'; | ||
| import { PathInfo } from '../../../apps/main/device/service'; | ||
| import path from 'node:path'; | ||
|
|
||
| export async function getPathFromDialog(): Promise<Omit<PathInfo, 'isDirectory'> | null> { | ||
| const parentWindow = BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows().find((w) => w.isVisible()); | ||
|
|
||
| if (parentWindow) { | ||
| parentWindow.hide(); | ||
| } | ||
|
|
||
| const result = await dialog.showOpenDialog({ | ||
| properties: ['openDirectory'], | ||
| }); | ||
|
|
||
| if (parentWindow && !parentWindow.isDestroyed()) { | ||
| parentWindow.show(); | ||
| } | ||
|
|
||
| if (result.canceled) { | ||
| return null; | ||
| } | ||
|
|
||
| const chosenPath = result.filePaths[0]; | ||
|
|
||
| const itemPath = `${chosenPath}${chosenPath.endsWith(path.sep) ? '' : path.sep}`; | ||
|
|
||
| const itemName = path.basename(itemPath); | ||
|
|
||
| return { | ||
| path: itemPath, | ||
| itemName, | ||
| }; | ||
| } |
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.
To avoid having to create this fake object here, you can create a mock using mockDeep.
This way, you save yourself from having to define functions in the object manually. The only thing you would have to do is manually define the return of the isVisible and isDestroyed functions in beforeEach.