habitica/test/spec/services/questServicesSpec.js

120 lines
3.6 KiB
JavaScript
Raw Normal View History

'use strict';
describe('Quests Service', function() {
var scope, rootScope, groupsService, quest, questsService, user, content;
beforeEach(function() {
user = specHelper.newUser();
2015-07-14 19:24:11 +00:00
user.ops = {
buyQuest: sandbox.spy()
};
user.achievements.quests = {};
quest = {lvl:20};
module(function($provide) {
$provide.value('User', {user: user});
});
2015-07-14 19:55:13 +00:00
inject(function($rootScope, $controller, Quests, Groups, Content) {
2015-07-14 19:24:11 +00:00
scope = $rootScope.$new();
rootScope = $rootScope;
2015-07-14 19:24:11 +00:00
$controller('RootCtrl', {$scope: scope, User: {user: user}});
questsService = Quests;
2015-07-14 17:43:02 +00:00
groupsService = Groups;
2015-07-14 19:55:13 +00:00
content = Content;
});
2015-07-14 17:43:02 +00:00
sandbox.stub(groupsService, 'inviteOrStartParty');
sandbox.stub(rootScope, 'openModal');
2015-07-14 17:43:02 +00:00
sandbox.stub(window,'confirm',function(){return true});
sandbox.stub(window,'alert');
});
context('functions', function() {
describe('lock quest', function() {
it('locks quest when user does not meet level requirement', function() {
user.stats.lvl = 15;
2015-07-14 16:46:13 +00:00
expect(questsService.lockQuest(quest)).to.be.ok;
});
it('does not lock quest if we ignore level requirement', function() {
user.stats.lvl = 15;
2015-07-14 16:46:13 +00:00
expect(questsService.lockQuest(quest,true)).to.not.be.ok;
});
it('does not lock quest if user meets level requirement', function() {
user.stats.lvl = 20;
2015-07-14 16:46:13 +00:00
expect(questsService.lockQuest(quest)).to.not.be.ok;
});
it('locks quest if user has not completed previous quest in series', function() {
quest.previous = 'priorQuest';
user.stats.lvl = 25;
2015-07-14 16:46:13 +00:00
expect(questsService.lockQuest(quest)).to.be.ok;
});
it('does not lock quest if user has completed previous quest in series', function() {
quest.previous = 'priorQuest';
user.stats.lvl = 25;
user.achievements.quests.priorQuest = 1;
2015-07-14 16:46:13 +00:00
expect(questsService.lockQuest(quest)).to.not.be.ok;
});
});
2015-07-14 17:43:02 +00:00
describe('buy quest', function() {
it('prompts user to invite friends to party for invite reward quests', function() {
2015-07-14 19:27:25 +00:00
questsService.buyQuest('basilist');
2015-07-14 17:43:02 +00:00
expect(window.confirm).to.have.been.calledOnce;
expect(groupsService.inviteOrStartParty).to.have.been.calledOnce;
expect(rootScope.openModal).to.have.been.notCalled;
2015-07-14 17:43:02 +00:00
});
it('does not allow user to buy quests whose previous quests are incomplete', function() {
2015-07-14 19:27:25 +00:00
user.stats.lvl = 100;
2015-07-14 17:43:02 +00:00
2015-07-14 19:27:25 +00:00
questsService.buyQuest('goldenknight2');
2015-07-14 17:43:02 +00:00
expect(window.alert).to.have.been.calledOnce;
expect(rootScope.openModal).to.have.been.notCalled;
2015-07-14 17:43:02 +00:00
});
it('does not allow user to buy quests beyond their level', function() {
user.stats.lvl = 1;
2015-07-14 19:27:25 +00:00
questsService.buyQuest('vice1');
2015-07-14 17:43:02 +00:00
expect(window.alert).to.have.been.calledOnce;
expect(rootScope.openModal).to.have.been.notCalled;
2015-07-14 17:43:02 +00:00
});
it('opens purchase modal if all prerequisites are met', function() {
2015-07-14 19:27:25 +00:00
user.stats.lvl = 100;
user.achievements.quests.atom1 = 2;
2015-07-14 17:43:02 +00:00
2015-07-14 19:27:25 +00:00
questsService.buyQuest('atom2');
2015-07-14 19:55:13 +00:00
expect(scope.selectedQuest).to.eql(content.quests.atom2);
expect(rootScope.openModal).to.have.been.calledOnce;
expect(rootScope.openModal).to.have.been.calledWith('buyQuest');
2015-07-14 17:43:02 +00:00
});
it('calls user ops buyQuest if quest is Gold-purchasable', function() {
2015-07-14 19:27:25 +00:00
questsService.buyQuest('dilatoryDistress1');
2015-07-14 17:43:02 +00:00
expect(user.ops.buyQuest).to.have.been.calledOnce;
expect(rootScope.openModal).to.have.been.notCalled;
2015-07-14 17:43:02 +00:00
});
});
});
2015-07-14 16:46:13 +00:00
});