github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/integration/components/topo-viz/datacenter-test.js (about)

     1  import { find } from '@ember/test-helpers';
     2  import { module, test } from 'qunit';
     3  import { setupRenderingTest } from 'ember-qunit';
     4  import hbs from 'htmlbars-inline-precompile';
     5  import { setupMirage } from 'ember-cli-mirage/test-support';
     6  import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
     7  import { create } from 'ember-cli-page-object';
     8  import sinon from 'sinon';
     9  import faker from 'nomad-ui/mirage/faker';
    10  import topoVizDatacenterPageObject from 'nomad-ui/tests/pages/components/topo-viz/datacenter';
    11  
    12  const TopoVizDatacenter = create(topoVizDatacenterPageObject());
    13  
    14  const nodeGen = (name, datacenter, memory, cpu, allocations = []) => ({
    15    datacenter,
    16    memory,
    17    cpu,
    18    node: { name },
    19    allocations: allocations.map(alloc => ({
    20      memory: alloc.memory,
    21      cpu: alloc.cpu,
    22      memoryPercent: alloc.memory / memory,
    23      cpuPercent: alloc.cpu / cpu,
    24      allocation: {
    25        id: faker.random.uuid(),
    26        isScheduled: true,
    27      },
    28    })),
    29  });
    30  
    31  // Used in Array#reduce to sum by a property common to an array of objects
    32  const sumBy = prop => (sum, obj) => (sum += obj[prop]);
    33  
    34  module('Integration | Component | TopoViz::Datacenter', function(hooks) {
    35    setupRenderingTest(hooks);
    36    setupMirage(hooks);
    37  
    38    const commonProps = props => ({
    39      isSingleColumn: true,
    40      isDense: false,
    41      heightScale: () => 50,
    42      onAllocationSelect: sinon.spy(),
    43      onNodeSelect: sinon.spy(),
    44      ...props,
    45    });
    46  
    47    const commonTemplate = hbs`
    48      <TopoViz::Datacenter
    49        @datacenter={{this.datacenter}}
    50        @isSingleColumn={{this.isSingleColumn}}
    51        @isDense={{this.isDense}}
    52        @heightScale={{this.heightScale}}
    53        @onAllocationSelect={{this.onAllocationSelect}}
    54        @onNodeSelect={{this.onNodeSelect}} />
    55    `;
    56  
    57    test('presents as a div with a label and a FlexMasonry with a collection of nodes', async function(assert) {
    58      this.setProperties(
    59        commonProps({
    60          datacenter: {
    61            name: 'dc1',
    62            nodes: [nodeGen('node-1', 'dc1', 1000, 500)],
    63          },
    64        })
    65      );
    66  
    67      await this.render(commonTemplate);
    68  
    69      assert.ok(TopoVizDatacenter.isPresent);
    70      assert.equal(TopoVizDatacenter.nodes.length, this.datacenter.nodes.length);
    71  
    72      await componentA11yAudit(this.element, assert);
    73    });
    74  
    75    test('datacenter stats are an aggregate of node stats', async function(assert) {
    76      this.setProperties(
    77        commonProps({
    78          datacenter: {
    79            name: 'dc1',
    80            nodes: [
    81              nodeGen('node-1', 'dc1', 1000, 500, [
    82                { memory: 100, cpu: 300 },
    83                { memory: 200, cpu: 50 },
    84              ]),
    85              nodeGen('node-2', 'dc1', 1500, 100, [
    86                { memory: 50, cpu: 80 },
    87                { memory: 100, cpu: 20 },
    88              ]),
    89              nodeGen('node-3', 'dc1', 2000, 300),
    90              nodeGen('node-4', 'dc1', 3000, 200),
    91            ],
    92          },
    93        })
    94      );
    95  
    96      await this.render(commonTemplate);
    97  
    98      const allocs = this.datacenter.nodes.reduce(
    99        (allocs, node) => allocs.concat(node.allocations),
   100        []
   101      );
   102      const memoryReserved = allocs.reduce(sumBy('memory'), 0);
   103      const cpuReserved = allocs.reduce(sumBy('cpu'), 0);
   104      const memoryTotal = this.datacenter.nodes.reduce(sumBy('memory'), 0);
   105      const cpuTotal = this.datacenter.nodes.reduce(sumBy('cpu'), 0);
   106  
   107      assert.ok(TopoVizDatacenter.label.includes(this.datacenter.name));
   108      assert.ok(TopoVizDatacenter.label.includes(`${this.datacenter.nodes.length} Nodes`));
   109      assert.ok(TopoVizDatacenter.label.includes(`${allocs.length} Allocs`));
   110      assert.ok(TopoVizDatacenter.label.includes(`${memoryReserved}/${memoryTotal} MiB`));
   111      assert.ok(TopoVizDatacenter.label.includes(`${cpuReserved}/${cpuTotal} MHz`));
   112    });
   113  
   114    test('when @isSingleColumn is true, the FlexMasonry layout gets one column, otherwise it gets two', async function(assert) {
   115      this.setProperties(
   116        commonProps({
   117          isSingleColumn: true,
   118          datacenter: {
   119            name: 'dc1',
   120            nodes: [nodeGen('node-1', 'dc1', 1000, 500), nodeGen('node-2', 'dc1', 1000, 500)],
   121          },
   122        })
   123      );
   124  
   125      await this.render(commonTemplate);
   126  
   127      assert.ok(find('[data-test-flex-masonry].flex-masonry-columns-1'));
   128  
   129      this.set('isSingleColumn', false);
   130      assert.ok(find('[data-test-flex-masonry].flex-masonry-columns-2'));
   131    });
   132  
   133    test('args get passed down to the TopViz::Node children', async function(assert) {
   134      const heightSpy = sinon.spy();
   135      this.setProperties(
   136        commonProps({
   137          isDense: true,
   138          heightScale: (...args) => {
   139            heightSpy(...args);
   140            return 50;
   141          },
   142          datacenter: {
   143            name: 'dc1',
   144            nodes: [nodeGen('node-1', 'dc1', 1000, 500, [{ memory: 100, cpu: 300 }])],
   145          },
   146        })
   147      );
   148  
   149      await this.render(commonTemplate);
   150  
   151      TopoVizDatacenter.nodes[0].as(async TopoVizNode => {
   152        assert.notOk(TopoVizNode.labelIsPresent);
   153        assert.ok(heightSpy.calledWith(this.datacenter.nodes[0].memory));
   154  
   155        await TopoVizNode.selectNode();
   156        assert.ok(this.onNodeSelect.calledWith(this.datacenter.nodes[0]));
   157  
   158        await TopoVizNode.memoryRects[0].select();
   159        assert.ok(this.onAllocationSelect.calledWith(this.datacenter.nodes[0].allocations[0]));
   160      });
   161    });
   162  });