From 1da578dc6aad64498f6275760b8df532b2e5845d Mon Sep 17 00:00:00 2001 From: Shane McLaughlin Date: Mon, 29 Jan 2024 15:39:35 -0600 Subject: [PATCH] fix: logout confirmation messages (#923) * fix: correct logout confirmation message for single-org * test: ut for single-org prompt message --- src/commands/org/logout.ts | 8 ++++++-- test/commands/org/logout.test.ts | 34 ++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/commands/org/logout.ts b/src/commands/org/logout.ts index 69353d82..57f88087 100644 --- a/src/commands/org/logout.ts +++ b/src/commands/org/logout.ts @@ -134,10 +134,14 @@ const promptForOrgsToRemove = async (orgAuths: OrgAuthorization[], all: boolean) loop: true, }); -const getOrgConfirmationMessage = (selectedOrgs: OrgAuthorization[], originalOrgCount: number): string => - selectedOrgs.length === originalOrgCount +const getOrgConfirmationMessage = (selectedOrgs: OrgAuthorization[], originalOrgCount: number): string => { + if (selectedOrgs.length === 1) { + return messages.getMessage('prompt.confirm.single', [selectedOrgs[0].username]); + } + return selectedOrgs.length === originalOrgCount ? messages.getMessage('prompt.confirm-all') : messages.getMessage('prompt.confirm', [selectedOrgs.length, selectedOrgs.length > 1 ? 's' : '']); +}; /** A whole bunch of custom formatting to make the list look nicer */ const buildChoices = (orgAuths: OrgAuthorization[], all: boolean): Array => { diff --git a/test/commands/org/logout.test.ts b/test/commands/org/logout.test.ts index 21fcefba..c6d7ea3a 100644 --- a/test/commands/org/logout.test.ts +++ b/test/commands/org/logout.test.ts @@ -5,13 +5,16 @@ * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ -import { AuthRemover, ConfigContents, Global, Mode } from '@salesforce/core'; +import { AuthRemover, ConfigContents, Global, Mode, Messages } from '@salesforce/core'; import { MockTestOrgData, TestContext } from '@salesforce/core/lib/testSetup.js'; import { expect } from 'chai'; import { Config } from '@oclif/core'; -import { SfCommand } from '@salesforce/sf-plugins-core'; +import { stubPrompter } from '@salesforce/sf-plugins-core'; import Logout from '../../../src/commands/org/logout.js'; +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); +const messages = Messages.loadMessages('@salesforce/plugin-auth', 'logout'); + interface Options { authFiles?: string[]; 'target-org'?: string; @@ -28,12 +31,12 @@ describe('org:logout', () => { const testOrg1 = new MockTestOrgData(); const testOrg2 = new MockTestOrgData(); const testOrg3 = new MockTestOrgData(); - + let promptStub: ReturnType; let authRemoverSpy: sinon.SinonSpy; async function prepareStubs(options: Options = {}): Promise { const authInfo = await testOrg1.getConfig(); - + promptStub = stubPrompter($$.SANDBOX); authRemoverSpy = $$.SANDBOX.spy(AuthRemover.prototype, 'removeAuth'); if (!options.authInfoConfigDoesNotExist) { @@ -110,12 +113,23 @@ describe('org:logout', () => { } }); - it('should do nothing when prompt is answered with no', async () => { - await prepareStubs(); - $$.SANDBOX.stub(SfCommand.prototype, 'confirm').resolves(false); - const logout = new Logout(['-o', testOrg1.username], {} as Config); - const response = await logout.run(); - expect(response).to.deep.equal([]); + describe('prompts', () => { + it('shows correct prompt for single org', async () => { + await prepareStubs(); + promptStub.confirm.resolves(false); + const logout = new Logout(['-o', testOrg1.username], {} as Config); + await logout.run(); + expect(promptStub.confirm.args[0][0].message).to.equal( + messages.getMessage('prompt.confirm.single', [testOrg1.username]) + ); + }); + it('should do nothing when prompt is answered with no', async () => { + await prepareStubs(); + promptStub.confirm.resolves(false); + const logout = new Logout(['-o', testOrg1.username], {} as Config); + const response = await logout.run(); + expect(response).to.deep.equal([]); + }); }); it('should remove auth when alias is specified', async () => {