Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit efc71c4

Browse files
author
Nick Litwin
committed
Add unit tests for both submissions controllers
1 parent 58d35bd commit efc71c4

File tree

3 files changed

+258
-21
lines changed

3 files changed

+258
-21
lines changed

app/submissions/submissions.spec.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* jshint -W117, -W030 */
22
describe('Submissions Controller', function() {
3-
var controller;
4-
var vm;
3+
var controller, vm;
54

65
var mockChallenge = {
76
challenge: {
@@ -28,4 +27,10 @@ describe('Submissions Controller', function() {
2827
it('should exist', function() {
2928
expect(vm).to.exist;
3029
});
30+
31+
it('should have properties on vm from the routes resolve', function() {
32+
expect(vm.challengeTitle).to.equal(mockChallenge.challenge.name);
33+
expect(vm.challengeId).to.equal(30049240);
34+
expect(vm.track).to.equal(mockChallenge.challenge.track.toLowerCase());
35+
});
3136
});

app/submissions/submit-file/submit-file.controller.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,20 @@
9999
fileObject.mediaType = file.type;
100100
}
101101

102-
// If user picks a new file, replace the that file's fileObject with a new one
103-
// Or add it the list if it's not there
104-
if (vm.submissionsBody.data.files.length) {
105-
vm.submissionsBody.data.files.some(function(file, i, filesArray) {
106-
if (file.type === fileObject.type) {
107-
file = fileObject;
108-
} else if (filesArray.length === i + 1) {
109-
filesArray.push(fileObject);
110-
}
111-
});
112-
} else {
102+
// If user changes a file input's file, update the file details
103+
var isFound = vm.submissionsBody.data.files.reduce(function(isFound, file, i, filesArray) {
104+
if (isFound) { return true; }
105+
106+
if (file.type === fileObject.type) {
107+
filesArray[i] = fileObject;
108+
return true;
109+
}
110+
111+
return false;
112+
}, false);
113+
114+
// Add new files to the list
115+
if (!isFound) {
113116
vm.submissionsBody.data.files.push(fileObject);
114117
}
115118
}

app/submissions/submit-file/submit-file.spec.js

Lines changed: 237 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* jshint -W117, -W030 */
22
describe('Submit File Controller', function() {
3-
var controller;
4-
var vm;
5-
var scope;
3+
var controller, vm, scope;
64

75
var mockChallenge = {
86
challenge: {
@@ -12,14 +10,18 @@ describe('Submit File Controller', function() {
1210
}
1311
};
1412

15-
userService = {
13+
var userService = {
1614
getUserIdentity: function() {
1715
return {
1816
userId: 123456
1917
};
2018
}
2119
};
2220

21+
var submissionsService = {
22+
getPresignedURL: function() {}
23+
};
24+
2325
beforeEach(function() {
2426
bard.appModule('tc.submissions');
2527
bard.inject(this, '$controller', '$rootScope');
@@ -33,7 +35,8 @@ describe('Submit File Controller', function() {
3335
controller = $controller('SubmitFileController', {
3436
$scope: scope,
3537
UserService: userService,
36-
challengeToSubmitTo: mockChallenge
38+
challengeToSubmitTo: mockChallenge,
39+
SubmissionsService: submissionsService
3740
});
3841
vm = controller;
3942
});
@@ -42,9 +45,235 @@ describe('Submit File Controller', function() {
4245
expect(vm).to.exist;
4346
});
4447

45-
describe('updateProgress ', function() {
46-
it('should update PREPARE phase end ', function() {
47-
48+
describe('setRankTo1', function() {
49+
it('returns 1 if the input is blank', function() {
50+
expect(vm.setRankTo1('')).to.equal(1);
51+
});
52+
53+
it('returns the input value if not blank', function() {
54+
var inputText = 'sample input text';
55+
var result = vm.setRankTo1(inputText);
56+
57+
expect(result).to.equal(inputText);
58+
});
59+
});
60+
61+
62+
describe('setFileReference', function() {
63+
var file, fieldId;
64+
65+
beforeEach(function() {
66+
file = {
67+
name: 'Dashboard 2.png',
68+
size: 575548,
69+
type: 'image/png'
70+
};
71+
fieldId = 'DESIGN_COVER';
72+
73+
vm.setFileReference(file, fieldId);
74+
scope.$digest();
75+
});
76+
77+
afterEach(function() {
78+
file = undefined;
79+
fieldId = undefined;
80+
});
81+
82+
it('adds a file object to the submissions body', function() {
83+
expect(vm.submissionsBody.data.files).to.have.length(1);
84+
});
85+
86+
it('replaces a file object with a new one if it has the same fieldId', function() {
87+
expect(vm.submissionsBody.data.files).to.have.length(1);
88+
89+
var newFile = {
90+
name: 'different_image.png',
91+
size: 4321,
92+
type: 'image/png'
93+
};
94+
95+
vm.setFileReference(newFile, fieldId);
96+
scope.$digest();
97+
98+
expect(vm.submissionsBody.data.files).to.have.length(1);
99+
expect(vm.submissionsBody.data.files[0].name).to.equal('different_image.png');
100+
});
101+
102+
it('sets the correct mediaTypes on the fileObject', function() {
103+
expect(vm.submissionsBody.data.files[0].mediaType).to.equal('image/png');
104+
105+
var newFile = {
106+
name: 'submission.zip',
107+
size: 43121,
108+
type: 'application/zip'
109+
};
110+
var newFieldId = 'SUBMISSION_ZIP';
111+
112+
vm.setFileReference(newFile, newFieldId);
113+
scope.$digest();
114+
115+
expect(vm.submissionsBody.data.files[1].mediaType).to.equal('application/octet-stream');
116+
117+
var newFile2 = {
118+
name: 'source.zip',
119+
size: 2314,
120+
type: 'application/zip'
121+
};
122+
var newFieldId2 = 'SOURCE_ZIP';
123+
124+
vm.setFileReference(newFile2, newFieldId2);
125+
scope.$digest();
126+
127+
expect(vm.submissionsBody.data.files[2].mediaType).to.equal('application/octet-stream');
128+
});
129+
});
130+
131+
describe('uploadSubmission', function() {
132+
it('adds comments to the submissions body', function() {
133+
vm.comments = 'test comments';
134+
scope.$digest();
135+
136+
vm.uploadSubmission();
137+
scope.$digest();
138+
139+
expect(vm.submissionsBody.data.submitterComments).to.equal('test comments');
140+
});
141+
142+
it('adds the rank to the submissions body', function() {
143+
vm.submissionForm.submitterRank = 3;
144+
scope.$digest();
145+
146+
vm.uploadSubmission();
147+
scope.$digest();
148+
149+
expect(vm.submissionsBody.data.submitterRank).to.equal(3);
150+
});
151+
152+
it('calls the submission service', function() {
153+
var mockAPICall = sinon.spy(submissionsService, 'getPresignedURL');
154+
155+
vm.uploadSubmission();
156+
scope.$digest();
157+
158+
expect(mockAPICall).calledOnce;
159+
});
160+
161+
describe('processes the stockart and', function() {
162+
it('returns an empty array if no stockart given', function() {
163+
vm.formStockarts = [];
164+
scope.$digest();
165+
166+
vm.uploadSubmission();
167+
scope.$digest();
168+
169+
expect(vm.submissionsBody.data.stockArts).to.deep.equal([]);
170+
});
171+
172+
it('removes the required properties and id from each stockart', function() {
173+
vm.formStockarts = [
174+
{
175+
id: 0,
176+
description: 'first stockart',
177+
sourceUrl: 'url.com',
178+
fileNumber: '123',
179+
isPhotoDescriptionRequired: false,
180+
isPhotoURLRequired: false,
181+
isFileNumberRequired: false
182+
},
183+
{
184+
id: 1,
185+
description: 'second stockart',
186+
sourceUrl: 'url2.com',
187+
fileNumber: '234',
188+
isPhotoDescriptionRequired: false,
189+
isPhotoURLRequired: false,
190+
isFileNumberRequired: false
191+
}
192+
];
193+
var processedStockart = [
194+
{
195+
description: 'first stockart',
196+
sourceUrl: 'url.com',
197+
fileNumber: '123',
198+
},
199+
{
200+
description: 'second stockart',
201+
sourceUrl: 'url2.com',
202+
fileNumber: '234',
203+
}
204+
];
205+
scope.$digest();
206+
207+
vm.uploadSubmission();
208+
scope.$digest();
209+
expect(vm.submissionsBody.data.stockArts).to.deep.equal(processedStockart);
210+
211+
});
212+
});
213+
describe('processes the fonts and', function() {
214+
it('returns an empty array if no fonts given', function() {
215+
vm.formFonts = [];
216+
scope.$digest();
217+
218+
vm.uploadSubmission();
219+
scope.$digest();
220+
221+
expect(vm.submissionsBody.data.fonts).to.deep.equal([]);
222+
});
223+
224+
it('removes the required properties and id from each font', function() {
225+
vm.formFonts = [
226+
{
227+
id: 0,
228+
source: 'STUDIO_STANDARD_FONTS_LIST',
229+
name: 'my font',
230+
isFontUrlRequired: false,
231+
isFontUrlDisabled: true,
232+
isFontNameRequired: false,
233+
isFontNameDisabled: true,
234+
isFontSourceRequired: false
235+
},
236+
{
237+
id: 1,
238+
source: 'FONTS_DOT_COM',
239+
name: 'my other font',
240+
sourceUrl: 'fontsource.com',
241+
isFontUrlRequired: false,
242+
isFontUrlDisabled: true,
243+
isFontNameRequired: false,
244+
isFontNameDisabled: true,
245+
isFontSourceRequired: false
246+
}
247+
];
248+
var processedFonts = [
249+
{
250+
source: 'STUDIO_STANDARD_FONTS_LIST',
251+
name: 'my font',
252+
},
253+
{
254+
source: 'FONTS_DOT_COM',
255+
name: 'my other font',
256+
sourceUrl: 'fontsource.com',
257+
}
258+
];
259+
scope.$digest();
260+
261+
vm.uploadSubmission();
262+
scope.$digest();
263+
expect(vm.submissionsBody.data.fonts).to.deep.equal(processedFonts);
264+
});
265+
});
266+
});
267+
268+
describe('cancelRetry', function() {
269+
it('sets showProgress to false', function() {
270+
vm.showProgress = true;
271+
scope.$digest();
272+
expect(vm.showProgress).to.be.true;
273+
274+
vm.cancelRetry();
275+
scope.$digest();
276+
expect(vm.showProgress).to.be.false;
48277
});
49278
});
50279
});

0 commit comments

Comments
 (0)