2013-04-26 14:52:40 -04:00
/*global waitsFor:true expect:true describe:true beforeEach:true it:true */
describe ( "Discourse.Report" , function ( ) {
2013-06-11 06:48:50 +10:00
function dateString ( days ) {
return moment ( ) . subtract ( "days" , days ) . format ( 'YYYY-MM-DD' ) ;
2013-04-26 14:52:40 -04:00
}
function reportWithData ( data ) {
var arr = [ ] ;
2013-06-11 06:48:50 +10:00
_ . each ( data , function ( val , index ) {
arr . push ( { x : dateString ( index ) , y : val } ) ;
2013-04-26 14:52:40 -04:00
} ) ;
return Discourse . Report . create ( { type : 'topics' , data : arr } ) ;
}
describe ( "todayCount" , function ( ) {
2013-04-26 17:13:20 -04:00
it ( "returns the correct value" , function ( ) {
2013-04-26 14:52:40 -04:00
expect ( reportWithData ( [ 5 , 4 , 3 , 2 , 1 ] ) . get ( 'todayCount' ) ) . toBe ( 5 ) ;
} ) ;
} ) ;
describe ( "yesterdayCount" , function ( ) {
2013-04-26 17:13:20 -04:00
it ( "returns the correct value" , function ( ) {
2013-04-26 14:52:40 -04:00
expect ( reportWithData ( [ 5 , 4 , 3 , 2 , 1 ] ) . get ( 'yesterdayCount' ) ) . toBe ( 4 ) ;
} ) ;
} ) ;
describe ( "sumDays" , function ( ) {
it ( "adds the values for the given range of days, inclusive" , function ( ) {
expect ( reportWithData ( [ 1 , 2 , 3 , 5 , 8 , 13 ] ) . sumDays ( 2 , 4 ) ) . toBe ( 16 ) ;
} ) ;
} ) ;
describe ( "lastSevenDaysCount" , function ( ) {
2013-04-26 17:13:20 -04:00
it ( "returns the correct value" , function ( ) {
2013-04-26 14:52:40 -04:00
expect ( reportWithData ( [ 100 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 200 , 300 , 400 ] ) . get ( 'lastSevenDaysCount' ) ) . toBe ( 42 ) ;
} ) ;
} ) ;
2013-04-26 17:13:20 -04:00
describe ( "percentChangeString" , function ( ) {
it ( "returns correct value when value increased" , function ( ) {
expect ( reportWithData ( [ ] ) . percentChangeString ( 8 , 5 ) ) . toBe ( "+60%" ) ;
} ) ;
it ( "returns correct value when value decreased" , function ( ) {
expect ( reportWithData ( [ ] ) . percentChangeString ( 2 , 8 ) ) . toBe ( "-75%" ) ;
} ) ;
it ( "returns 0 when value is unchanged" , function ( ) {
expect ( reportWithData ( [ ] ) . percentChangeString ( 8 , 8 ) ) . toBe ( "0%" ) ;
} ) ;
it ( "returns Infinity when previous value was 0" , function ( ) {
expect ( reportWithData ( [ ] ) . percentChangeString ( 8 , 0 ) ) . toBe ( null ) ;
} ) ;
it ( "returns -100 when yesterday's value was 0" , function ( ) {
expect ( reportWithData ( [ ] ) . percentChangeString ( 0 , 8 ) ) . toBe ( '-100%' ) ;
} ) ;
it ( "returns NaN when both yesterday and the previous day were both 0" , function ( ) {
expect ( reportWithData ( [ ] ) . percentChangeString ( 0 , 0 ) ) . toBe ( null ) ;
} ) ;
} ) ;
describe ( "yesterdayCountTitle" , function ( ) {
it ( "displays percent change and previous value" , function ( ) {
var title = reportWithData ( [ 6 , 8 , 5 , 2 , 1 ] ) . get ( 'yesterdayCountTitle' )
expect ( title . indexOf ( '+60%' ) ) . not . toBe ( - 1 ) ;
expect ( title ) . toMatch ( "Was 5" ) ;
} ) ;
it ( "handles when two days ago was 0" , function ( ) {
var title = reportWithData ( [ 6 , 8 , 0 , 2 , 1 ] ) . get ( 'yesterdayCountTitle' )
expect ( title ) . toMatch ( "Was 0" ) ;
expect ( title ) . not . toMatch ( "%" ) ;
} ) ;
} ) ;
describe ( "sevenDayCountTitle" , function ( ) {
it ( "displays percent change and previous value" , function ( ) {
var title = reportWithData ( [ 100 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 100 , 100 ] ) . get ( 'sevenDayCountTitle' ) ;
expect ( title ) . toMatch ( "-50%" ) ;
expect ( title ) . toMatch ( "Was 14" ) ;
} ) ;
} ) ;
describe ( "thirtyDayCountTitle" , function ( ) {
it ( "displays percent change and previous value" , function ( ) {
var report = reportWithData ( [ 5 , 5 , 5 , 5 ] ) ;
report . set ( 'prev30Days' , 10 ) ;
var title = report . get ( 'thirtyDayCountTitle' ) ;
expect ( title . indexOf ( '+50%' ) ) . not . toBe ( - 1 ) ;
expect ( title ) . toMatch ( "Was 10" ) ;
} ) ;
} ) ;
2013-04-26 14:52:40 -04:00
} ) ;