Skip to content

Commit 7f8aa20

Browse files
committed
test: fix scratch org and sandbox UTs
1 parent 50cf4e5 commit 7f8aa20

File tree

3 files changed

+60
-31
lines changed

3 files changed

+60
-31
lines changed

src/commands/force/org/beta/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class Create extends SfdxCommand {
118118
throw new SfError(messages.getMessage('requiresUsername'));
119119
}
120120
if (this.flags.retry !== 0) {
121-
throw new SfError(messages.getMessage('retryIsNotValidForSandboxes'));
121+
throw new SfError(messages.getMessage('retryIsNotValidForSandboxes'), 'retryIsNotValidForSandboxes');
122122
}
123123

124124
if (this.flags.clientid) {

test/commands/force/org/sandboxCreate.test.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import {
8-
Aliases,
8+
GlobalInfo,
99
Config,
1010
Lifecycle,
1111
Messages,
1212
Org,
1313
SandboxEvents,
1414
SandboxProcessObject,
1515
SandboxUserAuthResponse,
16-
SfdxProject,
16+
StatusEvent,
17+
SfProject,
1718
} from '@salesforce/core';
1819
import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon';
1920
import * as sinon from 'sinon';
20-
import { expect, IConfig } from '@salesforce/command/lib/test';
21+
import { expect } from '@salesforce/command/lib/test';
22+
import { Config as IConfig } from '@oclif/core';
2123
import { UX } from '@salesforce/command';
2224
import { assert } from 'sinon';
2325
import { Create } from '../../../../src/commands/force/org/beta/create';
@@ -26,7 +28,7 @@ Messages.importMessagesDirectory(__dirname);
2628
const messages = Messages.loadMessages('@salesforce/plugin-org', 'create');
2729
describe('org:create', () => {
2830
const sandbox = sinon.createSandbox();
29-
const oclifConfigStub = fromStub(stubInterface<IConfig.IConfig>(sandbox));
31+
const oclifConfigStub = fromStub(stubInterface<IConfig>(sandbox));
3032

3133
// stubs
3234
let resolveProjectConfigStub: sinon.SinonStub;
@@ -35,6 +37,7 @@ describe('org:create', () => {
3537
let uxLogStub: sinon.SinonStub;
3638
let uxTableStub: sinon.SinonStub;
3739
let uxStyledHeaderStub: sinon.SinonStub;
40+
let aliasSetStub: sinon.SinonSpy;
3841
let cmd: TestCreate;
3942

4043
class TestCreate extends Create {
@@ -45,7 +48,7 @@ describe('org:create', () => {
4548
public setOrg(org: Org) {
4649
this.org = org;
4750
}
48-
public setProject(project: SfdxProject) {
51+
public setProject(project: SfProject) {
4952
this.project = project;
5053
}
5154
}
@@ -54,7 +57,7 @@ describe('org:create', () => {
5457
cmd = new TestCreate(params, oclifConfigStub);
5558
stubMethod(sandbox, cmd, 'assignProject').callsFake(() => {
5659
const sfdxProjectStub = fromStub(
57-
stubInterface<SfdxProject>(sandbox, {
60+
stubInterface<SfProject>(sandbox, {
5861
resolveProjectConfig: resolveProjectConfigStub,
5962
})
6063
);
@@ -239,10 +242,10 @@ describe('org:create', () => {
239242
);
240243
});
241244

242-
const data = {
245+
const data: StatusEvent = {
243246
sandboxProcessObj,
244247
interval: 30,
245-
retries: 1,
248+
remainingWait: 30,
246249
waitingOnAuth: false,
247250
};
248251

@@ -266,7 +269,14 @@ describe('org:create', () => {
266269
});
267270
stubMethod(sandbox, Org, 'create').resolves(Org.prototype);
268271
stubMethod(sandbox, Org.prototype, 'createSandbox');
269-
const aliasStub = stubMethod(sandbox, Aliases.prototype, 'set');
272+
273+
aliasSetStub = sinon.spy();
274+
stubMethod(sandbox, GlobalInfo, 'getInstance').returns({
275+
aliases: {
276+
set: aliasSetStub,
277+
},
278+
write: sinon.stub(),
279+
});
270280
const configStub = stubMethod(sandbox, Config.prototype, 'set');
271281
await command.runIt();
272282

@@ -325,7 +335,7 @@ describe('org:create', () => {
325335
value: 'newSandboxUsername',
326336
},
327337
]);
328-
expect(aliasStub.firstCall.args).to.deep.equal(['sandboxAlias', 'newSandboxUsername']);
338+
expect(aliasSetStub.firstCall.args).to.deep.equal(['sandboxAlias', 'newSandboxUsername']);
329339
expect(configStub.firstCall.args).to.deep.equal(['defaultusername', 'newSandboxUsername']);
330340
});
331341

@@ -342,10 +352,9 @@ describe('org:create', () => {
342352

343353
it('will calculate the correct human readable message (1h 33min 00seconds seconds left)', async () => {
344354
const data = {
345-
// 186*30 = 5580 = 1 hour, 33 min, 0 seconds. so 186 attempts left, at a 30 second polling interval
346355
sandboxProcessObj,
347356
interval: 30,
348-
retries: 186,
357+
remainingWait: 5580,
349358
waitingOnAuth: false,
350359
};
351360
const res = SandboxReporter.sandboxProgress(data);
@@ -355,10 +364,10 @@ describe('org:create', () => {
355364
});
356365

357366
it('will calculate the correct human readable message (5 min 30seconds seconds left)', async () => {
358-
const data = {
367+
const data: StatusEvent = {
359368
sandboxProcessObj,
360369
interval: 30,
361-
retries: 11,
370+
remainingWait: 330,
362371
waitingOnAuth: false,
363372
};
364373
const res = SandboxReporter.sandboxProgress(data);
@@ -392,10 +401,10 @@ describe('org:create', () => {
392401
);
393402
});
394403

395-
const data = {
404+
const data: StatusEvent = {
396405
sandboxProcessObj,
397406
interval: 30,
398-
retries: 1,
407+
remainingWait: 30,
399408
waitingOnAuth: false,
400409
};
401410

test/commands/force/org/scratchOrgCreate.test.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import { Aliases, Config, SfdxError, Messages, Org, SfdxProject } from '@salesforce/core';
7+
import { Config, GlobalInfo, SfError, Messages, Org, SfProject } from '@salesforce/core';
88
import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon';
99
import * as sinon from 'sinon';
10-
import { expect, IConfig } from '@salesforce/command/lib/test';
10+
import { expect } from '@salesforce/command/lib/test';
11+
import { Config as IConfig } from '@oclif/core';
1112
import { UX } from '@salesforce/command';
1213
import { assert } from 'sinon';
1314
import { Create } from '../../../../src/commands/force/org/beta/create';
@@ -35,14 +36,17 @@ const CREATE_RESULT = {
3536

3637
describe('org:create', () => {
3738
const sandbox = sinon.createSandbox();
38-
const oclifConfigStub = fromStub(stubInterface<IConfig.IConfig>(sandbox));
39+
const oclifConfigStub = fromStub(stubInterface<IConfig>(sandbox));
3940
const clientSecret = '123456';
4041
// stubs
4142
let resolveProjectConfigStub: sinon.SinonStub;
4243
let scratchOrgCreateStub: sinon.SinonStub;
4344
let uxLogStub: sinon.SinonStub;
4445
let uxWarnStub: sinon.SinonStub;
4546
let promptStub: sinon.SinonStub;
47+
let aliasGetStub: sinon.SinonStub;
48+
let aliasSetStub: sinon.SinonSpy;
49+
let aliasUpdateStub: sinon.SinonSpy;
4650
let cmd: TestCreate;
4751

4852
class TestCreate extends Create {
@@ -53,7 +57,7 @@ describe('org:create', () => {
5357
public setOrg(org: Org) {
5458
this.org = org;
5559
}
56-
public setProject(project: SfdxProject) {
60+
public setProject(project: SfProject) {
5761
this.project = project;
5862
}
5963
}
@@ -62,7 +66,7 @@ describe('org:create', () => {
6266
cmd = new TestCreate(params, oclifConfigStub);
6367
stubMethod(sandbox, cmd, 'assignProject').callsFake(() => {
6468
const sfdxProjectStub = fromStub(
65-
stubInterface<SfdxProject>(sandbox, {
69+
stubInterface<SfProject>(sandbox, {
6670
resolveProjectConfig: resolveProjectConfigStub,
6771
})
6872
);
@@ -198,8 +202,16 @@ describe('org:create', () => {
198202
...CREATE_RESULT,
199203
username: 'newScratchUsername',
200204
});
201-
const updateValueStub = stubMethod(sandbox, Aliases.prototype, 'updateValue');
202-
const getKeysByValueStub = stubMethod(sandbox, Aliases.prototype, 'getKeysByValue').returns([]);
205+
aliasGetStub = sinon.stub().returns('');
206+
aliasSetStub = sinon.spy();
207+
aliasUpdateStub = sinon.spy();
208+
stubMethod(sandbox, GlobalInfo, 'getInstance').returns({
209+
aliases: {
210+
get: aliasGetStub,
211+
set: aliasSetStub,
212+
update: aliasUpdateStub,
213+
},
214+
});
203215
const configStub = stubMethod(sandbox, Config.prototype, 'set');
204216
await command.runIt();
205217
expect(prodOrg.firstCall.args[0]).to.deep.equal({
@@ -217,8 +229,8 @@ describe('org:create', () => {
217229
retry: 0,
218230
orgConfig: {},
219231
});
220-
expect(updateValueStub.firstCall.args).to.deep.equal(['scratchOrgAlias', 'newScratchUsername']);
221-
expect(getKeysByValueStub.firstCall.args).to.deep.equal(['newScratchUsername']);
232+
expect(aliasUpdateStub.firstCall.args).to.deep.equal(['scratchOrgAlias', 'newScratchUsername']);
233+
expect(aliasGetStub.firstCall.args).to.deep.equal(['newScratchUsername']);
222234
expect(configStub.firstCall.args).to.deep.equal(['defaultusername', 'newScratchUsername']);
223235
});
224236

@@ -242,8 +254,16 @@ describe('org:create', () => {
242254
...CREATE_RESULT,
243255
username: 'newScratchUsername',
244256
});
245-
const updateValueStub = stubMethod(sandbox, Aliases.prototype, 'updateValue');
246-
const getKeysByValueStub = stubMethod(sandbox, Aliases.prototype, 'getKeysByValue').returns(['scratchOrgAlias']);
257+
258+
aliasGetStub = sinon.stub().returns('scratchOrgAlias');
259+
aliasUpdateStub = sinon.spy();
260+
stubMethod(sandbox, GlobalInfo, 'getInstance').returns({
261+
aliases: {
262+
get: aliasGetStub,
263+
update: aliasUpdateStub,
264+
},
265+
});
266+
247267
const configStub = stubMethod(sandbox, Config.prototype, 'set');
248268
await command.runIt();
249269
expect(prodOrg.firstCall.args[0]).to.deep.equal({
@@ -261,8 +281,8 @@ describe('org:create', () => {
261281
retry: 0,
262282
orgConfig: {},
263283
});
264-
expect(updateValueStub.firstCall.args).to.deep.equal(['scratchOrgAlias', 'newScratchUsername']);
265-
expect(getKeysByValueStub.firstCall.args).to.deep.equal(['newScratchUsername']);
284+
expect(aliasUpdateStub.firstCall.args).to.deep.equal(['scratchOrgAlias', 'newScratchUsername']);
285+
expect(aliasGetStub.firstCall.args).to.deep.equal(['newScratchUsername']);
266286
expect(configStub.firstCall.args).to.deep.equal(['defaultusername', 'scratchOrgAlias']);
267287
});
268288

@@ -321,7 +341,7 @@ describe('org:create', () => {
321341

322342
scratchOrgCreateStub.restore();
323343
stubMethod(sandbox, Org, 'create').resolves(Org.prototype);
324-
stubMethod(sandbox, Org.prototype, 'scratchOrgCreate').rejects(new SfdxError(errorMessage));
344+
stubMethod(sandbox, Org.prototype, 'scratchOrgCreate').rejects(new SfError(errorMessage));
325345
try {
326346
await command.runIt();
327347
assert.fail('the above should throw an error');

0 commit comments

Comments
 (0)