Skip to content
This repository has been archived by the owner on Jun 16, 2018. It is now read-only.

Commit

Permalink
More tests for scores (needs a bit of love still)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkertkoppes committed Dec 25, 2014
1 parent 6535ef0 commit 5008e97
Showing 1 changed file with 110 additions and 47 deletions.
157 changes: 110 additions & 47 deletions spec/services/ng-scoresSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

describe('ng-scores',function() {
"use strict";
var ngServices = factory('services/ng-services');
var module = factory('services/ng-scores',{
'services/ng-services': ngServices,
Expand Down Expand Up @@ -76,13 +75,67 @@ describe('ng-scores',function() {
});
}

describe('initialize',function() {
describe('init',function() {
it('should load mock score initially',function() {
expect(filteredScores()).toEqual([mockScore]);
});
});

describe('adding scores',function() {
describe('clear',function() {
it('should clear the scores',function() {
expect(filteredScores()).toEqual([mockScore]);
$scores.clear();
expect(filteredScores()).toEqual([]);
});
});

describe('save',function() {
it('should write scores to scores.json',function() {
return $scores.save().then(function() {
expect(fsMock.write).toHaveBeenCalledWith(
'scores.json',
{
version: 2,
scores: [rawMockScore],
sheets: []
}
);
});
});

it('should log an error if writing fails',function() {
fsMock.write.andReturn(Q.reject('write err'));
return $scores.save().then(function() {
expect(logMock).toHaveBeenCalledWith('scores write error','write err');
});
});
});

describe('load',function() {
it('should load from scores.json',function() {
return $scores.load().then(function() {
expect(fsMock.read).toHaveBeenCalledWith('scores.json');
expect(filteredScores()).toEqual([mockScore]);
});
});

it('should log an error if loading fails',function() {
fsMock.read.andReturn(Q.reject('read err'));
return $scores.load().then(function() {
expect(logMock).toHaveBeenCalledWith('scores read error','read err');
});
});
});

describe('remove',function() {
it('should remove the provided index', function() {
expect(filteredScores()).toEqual([mockScore]);
$scores.remove(0);
expect(filteredScores()).toEqual([]);
});
});

describe('add',function() {
beforeEach(function() {
$scores.clear();
expect(filteredScores()).toEqual([]);
Expand Down Expand Up @@ -128,52 +181,20 @@ describe('ng-scores',function() {
expect($scores.scores[0].score).toEqual(null);
expect($scores.validationErrors.length).toEqual(1);
});
});

describe('loading',function() {
it('should load from scores.json',function() {
return $scores.load().then(function() {
expect(fsMock.read).toHaveBeenCalledWith('scores.json');
expect(filteredScores()).toEqual([mockScore]);
});
it('should store the edited date of a score',function() {
var tmp = angular.copy(mockScore);
tmp.edited = new Date(2015,1,7);
$scores.add(tmp);
expect($scores.scores[0].edited).toEqual('Sat Feb 07 2015 00:00:00 GMT+0100 (W. Europe Standard Time)');
});
});

describe('clearing',function() {
it('should clear the scores',function() {
expect(filteredScores()).toEqual([mockScore]);
describe('update', function() {
beforeEach(function() {
$scores.clear();
expect(filteredScores()).toEqual([]);
});
});

describe('saving',function() {
it('should write scores to scores.json',function() {
return $scores.save().then(function() {
expect(fsMock.write).toHaveBeenCalledWith(
'scores.json',
{
version: 2,
scores: [rawMockScore],
sheets: []
}
);
});
});
});

describe('removing',function() {
it('should remove the provided index', function() {
expect(filteredScores()).toEqual([mockScore]);
$scores.remove(0);
expect(filteredScores()).toEqual([]);
$scores.add(mockScore);
});
});

describe('modification', function() {
it('should mark modified scores', function() {
$scores.clear();
$scores.add(mockScore);
mockScore.score++;
// Simply changing the added score shouldn't matter...
expect($scores.scores[0].score).toEqual(150);
Expand All @@ -185,14 +206,24 @@ describe('ng-scores',function() {
expect($scores.scores[0].edited).toBeTruthy();
});
it('should accept numeric scores as strings',function() {
$scores.clear();
$scores.add(mockScore);
mockScore.score = "151";
$scores.update(0, mockScore);
// Note: the 'accepted' score should really be a number, not a string
expect($scores.scores[0].originalScore).toEqual(150);
expect($scores.scores[0].score).toEqual(151);
});
it('should throw an error if a score out of range is edited',function() {
var f = function() {
$scores.update(-1,mockScore);
};
expect(f).toThrow('unknown score index: -1');
});
it('should throw an error if a score out of range is edited',function() {
var f = function() {
$scores.update(1,mockScore);
};
expect(f).toThrow('unknown score index: 1');
});
});

describe('scoreboard', function() {
Expand Down Expand Up @@ -316,7 +347,7 @@ describe('ng-scores',function() {
], true);
$scores.scores.forEach(function(score) {
expect(score.error).toBeInstanceOf($scores.InvalidScoreError);
})
});
expect(board["test"].length).toEqual(0);
expect($scores.validationErrors.length).toEqual(6);
});
Expand Down Expand Up @@ -420,6 +451,38 @@ describe('ng-scores',function() {
);
});
});

describe('error recovery',function() {
it('should continue with no sheets when a 404 is returned',function() {
fsMock.list.andReturn(Q.reject({status:404}));
$scores.save = jasmine.createSpy('save');
return $scores.pollSheets().then(function() {
expect(fsMock.write).not.toHaveBeenCalled();
expect($scores.save).not.toHaveBeenCalled();
});
});

it('throw an error if an http error is received',function() {
fsMock.list.andReturn(Q.reject({status:500,responseText:'server error',statusText:'foo'}));
return $scores.pollSheets().catch(function(err) {
expect(err.message).toEqual('error 500 (foo): server error');
});
});

it('should rethrow the error if something just goes wrong',function() {
fsMock.list.andReturn(Q.reject(new Error('squeek')));
return $scores.pollSheets().catch(function(err) {
expect(err.message).toEqual('squeek');
});
});

it('should throw an unknown error if strange stuff is returned',function() {
fsMock.list.andReturn(Q.reject('darn'));
return $scores.pollSheets().catch(function(err) {
expect(err.message).toEqual('unknown error: darn');
});
});
});
});

});

0 comments on commit 5008e97

Please sign in to comment.