Skip to content

Commit 2b749a3

Browse files
committed
add testing to ec2InstanceNode
1 parent 10566c1 commit 2b749a3

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/ec2/explorer/ec2InstanceNode.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import globals from '../../shared/extensionGlobals'
1111
import { Ec2Selection, getIconCodeForInstanceStatus } from '../utils'
1212
import { Ec2ParentNode } from './ec2ParentNode'
1313

14+
export const Ec2InstanceRunningContext = 'awsEc2RunningNode'
15+
export const Ec2InstanceStoppedContext = 'awsEc2StoppedNode'
16+
export const Ec2InstancePendingContext = 'awsEc2PendingNode'
17+
1418
type Ec2InstanceNodeContext = 'awsEc2RunningNode' | 'awsEc2StoppedNode' | 'awsEc2PendingNode'
1519

1620
export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode {
@@ -19,7 +23,7 @@ export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode
1923
public readonly client: Ec2Client,
2024
public override readonly regionCode: string,
2125
private readonly partitionId: string,
22-
private instance: Ec2Instance
26+
protected instance: Ec2Instance
2327
) {
2428
super('')
2529
this.updateInstance(instance)
@@ -40,14 +44,14 @@ export class Ec2InstanceNode extends AWSTreeNodeBase implements AWSResourceNode
4044

4145
private getContext(): Ec2InstanceNodeContext {
4246
if (this.instance.status == 'running') {
43-
return 'awsEc2RunningNode'
47+
return Ec2InstanceRunningContext
4448
}
4549

4650
if (this.instance.status == 'stopped') {
47-
return 'awsEc2StoppedNode'
51+
return Ec2InstanceStoppedContext
4852
}
4953

50-
return 'awsEc2PendingNode'
54+
return Ec2InstancePendingContext
5155
}
5256

5357
public setInstance(newInstance: Ec2Instance) {

src/test/ec2/explorer/ec2InstanceNode.test.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
*/
55

66
import * as assert from 'assert'
7-
import { Ec2InstanceNode } from '../../../ec2/explorer/ec2InstanceNode'
7+
import {
8+
Ec2InstanceNode,
9+
Ec2InstancePendingContext,
10+
Ec2InstanceRunningContext,
11+
Ec2InstanceStoppedContext,
12+
} from '../../../ec2/explorer/ec2InstanceNode'
813
import { Ec2Client, Ec2Instance, getNameOfInstance } from '../../../shared/clients/ec2Client'
914
import { Ec2ParentNode } from '../../../ec2/explorer/ec2ParentNode'
1015

@@ -16,14 +21,14 @@ describe('ec2InstanceNode', function () {
1621

1722
before(function () {
1823
testInstance = {
19-
InstanceId: 'testId',
24+
InstanceId: 'running-testId',
2025
Tags: [
2126
{
2227
Key: 'Name',
2328
Value: 'testName',
2429
},
2530
],
26-
status: 'testing',
31+
status: 'running',
2732
}
2833
const testClient = new Ec2Client('')
2934
const testParentNode = new Ec2ParentNode(testRegion, testPartition, testClient)
@@ -59,4 +64,18 @@ describe('ec2InstanceNode', function () {
5964
it('intializes the client', async function () {
6065
assert.ok(testNode.client instanceof Ec2Client)
6166
})
67+
68+
it('sets context value based on status', async function () {
69+
const stoppedInstance = { ...testInstance, status: 'stopped' }
70+
testNode.updateInstance(stoppedInstance)
71+
assert.strictEqual(testNode.contextValue, Ec2InstanceStoppedContext)
72+
73+
const runningInstance = { ...testInstance, status: 'running' }
74+
testNode.updateInstance(runningInstance)
75+
assert.strictEqual(testNode.contextValue, Ec2InstanceRunningContext)
76+
77+
const pendingInstance = { ...testInstance, status: 'pending' }
78+
testNode.updateInstance(pendingInstance)
79+
assert.strictEqual(testNode.contextValue, Ec2InstancePendingContext)
80+
})
6281
})

src/test/ec2/explorer/ec2ParentNode.test.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import * as assert from 'assert'
7-
import { Ec2ParentNode, parentContextValue } from '../../../ec2/explorer/ec2ParentNode'
7+
import { Ec2ParentNode } from '../../../ec2/explorer/ec2ParentNode'
88
import { stub } from '../../utilities/stubber'
99
import { Ec2Client, Ec2Instance } from '../../../shared/clients/ec2Client'
1010
import { intoCollection } from '../../../shared/utilities/collectionUtils'
@@ -36,8 +36,8 @@ describe('ec2ParentNode', function () {
3636

3737
beforeEach(function () {
3838
instances = [
39-
{ name: 'firstOne', InstanceId: '0' },
40-
{ name: 'secondOne', InstanceId: '1' },
39+
{ name: 'firstOne', InstanceId: '0', status: 'running' },
40+
{ name: 'secondOne', InstanceId: '1', status: 'stopped' },
4141
]
4242

4343
testNode = new Ec2ParentNode(testRegion, testPartition, createClient())
@@ -61,14 +61,6 @@ describe('ec2ParentNode', function () {
6161
)
6262
})
6363

64-
it('has child nodes with ec2 contextValuue', async function () {
65-
const childNodes = await testNode.getChildren()
66-
67-
childNodes.forEach(node =>
68-
assert.strictEqual(node.contextValue, parentContextValue, 'expected the node to have a ec2 contextValue')
69-
)
70-
})
71-
7264
it('sorts child nodes', async function () {
7365
const sortedText = ['aa', 'ab', 'bb', 'bc', 'cc', 'cd']
7466
instances = [

0 commit comments

Comments
 (0)