Introduce new dialog box with "No, never ask me again" option

This commit is contained in:
Elena Tanasoiu
2023-04-19 16:30:09 +00:00
parent ae320d0fef
commit 8392284cc1
2 changed files with 88 additions and 0 deletions

View File

@@ -256,6 +256,46 @@ export function isWorkspaceFolderOnDisk(
return workspaceFolder.uri.scheme === "file";
}
/**
* Opens a modal dialog for the user to make a choice between yes/no/never be asked again.
*
* @param message The message to show.
* @param modal If true (the default), show a modal dialog box, otherwise dialog is non-modal and can
* be closed even if the user does not make a choice.
* @param yesTitle The text in the box indicating the affirmative choice.
* @param noTitle The text in the box indicating the negative choice.
* @param neverTitle The text in the box indicating the opt out choice.
*
* @return
* `Yes` if the user clicks 'Yes',
* `No` if the user clicks 'No' or cancels the dialog,
* `No, and never ask me again` if the user clicks 'No, and never ask me again',
* `undefined` if the dialog is closed without the user making a choice.
*/
export async function showNeverAskAgainDialog(
message: string,
modal = true,
yesTitle = "Yes",
noTitle = "No",
neverAskAgainTitle = "No, and never ask me again",
): Promise<string | undefined> {
const yesItem = { title: yesTitle, isCloseAffordance: true };
const noItem = { title: noTitle, isCloseAffordance: false };
const neverAskAgainItem = {
title: neverAskAgainTitle,
isCloseAffordance: false,
};
const chosenItem = await Window.showInformationMessage(
message,
{ modal },
yesItem,
noItem,
neverAskAgainItem,
);
return chosenItem?.title;
}
/** Gets all active workspace folders that are on the filesystem. */
export function getOnDiskWorkspaceFoldersObjects() {
const workspaceFolders = workspace.workspaceFolders ?? [];

View File

@@ -36,6 +36,7 @@ import {
showBinaryChoiceDialog,
showBinaryChoiceWithUrlDialog,
showInformationMessageWithAction,
showNeverAskAgainDialog,
walkDirectory,
} from "../../../src/helpers";
import { reportStreamProgress } from "../../../src/progress";
@@ -528,6 +529,53 @@ describe("helpers", () => {
expect(showInformationMessageSpy).toHaveBeenCalledTimes(5);
});
});
describe("showNeverAskAgainDialog", () => {
let showInformationMessageSpy: jest.SpiedFunction<
typeof window.showInformationMessage
>;
beforeEach(() => {
showInformationMessageSpy = jest
.spyOn(window, "showInformationMessage")
.mockResolvedValue(undefined);
});
const resolveArg =
(index: number) =>
(...args: any[]) =>
Promise.resolve(args[index]);
const title =
"We've noticed you don't have a CodeQL pack available to analyze this database. Can we set up a query pack for you?";
it("should show a binary choice dialog and return `Yes`", async () => {
// pretend user chooses 'Yes'
const yesItem = resolveArg(2);
showInformationMessageSpy.mockImplementationOnce(yesItem);
const answer = await showNeverAskAgainDialog(title);
expect(answer).toBe("Yes");
});
it("should show a binary choice dialog and return `No`", async () => {
// pretend user chooses 'No'
const noItem = resolveArg(3);
showInformationMessageSpy.mockImplementationOnce(noItem);
const answer = await showNeverAskAgainDialog(title);
expect(answer).toBe("No");
});
it("should show a binary choice dialog and return `No, and never ask me again`", async () => {
// pretend user chooses 'No, and never ask me again'
const neverAskAgainItem = resolveArg(4);
showInformationMessageSpy.mockImplementationOnce(neverAskAgainItem);
const answer = await showNeverAskAgainDialog(title);
expect(answer).toBe("No, and never ask me again");
});
});
});
describe("walkDirectory", () => {