|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import { Org } from '@salesforce/core'; |
| 8 | +import { testSetup } from '@salesforce/core/lib/testSetup'; |
| 9 | +import { fromStub, stubInterface, stubMethod } from '@salesforce/ts-sinon'; |
| 10 | +import { Config } from '@oclif/core'; |
| 11 | +import { expect } from 'chai'; |
| 12 | +import { PackageSaveResult, PackageVersion } from '@salesforce/packaging'; |
| 13 | +import { Result } from '@salesforce/command'; |
| 14 | +import { PackageVersionDeleteCommand } from '../../../../src/commands/force/package/beta/version/delete'; |
| 15 | +const $$ = testSetup(); |
| 16 | +const oclifConfigStub = fromStub(stubInterface<Config>($$.SANDBOX)); |
| 17 | +let uxLogStub: sinon.SinonStub; |
| 18 | +let uxConfirmStub: sinon.SinonStub; |
| 19 | +let apiVersionStub: sinon.SinonStub; |
| 20 | +let queryStub: sinon.SinonStub; |
| 21 | +let packageVersionStub: sinon.SinonStub; |
| 22 | +let deleteStub: sinon.SinonStub; |
| 23 | +let undeleteStub: sinon.SinonStub; |
| 24 | + |
| 25 | +class TestCommand extends PackageVersionDeleteCommand { |
| 26 | + public async runIt(confirm: boolean) { |
| 27 | + this.result = new Result(this.statics.result); |
| 28 | + await this.init(); |
| 29 | + uxLogStub = stubMethod($$.SANDBOX, this.ux, 'log'); |
| 30 | + uxConfirmStub = stubMethod($$.SANDBOX, this.ux, 'confirm'); |
| 31 | + if (confirm) { |
| 32 | + uxConfirmStub.resolves(confirm); |
| 33 | + } |
| 34 | + this.result.data = await this.run(); |
| 35 | + await this.finally(undefined); |
| 36 | + return this.result.data; |
| 37 | + } |
| 38 | + public setHubOrg(org: Org) { |
| 39 | + this.hubOrg = org; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +const runCmd = async (params: string[], confirm?: boolean) => { |
| 44 | + const cmd = new TestCommand(params, oclifConfigStub); |
| 45 | + stubMethod($$.SANDBOX, cmd, 'assignOrg').callsFake(() => { |
| 46 | + const orgStub = fromStub( |
| 47 | + stubInterface<Org>($$.SANDBOX, { |
| 48 | + getUsername: () => '[email protected]', |
| 49 | + getConnection: () => ({ |
| 50 | + getApiVersion: apiVersionStub, |
| 51 | + tooling: { |
| 52 | + query: queryStub, |
| 53 | + }, |
| 54 | + }), |
| 55 | + }) |
| 56 | + ); |
| 57 | + cmd.setHubOrg(orgStub); |
| 58 | + }); |
| 59 | + return cmd.runIt(confirm); |
| 60 | +}; |
| 61 | + |
| 62 | +describe('force:package:version:delete', () => { |
| 63 | + beforeEach(() => { |
| 64 | + apiVersionStub = $$.SANDBOX.stub().returns('55.0'); |
| 65 | + queryStub = $$.SANDBOX.stub(); |
| 66 | + deleteStub = $$.SANDBOX.stub(); |
| 67 | + undeleteStub = $$.SANDBOX.stub(); |
| 68 | + |
| 69 | + // The PackageVersion class is tested in the packaging library, so |
| 70 | + // we just stub the public APIs used by the command. |
| 71 | + packageVersionStub = $$.SANDBOX.stub().callsFake(() => ({ |
| 72 | + delete: deleteStub, |
| 73 | + undelete: undeleteStub, |
| 74 | + })); |
| 75 | + Object.setPrototypeOf(PackageVersion, packageVersionStub); |
| 76 | + }); |
| 77 | + afterEach(() => { |
| 78 | + $$.SANDBOX.restore(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should error without required --package param', async () => { |
| 82 | + try { |
| 83 | + await runCmd([]); |
| 84 | + expect(false, 'Expected required flag error').to.be.true; |
| 85 | + } catch (err) { |
| 86 | + const error = err as Error; |
| 87 | + expect(error.name).to.equal('Error'); |
| 88 | + expect(error.message).to.include('Missing required flag:\n -p, --package'); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + it('should error pkg version not found in project', async () => { |
| 93 | + try { |
| 94 | + await runCmd(['-p', '04t6A000002zgKSQAY', '-v', '[email protected]'], true); |
| 95 | + expect(false, 'Expected invalid id error').to.be.true; |
| 96 | + } catch (err) { |
| 97 | + const error = err as Error; |
| 98 | + expect(error.name).to.equal('ErrorInvalidIdNoMatchingVersionIdError'); |
| 99 | + } |
| 100 | + }); |
| 101 | + it('should delete a package version', async () => { |
| 102 | + deleteStub.reset(); |
| 103 | + deleteStub = $$.SANDBOX.stub(PackageVersion.prototype, 'delete').resolves({ |
| 104 | + errors: [], |
| 105 | + id: 'testId', |
| 106 | + success: true, |
| 107 | + } as PackageSaveResult); |
| 108 | + const results: PackageSaveResult = (await runCmd( |
| 109 | + ['-p', '04t6A000002zgKSQAY', '-v', '[email protected]'], |
| 110 | + true |
| 111 | + )) as PackageSaveResult; |
| 112 | + expect(results.id).to.equal('testId'); |
| 113 | + expect(uxLogStub.calledOnce).to.be.true; |
| 114 | + const msg = 'Successfully deleted the package version. testId'; |
| 115 | + expect(uxLogStub.args[0][0]).to.equal(msg); |
| 116 | + expect(results.id).to.equal('testId'); |
| 117 | + }); |
| 118 | + it('should undelete a package version', async () => { |
| 119 | + deleteStub.reset(); |
| 120 | + deleteStub = $$.SANDBOX.stub(PackageVersion.prototype, 'undelete').resolves({ |
| 121 | + errors: [], |
| 122 | + id: 'testId', |
| 123 | + success: true, |
| 124 | + } as PackageSaveResult); |
| 125 | + const results: PackageSaveResult = (await runCmd( |
| 126 | + ['-p', '04t6A000002zgKSQAY', '-v', '[email protected]', '--undelete'], |
| 127 | + true |
| 128 | + )) as PackageSaveResult; |
| 129 | + expect(uxLogStub.calledOnce).to.be.true; |
| 130 | + const msg = 'Successfully undeleted the package version. testId'; |
| 131 | + expect(uxLogStub.args[0][0]).to.equal(msg); |
| 132 | + expect(results.id).to.equal('testId'); |
| 133 | + }); |
| 134 | +}); |
0 commit comments