1 | describe('ng-teams',function() {
|
2 | var ngServices = factory('services/ng-services');
|
3 | var module = factory('services/ng-teams',{
|
4 | 'services/ng-services': ngServices,
|
5 | 'services/log': logMock
|
6 | });
|
7 |
|
8 | var $teams;
|
9 | var rawMockTeam = { number: "123", name: "Oefenrondes", cityState: "foo" };
|
10 | var rawMockTeam2 = { number: "123", name: "Oefenrondes", translationNeeded: true };
|
11 | var savedMockTeam = {
|
12 | number: 123,
|
13 | name: "Oefenrondes",
|
14 | affiliation: "",
|
15 | cityState: "foo",
|
16 | country: "",
|
17 | coach1: "",
|
18 | coach2: "",
|
19 | judgingGroup: "",
|
20 | pitLocation: "",
|
21 | translationNeeded: false
|
22 | };
|
23 | var mockTeam = {
|
24 | index: 0,
|
25 | number: 123,
|
26 | name: "Oefenrondes",
|
27 | affiliation: "",
|
28 | cityState: "foo",
|
29 | country: "",
|
30 | coach1: "",
|
31 | coach2: "",
|
32 | judgingGroup: "",
|
33 | pitLocation: "",
|
34 | translationNeeded: false
|
35 | };
|
36 | var mockTeam2 = {
|
37 | index: 0,
|
38 | number: 123,
|
39 | name: "Oefenrondes",
|
40 | affiliation: "",
|
41 | cityState: "",
|
42 | country: "",
|
43 | coach1: "",
|
44 | coach2: "",
|
45 | judgingGroup: "",
|
46 | pitLocation: "",
|
47 | translationNeeded: true
|
48 | };
|
49 | var fsMock;
|
50 | var httpMock;
|
51 | beforeEach(function() {
|
52 | angular.mock.module(module.name);
|
53 | httpMock = createHttpMock({
|
54 | get: {
|
55 | '/teams': { data: [savedMockTeam] }
|
56 | },
|
57 | post: {
|
58 | '/teams/save': {teams:[savedMockTeam]}
|
59 | }
|
60 | });
|
61 | angular.mock.module(function($provide) {
|
62 | $provide.value('$http', httpMock);
|
63 | });
|
64 | angular.mock.inject(["$teams", function(_$teams_) {
|
65 | $teams = _$teams_;
|
66 | }]);
|
67 | });
|
68 |
|
69 | describe('initializing',function() {
|
70 | beforeEach(function(done){
|
71 | $teams.init().then(()=>{done();});
|
72 | });
|
73 |
|
74 | it('should load teams by default', function() {
|
75 | expect($teams.teams).toEqual([mockTeam]);
|
76 | });
|
77 | });
|
78 |
|
79 | describe('save',function() {
|
80 | it('should write teams to teams.json',function(done) {
|
81 | return $teams.save().then(function() {
|
82 | expect(httpMock.post).toHaveBeenCalledWith('/teams/save',{teams: [savedMockTeam]});
|
83 | done();
|
84 | });
|
85 | });
|
86 |
|
87 | it('should log an error if writing fails',function(done) {
|
88 | httpMock.post.and.returnValue(Q.reject('foo'));
|
89 | return $teams.save().then(function() {
|
90 | expect(logMock).toHaveBeenCalledWith('Teams write error','foo');
|
91 | done();
|
92 | });
|
93 | });
|
94 | });
|
95 |
|
96 | describe('load', function() {
|
97 | beforeEach((done) => $teams.init().then(() => done(), (err) => { console.error(err); done(); }));
|
98 | it('should load and sanitize teams',function(done) {
|
99 | $teams.clear();
|
100 | httpMock.addResponse('get','/teams',{data: [savedMockTeam]});
|
101 | return $teams.load().then(function() {
|
102 | expect($teams.teams).toEqual([mockTeam]);
|
103 | done();
|
104 | }, (err) => {
|
105 | console.error(err);
|
106 | done();
|
107 | });
|
108 | });
|
109 |
|
110 | it('should log an error if loading fails',function(done) {
|
111 | httpMock.get.and.returnValue(Q.reject('foo'));
|
112 | return $teams.load().then(function() {
|
113 | expect(logMock).toHaveBeenCalledWith('teams read error','foo');
|
114 | done();
|
115 | });
|
116 | });
|
117 | });
|
118 |
|
119 | describe('remove',function() {
|
120 |
|
121 | beforeEach((done) => $teams.init().then(() => done(), (err) => { console.error(err); done(); }));
|
122 |
|
123 | it('should remove the provided id',function() {
|
124 | expect($teams.teams).toEqual([mockTeam]);
|
125 | $teams.remove(123);
|
126 | expect($teams.teams).toEqual([]);
|
127 | });
|
128 |
|
129 | it('should not remove the team if not found',function() {
|
130 | expect($teams.teams).toEqual([mockTeam]);
|
131 | $teams.remove(42);
|
132 | expect($teams.teams).toEqual([mockTeam]);
|
133 | });
|
134 | });
|
135 |
|
136 | describe('add',function() {
|
137 | it('should add a team to the list and add autogen properties',function() {
|
138 | $teams.clear();
|
139 | var res = $teams.add(rawMockTeam);
|
140 | expect($teams.teams).toEqual([mockTeam]);
|
141 | });
|
142 | it('should add a team to the list and add autogen properties',function() {
|
143 | $teams.clear();
|
144 | var res = $teams.add(rawMockTeam2);
|
145 | expect($teams.teams).toEqual([mockTeam2]);
|
146 | });
|
147 | it('should reject duplicate team ids',function() {
|
148 | $teams.clear();
|
149 | $teams.add(rawMockTeam);
|
150 | expect(function() {
|
151 | $teams.add(rawMockTeam);
|
152 | }).toThrow();
|
153 | });
|
154 | it('should maintain existing teams array', function() {
|
155 | $teams.clear();
|
156 | var teams = $teams.teams;
|
157 | expect(teams).toEqual([]);
|
158 | $teams.add(rawMockTeam);
|
159 | expect(teams).toEqual([mockTeam]);
|
160 | });
|
161 | });
|
162 |
|
163 | describe('get',function() {
|
164 | beforeEach((done) => $teams.init().then(() => done(), (err) => { console.error(err); done(); }));
|
165 | it('should get a sanitized team', function() {
|
166 | expect($teams.get(123)).toEqual(mockTeam);
|
167 | });
|
168 | });
|
169 |
|
170 | describe('_update',function() {
|
171 | it('should throw an error if team is present twice',function() {
|
172 | $teams._rawTeams = [rawMockTeam,rawMockTeam];
|
173 | expect(function() {
|
174 | $teams._update();
|
175 | }).toThrowError('duplicate team number 123');
|
176 | });
|
177 | });
|
178 | });
|