|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as assert from 'assert' |
| 7 | +import { Ec2ParentNode, contextValueEc2 } from '../../../ec2/explorer/ec2ParentNode' |
| 8 | +import { stub } from '../../utilities/stubber' |
| 9 | +import { Ec2Client } from '../../../shared/clients/ec2Client' |
| 10 | +import { intoCollection } from '../../../shared/utilities/collectionUtils' |
| 11 | +import { |
| 12 | + assertNodeListOnlyHasErrorNode, |
| 13 | + assertNodeListOnlyHasPlaceholderNode, |
| 14 | +} from '../../utilities/explorerNodeAssertions' |
| 15 | +import { Ec2InstanceNode } from '../../../ec2/explorer/ec2InstanceNode' |
| 16 | + |
| 17 | +describe('ec2ParentNode', function () { |
| 18 | + let testNode: Ec2ParentNode |
| 19 | + let instanceNames: string[] |
| 20 | + const testRegion = 'testRegion' |
| 21 | + const testPartition = 'testPartition' |
| 22 | + |
| 23 | + function createClient() { |
| 24 | + const client = stub(Ec2Client, { regionCode: testRegion }) |
| 25 | + client.getInstances.callsFake(async () => |
| 26 | + intoCollection( |
| 27 | + instanceNames.map(name => ({ InstanceId: name + name, Tags: [{ Key: 'Name', Value: name }] })) |
| 28 | + ) |
| 29 | + ) |
| 30 | + |
| 31 | + return client |
| 32 | + } |
| 33 | + |
| 34 | + beforeEach(function () { |
| 35 | + instanceNames = ['firstOne', 'secondOne'] |
| 36 | + testNode = new Ec2ParentNode(testRegion, testPartition, createClient()) |
| 37 | + }) |
| 38 | + |
| 39 | + it('returns placeholder node if no children are present', async function () { |
| 40 | + instanceNames = [] |
| 41 | + |
| 42 | + const childNodes = await testNode.getChildren() |
| 43 | + |
| 44 | + assertNodeListOnlyHasPlaceholderNode(childNodes) |
| 45 | + }) |
| 46 | + |
| 47 | + it('has instance child nodes', async function () { |
| 48 | + const childNodes = await testNode.getChildren() |
| 49 | + |
| 50 | + assert.strictEqual(childNodes.length, instanceNames.length, 'Unexpected child count') |
| 51 | + |
| 52 | + childNodes.forEach(node => |
| 53 | + assert.ok(node instanceof Ec2InstanceNode, 'Expected child node to be Ec2InstanceNode') |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + it('has child nodes with ec2 contextValuue', async function () { |
| 58 | + const childNodes = await testNode.getChildren() |
| 59 | + |
| 60 | + childNodes.forEach(node => |
| 61 | + assert.strictEqual(node.contextValue, contextValueEc2, 'expected the node to have a ec2 contextValue') |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + it('sorts child nodes', async function () { |
| 66 | + const sortedText = ['aa', 'ab', 'bb', 'bc', 'cc', 'cd'] |
| 67 | + instanceNames = ['ab', 'bb', 'bc', 'aa', 'cc', 'cd'] |
| 68 | + |
| 69 | + const childNodes = await testNode.getChildren() |
| 70 | + |
| 71 | + const actualChildOrder = childNodes.map(node => node.label) |
| 72 | + assert.deepStrictEqual(actualChildOrder, sortedText, 'Unexpected child sort order') |
| 73 | + }) |
| 74 | + |
| 75 | + it('has an error node for a child if an error happens during loading', async function () { |
| 76 | + const client = createClient() |
| 77 | + client.getInstances.throws(new Error()) |
| 78 | + |
| 79 | + const node = new Ec2ParentNode(testRegion, testPartition, client) |
| 80 | + assertNodeListOnlyHasErrorNode(await node.getChildren()) |
| 81 | + }) |
| 82 | +}) |
0 commit comments