1
1
/* 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 ;
4
4
5
5
beforeEach ( function ( ) {
6
6
bard . appModule ( 'topcoder' ) ;
@@ -28,11 +28,19 @@ describe.only('Topcoder File Input Directive', function() {
28
28
isolateScope = element . isolateScope ( ) ;
29
29
} ) ;
30
30
31
+ beforeEach ( function ( ) {
32
+ fileInput = $ ( element ) . find ( '.none' ) [ 0 ] ;
33
+ } ) ;
34
+
35
+ afterEach ( function ( ) {
36
+ scope . $destroy ( ) ;
37
+ fileInput = undefined ;
38
+ } ) ;
39
+
31
40
bard . verifyNoOutstandingHttpRequests ( ) ;
32
41
33
42
describe ( 'selectFile' , function ( ) {
34
43
it ( 'triggers a click on the file input' , function ( ) {
35
- var fileInput = $ ( element ) . find ( '.none' ) [ 0 ] ;
36
44
var mockClick = sinon . spy ( fileInput , 'click' ) ;
37
45
38
46
isolateScope . selectFile ( ) ;
@@ -42,17 +50,99 @@ describe.only('Topcoder File Input Directive', function() {
42
50
} ) ;
43
51
} ) ;
44
52
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' ) ;
48
84
} ) ;
49
85
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
+ } ) ;
52
125
} ) ;
53
126
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
+ } ) ;
56
145
} ) ;
146
+
57
147
} ) ;
58
148
} ) ;
0 commit comments