Skip to content

Commit 03d2319

Browse files
committed
chore: use kit and test negative case
1 parent ef5c4e8 commit 03d2319

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/commands/force/org/clone.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
ResultEvent,
2323
SandboxProcessObject,
2424
} from '@salesforce/core';
25-
import { Duration } from '@salesforce/kit';
25+
import { Duration, upperFirst } from '@salesforce/kit';
2626
import { SandboxReporter } from '../../../shared/sandboxReporter';
2727

2828
Messages.importMessagesDirectory(__dirname);
@@ -88,7 +88,7 @@ export class OrgCloneCommand extends SfdxCommand {
8888
],
8989
});
9090

91-
if (results.sandboxRes && results.sandboxRes.authUserName) {
91+
if (results?.sandboxRes?.authUserName) {
9292
if (this.flags.setalias) {
9393
const alias = await Aliases.create({});
9494
const result = alias.set(this.flags.setalias, results.sandboxRes.authUserName);
@@ -157,15 +157,7 @@ export class OrgCloneCommand extends SfdxCommand {
157157
private lowerToUpper(object: Record<string, unknown>): Record<string, unknown> {
158158
// the API has keys defined in capital camel case, while the definition schema has them as lower camel case
159159
// we need to convert lower camel case to upper before merging options so they will override properly
160-
Object.keys(object).map((key) => {
161-
const upperCase = key.charAt(0).toUpperCase();
162-
if (key.charAt(0) !== upperCase) {
163-
const capitalKey = upperCase + key.slice(1);
164-
object[capitalKey] = object[key];
165-
delete object[key];
166-
}
167-
});
168-
return object;
160+
return Object.fromEntries(Object.entries(object).map(([key, value]) => [upperFirst(key), value]));
169161
}
170162

171163
private readJsonDefFile(): Record<string, unknown> {

test/commands/force/org/clone.test.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,14 @@ describe('org:clone', () => {
8383
}
8484
}
8585

86-
const runCloneCommand = async (params: string[]) => {
86+
const runCloneCommand = async (params: string[], fails?: boolean) => {
8787
cmd = new TestOrgCloneCommand(params, oclifConfigStub);
8888
stubMethod(sandbox, cmd, 'assignOrg').callsFake(() => {
8989
cloneSandboxStub = sandbox.stub().callsFake(async () => {
90-
return sandboxProcessObj;
90+
if (!fails) {
91+
return sandboxProcessObj;
92+
}
93+
throw new Error('MyError');
9194
});
9295
const orgStubOptions = {
9396
cloneSandbox: cloneSandboxStub,
@@ -105,11 +108,15 @@ describe('org:clone', () => {
105108
configAggregatorStub = fromStub(stubInterface<ConfigAggregator>(sandbox, configAggregatorStubOptions));
106109
cmd.setConfigAggregator(configAggregatorStub);
107110
});
108-
onStub = sandbox
109-
.stub()
110-
.callsArgWith(1, sandboxProcessObj)
111-
.callsArgWith(1, statusEvent)
112-
.callsArgWith(1, resultObject);
111+
if (!fails) {
112+
onStub = sandbox
113+
.stub()
114+
.callsArgWith(1, sandboxProcessObj)
115+
.callsArgWith(1, statusEvent)
116+
.callsArgWith(1, resultObject);
117+
} else {
118+
onStub = sandbox.stub().resolves(true);
119+
}
113120
stubMethod(sandbox, Lifecycle, 'getInstance').returns({
114121
on: onStub,
115122
});
@@ -203,6 +210,32 @@ describe('org:clone', () => {
203210
expect(res).to.deep.equal(sandboxProcessObj);
204211
});
205212

213+
it('cloneSandbox fails and wont set alias or default username', async () => {
214+
try {
215+
await runCloneCommand(
216+
['-t', 'sandbox', '-u', 'DevHub', '-f', 'sandbox-def.json', '-a', sandboxalias, '-s'],
217+
true
218+
);
219+
sinon.assert.fail('the above should throw an error');
220+
} catch (e) {
221+
expect(uxStyledHeaderStub.calledOnce).to.be.false;
222+
expect(uxTableStub.calledOnce).to.be.false;
223+
expect(readFileSyncStub.calledOnce).to.be.true;
224+
expect(uxLogStub.callCount).to.be.equal(0);
225+
expect(aliasSetStub.callCount).to.be.equal(0);
226+
expect(configSetStub.callCount).to.be.equal(0);
227+
expect(onStub.firstCall.firstArg).to.be.equal(SandboxEvents.EVENT_ASYNC_RESULT);
228+
expect(onStub.secondCall.firstArg).to.be.equal(SandboxEvents.EVENT_STATUS);
229+
expect(onStub.thirdCall.firstArg).to.be.equal(SandboxEvents.EVENT_RESULT);
230+
expect(onStub.callCount).to.be.equal(3);
231+
expect(cloneSandboxStub.firstCall.firstArg).to.deep.equal({
232+
LicenseType: 'Developer',
233+
SandboxName: 'newSandbox',
234+
});
235+
expect(configWriteStub.calledOnce).to.be.false;
236+
}
237+
});
238+
206239
afterEach(() => {
207240
sandbox.restore();
208241
});

0 commit comments

Comments
 (0)