github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/unit/components/scale-events-chart-test.js (about)

     1  import { module, test } from 'qunit';
     2  import { setupTest } from 'ember-qunit';
     3  import sinon from 'sinon';
     4  import setupGlimmerComponentFactory from 'nomad-ui/tests/helpers/glimmer-factory';
     5  
     6  module('Unit | Component | scale-events-chart', function(hooks) {
     7    setupTest(hooks);
     8    setupGlimmerComponentFactory(hooks, 'scale-events-chart');
     9  
    10    hooks.beforeEach(function() {
    11      this.refTime = new Date();
    12      this.clock = sinon.useFakeTimers(this.refTime);
    13    });
    14  
    15    hooks.afterEach(function() {
    16      this.clock.restore();
    17      delete this.refTime;
    18    });
    19  
    20    test('the current date is appended as a datum for the line chart to render', function(assert) {
    21      const events = [
    22        { time: new Date('2020-08-02T04:06:00'), count: 2, hasCount: true },
    23        { time: new Date('2020-08-01T04:06:00'), count: 2, hasCount: true },
    24      ];
    25  
    26      const chart = this.createComponent({ events });
    27  
    28      assert.equal(chart.data.length, events.length + 1);
    29      assert.deepEqual(chart.data.slice(0, events.length), events.sortBy('time'));
    30  
    31      const appendedDatum = chart.data[chart.data.length - 1];
    32      assert.equal(appendedDatum.count, events.sortBy('time').lastObject.count);
    33      assert.equal(+appendedDatum.time, +this.refTime);
    34    });
    35  
    36    test('if the earliest annotation is outside the domain of the events, the earliest annotation time is added as a datum for the line chart to render', function(assert) {
    37      const annotationOutside = [
    38        { time: new Date('2020-08-01T04:06:00'), hasCount: false, error: true },
    39        { time: new Date('2020-08-02T04:06:00'), count: 2, hasCount: true },
    40        { time: new Date('2020-08-03T04:06:00'), count: 2, hasCount: true },
    41      ];
    42      const annotationInside = [
    43        { time: new Date('2020-08-02T04:06:00'), count: 2, hasCount: true },
    44        { time: new Date('2020-08-02T12:06:00'), hasCount: false, error: true },
    45        { time: new Date('2020-08-03T04:06:00'), count: 2, hasCount: true },
    46      ];
    47  
    48      const chart = this.createComponent({ events: annotationOutside });
    49  
    50      assert.equal(chart.data.length, annotationOutside.length + 1);
    51      assert.deepEqual(
    52        chart.data.slice(1, annotationOutside.length),
    53        annotationOutside.filterBy('hasCount')
    54      );
    55  
    56      const appendedDatum = chart.data[0];
    57      assert.equal(appendedDatum.count, annotationOutside[1].count);
    58      assert.equal(+appendedDatum.time, +annotationOutside[0].time);
    59  
    60      chart.args.events = annotationInside;
    61  
    62      assert.equal(chart.data.length, annotationOutside.length);
    63      assert.deepEqual(
    64        chart.data.slice(0, annotationOutside.length - 1),
    65        annotationOutside.filterBy('hasCount')
    66      );
    67    });
    68  });