1 | describe('ng-stages',function() {
|
2 | "use strict";
|
3 |
|
4 | var ngServices = factory('services/ng-services');
|
5 | var module = factory('services/ng-stages',{
|
6 | 'services/ng-services': ngServices,
|
7 | 'services/log': logMock
|
8 | });
|
9 |
|
10 | var $rootScope;
|
11 | var $stages;
|
12 | var $q;
|
13 | var mockStage;
|
14 | var mockStageSanitized;
|
15 | var unusedMockStage;
|
16 | var unusedMockStageSanitized;
|
17 |
|
18 |
|
19 |
|
20 | beforeEach(function() {
|
21 | mockStage = { id: "practice", name: "Practice Rounds", rounds: 2 };
|
22 | mockStageSanitized = { index: 0, id: "practice", name: "Practice Rounds", rounds: 2, $rounds: [1, 2] };
|
23 | unusedMockStage = { id: "unused", name: "Foobar", rounds: 0 };
|
24 | unusedMockStageSanitized = { index: 1, id: "unused", name: "Foobar", rounds: 0, $rounds: [] };
|
25 | })
|
26 | var httpMock;
|
27 | beforeEach(function() {
|
28 | angular.mock.module(module.name);
|
29 | httpMock = createHttpMock({
|
30 | get: {
|
31 | '/stages': { data: [mockStageSanitized] }
|
32 | },
|
33 | post: {
|
34 | '/stages/save': {stages: [mockStageSanitized]}
|
35 | }
|
36 | });
|
37 | angular.mock.module(function($provide) {
|
38 | $provide.value('$http', httpMock);
|
39 | });
|
40 | angular.mock.inject(["$q", "$stages", "$rootScope", function(_$q_, _$stages_, _$rootScope_) {
|
41 | $q = _$q_;
|
42 | $rootScope = _$rootScope_;
|
43 | $stages = _$stages_;
|
44 | }]);
|
45 | });
|
46 |
|
47 | describe('init',function() {
|
48 | beforeEach(function(done){
|
49 | done();
|
50 | });
|
51 | it('should load stages by default', function (done) {
|
52 | return $stages.init().then(function () {
|
53 | expect(httpMock.get).toHaveBeenCalledWith('/stages');
|
54 | expect($stages.stages).toEqual([mockStageSanitized]);
|
55 | done();
|
56 | });
|
57 | });
|
58 | });
|
59 |
|
60 | describe('save',function() {
|
61 |
|
62 | it('should write stages to stages.json',function(done) {
|
63 | $stages.stages = [mockStageSanitized];
|
64 | return $stages.save().then(function() {
|
65 | expect(httpMock.post).toHaveBeenCalledWith('/stages/save',{stages: [mockStageSanitized]});
|
66 | done();
|
67 | });
|
68 | });
|
69 | it('should log an error if writing fails',function(done) {
|
70 | httpMock.post.and.returnValue(Q.reject('aargh'));
|
71 | return $stages.save().then(function() {
|
72 | expect(logMock).toHaveBeenCalledWith('stages write error','aargh');
|
73 | done();
|
74 | });
|
75 | });
|
76 | });
|
77 |
|
78 | describe('load', function() {
|
79 | beforeEach(function(done){
|
80 | $stages.init().then(()=>{done();});
|
81 | });
|
82 |
|
83 | it('should load and sanitize stages',function(done) {
|
84 | return $stages.load().then(function() {
|
85 | expect($stages.stages).toEqual([mockStageSanitized]);
|
86 | done();
|
87 | });
|
88 | });
|
89 | it('should log an error if reading fails',function(done) {
|
90 | httpMock.get.and.returnValue(Q.reject('squeek'));
|
91 | return $stages.load().then(function() {
|
92 | expect(logMock).toHaveBeenCalledWith('stages read error','squeek');
|
93 | done();
|
94 | });
|
95 | });
|
96 | it('should initialize with default stages if reading fails',function(done) {
|
97 | httpMock.get.and.returnValue(Q.reject('squeek'));
|
98 | return $stages.load().then(function() {
|
99 | expect(logMock).toHaveBeenCalledWith('stages using defaults');
|
100 | expect($stages.allStages).toEqual([
|
101 | {index:0,id:"practice",name:"Practice Rounds",rounds:1,$rounds:[1]},
|
102 | {index:1,id:"qualifying",name:"Qualification Rounds",rounds:3,$rounds:[1,2,3]},
|
103 | {index:2,id:"quarter",name:"Quarterfinals",rounds:0,$rounds:[]},
|
104 | {index:3,id:"semi",name:"Semifinals",rounds:0,$rounds:[]},
|
105 | {index:4,id:"final",name:"Final",rounds:0,$rounds:[]}
|
106 | ]);
|
107 | done();
|
108 | });
|
109 | });
|
110 | });
|
111 |
|
112 | describe('remove',function() {
|
113 |
|
114 | beforeEach(function(done){
|
115 | $stages.init().then(()=>{done();});
|
116 | });
|
117 |
|
118 | it('should remove the provided id',function() {
|
119 | expect($stages.stages).toEqual([mockStageSanitized]);
|
120 | $stages.remove("practice");
|
121 | expect($stages.stages).toEqual([]);
|
122 | });
|
123 | it('should do nothing if id not found',function() {
|
124 | expect($stages.stages).toEqual([mockStageSanitized]);
|
125 | $stages.remove("foobar");
|
126 | expect($stages.stages).toEqual([mockStageSanitized]);
|
127 | });
|
128 | });
|
129 |
|
130 | describe('add',function() {
|
131 |
|
132 | it('should add a stage to the list and add autogen properties',function() {
|
133 | $stages.clear();
|
134 | var res = $stages.add(mockStage);
|
135 | expect($stages.stages).toEqual([mockStageSanitized]);
|
136 | });
|
137 | it('should reject duplicate stage ids',function() {
|
138 | $stages.clear();
|
139 | $stages.add(mockStage);
|
140 | expect(function() {
|
141 | $stages.add(mockStage);
|
142 | }).toThrow();
|
143 | });
|
144 | it('should reject a stage without an id, name, or rounds',function() {
|
145 | $stages.clear();
|
146 | $stages.add({ id: "foo", name: "bar", rounds: 0 });
|
147 | expect(function() {
|
148 | $stages.add(null);
|
149 | }).toThrow();
|
150 | expect(function() {
|
151 | $stages.add("meh");
|
152 | }).toThrow();
|
153 | expect(function() {
|
154 | $stages.add({ id: "foo", name: "bar" });
|
155 | }).toThrow();
|
156 | expect(function() {
|
157 | $stages.add({ id: "foo", rounds: 0 });
|
158 | }).toThrow();
|
159 | expect(function() {
|
160 | $stages.add({ name: "bar", rounds: 0 });
|
161 | }).toThrow();
|
162 | });
|
163 | it('should reject a stage with non-string id or name',function() {
|
164 | $stages.clear();
|
165 | expect(function() {
|
166 | $stages.add({ id: 1, name: "bar" });
|
167 | }).toThrow();
|
168 | expect(function() {
|
169 | $stages.add({ id: "foo", name: 2 });
|
170 | }).toThrow();
|
171 | });
|
172 | it('should maintain existing stages and allStages arrays', function() {
|
173 | $stages.clear();
|
174 | var allStages = $stages.allStages;
|
175 | var stages = $stages.stages;
|
176 | expect(allStages).toEqual([]);
|
177 | expect(stages).toEqual([]);
|
178 | $stages.add(mockStage);
|
179 | $stages.add(unusedMockStage);
|
180 | expect(allStages).toEqual([mockStageSanitized, unusedMockStageSanitized]);
|
181 | expect(stages).toEqual([mockStageSanitized]);
|
182 | });
|
183 | });
|
184 |
|
185 | describe('moveStage',function() {
|
186 | beforeEach(function() {
|
187 |
|
188 | $stages.clear();
|
189 | $stages.add(mockStage);
|
190 | $stages.add(unusedMockStage);
|
191 | })
|
192 | it('move down 1 step',function() {
|
193 | $stages.moveStage(mockStageSanitized,1);
|
194 | expect($stages.allStages).toEqual([
|
195 | { index: 0, id: "unused", name: "Foobar", rounds: 0, $rounds: [] },
|
196 | { index: 1, id:"practice", name:"Practice Rounds", rounds:2, $rounds:[1,2] }
|
197 | ]);
|
198 | });
|
199 | it('move up 1 step',function() {
|
200 | $stages.moveStage(unusedMockStageSanitized,-1);
|
201 | expect($stages.allStages).toEqual([
|
202 | { index: 0, id: "unused", name: "Foobar", rounds: 0, $rounds: [] },
|
203 | { index: 1, id:"practice", name:"Practice Rounds", rounds:2, $rounds:[1,2] }
|
204 | ]);
|
205 | });
|
206 | });
|
207 |
|
208 | describe('get',function() {
|
209 |
|
210 | beforeEach(function(done){
|
211 | $stages.init().then(()=> done());
|
212 | });
|
213 |
|
214 | it('should get a sanitized stage', function() {
|
215 | expect($stages.get("practice")).toEqual(mockStageSanitized);
|
216 | });
|
217 | });
|
218 |
|
219 | describe('_update',function() {
|
220 | it('should throw an error if stage is present twice',function() {
|
221 | $stages._rawStages = [mockStage,mockStage];
|
222 | expect(function() {
|
223 | $stages._update();
|
224 | }).toThrowError('duplicate stage id practice');
|
225 | });
|
226 | });
|
227 | });
|