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

Commit 8a7391b

Browse files
author
Nick Litwin
committed
Add tests for tc-file-input
1 parent 2d90cc1 commit 8a7391b

File tree

5 files changed

+114
-12
lines changed

5 files changed

+114
-12
lines changed

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

+2-2
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

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

+100-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* jshint -W117, -W030 */
2-
describe.only('Topcoder File Input Directive', function() {
3-
var scope, element, isolateScope;
2+
describe('Topcoder File Input Directive', function() {
3+
var scope, element, isolateScope, fileInput;
44

55
beforeEach(function() {
66
bard.appModule('topcoder');
@@ -28,11 +28,19 @@ describe.only('Topcoder File Input Directive', function() {
2828
isolateScope = element.isolateScope();
2929
});
3030

31+
beforeEach(function() {
32+
fileInput = $(element).find('.none')[0];
33+
});
34+
35+
afterEach(function() {
36+
scope.$destroy();
37+
fileInput = undefined;
38+
});
39+
3140
bard.verifyNoOutstandingHttpRequests();
3241

3342
describe('selectFile', function() {
3443
it('triggers a click on the file input', function() {
35-
var fileInput = $(element).find('.none')[0];
3644
var mockClick = sinon.spy(fileInput, 'click');
3745

3846
isolateScope.selectFile();
@@ -42,17 +50,99 @@ describe.only('Topcoder File Input Directive', function() {
4250
});
4351
});
4452

45-
describe('change events on the file input', function() {
46-
it('do things', function() {
47-
expect(false).to.be.true;
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');
4884
});
4985

50-
it('do other things', function() {
51-
expect(false).to.be.true;
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+
});
52125
});
53126

54-
it('yet some more things', function() {
55-
expect(false).to.be.true;
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+
});
56145
});
146+
57147
});
58148
});

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

+4
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

+4
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

+4
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() {

0 commit comments

Comments
 (0)