From c589b4b187e1366af840b2911b05715ad7a6814f Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 26 Apr 2013 14:52:40 -0400 Subject: [PATCH] Add a js spec for admin/report model, and find a bug too --- app/assets/javascripts/admin/models/report.js | 4 +- spec/javascripts/models/report_spec.js | 41 +++++++++++++++++++ spec/javascripts/spec.js | 1 + 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 spec/javascripts/models/report_spec.js diff --git a/app/assets/javascripts/admin/models/report.js b/app/assets/javascripts/admin/models/report.js index 254f18c7952..bdcd164bbfe 100644 --- a/app/assets/javascripts/admin/models/report.js +++ b/app/assets/javascripts/admin/models/report.js @@ -16,8 +16,8 @@ Discourse.Report = Discourse.Model.extend({ sumDays: function(startDaysAgo, endDaysAgo) { if (this.data) { - var earliestDate = Date.create(endDaysAgo + ' days ago', 'en'); - var latestDate = Date.create(startDaysAgo + ' days ago', 'en'); + var earliestDate = Date.create(endDaysAgo + ' days ago', 'en').beginningOfDay(); + var latestDate = Date.create(startDaysAgo + ' days ago', 'en').beginningOfDay(); var d, sum = 0; this.data.each(function(datum){ d = Date.create(datum.x); diff --git a/spec/javascripts/models/report_spec.js b/spec/javascripts/models/report_spec.js new file mode 100644 index 00000000000..0427a8893d6 --- /dev/null +++ b/spec/javascripts/models/report_spec.js @@ -0,0 +1,41 @@ +/*global waitsFor:true expect:true describe:true beforeEach:true it:true */ + +describe("Discourse.Report", function() { + + function dateString(arg) { + return Date.create(arg, 'en').format('{yyyy}-{MM}-{dd}'); + } + + function reportWithData(data) { + var arr = []; + data.each(function(val, index) { + arr.push({x: dateString(index + ' days ago'), y: val}); + }); + return Discourse.Report.create({ type: 'topics', data: arr }); + } + + describe("todayCount", function() { + it("displays the correct value", function() { + expect( reportWithData([5,4,3,2,1]).get('todayCount') ).toBe(5); + }); + }); + + describe("yesterdayCount", function() { + it("displays the correct value", function() { + 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() { + it("displays the correct value", function() { + expect( reportWithData([100,9,8,7,6,5,4,3,200,300,400]).get('lastSevenDaysCount') ).toBe(42); + }); + }); + +}); diff --git a/spec/javascripts/spec.js b/spec/javascripts/spec.js index dbf9cb3e4fa..72fa26e2c82 100644 --- a/spec/javascripts/spec.js +++ b/spec/javascripts/spec.js @@ -39,6 +39,7 @@ //= require_tree ../../app/assets/javascripts/discourse/helpers //= require_tree ../../app/assets/javascripts/discourse/templates //= require_tree ../../app/assets/javascripts/discourse/routes +//= require_tree ../../app/assets/javascripts/admin/models //= require_tree ../../app/assets/javascripts/defer