1 | describe('ng-score',function() {
|
2 | var ngServices = factory('services/ng-services');
|
3 | var module = factory('services/ng-score',{
|
4 | 'services/ng-services': ngServices
|
5 | });
|
6 |
|
7 | var $score;
|
8 |
|
9 | beforeEach(function() {
|
10 | angular.mock.module(module.name);
|
11 | angular.mock.inject(["$score", function(_$score_) {
|
12 | $score = _$score_;
|
13 | }]);
|
14 | });
|
15 |
|
16 | describe('compare', function() {
|
17 | var score1, score2;
|
18 |
|
19 | beforeEach(function() {
|
20 | score1 = new $score({
|
21 | score: 150
|
22 | });
|
23 | score2 = new $score({
|
24 | score: 110
|
25 | })
|
26 | });
|
27 |
|
28 | it('returns 0 when comparing score with itself', function() {
|
29 | expect($score.compare(score1, score1)).toBe(0);
|
30 | });
|
31 |
|
32 | it('returns a negative result when comparing score with higher score', function() {
|
33 | expect($score.compare(score1, score2) < 0).toBe(true);
|
34 | });
|
35 |
|
36 | it('returns a positive result when comparing score with lower score', function() {
|
37 | expect($score.compare(score2, score1) > 0).toBe(true);
|
38 | });
|
39 | });
|
40 |
|
41 | describe('generateUniqueId', function() {
|
42 | var RANDOM_COEFICCIENT = 20;
|
43 |
|
44 | it(`can generate ${RANDOM_COEFICCIENT} unique ids`, function() {
|
45 | var arr = [];
|
46 |
|
47 | for(var i = 0; i < RANDOM_COEFICCIENT; i++) {
|
48 | arr.push($score.generateUniqueId());
|
49 | }
|
50 |
|
51 | for(var i = 0; i < arr.length; i++) {
|
52 | for(var j = 0; j < i; j++) {
|
53 | expect(arr[i]).not.toBe(arr[j]);
|
54 | }
|
55 | }
|
56 | });
|
57 |
|
58 | it(`generates an id with length 8 every time out of ${RANDOM_COEFICCIENT}`, function() {
|
59 | for(var i = 0; i < RANDOM_COEFICCIENT; i++) {
|
60 | expect($score.generateUniqueId().length).toBe(8);
|
61 | }
|
62 | });
|
63 | });
|
64 |
|
65 | describe('santize', function() {
|
66 | var unsanitizedScore1, unsanitizedScore2;
|
67 | var sanitizedScore1, sanitizedScore2;
|
68 |
|
69 | beforeEach(function() {
|
70 | unsanitizedScore1 = {
|
71 | id: 'ade349b0',
|
72 | teamNumber: 143,
|
73 | stageId: 'anotherStageId',
|
74 | round: 2,
|
75 | score: 296,
|
76 | edited: 'Wed Nov 26 2014 21:11:43 GMT+0100 (CET)',
|
77 | table: 'red 1',
|
78 | published: true,
|
79 | otherProperty: 'other property value'
|
80 | };
|
81 | unsanitizedScore2 = {
|
82 | team: { number: 111 },
|
83 | stage: { id: 'qualification' },
|
84 | round: 1,
|
85 | score: 542,
|
86 | edited: 'Wed Nov 26 2014 21:11:43 GMT+0100 (CET)',
|
87 | table: 'blue 2'
|
88 | };
|
89 | sanitizedScore1 = $score.sanitize(unsanitizedScore1);
|
90 | sanitizedScore2 = $score.sanitize(unsanitizedScore2);
|
91 | });
|
92 |
|
93 | it('removes all redandent properties', function() {
|
94 | expect(sanitizedScore1.otherProperty).not.toBeDefined();
|
95 | });
|
96 |
|
97 | it('keeps the id property if it exists', function() {
|
98 | expect(sanitizedScore1.id).toBeDefined();
|
99 | });
|
100 |
|
101 | it('creates the id property if it doesn\'t exists', function() {
|
102 | expect(sanitizedScore2.id).toBeDefined();
|
103 | });
|
104 |
|
105 | it('keeps the teamNumber property if it exists', function() {
|
106 | expect(sanitizedScore1.teamNumber).toBeDefined();
|
107 | });
|
108 |
|
109 | it('creates the teamNumber property if there\'s only a team property', function() {
|
110 | expect(sanitizedScore2.teamNumber).toBeDefined();
|
111 | });
|
112 |
|
113 | it('keeps the stageId property if it exists', function() {
|
114 | expect(sanitizedScore1.stageId).toBeDefined();
|
115 | });
|
116 |
|
117 | it('creates the stageId property if there\'s only a stage property', function() {
|
118 | expect(sanitizedScore2.stageId).toBeDefined();
|
119 | });
|
120 |
|
121 | it('keeps the round property if it exists', function() {
|
122 | expect(sanitizedScore1.round).toBeDefined();
|
123 | });
|
124 |
|
125 | it('keeps the score property if it exists', function() {
|
126 | expect(sanitizedScore1.score).toBeDefined();
|
127 | });
|
128 |
|
129 | it('keeps the table property if it exists', function() {
|
130 | expect(sanitizedScore1.table).toBeDefined();
|
131 | });
|
132 |
|
133 | it('keeps the published property if it exists', function() {
|
134 | expect(sanitizedScore1.published).toBeDefined();
|
135 | });
|
136 |
|
137 | it('creates the published property it doesn\'t exists', function() {
|
138 | expect(sanitizedScore2.published).toBeDefined();
|
139 | });
|
140 | });
|
141 |
|
142 | describe('calcFilename', function() {
|
143 | var score;
|
144 | var filename;
|
145 |
|
146 | beforeEach(function() {
|
147 | score = new $score({
|
148 | id: '42cda0ib',
|
149 | stage: { id: 'qual' },
|
150 | round: 3,
|
151 | table: 'red 2',
|
152 | referee: 'tom',
|
153 | team: { number: 132 }
|
154 | });
|
155 | filename = 'score_qual_round3_tablered 2_reftom_team132_42cda0ib.json';
|
156 | });
|
157 |
|
158 | it('returns a correctly calculated filename', function() {
|
159 | expect(score.calcFilename()).toBe(filename);
|
160 | });
|
161 |
|
162 | it('save a correctly calculated filename in the score\'s file property', function() {
|
163 | score.calcFilename();
|
164 | expect(score.file).toBe(filename);
|
165 | });
|
166 | });
|
167 |
|
168 | });
|