habitica-self-host/test/spec/controllers/settingsCtrlSpec.js

170 lines
4.9 KiB
JavaScript
Raw Normal View History

2015-09-05 20:32:49 +00:00
'use strict';
describe('Settings Controller', function() {
var rootScope, scope, user, User, ctrl;
beforeEach(function() {
module(function($provide) {
user = specHelper.newUser();
User = {
set: sandbox.stub(),
user: user
};
User.user.ops = {
reroll: sandbox.stub(),
rebirth: sandbox.stub(),
};
2015-09-05 20:32:49 +00:00
$provide.value('User', User);
$provide.value('Guide', sandbox.stub());
});
inject(function(_$rootScope_, _$controller_) {
scope = _$rootScope_.$new();
rootScope = _$rootScope_;
// Load RootCtrl to ensure shared behaviors are loaded
_$controller_('RootCtrl', {$scope: scope, User: User});
ctrl = _$controller_('SettingsCtrl', {$scope: scope, User: User});
});
});
describe('#openDayStartModal', function() {
beforeEach(function() {
sandbox.stub(rootScope, 'openModal');
sandbox.stub(window, 'alert');
});
2015-09-06 14:36:29 +00:00
it('opens the day start modal', function() {
scope.openDayStartModal(5);
2015-09-05 20:32:49 +00:00
2015-09-06 14:36:29 +00:00
expect(rootScope.openModal).to.be.calledOnce;
expect(rootScope.openModal).to.be.calledWith('change-day-start', {scope: scope});
2015-09-05 20:32:49 +00:00
});
2015-09-06 14:36:29 +00:00
it('sets nextCron variable', function() {
expect(scope.nextCron).to.not.exist;
2015-09-05 20:32:49 +00:00
2015-09-06 14:36:29 +00:00
scope.openDayStartModal(5);
2015-09-05 20:32:49 +00:00
2015-09-06 14:36:29 +00:00
expect(scope.nextCron).to.exist;
});
2015-09-05 20:32:49 +00:00
2015-09-06 14:36:29 +00:00
it('calculates the next time cron will run', function() {
var fakeCurrentTime = new Date(2013, 3, 1, 3, 12).getTime();
var expectedTime = new Date(2013, 3, 1, 5, 0, 0).getTime();
sandbox.useFakeTimers(fakeCurrentTime);
2015-09-05 20:32:49 +00:00
2015-09-06 14:36:29 +00:00
scope.openDayStartModal(5);
2015-09-05 20:32:49 +00:00
2015-09-06 14:36:29 +00:00
expect(scope.nextCron).to.eq(expectedTime);
});
2015-09-05 20:32:49 +00:00
2015-09-06 14:36:29 +00:00
it('calculates the next time cron will run and adds a day if cron would have already passed', function() {
var fakeCurrentTime = new Date(2013, 3, 1, 8, 12).getTime();
var expectedTime = new Date(2013, 3, 2, 5, 0, 0).getTime();
sandbox.useFakeTimers(fakeCurrentTime);
2015-09-05 20:32:49 +00:00
2015-09-06 14:36:29 +00:00
scope.openDayStartModal(5);
2015-09-05 20:32:49 +00:00
2015-09-06 14:36:29 +00:00
expect(scope.nextCron).to.eq(expectedTime);
2015-09-05 20:32:49 +00:00
});
});
describe('#saveDayStart', function() {
it('updates user\'s custom day start and last cron', function() {
var fakeCurrentTime = new Date(2013, 3, 1, 8, 12).getTime();
var expectedTime = fakeCurrentTime;
sandbox.useFakeTimers(fakeCurrentTime);
scope.dayStart = 5;
scope.saveDayStart();
expect(User.set).to.be.calledOnce;
expect(User.set).to.be.calledWith({
'preferences.dayStart': 5,
'lastCron': expectedTime
});
});
});
describe('#reroll', function() {
beforeEach(function() {
scope.clickReroll(document.createElement('div'));
});
it('destroys the previous popover if it exists', function() {
expect(scope.popoverEl).to.exist;
sandbox.spy($.fn, 'popover');
scope.reroll(false);
$.fn.popover.should.have.been.calledWith('destroy');
});
it('doesn\'t call reroll when not confirmed', function() {
scope.reroll(false);
user.ops.reroll.should.not.have.been.called;
});
it('calls reroll on the user when confirmed and navigates to tasks', function() {
sandbox.stub(rootScope.$state, 'go');
scope.reroll(true);
user.ops.reroll.should.have.been.calledWith({});
rootScope.$state.go.should.have.been.calledWith('tasks');
});
});
describe('#clickReroll', function() {
it('displays a confirmation popover for the user', function() {
sandbox.spy($.fn, 'popover');
scope.clickReroll(document.createElement('div'));
$.fn.popover.should.have.been.calledWith('destroy');
$.fn.popover.should.have.been.called;
$.fn.popover.should.have.been.calledWith('show');
});
});
describe('#rebirth', function() {
beforeEach(function() {
scope.clickRebirth(document.createElement('div'));
});
it('destroys the previous popover if it exists', function() {
expect(scope.popoverEl).to.exist;
sandbox.spy($.fn, 'popover');
scope.rebirth(false);
$.fn.popover.should.have.been.calledWith('destroy');
});
it('doesn\'t call rebirth when not confirmed', function() {
scope.rebirth(false);
user.ops.rebirth.should.not.have.been.called;
});
it('calls rebirth on the user when confirmed and navigates to tasks', function() {
sandbox.stub(rootScope.$state, 'go');
scope.rebirth(true);
user.ops.rebirth.should.have.been.calledWith({});
rootScope.$state.go.should.have.been.calledWith('tasks');
});
});
describe('#clickRebirth', function() {
it('displays a confirmation popover for the user', function() {
sandbox.spy($.fn, 'popover');
scope.clickRebirth(document.createElement('div'));
$.fn.popover.should.have.been.calledWith('destroy');
$.fn.popover.should.have.been.called;
$.fn.popover.should.have.been.calledWith('show');
});
});
2015-09-05 20:32:49 +00:00
});