From f9cb2ff214e43d7e10979c70dcc4dbfa8fb490fa Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Tue, 4 Aug 2020 22:00:10 -0700 Subject: [PATCH] Unit test coverage for the ScaleEventsChart data domain logic --- ui/app/components/scale-events-chart.js | 4 +- .../components/scale-events-chart-test.js | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 ui/tests/unit/components/scale-events-chart-test.js diff --git a/ui/app/components/scale-events-chart.js b/ui/app/components/scale-events-chart.js index 9e5fcb97c..158724936 100644 --- a/ui/app/components/scale-events-chart.js +++ b/ui/app/components/scale-events-chart.js @@ -12,7 +12,7 @@ export default class ScaleEventsChart extends Component { @computed('events.[]') get data() { - const data = this.events.filterBy('hasCount'); + const data = this.events.filterBy('hasCount').sortBy('time'); // Extend the domain of the chart to the current time data.push({ @@ -29,7 +29,7 @@ export default class ScaleEventsChart extends Component { }); } - return data.sortBy('time'); + return data; } @computed('events.[]') diff --git a/ui/tests/unit/components/scale-events-chart-test.js b/ui/tests/unit/components/scale-events-chart-test.js new file mode 100644 index 000000000..e52b23e33 --- /dev/null +++ b/ui/tests/unit/components/scale-events-chart-test.js @@ -0,0 +1,68 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import sinon from 'sinon'; + +module('Unit | Component | scale-events-chart', function(hooks) { + setupTest(hooks); + + hooks.beforeEach(function() { + this.refTime = new Date(); + this.clock = sinon.useFakeTimers(this.refTime); + }); + + hooks.afterEach(function() { + this.clock.restore(); + delete this.refTime; + }); + + test('the current date is appended as a datum for the line chart to render', function(assert) { + const chart = this.owner.factoryFor('component:scale-events-chart').create(); + const events = [ + { time: new Date('2020-08-02T04:06:00'), count: 2, hasCount: true }, + { time: new Date('2020-08-01T04:06:00'), count: 2, hasCount: true }, + ]; + + chart.set('events', events); + + assert.equal(chart.data.length, events.length + 1); + assert.deepEqual(chart.data.slice(0, events.length), events.sortBy('time')); + + const appendedDatum = chart.data[chart.data.length - 1]; + assert.equal(appendedDatum.count, events.sortBy('time').lastObject.count); + assert.equal(+appendedDatum.time, +this.refTime); + }); + + 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) { + const chart = this.owner.factoryFor('component:scale-events-chart').create(); + const annotationOutside = [ + { time: new Date('2020-08-01T04:06:00'), hasCount: false, error: true }, + { time: new Date('2020-08-02T04:06:00'), count: 2, hasCount: true }, + { time: new Date('2020-08-03T04:06:00'), count: 2, hasCount: true }, + ]; + const annotationInside = [ + { time: new Date('2020-08-02T04:06:00'), count: 2, hasCount: true }, + { time: new Date('2020-08-02T12:06:00'), hasCount: false, error: true }, + { time: new Date('2020-08-03T04:06:00'), count: 2, hasCount: true }, + ]; + + chart.set('events', annotationOutside); + + assert.equal(chart.data.length, annotationOutside.length + 1); + assert.deepEqual( + chart.data.slice(1, annotationOutside.length), + annotationOutside.filterBy('hasCount') + ); + + const appendedDatum = chart.data[0]; + assert.equal(appendedDatum.count, annotationOutside[1].count); + assert.equal(+appendedDatum.time, +annotationOutside[0].time); + + chart.set('events', annotationInside); + + assert.equal(chart.data.length, annotationOutside.length); + assert.deepEqual( + chart.data.slice(0, annotationOutside.length - 1), + annotationOutside.filterBy('hasCount') + ); + }); +});