generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: fix/add unit tests Adds some tests. Refactors some tests. Deletes some tests. Changes some public methods to be protected. * test: adds tests for retrieve result formatter
- Loading branch information
Showing
13 changed files
with
557 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,59 +5,132 @@ | |
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import { Dictionary } from '@salesforce/ts-types'; | ||
import { DeployResult } from '@salesforce/source-deploy-retrieve'; | ||
import { join } from 'path'; | ||
import * as sinon from 'sinon'; | ||
import { expect } from 'chai'; | ||
import { fromStub, spyMethod, stubInterface, stubMethod } from '@salesforce/ts-sinon'; | ||
import { ConfigFile, Org, SfdxProject } from '@salesforce/core'; | ||
import { IConfig } from '@oclif/config'; | ||
import { UX } from '@salesforce/command'; | ||
import { Cancel } from '../../../src/commands/force/source/deploy/cancel'; | ||
import { exampleDeployResponse } from './testConsts'; | ||
import { DeployCancelResultFormatter } from '../../../src/formatters/deployCancelResultFormatter'; | ||
import { DeployCommandResult } from '../../../src/formatters/deployResultFormatter'; | ||
import { getDeployResult } from './deployResponses'; | ||
|
||
// TODO: Rewrite tests for changes | ||
describe.skip('force:source:cancel', () => { | ||
const jobid = '0Af1k00000r2BebCAE'; | ||
describe('force:source:cancel', () => { | ||
const sandbox = sinon.createSandbox(); | ||
const username = '[email protected]'; | ||
const defaultDir = join('my', 'default', 'package'); | ||
const stashedDeployId = 'IMA000STASHID'; | ||
|
||
const run = async ( | ||
flags: Dictionary<boolean | string | number | string[]> = {}, | ||
id?: string | ||
): Promise<DeployResult> => { | ||
// TODO: fix this test for SDRL | ||
// Run the command | ||
// all the stubs will change with SDRL implementation, just call it good for now | ||
return Cancel.prototype.run.call({ | ||
flags: Object.assign({}, flags), | ||
getConfig: () => { | ||
return { readSync: () => {}, get: () => jobid }; | ||
}, | ||
deployReport: () => exampleDeployResponse, | ||
org: { | ||
getConnection: () => { | ||
return { | ||
const deployResult = getDeployResult('canceled'); | ||
const expectedResults = deployResult.response as DeployCommandResult; | ||
expectedResults.deployedSource = deployResult.getFileResponses(); | ||
expectedResults.outboundFiles = []; | ||
expectedResults.deploys = [deployResult.response]; | ||
|
||
// Stubs | ||
const oclifConfigStub = fromStub(stubInterface<IConfig>(sandbox)); | ||
let checkDeployStatusStub: sinon.SinonStub; | ||
let invokeStub: sinon.SinonStub; | ||
let uxLogStub: sinon.SinonStub; | ||
|
||
class TestCancel extends Cancel { | ||
public async runIt() { | ||
await this.init(); | ||
return this.run(); | ||
} | ||
public setOrg(org: Org) { | ||
this.org = org; | ||
} | ||
public setProject(project: SfdxProject) { | ||
this.project = project; | ||
} | ||
} | ||
|
||
const runCancelCmd = async (params: string[]) => { | ||
const cmd = new TestCancel(params, oclifConfigStub); | ||
stubMethod(sandbox, cmd, 'assignProject').callsFake(() => { | ||
const sfdxProjectStub = fromStub( | ||
stubInterface<SfdxProject>(sandbox, { | ||
getUniquePackageDirectories: () => [{ fullPath: defaultDir }], | ||
}) | ||
); | ||
cmd.setProject(sfdxProjectStub); | ||
}); | ||
stubMethod(sandbox, cmd, 'assignOrg').callsFake(() => { | ||
const orgStub = fromStub( | ||
stubInterface<Org>(sandbox, { | ||
getUsername: () => username, | ||
getConnection: () => ({ | ||
metadata: { | ||
_invoke: () => { | ||
return { | ||
id: id || jobid, | ||
}; | ||
}, | ||
checkDeployStatus: checkDeployStatusStub, | ||
// TODO: FIX FOR SDR CHANGES WHEN THEY ARE PUBLISHED | ||
_invoke: invokeStub, | ||
}, | ||
}; | ||
}, | ||
}, | ||
}) as Promise<DeployResult>; | ||
}), | ||
}) | ||
); | ||
cmd.setOrg(orgStub); | ||
}); | ||
uxLogStub = stubMethod(sandbox, UX.prototype, 'log'); | ||
stubMethod(sandbox, ConfigFile.prototype, 'readSync'); | ||
stubMethod(sandbox, ConfigFile.prototype, 'get').returns({ jobid: stashedDeployId }); | ||
checkDeployStatusStub = sandbox.stub().resolves(expectedResults); | ||
invokeStub = sandbox.stub().resolves(); | ||
|
||
return cmd.runIt(); | ||
}; | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('should read from ~/.sfdx/stash.json', async () => { | ||
const result = await run({ json: true }); | ||
expect(result).to.deep.equal(exampleDeployResponse); | ||
it('should use stashed deploy ID', async () => { | ||
const getStashSpy = spyMethod(sandbox, Cancel.prototype, 'getStash'); | ||
const result = await runCancelCmd(['--json']); | ||
expect(result).to.deep.equal(expectedResults); | ||
expect(getStashSpy.called).to.equal(true); | ||
expect(checkDeployStatusStub.firstCall.args[0]).to.equal(stashedDeployId); | ||
expect(invokeStub.firstCall.args[1]).to.deep.equal({ deployId: stashedDeployId }); | ||
}); | ||
|
||
it('should display stashed deploy ID', async () => { | ||
const result = await runCancelCmd([]); | ||
expect(result).to.deep.equal(expectedResults); | ||
expect(uxLogStub.firstCall.args[0]).to.contain(stashedDeployId); | ||
}); | ||
|
||
it('should use the jobid flag', async () => { | ||
const jobIdFlag = '0Af1k00000r29C9CAI'; | ||
const result = await run({ json: true, jobid: jobIdFlag }, jobIdFlag); | ||
expect(result).to.deep.equal(exampleDeployResponse); | ||
const getStashSpy = spyMethod(sandbox, Cancel.prototype, 'getStash'); | ||
const result = await runCancelCmd(['--json', '--jobid', expectedResults.id]); | ||
expect(result).to.deep.equal(expectedResults); | ||
expect(getStashSpy.called).to.equal(false); | ||
expect(checkDeployStatusStub.firstCall.args[0]).to.equal(expectedResults.id); | ||
expect(invokeStub.firstCall.args[1]).to.deep.equal({ deployId: expectedResults.id }); | ||
}); | ||
|
||
it('should display the jobid flag', async () => { | ||
const result = await runCancelCmd(['--jobid', expectedResults.id]); | ||
expect(result).to.deep.equal(expectedResults); | ||
expect(uxLogStub.firstCall.args[0]).to.contain(expectedResults.id); | ||
}); | ||
|
||
it('should display output with no --json', async () => { | ||
const displayStub = sandbox.stub(DeployCancelResultFormatter.prototype, 'display'); | ||
const getJsonStub = sandbox.stub(DeployCancelResultFormatter.prototype, 'getJson'); | ||
await runCancelCmd([]); | ||
expect(displayStub.calledOnce).to.equal(true); | ||
expect(getJsonStub.calledOnce).to.equal(true); | ||
expect(uxLogStub.called).to.equal(true); | ||
}); | ||
|
||
it('should NOT display output with --json', async () => { | ||
const displayStub = sandbox.stub(DeployCancelResultFormatter.prototype, 'display'); | ||
const getJsonStub = sandbox.stub(DeployCancelResultFormatter.prototype, 'getJson'); | ||
await runCancelCmd(['--json']); | ||
expect(displayStub.calledOnce).to.equal(false); | ||
expect(getJsonStub.calledOnce).to.equal(true); | ||
expect(uxLogStub.called).to.equal(false); | ||
}); | ||
}); |
Oops, something went wrong.