github.com/hernad/nomad@v1.6.112/ui/tests/integration/components/primary-metric/primary-metric.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import EmberObject, { computed } from '@ember/object';
     7  import Service from '@ember/service';
     8  import { find, render, clearRender } from '@ember/test-helpers';
     9  import { test } from 'qunit';
    10  import { task } from 'ember-concurrency';
    11  import sinon from 'sinon';
    12  
    13  export function setupPrimaryMetricMocks(hooks, tasks = []) {
    14    hooks.beforeEach(function () {
    15      const getTrackerSpy = (this.getTrackerSpy = sinon.spy());
    16      const trackerPollSpy = (this.trackerPollSpy = sinon.spy());
    17      const trackerSignalPauseSpy = (this.trackerSignalPauseSpy = sinon.spy());
    18  
    19      const MockTracker = EmberObject.extend({
    20        poll: task(function* () {
    21          yield trackerPollSpy();
    22        }),
    23        signalPause: task(function* () {
    24          yield trackerSignalPauseSpy();
    25        }),
    26  
    27        cpu: computed(function () {
    28          return [];
    29        }),
    30        memory: computed(function () {
    31          return [];
    32        }),
    33        tasks,
    34      });
    35  
    36      const mockStatsTrackersRegistry = Service.extend({
    37        getTracker(...args) {
    38          getTrackerSpy(...args);
    39          return MockTracker.create();
    40        },
    41      });
    42  
    43      this.owner.register(
    44        'service:stats-trackers-registry',
    45        mockStatsTrackersRegistry
    46      );
    47      this.statsTrackersRegistry = this.owner.lookup(
    48        'service:stats-trackers-registry'
    49      );
    50    });
    51  }
    52  
    53  export function primaryMetric({ template, findResource, preload }) {
    54    test('Contains a line chart, a percentage bar, a percentage figure, and an absolute usage figure', async function (assert) {
    55      const metric = 'cpu';
    56  
    57      await preload(this.store);
    58  
    59      const resource = findResource(this.store);
    60      this.setProperties({ resource, metric });
    61  
    62      await render(template);
    63  
    64      assert.ok(find('[data-test-line-chart]'), 'Line chart');
    65      assert.ok(find('[data-test-percentage-bar]'), 'Percentage bar');
    66      assert.ok(find('[data-test-percentage]'), 'Percentage figure');
    67      assert.ok(find('[data-test-absolute-value]'), 'Absolute usage figure');
    68    });
    69  
    70    test('The CPU metric maps to is-info', async function (assert) {
    71      const metric = 'cpu';
    72  
    73      await preload(this.store);
    74  
    75      const resource = findResource(this.store);
    76      this.setProperties({ resource, metric });
    77  
    78      await render(template);
    79  
    80      assert.ok(
    81        find('[data-test-current-value]').classList.contains('is-info'),
    82        'Info class for CPU metric'
    83      );
    84    });
    85  
    86    test('The Memory metric maps to is-danger', async function (assert) {
    87      const metric = 'memory';
    88  
    89      await preload(this.store);
    90  
    91      const resource = findResource(this.store);
    92      this.setProperties({ resource, metric });
    93  
    94      await render(template);
    95  
    96      assert.ok(
    97        find('[data-test-current-value]').classList.contains('is-danger'),
    98        'Danger class for Memory metric'
    99      );
   100    });
   101  
   102    test('Gets the tracker from the tracker registry', async function (assert) {
   103      const metric = 'cpu';
   104  
   105      await preload(this.store);
   106  
   107      const resource = findResource(this.store);
   108      this.setProperties({ resource, metric });
   109  
   110      await render(template);
   111  
   112      const spy =
   113        this.getTrackerSpy.calledWith(resource) ||
   114        this.getTrackerSpy.calledWith(resource.allocation);
   115  
   116      assert.ok(
   117        spy,
   118        'Uses the tracker registry to get the tracker for the provided resource'
   119      );
   120    });
   121  
   122    test('Immediately polls the tracker', async function (assert) {
   123      const metric = 'cpu';
   124  
   125      await preload(this.store);
   126  
   127      const resource = findResource(this.store);
   128      this.setProperties({ resource, metric });
   129  
   130      await render(template);
   131  
   132      assert.ok(
   133        this.trackerPollSpy.calledOnce,
   134        'The tracker is polled immediately'
   135      );
   136    });
   137  
   138    test('A pause signal is sent to the tracker when the component is destroyed', async function (assert) {
   139      const metric = 'cpu';
   140  
   141      // Capture a reference to the spy before the component is destroyed
   142      const trackerSignalPauseSpy = this.trackerSignalPauseSpy;
   143  
   144      await preload(this.store);
   145  
   146      const resource = findResource(this.store);
   147      this.setProperties({ resource, metric });
   148      await render(template);
   149  
   150      assert.notOk(
   151        trackerSignalPauseSpy.called,
   152        'No pause signal has been sent yet'
   153      );
   154      await clearRender();
   155  
   156      assert.ok(
   157        trackerSignalPauseSpy.calledOnce,
   158        'A pause signal is sent to the tracker'
   159      );
   160    });
   161  }