Skip to content

Commit dc47007

Browse files
committed
fix: mock inquirer in tests
1 parent 43b99c7 commit dc47007

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/commands/project/delete/source.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import fs from 'node:fs';
99
import path from 'node:path';
1010
import os from 'node:os';
1111
import { Interfaces } from '@oclif/core';
12-
import { checkbox } from '@inquirer/prompts';
12+
import * as inquirerPrompts from '@inquirer/prompts';
1313
import { Lifecycle, Messages, Org, SfError } from '@salesforce/core';
1414
import {
1515
ComponentSet,
@@ -137,6 +137,7 @@ export class Source extends SfCommand<DeleteSourceJson> {
137137
private org!: Org;
138138
private componentSet!: ComponentSet;
139139
private deployResult!: DeployResult;
140+
private inquirer = inquirerPrompts;
140141

141142
public async run(): Promise<DeleteSourceJson> {
142143
this.flags = (await this.parse(Source)).flags;
@@ -194,7 +195,7 @@ export class Source extends SfCommand<DeleteSourceJson> {
194195
if (!this.flags['no-prompt']) {
195196
const genAiPlugins = this.componentSet.toArray().filter((comp) => comp.type.name === 'GenAiPlugin');
196197
if (genAiPlugins?.length) {
197-
const funcsToDelete = await checkbox<string | null>({
198+
const funcsToDelete = await this.inquirer.checkbox<string | null>({
198199
message: 'Select related topics to delete',
199200
choices: genAiPlugins.map((plugin) => ({ name: plugin.fullName, value: plugin.fullName })),
200201
});

test/commands/delete/source.test.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ describe('project delete source', () => {
143143
let resolveProjectConfigStub: sinon.SinonStub;
144144
let rmStub: sinon.SinonStub;
145145
let compSetFromSourceStub: sinon.SinonStub;
146-
let confirmStub: sinon.SinonStub;
147146
let handlePromptStub: sinon.SinonStub;
148147

149148
class TestDelete extends Source {
@@ -159,7 +158,7 @@ describe('project delete source', () => {
159158
params: string[],
160159
options?: {
161160
sourceApiVersion?: string;
162-
confirm?: boolean;
161+
inquirerMock?: { checkbox: sinon.SinonStub };
163162
}
164163
) => {
165164
const cmd = new TestDelete(params, oclifConfigStub);
@@ -184,8 +183,14 @@ describe('project delete source', () => {
184183
onCancel: () => {},
185184
onError: () => {},
186185
});
187-
confirmStub = stubMethod($$.SANDBOX, cmd, 'confirm').returns(options?.confirm ?? true);
188186
handlePromptStub = stubMethod($$.SANDBOX, cmd, 'handlePrompt').returns(confirm);
187+
if (options?.inquirerMock) {
188+
// @ts-expect-error stubbing private member of the command
189+
cmd.inquirer = options.inquirerMock;
190+
} else {
191+
// @ts-expect-error stubbing private member of the command
192+
cmd.inquirer = { checkbox: $$.SANDBOX.stub().resolves([]) };
193+
}
189194
rmStub = stubMethod($$.SANDBOX, fs.promises, 'rm').resolves();
190195
stubMethod($$.SANDBOX, DeployCache, 'update').resolves();
191196

@@ -269,10 +274,13 @@ describe('project delete source', () => {
269274

270275
it('should pass along metadata and org for pseudo-type matching with plugins', async () => {
271276
const agentCompSet = new ComponentSet();
277+
const pluginNames = [agentComponents[2].name, agentComponents[3].name];
272278
agentComponents.map((comp) => agentCompSet.add(comp));
273279
compSetFromSourceStub = compSetFromSourceStub.returns(agentCompSet);
280+
const inquirerCheckboxStub = $$.SANDBOX.stub().resolves(pluginNames);
281+
const inquirerMock = { checkbox: inquirerCheckboxStub };
274282
const metadata = ['Agent:My_Agent'];
275-
await runDeleteCmd(['--metadata', metadata[0], '--json']);
283+
await runDeleteCmd(['--metadata', metadata[0], '--json'], { inquirerMock });
276284
ensureCreateComponentSetArgs({
277285
metadata: {
278286
metadataEntries: metadata,
@@ -285,12 +293,12 @@ describe('project delete source', () => {
285293
});
286294
ensureHookArgs();
287295
expect(compSetFromSourceStub.calledOnce).to.be.true;
288-
expect(confirmStub.calledOnce).to.be.true;
289-
expect(confirmStub.firstCall.firstArg)
290-
.has.deep.property('message')
291-
.that.includes('Do you want to delete ALL related topics?')
292-
.and.includes(agentComponents[2].xml)
293-
.and.includes(agentComponents[3].xml);
296+
expect(inquirerCheckboxStub.calledOnce).to.be.true;
297+
expect(inquirerCheckboxStub.firstCall.firstArg).has.property('message', 'Select related topics to delete');
298+
expect(inquirerCheckboxStub.firstCall.firstArg).has.deep.property('choices', [
299+
{ name: 'Test_Plugin1', value: 'Test_Plugin1' },
300+
{ name: 'Test_Plugin2', value: 'Test_Plugin2' },
301+
]);
294302
expect(handlePromptStub.calledOnce).to.be.true;
295303
expect(lifecycleEmitStub.firstCall.args[1]).to.deep.equal(agentComponents);
296304
});
@@ -299,8 +307,10 @@ describe('project delete source', () => {
299307
const agentCompSet = new ComponentSet();
300308
agentComponents.map((comp) => agentCompSet.add(comp));
301309
compSetFromSourceStub = compSetFromSourceStub.returns(agentCompSet);
310+
const inquirerCheckboxStub = $$.SANDBOX.stub().resolves([]);
311+
const inquirerMock = { checkbox: inquirerCheckboxStub };
302312
const metadata = ['Agent:My_Agent'];
303-
await runDeleteCmd(['--metadata', metadata[0], '--json'], { confirm: false });
313+
await runDeleteCmd(['--metadata', metadata[0], '--json'], { inquirerMock });
304314
ensureCreateComponentSetArgs({
305315
metadata: {
306316
metadataEntries: metadata,
@@ -313,12 +323,11 @@ describe('project delete source', () => {
313323
});
314324
ensureHookArgs();
315325
expect(compSetFromSourceStub.calledOnce).to.be.true;
316-
expect(confirmStub.calledOnce).to.be.true;
317-
expect(confirmStub.firstCall.firstArg)
318-
.has.deep.property('message')
319-
.that.includes('Do you want to delete ALL related topics?')
320-
.and.includes(agentComponents[2].xml)
321-
.and.includes(agentComponents[3].xml);
326+
expect(inquirerCheckboxStub.calledOnce).to.be.true;
327+
expect(inquirerCheckboxStub.firstCall.firstArg).has.deep.property('choices', [
328+
{ name: 'Test_Plugin1', value: 'Test_Plugin1' },
329+
{ name: 'Test_Plugin2', value: 'Test_Plugin2' },
330+
]);
322331
expect(handlePromptStub.calledOnce).to.be.true;
323332
expect(lifecycleEmitStub.firstCall.args[1]).to.deep.equal([agentComponents[0], agentComponents[1]]);
324333
});

0 commit comments

Comments
 (0)