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

Commit c310e68

Browse files
author
Nick Litwin
committed
Add unit tests for tc-form-stockart
1 parent b6640a7 commit c310e68

File tree

7 files changed

+224
-29
lines changed

7 files changed

+224
-29
lines changed

app/directives/tc-form-stockart/tc-form-stockart.directive.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
isFileNumberRequired: false
2727
};
2828

29+
// Initialize stockart form data
30+
$scope.formStockarts = { 0: _.assign({id: 0}, angular.copy(emptyStockart)) };
31+
2932
$scope.urlRegEx = new RegExp(/^(http(s?):\/\/)?(www\.)?[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,3})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/);
3033

3134
$scope.createAdditionalStockartFieldset = function() {
Lines changed: 204 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,216 @@
11
/* jshint -W117, -W030 */
22
describe('Topcoder Form Stockart Directive', function() {
3-
var scope;
4-
5-
// USE AS TEMPLATE FOR DIRECTIVES
3+
var scope, element, isolateScope;
64

75
beforeEach(function() {
8-
bard.appModule('tcUIComponents');
6+
bard.appModule('topcoder');
97
bard.inject(this, '$compile', '$rootScope');
8+
scope = $rootScope.$new();
9+
10+
var form = angular.element('<form><tc-form-stockart /></form>');
11+
element = form.find('tc-form-stockart');
12+
var formElement = $compile(form)(scope);
13+
scope.$digest();
14+
15+
isolateScope = element.isolateScope();
1016
});
1117

1218
bard.verifyNoOutstandingHttpRequests();
1319

14-
xdescribe('', function() {
15-
beforeEach(function() {});
20+
it('starts with empty stockart data', function() {
21+
defaultFormStockart = isolateScope.formStockarts[0];
22+
23+
expect(defaultFormStockart.id).to.equal(0);
24+
expect(defaultFormStockart.description).to.equal('');
25+
expect(defaultFormStockart.sourceUrl).to.equal('');
26+
expect(defaultFormStockart.fileNumber).to.equal('');
27+
expect(defaultFormStockart.isPhotoDescriptionRequired).to.equal(false);
28+
expect(defaultFormStockart.isPhotoURLRequired).to.equal(false);
29+
expect(defaultFormStockart.isFileNumberRequired).to.equal(false);
30+
});
31+
32+
describe('createAdditionalStockartFieldset', function() {
33+
it('creates a new fieldset', function() {
34+
expect(Object.keys(isolateScope.formStockarts).length).to.equal(1);
35+
36+
isolateScope.createAdditionalStockartFieldset();
37+
scope.$digest();
38+
39+
expect(Object.keys(isolateScope.formStockarts).length).to.equal(2);
40+
});
41+
42+
it('adds an incremented id to the new fieldset', function() {
43+
var id = isolateScope.formStockarts[0].id;
44+
45+
isolateScope.createAdditionalStockartFieldset();
46+
scope.$digest();
47+
48+
expect(isolateScope.formStockarts[1]).to.exist;
49+
expect(isolateScope.formStockarts[1].id).to.equal(id + 1);
50+
});
51+
})
52+
53+
describe('deleteStockartFieldset', function() {
54+
it('deletes the selected stockart fieldset', function() {
55+
isolateScope.createAdditionalStockartFieldset();
56+
scope.$digest();
57+
58+
expect(Object.keys(isolateScope.formStockarts).length).to.equal(2);
59+
60+
isolateScope.deleteStockartFieldset(1);
61+
scope.$digest();
62+
63+
expect(Object.keys(isolateScope.formStockarts).length).to.equal(1);
64+
});
65+
66+
it('resets the stockart fieldset when it\'s the only one', function() {
67+
var stockart = isolateScope.formStockarts[0];
68+
69+
expect(stockart.description).to.equal('');
70+
71+
stockart.description = 'a funny cat picture';
72+
scope.$digest();
73+
74+
expect(stockart.description).to.equal('a funny cat picture');
75+
76+
isolateScope.deleteStockartFieldset(0);
77+
scope.$digest();
78+
79+
expect(isolateScope.formStockarts[0].description).to.equal('');
80+
});
81+
});
82+
83+
describe('isButtonDisabled', function() {
84+
var button;
85+
86+
beforeEach(function() {
87+
button = $(element).find('.fieldset__button')[0];
88+
});
89+
90+
afterEach(function() {
91+
button = undefined;
92+
});
93+
94+
it('disables the button when no fields are filled out', function() {
95+
expect(button.disabled).to.be.true;
96+
});
97+
98+
it('disables the button when 1 field is filled out', function() {
99+
isolateScope.formStockarts[0].description = 'test description';
100+
scope.$digest();
101+
102+
expect(button.disabled).to.be.true;
103+
});
104+
105+
it('disables the button when 2 fields are filled out', function() {
106+
isolateScope.formStockarts[0].description = 'test description';
107+
isolateScope.formStockarts[0].sourceUrl = 'url';
108+
scope.$digest();
109+
110+
expect(button.disabled).to.be.true;
111+
});
112+
113+
it('enables the button when all fields are filled out', function() {
114+
isolateScope.formStockarts[0].description = 'test description';
115+
isolateScope.formStockarts[0].sourceUrl = 'url';
116+
isolateScope.formStockarts[0].fileNumber = '123';
117+
scope.$digest();
118+
119+
expect(button.disabled).to.be.false;
120+
});
121+
122+
it('disables the button when any field in any fieldset is empty', function() {
123+
expect(button.disabled).to.be.true;
124+
125+
// Fill out first fieldset
126+
isolateScope.formStockarts[0].description = 'test description';
127+
isolateScope.formStockarts[0].sourceUrl = 'url.com';
128+
isolateScope.formStockarts[0].fileNumber = '123';
129+
scope.$digest();
130+
131+
expect(button.disabled).to.be.false;
132+
133+
isolateScope.createAdditionalStockartFieldset();
134+
scope.$digest();
135+
136+
expect(button.disabled).to.be.true;
137+
138+
// Fill out second fieldset
139+
isolateScope.formStockarts[1].description = 'test description2';
140+
isolateScope.formStockarts[1].sourceUrl = 'url2.com';
141+
isolateScope.formStockarts[1].fileNumber = '1232';
142+
scope.$digest();
143+
144+
expect(button.disabled).to.be.false;
145+
146+
// Empty a field in the first fieldset
147+
isolateScope.formStockarts[0].fileNumber = '';
148+
scope.$digest();
149+
150+
expect(button.disabled).to.be.true;
151+
});
152+
})
153+
154+
describe('showMandatoryMessage', function() {
155+
describe('sets the stockart required properties to false when all fields are', function() {
156+
var stockart;
157+
158+
beforeEach(function() {
159+
stockart = isolateScope.formStockarts[0];
160+
stockart.description = 'test description';
161+
stockart.sourceUrl = 'url.com';
162+
stockart.fileNumber = '123';
163+
scope.$digest();
164+
});
165+
166+
afterEach(function() {
167+
stockart = undefined;
168+
});
169+
170+
it('filled out', function() {
171+
expect(stockart.isPhotoDescriptionRequired).to.be.false;
172+
expect(stockart.isPhotoURLRequired).to.be.false;
173+
expect(stockart.isFileNumberRequired).to.be.false;
174+
});
175+
176+
it('empty', function() {
177+
// Reset stockart fields
178+
stockart.description = '';
179+
stockart.sourceUrl = '';
180+
stockart.fileNumber = '';
181+
scope.$digest();
182+
183+
expect(stockart.isPhotoDescriptionRequired).to.be.false;
184+
expect(stockart.isPhotoURLRequired).to.be.false;
185+
expect(stockart.isFileNumberRequired).to.be.false;
186+
});
187+
});
188+
189+
190+
describe('sets the stockart required properties to false when all fields are', function() {
191+
var stockart;
192+
193+
beforeEach(function() {
194+
stockart = isolateScope.formStockarts[0];
195+
stockart.description = 'test description';
196+
stockart.sourceUrl = 'url.com';
197+
stockart.fileNumber = '123';
198+
scope.$digest();
199+
});
200+
201+
afterEach(function() {
202+
stockart = undefined;
203+
});
204+
205+
it('sets the stockart required properties to true if any field is blank', function() {
206+
// Reset stockart fields
207+
stockart.description = '';
208+
scope.$digest();
16209

17-
it('', function() {});
210+
expect(stockart.isPhotoDescriptionRequired).to.be.true;
211+
expect(stockart.isPhotoURLRequired).to.be.true;
212+
expect(stockart.isFileNumberRequired).to.be.true;
213+
});
214+
});
18215
});
19216
});

app/directives/tc-input/tc-input.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* jshint -W117, -W030 */
22
describe('Topcoder Input Directive', function() {
3-
var scope, element, controller;
3+
var scope, element;
44

55
beforeEach(function() {
66
bard.appModule('topcoder');

app/filters/filters.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ describe('filters', function() {
131131
expect(ternaryFilter(true, 1, 2)).to.be.equal(1);
132132
expect(ternaryFilter(false, 1, 2)).to.be.equal(2);
133133
expect(ternaryFilter(0, 1, 2)).to.be.equal(2);
134-
console.log(jstz.determine().name());
135134
expect(ternaryFilter(true, 'm', 'n')).to.be.equal('m');
136135
});
137136
});

app/index.jade

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ html
156156
script(src='../bower_components/react/react.js')
157157
script(src='../bower_components/react/react-dom.js')
158158
script(src='../bower_components/classnames/index.js')
159-
script(src='../bower_components/classnames/bind.js')
160-
script(src='../bower_components/classnames/dedupe.js')
161159
script(src='../bower_components/react-input-autosize/dist/react-input-autosize.min.js')
162160
script(src='../bower_components/react-select/dist/react-select.min.js')
163161
script(src='../bower_components/ngReact/ngReact.js')

app/specs.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ <h1><a href="specs.html">Spec Runner</a></h1>
4444
</script>
4545

4646
<!-- bower:js -->
47+
<script src="../bower_components/zepto/zepto.js"></script>
4748
<script src="../bower_components/angular/angular.js"></script>
4849
<script src="../bower_components/a0-angular-storage/dist/angular-storage.js"></script>
4950
<script src="../bower_components/angucomplete-alt/angucomplete-alt.js"></script>
@@ -69,8 +70,6 @@ <h1><a href="specs.html">Spec Runner</a></h1>
6970
<script src="../bower_components/react/react.js"></script>
7071
<script src="../bower_components/react/react-dom.js"></script>
7172
<script src="../bower_components/classnames/index.js"></script>
72-
<script src="../bower_components/classnames/bind.js"></script>
73-
<script src="../bower_components/classnames/dedupe.js"></script>
7473
<script src="../bower_components/react-input-autosize/dist/react-input-autosize.min.js"></script>
7574
<script src="../bower_components/react-select/dist/react-select.min.js"></script>
7675
<script src="../bower_components/ngReact/ngReact.js"></script>
@@ -268,8 +267,8 @@ <h1><a href="specs.html">Spec Runner</a></h1>
268267
<script src="/app/topcoder.interceptors.spec.js"></script>
269268
<script src="/app/filters/filters.spec.js"></script>
270269
<script src="/app/my-challenges/my-challenges.spec.js"></script>
271-
<script src="/app/my-srms/my-srms.spec.js"></script>
272270
<script src="/app/my-dashboard/my-dashboard.spec.js"></script>
271+
<script src="/app/my-srms/my-srms.spec.js"></script>
273272
<script src="/app/profile/profile.controller.spec.js"></script>
274273
<script src="/app/services/authToken.service.spec.js"></script>
275274
<script src="/app/services/challenge.service.spec.js"></script>
@@ -285,8 +284,8 @@ <h1><a href="specs.html">Spec Runner</a></h1>
285284
<script src="/app/skill-picker/skill-picker.spec.js"></script>
286285
<script src="/app/submissions/submissions.spec.js"></script>
287286
<script src="/app/account/login/login.spec.js"></script>
288-
<script src="/app/account/logout/logout.controller.spec.js"></script>
289287
<script src="/app/account/register/register.spec.js"></script>
288+
<script src="/app/account/logout/logout.controller.spec.js"></script>
290289
<script src="/app/account/reset-password/reset-password.spec.js"></script>
291290
<script src="/app/blocks/exception/exception-handler.provider.spec.js"></script>
292291
<script src="/app/directives/badges/badge-tooltip.spec.js"></script>

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,7 @@
3232
isFontSourceRequired: false
3333
}
3434
};
35-
vm.formStockarts = {
36-
0: {
37-
id: 1,
38-
description: '',
39-
sourceUrl: '',
40-
fileNumber: '',
41-
isPhotoDescriptionRequired: false,
42-
isPhotoURLRequired: false,
43-
isFileNumberRequired: false
44-
}
45-
};
35+
vm.formStockarts = {};
4636
vm.submissionForm = {
4737
files: [],
4838

@@ -148,10 +138,18 @@
148138
vm.submissionsBody.data.submitterRank = vm.submissionForm.submitterRank;
149139

150140
// Process stock art
151-
var processedStockarts = _.map(vm.formStockarts, function(formStockart) {
141+
var processedStockarts = _.reduce(vm.formStockarts, function(compiledStockarts, formStockart) {
142+
if (formStockart.description) {
152143
delete formStockart.id;
153-
return formStockart;
154-
});
144+
delete formStockart.isPhotoDescriptionRequired;
145+
delete formStockart.isPhotoURLRequired;
146+
delete formStockart.isFileNumberRequired;
147+
148+
compiledStockarts.push(formStockart);
149+
}
150+
151+
return compiledStockarts;
152+
}, []);
155153

156154
vm.submissionsBody.data.stockArts = processedStockarts;
157155

@@ -164,6 +162,7 @@
164162
delete formFont.isFontNameRequired;
165163
delete formFont.isFontNameDisabled;
166164
delete formFont.isFontSourceRequired;
165+
167166
compiledFonts.push(formFont);
168167
}
169168

0 commit comments

Comments
 (0)