Skip to content

Commit

Permalink
refine tests to cater for yes/no dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
thewahome authored Nov 27, 2024
1 parent bdc2f24 commit 92493e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ export class DeleteWorkspaceItemCommand extends Command {

public async execute(workspaceTreeItem: WorkspaceTreeItem): Promise<void> {
const type = workspaceTreeItem.category && isPluginType(workspaceTreeItem.category) ? "plugin" : "client";
const yesAnswer = vscode.l10n.t("Yes");
const yesAnswer: vscode.MessageItem = { title: vscode.l10n.t("Yes") };
const noAnswer: vscode.MessageItem = { title: vscode.l10n.t("No") };

const response = await vscode.window.showWarningMessage(
vscode.l10n.t("Do you want to delete this item?"),
yesAnswer,
vscode.l10n.t("No")
noAnswer
);
if (response === yesAnswer) {

if (response?.title === yesAnswer.title) {
const result = await this.deleteItem(type, workspaceTreeItem);
if (result) {
const isSuccess = result.some(k => k.message.includes('removed successfully'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ suite('DeleteWorkspaceItemCommand Tests', () => {
});

test('execute should show success message and refresh workspace on success', async () => {
const deleteItemStub = sinon.stub(command as any, 'deleteItem').resolves([{ message: 'removed successfully' }]);
const showWarningMessageStub = sinon.stub(vscode.window, 'showWarningMessage').resolves({ title: "Yes" });
const yesAnswer: vscode.MessageItem = { title: vscode.l10n.t("Yes") };

const showWarningMessageStub = sinon.stub(vscode.window, 'showWarningMessage').resolves(yesAnswer);
const showInformationMessageStub = sinon.stub(vscode.window, 'showInformationMessage').resolves();
const executeCommandStub = sinon.stub(vscode.commands, 'executeCommand').resolves();
const deleteItemStub = sinon.stub(command as any, 'deleteItem').resolves([{ message: 'removed successfully' }]);

await command.execute(workspaceTreeItem);

assert.strictEqual(showWarningMessageStub.calledOnce, true);
assert.strictEqual(showInformationMessageStub.calledOnce, true);
assert.strictEqual(executeCommandStub.calledWith('kiota.workspace.refresh'), true);
assert.strictEqual(deleteItemStub.calledOnce, true);
assert.strictEqual(showWarningMessageStub.calledOnceWith("Do you want to delete this item?", sinon.match("Yes"), sinon.match("No")), true);
assert.strictEqual(showInformationMessageStub.calledOnceWith('test-item removed successfully.'), true);
assert.strictEqual(executeCommandStub.calledOnceWith('kiota.workspace.refresh'), true);
});
});

0 comments on commit 92493e6

Please sign in to comment.