Skip to content

Commit

Permalink
test: update FileManager tests for file name prompt functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
wardellbagby committed Jan 13, 2025
1 parent 4a7acc5 commit 7f2c546
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/common-platform/main/files/FileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class FileManager implements Manager {
public static CONFIRM_NEW_FILE_TAG = 'confirm-new-file';
public static CONFIRM_OPEN_FILE_TAG = 'confirm-open-file';
public static CHOOSE_FILE_HANDLER_TAG = 'choose-file-handler';
public static PROMPT_FILE_NAME_TAG = 'file-name-dialog';

public initialFile: PlatformFile | null = null;

Expand Down Expand Up @@ -187,7 +188,7 @@ export class FileManager implements Manager {
): Promise<string | null> => {
const defaultFileName = FileManager.generateDefaultFileName(fileHandler);
const interactionData = await showRendererDialog(this.rendererDelegate, {
tag: 'file-name-dialog',
tag: FileManager.PROMPT_FILE_NAME_TAG,
type: 'alert',
title: 'Enter a file name',
textField: {
Expand Down
40 changes: 40 additions & 0 deletions packages/common-platform/test/FileManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('File Manager', () => {
type: 'text/plain',
}),
saveFile: Promise.resolve({ path: '/path/test2' }),
supportsChoosingFileName: Promise.resolve(true),
});

recentFiles = stubInterface<RecentFiles>({
Expand Down Expand Up @@ -656,6 +657,7 @@ describe('File Manager', () => {
},
});
});

it('saves the default file handler', async () => {
preferences.getPreferences.resolves({
...defaultPreferences,
Expand All @@ -681,4 +683,42 @@ describe('File Manager', () => {
defaultFileType: DefaultFileType.Plain_Text,
});
});

it('prompts the user for the file name', async () => {
rendererDelegate.invokeOnSet(
'dialog-interaction',
FileManager.PROMPT_FILE_NAME_TAG,
{
selectedButton: 'Save',
textField: 'gnx.lyrics',
}
);
files.supportsChoosingFileName.resolves(false);

manager.register();

await rendererDelegate.invoke('save-file-attempt', 'Peekaboo');

expect(files.saveFile).to.have.been.calledWith(
encode('Peekaboo'),
'gnx.lyrics'
);
});

it('prompts the user for the file name - cancelling does nothing', async () => {
rendererDelegate.invokeOnSet(
'dialog-interaction',
FileManager.PROMPT_FILE_NAME_TAG,
{
selectedButton: 'Cancel',
}
);
files.supportsChoosingFileName.resolves(false);

manager.register();

await rendererDelegate.invoke('save-file-attempt', 'Peekaboo');

expect(files.saveFile).to.not.have.been.called;
});
});

0 comments on commit 7f2c546

Please sign in to comment.