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