Skip to content

Commit 644f270

Browse files
Muzaffer Aydins7ab059789
authored andcommitted
test(docdb): add unit tests for dbClusterNode in dbClusterNode.test.ts
1 parent aa40cdc commit 644f270

File tree

1 file changed

+98
-10
lines changed

1 file changed

+98
-10
lines changed

packages/core/src/test/docdb/explorer/dbClusterNode.test.ts

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import assert from 'assert'
77
import sinon from 'sinon'
8-
import { installFakeClock } from '../../testUtil'
98
import { AWSTreeNodeBase } from '../../../shared/treeview/nodes/awsTreeNodeBase'
109
import { DBCluster } from '@aws-sdk/client-docdb'
1110
import { DBClusterNode } from '../../../docdb/explorer/dbClusterNode'
@@ -16,6 +15,11 @@ describe('DBClusterNode', function () {
1615
let mockClient: DocumentDBClient
1716
beforeEach(() => {
1817
mockClient = {} as DocumentDBClient
18+
DBClusterNode['globalPollingArns'].clear()
19+
})
20+
21+
afterEach(() => {
22+
DBClusterNode['globalPollingArns'].clear()
1923
})
2024

2125
const parentNode = {} as AWSTreeNodeBase
@@ -38,16 +42,100 @@ describe('DBClusterNode', function () {
3842
assert.strictEqual(otherNodes.length, 0)
3943
})
4044

41-
it('waits for status to change', async function () {
42-
const clock = installFakeClock()
43-
const clusterStatus = { Status: 'testing', ...cluster }
44-
const stub = sinon.stub().onFirstCall().resolves([cluster]).onSecondCall().resolves([clusterStatus])
45-
mockClient.listClusters = stub
46-
const node = new DBClusterNode(parentNode, cluster, mockClient)
45+
it('returns false for available status', function () {
46+
const clusterStatus = { ...cluster, Status: 'available' }
47+
const node = new DBClusterNode(parentNode, clusterStatus, mockClient)
48+
49+
const requiresPolling = node.isStatusRequiringPolling()
50+
51+
assert.strictEqual(requiresPolling, false, 'isStatusRequiringPolling should return false for available status')
52+
})
53+
54+
it('returns true for creating status', function () {
55+
const clusterStatus = { ...cluster, Status: 'creating' }
56+
const node = new DBClusterNode(parentNode, clusterStatus, mockClient)
57+
58+
const requiresPolling = node.isStatusRequiringPolling()
59+
60+
assert.strictEqual(requiresPolling, true, 'isStatusRequiringPolling should return true for creating status')
61+
})
62+
63+
it('starts tracking changes when status requires polling', function () {
64+
const clusterStatus = { ...cluster, Status: 'creating' }
65+
66+
const trackChangesSpy = sinon.spy(DBClusterNode.prototype, 'trackChanges')
67+
68+
// Initialize the node with a status that requires polling
69+
const node = new DBClusterNode(parentNode, clusterStatus, mockClient)
70+
71+
// Check the result of isStatusRequiringPolling
72+
const requiresPolling = node.isStatusRequiringPolling()
73+
assert.strictEqual(requiresPolling, true, 'isStatusRequiringPolling should return true for creating status')
74+
75+
// Assert that trackChanges was called
76+
assert.ok(trackChangesSpy.calledOnce, 'trackChanges should be called when polling is required')
77+
78+
// Verify the node is in the polling state
79+
assert.strictEqual(node.isPolling, true, 'Node should be in polling state')
80+
81+
trackChangesSpy.restore()
82+
})
83+
84+
it('does not start tracking changes when status does not require polling', function () {
85+
const clusterStatus = { ...cluster, Status: 'available' }
86+
87+
const trackChangesSpy = sinon.spy(DBClusterNode.prototype, 'trackChanges')
88+
89+
// Initialize the node with a status that does not require polling
90+
const node = new DBClusterNode(parentNode, clusterStatus, mockClient)
91+
92+
// Check the result of isStatusRequiringPolling
93+
const requiresPolling = node.isStatusRequiringPolling()
94+
assert.strictEqual(requiresPolling, false, 'isStatusRequiringPolling should return false for available status')
95+
96+
// Assert that trackChanges was not called
97+
assert.ok(trackChangesSpy.notCalled, 'trackChanges should not be called when polling is not required')
98+
99+
// Verify the node is not in the polling state
100+
assert.strictEqual(node.isPolling, false, 'Node should not be in polling state')
101+
102+
trackChangesSpy.restore()
103+
})
104+
105+
it('does not polling when status is available', function () {
106+
const clusterStatus = { ...cluster, Status: 'available' }
107+
108+
const trackChangesSpy = sinon.spy(DBClusterNode.prototype, 'trackChanges')
109+
110+
// Initialize the node with a status that does not require polling
111+
const node = new DBClusterNode(parentNode, clusterStatus, mockClient)
112+
113+
// Check the result of isStatusRequiringPolling
114+
const requiresPolling = node.isStatusRequiringPolling()
115+
assert.strictEqual(requiresPolling, false, 'isStatusRequiringPolling should return false for available status')
116+
117+
// Assert that trackChanges was not called
118+
assert.ok(trackChangesSpy.notCalled, 'trackChanges should not be called when polling is not required')
119+
120+
// Verify the node is not in the polling state
121+
assert.strictEqual(node.isPolling, false, 'Node should not be in polling state')
122+
123+
trackChangesSpy.restore()
124+
})
125+
126+
it('has isPolling set to false and getStatus returns available when status is available', async function () {
127+
const clusterStatus = { ...cluster, Status: 'available' }
128+
129+
// Mock the DocumentDB client to return the status
130+
mockClient.listClusters = sinon.stub().resolves([clusterStatus])
131+
132+
// Initialize the node with a status of 'available'
133+
const node = new DBClusterNode(parentNode, clusterStatus, mockClient)
47134

48-
void node.waitUntilStatusChanged()
135+
// Verify the node is not in the polling state
136+
assert.strictEqual(node.isPolling, false, 'Node should not be in polling state')
49137

50-
await clock.runAllAsync()
51-
assert(stub.calledTwice)
138+
// Get the status from the node and verify it is 'available'
139+
assert.strictEqual(node.status, 'available', 'getStatus should return available for the node')
52140
})
53141
})

0 commit comments

Comments
 (0)