Skip to content

Commit fd90498

Browse files
committed
add unit test and use globals setInterval
1 parent 12e7711 commit fd90498

File tree

2 files changed

+96
-11
lines changed

2 files changed

+96
-11
lines changed

packages/core/src/awsService/cloudformation/stacks/actions/changeSetDeletionWorkflow.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ import { createChangeSetDeletionParams } from './stackActionUtil'
1717
import { getLogger } from '../../../../shared/logger/logger'
1818
import { commandKey, extractErrorMessage } from '../../utils'
1919
import { commands } from 'vscode'
20+
import globals from '../../../../shared/extensionGlobals'
2021

2122
export class ChangeSetDeletion {
2223
private readonly id: string
23-
private readonly stackName: string
24-
private readonly changeSetName: string
25-
private readonly client: LanguageClient
2624
private status: StackActionPhase | undefined
2725

28-
constructor(stackName: string, changeSetName: string, client: LanguageClient) {
26+
constructor(
27+
private readonly stackName: string,
28+
private readonly changeSetName: string,
29+
private readonly client: LanguageClient
30+
) {
2931
this.id = uuidv4()
30-
this.stackName = stackName
31-
this.changeSetName = changeSetName
32-
this.client = client
3332
}
3433

3534
async delete() {
@@ -39,7 +38,7 @@ export class ChangeSetDeletion {
3938
}
4039

4140
private pollForProgress() {
42-
const interval = setInterval(() => {
41+
const interval = globals.clock.setInterval(() => {
4342
getChangeSetDeletionStatus(this.client, { id: this.id })
4443
.then(async (deletionResult) => {
4544
if (deletionResult.phase === this.status) {
@@ -68,7 +67,7 @@ export class ChangeSetDeletion {
6867
)
6968
}
7069
void commands.executeCommand(commandKey('stacks.refresh'))
71-
clearInterval(interval)
70+
globals.clock.clearInterval(interval)
7271
break
7372
case StackActionPhase.DELETION_FAILED: {
7473
const describeDeplomentStatusResult = await describeChangeSetDeletionStatus(this.client, {
@@ -80,15 +79,16 @@ export class ChangeSetDeletion {
8079
describeDeplomentStatusResult.FailureReason ?? 'No failure reason provided'
8180
)
8281
void commands.executeCommand(commandKey('stacks.refresh'))
83-
clearInterval(interval)
82+
globals.clock.clearInterval(interval)
8483
break
8584
}
8685
}
8786
})
8887
.catch(async (error) => {
8988
getLogger().error(`Error polling for deletion status: ${error}`)
9089
showErrorMessage(`Error polling for deletion status: ${extractErrorMessage(error)}`)
91-
clearInterval(interval)
90+
void commands.executeCommand(commandKey('stacks.refresh'))
91+
globals.clock.clearInterval(interval)
9292
})
9393
}, 1000)
9494
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import assert from 'assert'
7+
import { SinonSandbox, SinonStub, createSandbox } from 'sinon'
8+
import { commands } from 'vscode'
9+
import { ChangeSetDeletion } from '../../../../../awsService/cloudformation/stacks/actions/changeSetDeletionWorkflow'
10+
import {
11+
StackActionPhase,
12+
StackActionState,
13+
} from '../../../../../awsService/cloudformation/stacks/actions/stackActionRequestType'
14+
import { commandKey } from '../../../../../awsService/cloudformation/utils'
15+
import { globals } from '../../../../../shared'
16+
17+
describe('ChangeSetDeletion', function () {
18+
let sandbox: SinonSandbox
19+
20+
beforeEach(function () {
21+
sandbox = createSandbox()
22+
})
23+
24+
afterEach(function () {
25+
sandbox.restore()
26+
})
27+
28+
describe('delete', function () {
29+
let mockClient: any
30+
let executeCommandStub: SinonStub
31+
let getChangeSetDeletionStatusStub: SinonStub
32+
let describeChangeSetDeletionStatusStub: SinonStub
33+
let setIntervalStub: SinonStub
34+
35+
beforeEach(function () {
36+
mockClient = { sendRequest: sandbox.stub().resolves({}) }
37+
executeCommandStub = sandbox.stub(commands, 'executeCommand').resolves()
38+
39+
const stackActionApi = require('../../../../../awsService/cloudformation/stacks/actions/stackActionApi')
40+
getChangeSetDeletionStatusStub = sandbox.stub(stackActionApi, 'getChangeSetDeletionStatus')
41+
describeChangeSetDeletionStatusStub = sandbox.stub(stackActionApi, 'describeChangeSetDeletionStatus')
42+
sandbox.stub(stackActionApi, 'deleteChangeSet').resolves()
43+
44+
setIntervalStub = sandbox.stub(globals.clock, 'setInterval').callsFake((callback: () => void) => {
45+
setImmediate(() => callback())
46+
return 1 as any
47+
})
48+
sandbox.stub(globals.clock, 'clearInterval')
49+
})
50+
51+
it('should call refresh command after successful deletion', async function () {
52+
getChangeSetDeletionStatusStub.resolves({
53+
phase: StackActionPhase.DELETION_COMPLETE,
54+
state: StackActionState.SUCCESSFUL,
55+
})
56+
57+
const deletion = new ChangeSetDeletion('test-stack', 'test-changeset', mockClient)
58+
await deletion.delete()
59+
await new Promise((resolve) => setImmediate(resolve))
60+
61+
assert.ok(executeCommandStub.calledWith(commandKey('stacks.refresh')))
62+
})
63+
64+
it('should call refresh command after failed deletion', async function () {
65+
getChangeSetDeletionStatusStub.resolves({ phase: StackActionPhase.DELETION_FAILED })
66+
describeChangeSetDeletionStatusStub.resolves({ FailureReason: 'Test failure' })
67+
68+
const deletion = new ChangeSetDeletion('test-stack', 'test-changeset', mockClient)
69+
await deletion.delete()
70+
await new Promise((resolve) => setImmediate(resolve))
71+
72+
assert.ok(executeCommandStub.calledWith(commandKey('stacks.refresh')))
73+
})
74+
75+
it('should not call refresh command when polling encounters error', async function () {
76+
getChangeSetDeletionStatusStub.rejects(new Error('Polling error'))
77+
78+
const deletion = new ChangeSetDeletion('test-stack', 'test-changeset', mockClient)
79+
await deletion.delete()
80+
await new Promise((resolve) => setImmediate(resolve))
81+
82+
assert.ok(executeCommandStub.calledWith(commandKey('stacks.refresh')))
83+
})
84+
})
85+
})

0 commit comments

Comments
 (0)