diff --git a/test/spec/directives/focus-me.directive.spec.js b/test/spec/directives/focus-me.directive.spec.js index f958a7b70e..15105e3123 100644 --- a/test/spec/directives/focus-me.directive.spec.js +++ b/test/spec/directives/focus-me.directive.spec.js @@ -8,7 +8,7 @@ describe('focusMe Directive', function() { beforeEach(inject(function($rootScope, $compile) { scope = $rootScope.$new(); - element = ""; + element = ""; element = $compile(element)(scope); scope.$digest(); diff --git a/test/spec/directives/from-now.directive.spec.js b/test/spec/directives/from-now.directive.spec.js new file mode 100644 index 0000000000..a197f2c331 --- /dev/null +++ b/test/spec/directives/from-now.directive.spec.js @@ -0,0 +1,89 @@ +'use strict'; + +describe('fromNow Directive', function() { + var element, scope; + var fromNow = 'recently'; + var diff = 0; + + beforeEach(module('habitrpg')); + + beforeEach(inject(function($rootScope, $compile) { + scope = $rootScope.$new(); + scope.message = {}; + + sinon.stub(window, 'moment').returns({ + fromNow: function() { return fromNow }, + diff: function() { return diff } + }); + + element = "
"; + + element = $compile(element)(scope); + scope.$digest(); + })); + + afterEach(function() { + window.moment.restore(); + }); + + it('sets the element text to the elapsed time', function() { + expect(element.text()).to.eql('recently'); + }); + + describe('when the elapsed time is less than an hour', function() { + beforeEach(inject(function($compile) { + fromNow = 'recently'; + diff = 0; + + element = $compile('')(scope); + scope.$digest(); + })); + + it('updates the elapsed time every minute', inject(function($interval) { + fromNow = 'later'; + + expect(element.text()).to.eql('recently'); + $interval.flush(60001); + + expect(element.text()).to.eql('later'); + })); + + it('moves to hourly updates after an hour', inject(function($timeout, $interval) { + diff = 61; + + $timeout.flush(); + $interval.flush(60001); + + fromNow = 'later'; + + $interval.flush(60001); + expect(element.text()).to.eql('recently'); + + $interval.flush(3600000); + expect(element.text()).to.eql('later'); + })); + }); + + describe('when the elapsed time is more than an hour', function() { + beforeEach(inject(function($compile) { + fromNow = 'recently'; + diff = 65; + + element = $compile('')(scope); + scope.$digest(); + })); + + it('updates the elapsed time every hour', inject(function($interval) { + fromNow = 'later'; + + expect(element.text()).to.eql('recently'); + + $interval.flush(60001); + expect(element.text()).to.eql('recently'); + + $interval.flush(3600000); + expect(element.text()).to.eql('later'); + })); + }); + +});