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

Commit 58d35bd

Browse files
committedJan 18, 2016
Merge pull request #669 from appirio-tech/SUP-2935-tc-file-tests
Sup 2935 tc file tests
2 parents 8bcf316 + 8a7391b commit 58d35bd

File tree

6 files changed

+152
-11
lines changed

6 files changed

+152
-11
lines changed
 

‎app/directives/tc-file-input/tc-file-input.directive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
var fileInput = $(element[0]).find('.none');
2929
var fileNameInput = $(element[0]).find('input[type=text]');
3030

31-
fileInput.bind('change', function() {
32-
var file = fileInput[0].files[0];
31+
fileInput.bind('change', function(event) {
32+
var file = event.target.files[0];
3333

3434
// About 1 in 20 times, the file is undefined (must be race condition)
3535
// Return early in this case so no errors are thrown
Lines changed: 136 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,148 @@
11
/* jshint -W117, -W030 */
22
describe('Topcoder File Input Directive', function() {
3-
var scope;
4-
5-
// USE AS TEMPLATE FOR DIRECTIVES
3+
var scope, element, isolateScope, fileInput;
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 html = '' +
11+
'<form>' +
12+
'<tc-file-input ' +
13+
'label-text="Preview Image"' +
14+
'field-id="DESIGN_COVER"' +
15+
'button-text="Add File"' +
16+
'file-type="jpg,jpeg,png"' +
17+
'placeholder="Image file as .jpg or .png"' +
18+
'mandatory="true"' +
19+
'set-file-reference="vm.setFileReference(file, fieldId)"' +
20+
'ng-model="vm.submissionForm.submissionZip"' +
21+
' />' +
22+
'</form>';
23+
var form = angular.element(html);
24+
element = form.find('tc-file-input');
25+
var formElement = $compile(form)(scope);
26+
scope.$digest();
27+
28+
isolateScope = element.isolateScope();
29+
});
30+
31+
beforeEach(function() {
32+
fileInput = $(element).find('.none')[0];
33+
});
34+
35+
afterEach(function() {
36+
scope.$destroy();
37+
fileInput = undefined;
1038
});
1139

1240
bard.verifyNoOutstandingHttpRequests();
1341

14-
xdescribe('', function() {
15-
beforeEach(function() {});
42+
describe('selectFile', function() {
43+
it('triggers a click on the file input', function() {
44+
var mockClick = sinon.spy(fileInput, 'click');
45+
46+
isolateScope.selectFile();
47+
scope.$digest();
48+
49+
expect(mockClick).calledOnce;
50+
});
51+
});
52+
53+
describe('a change event on the file input', function() {
54+
var fileNameInput, fileList, mockSetFileReference;
55+
56+
beforeEach(function() {
57+
fileNameInput = $(element).find('input[type=text]')[0];
58+
fileList = {
59+
0: {
60+
name: 'test.png',
61+
size: 50,
62+
type: 'image/png'
63+
},
64+
length: 1,
65+
item: function (index) { return file; }
66+
};
67+
68+
mockSetFileReference = sinon.spy(isolateScope, 'setFileReference');
69+
});
70+
71+
afterEach(function() {
72+
fileNameInput = undefined;
73+
fileList = undefined;
74+
mockSetFileReference = undefined;
75+
});
76+
77+
it('sets the value of the fileNameInput with the name of the file', function() {
78+
$(fileInput).triggerHandler({
79+
type: 'change',
80+
target: { files: fileList }
81+
});
82+
83+
expect(fileNameInput.value).to.equal('test.png');
84+
});
85+
86+
describe('with a valid file', function() {
87+
beforeEach(function() {
88+
$(fileInput).triggerHandler({
89+
type: 'change',
90+
target: { files: fileList }
91+
});
92+
});
93+
94+
it('calls setFileReference', function() {
95+
expect(mockSetFileReference).calledOnce;
96+
});
97+
98+
it('has ng-valid-filesize class', function() {
99+
expect($(fileInput).hasClass('ng-valid-filesize')).to.be.true;
100+
});
101+
102+
it('has ng-valid-required class', function() {
103+
expect($(fileInput).hasClass('ng-valid-required')).to.be.true;
104+
});
105+
});
106+
107+
describe('with a file that\'s greater than 500MB', function() {
108+
beforeEach(function() {
109+
fileList[0].size = 500000001;
110+
111+
$(fileInput).triggerHandler({
112+
type: 'change',
113+
target: { files: fileList }
114+
});
115+
});
116+
117+
it('does not call setFileReference', function() {
118+
expect(mockSetFileReference).not.calledOnce;
119+
});
120+
121+
it('has ng-touched and ng-invalid-filesize classes', function() {
122+
expect($(fileInput).hasClass('ng-invalid-filesize')).to.be.true;
123+
expect($(fileInput).hasClass('ng-touched')).to.be.true;
124+
});
125+
});
126+
127+
describe('with a file type that\'s not in the list of fileTypes given to the directive', function() {
128+
beforeEach(function() {
129+
fileList[0].type = 'application/zip';
130+
131+
$(fileInput).triggerHandler({
132+
type: 'change',
133+
target: { files: fileList }
134+
});
135+
});
136+
137+
it('does not call setFileReference', function() {
138+
expect(mockSetFileReference).not.calledOnce;
139+
});
140+
141+
it('has ng-touched and ng-invalid-required classes', function() {
142+
expect($(fileInput).hasClass('ng-invalid-required')).to.be.true;
143+
expect($(fileInput).hasClass('ng-touched')).to.be.true;
144+
});
145+
});
16146

17-
it('', function() {});
18147
});
19148
});

‎app/directives/tc-form-fonts/tc-form-fonts.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ describe('Topcoder Form Fonts Directive', function() {
1515
isolateScope = element.isolateScope();
1616
});
1717

18+
afterEach(function() {
19+
scope.$destroy();
20+
});
21+
1822
bard.verifyNoOutstandingHttpRequests();
1923

2024
describe('is initialized with', function() {

‎app/directives/tc-form-stockart/tc-form-stockart.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ describe('Topcoder Form Stockart Directive', function() {
1515
isolateScope = element.isolateScope();
1616
});
1717

18+
afterEach(function() {
19+
scope.$destroy();
20+
});
21+
1822
bard.verifyNoOutstandingHttpRequests();
1923

2024
describe('is initialized with', function() {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ describe('Topcoder Input Directive', function() {
1111
scope.$digest();
1212
});
1313

14+
afterEach(function() {
15+
scope.$destroy();
16+
});
17+
1418
bard.verifyNoOutstandingHttpRequests();
1519

1620
it('should set inputType to text if no inputType given', function() {

‎app/specs.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ <h1><a href="specs.html">Spec Runner</a></h1>
270270
<script src="/app/my-dashboard/my-dashboard.spec.js"></script>
271271
<script src="/app/my-srms/my-srms.spec.js"></script>
272272
<script src="/app/profile/profile.controller.spec.js"></script>
273+
<script src="/app/settings/settings.spec.js"></script>
273274
<script src="/app/services/authToken.service.spec.js"></script>
274275
<script src="/app/services/challenge.service.spec.js"></script>
275276
<script src="/app/services/externalAccounts.service.spec.js"></script>
@@ -280,12 +281,11 @@ <h1><a href="specs.html">Spec Runner</a></h1>
280281
<script src="/app/services/tcAuth.service.spec.js"></script>
281282
<script src="/app/services/user.service.spec.js"></script>
282283
<script src="/app/services/userStats.service.spec.js"></script>
283-
<script src="/app/settings/settings.spec.js"></script>
284284
<script src="/app/skill-picker/skill-picker.spec.js"></script>
285285
<script src="/app/submissions/submissions.spec.js"></script>
286286
<script src="/app/account/login/login.spec.js"></script>
287-
<script src="/app/account/register/register.spec.js"></script>
288287
<script src="/app/account/logout/logout.controller.spec.js"></script>
288+
<script src="/app/account/register/register.spec.js"></script>
289289
<script src="/app/account/reset-password/reset-password.spec.js"></script>
290290
<script src="/app/blocks/exception/exception-handler.provider.spec.js"></script>
291291
<script src="/app/directives/badges/badge-tooltip.spec.js"></script>

0 commit comments

Comments
 (0)
This repository has been archived.