2020-08-05 05:00:10 +00:00
import { module , test } from 'qunit' ;
import { setupTest } from 'ember-qunit' ;
import sinon from 'sinon' ;
2021-02-23 01:47:18 +00:00
import setupGlimmerComponentFactory from 'nomad-ui/tests/helpers/glimmer-factory' ;
2020-08-05 05:00:10 +00:00
2021-12-28 14:45:20 +00:00
module ( 'Unit | Component | scale-events-chart' , function ( hooks ) {
2020-08-05 05:00:10 +00:00
setupTest ( hooks ) ;
2021-02-23 01:47:18 +00:00
setupGlimmerComponentFactory ( hooks , 'scale-events-chart' ) ;
2020-08-05 05:00:10 +00:00
2021-12-28 14:45:20 +00:00
hooks . beforeEach ( function ( ) {
2020-08-05 05:00:10 +00:00
this . refTime = new Date ( ) ;
this . clock = sinon . useFakeTimers ( this . refTime ) ;
} ) ;
2021-12-28 14:45:20 +00:00
hooks . afterEach ( function ( ) {
2020-08-05 05:00:10 +00:00
this . clock . restore ( ) ;
delete this . refTime ;
} ) ;
2021-12-28 14:45:20 +00:00
test ( 'the current date is appended as a datum for the line chart to render' , function ( assert ) {
2020-08-05 05:00:10 +00:00
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 } ,
] ;
2021-02-23 01:47:18 +00:00
const chart = this . createComponent ( { events } ) ;
2020-08-05 05:00:10 +00:00
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 ) ;
} ) ;
2021-12-28 14:45:20 +00:00
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 ) {
2020-08-05 05:00:10 +00:00
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 } ,
] ;
2021-02-23 01:47:18 +00:00
const chart = this . createComponent ( { events : annotationOutside } ) ;
2020-08-05 05:00:10 +00:00
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 ) ;
2021-02-23 01:47:18 +00:00
chart . args . events = annotationInside ;
2020-08-05 05:00:10 +00:00
assert . equal ( chart . data . length , annotationOutside . length ) ;
assert . deepEqual (
chart . data . slice ( 0 , annotationOutside . length - 1 ) ,
annotationOutside . filterBy ( 'hasCount' )
) ;
} ) ;
} ) ;