|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | + |
| 8 | +import { MockTestOrgData, TestContext } from '@salesforce/core/testSetup'; |
| 9 | +import { SfCommand } from '@salesforce/sf-plugins-core'; |
| 10 | +import { Config } from '@oclif/core'; |
| 11 | +import { expect } from 'chai'; |
| 12 | +import sinon from 'sinon'; |
| 13 | +import { PackagePushUpgrade } from '@salesforce/packaging'; |
| 14 | +import { PackagePushUpgradeAbortCommand } from '../../../src/commands/package/pushupgrade/abort.js'; |
| 15 | + |
| 16 | +describe('PackagePushUpgradeAbortCommand', () => { |
| 17 | + const $$ = new TestContext(); |
| 18 | + const testOrg = new MockTestOrgData(); |
| 19 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 20 | + const abortStub = $$.SANDBOX.stub(PackagePushUpgrade, 'abort'); |
| 21 | + const config = new Config({ root: import.meta.url }); |
| 22 | + |
| 23 | + // stubs |
| 24 | + let logStub: sinon.SinonStub; |
| 25 | + |
| 26 | + const stubSpinner = (cmd: PackagePushUpgradeAbortCommand) => { |
| 27 | + $$.SANDBOX.stub(cmd.spinner, 'start'); |
| 28 | + $$.SANDBOX.stub(cmd.spinner, 'stop'); |
| 29 | + }; |
| 30 | + |
| 31 | + before(async () => { |
| 32 | + await $$.stubAuths(testOrg); |
| 33 | + await config.load(); |
| 34 | + }); |
| 35 | + |
| 36 | + beforeEach(async () => { |
| 37 | + logStub = $$.SANDBOX.stub(SfCommand.prototype, 'log'); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + $$.restore(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should successfully abort a push request', async () => { |
| 45 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access |
| 46 | + const cmd = new PackagePushUpgradeAbortCommand(['-i', '0DVxx0000004CCG', '-v', 'test@hub.org'], config); |
| 47 | + stubSpinner(cmd); |
| 48 | + const res = await cmd.run(); |
| 49 | + |
| 50 | + expect(res).to.be.true; |
| 51 | + expect(abortStub.calledOnce).to.be.true; |
| 52 | + expect(logStub.callCount).to.equal(1); |
| 53 | + expect(logStub.args[0]).to.deep.equal(['Scheduled push upgrade ID 0DVxx0000004CCG was canceled.']); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should throw an error if push-request-id is missing', async () => { |
| 57 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access |
| 58 | + const cmd = new PackagePushUpgradeAbortCommand(['-v', 'test@hub.org'], config); |
| 59 | + stubSpinner(cmd); |
| 60 | + |
| 61 | + try { |
| 62 | + await cmd.run(); |
| 63 | + } catch (err) { |
| 64 | + const error = err as Error; |
| 65 | + expect(error.name).to.equal('Error'); |
| 66 | + expect(error.message).to.include('Missing required flag push-request-id'); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + it('should throw an error if push-request status is not "Created" or "Pending" or is missing', async () => { |
| 71 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access |
| 72 | + abortStub.rejects(new Error('Abortion is only allowed for "Created" or "Pending" statuses.')); |
| 73 | + const cmd = new PackagePushUpgradeAbortCommand(['-i', '0DVxx0000004CCG', '-v', 'test@hub.org'], config); |
| 74 | + stubSpinner(cmd); |
| 75 | + |
| 76 | + try { |
| 77 | + await cmd.run(); |
| 78 | + } catch (err) { |
| 79 | + const error = err as Error; |
| 80 | + const msg = 'Abortion is only allowed for "Created" or "Pending" statuses.'; |
| 81 | + expect(error.name).to.equal('Error'); |
| 82 | + expect(error.message).to.include(msg); |
| 83 | + } |
| 84 | + }); |
| 85 | +}); |
0 commit comments