|
| 1 | +/*eslint no-undef:0*/ |
| 2 | +import angular from 'angular' |
| 3 | + |
| 4 | +describe('Topcoder File Input Directive', function() { |
| 5 | + var scope, element, isolateScope, fileInput |
| 6 | + |
| 7 | + beforeEach(function() { |
| 8 | + bard.appModule('topcoder') |
| 9 | + bard.inject(this, '$compile', '$rootScope', '$timeout') |
| 10 | + scope = $rootScope.$new() |
| 11 | + |
| 12 | + var html = '' + |
| 13 | + '<form>' + |
| 14 | + '<tc-file-input ' + |
| 15 | + 'label-text="Submission"' + |
| 16 | + 'field-id="SUBMISSION_ZIP"' + |
| 17 | + 'button-text="Add File"' + |
| 18 | + 'file-type="zip"' + |
| 19 | + 'placeholder="Attach all visible files as a single .zip file"' + |
| 20 | + 'mandatory="true"' + |
| 21 | + 'set-file-reference="vm.setFileReference(file, fieldId)"' + |
| 22 | + 'ng-model="vm.submissionForm.submissionZip"' + |
| 23 | + ' />' + |
| 24 | + '</form>' |
| 25 | + var form = angular.element(html) |
| 26 | + element = form.find('tc-file-input') |
| 27 | + $compile(form)(scope) |
| 28 | + scope.$digest() |
| 29 | + |
| 30 | + isolateScope = element.isolateScope() |
| 31 | + }) |
| 32 | + |
| 33 | + beforeEach(function() { |
| 34 | + fileInput = $(element).find('.none')[0] |
| 35 | + }) |
| 36 | + |
| 37 | + afterEach(function() { |
| 38 | + scope.$destroy() |
| 39 | + fileInput = undefined |
| 40 | + }) |
| 41 | + |
| 42 | + bard.verifyNoOutstandingHttpRequests() |
| 43 | + |
| 44 | + describe('selectFile', function() { |
| 45 | + it('triggers a click on the file input', function() { |
| 46 | + var mockClick = sinon.spy(fileInput, 'click') |
| 47 | + |
| 48 | + isolateScope.selectFile() |
| 49 | + scope.$digest() |
| 50 | + |
| 51 | + expect(mockClick).calledOnce |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + describe('a change event on the file input', function() { |
| 56 | + var fileNameInput, fileList, mockSetFileReference |
| 57 | + |
| 58 | + beforeEach(function() { |
| 59 | + fileNameInput = $(element).find('input[type=text]')[0] |
| 60 | + fileList = { |
| 61 | + 0: { |
| 62 | + name: 'test.zip', |
| 63 | + size: 50, |
| 64 | + type: 'application/zip' |
| 65 | + }, |
| 66 | + length: 1, |
| 67 | + item: function (index) { return index } |
| 68 | + } |
| 69 | + |
| 70 | + mockSetFileReference = sinon.spy(isolateScope, 'setFileReference') |
| 71 | + }) |
| 72 | + |
| 73 | + afterEach(function() { |
| 74 | + fileNameInput = undefined |
| 75 | + fileList = undefined |
| 76 | + mockSetFileReference = undefined |
| 77 | + }) |
| 78 | + |
| 79 | + it('sets the value of the fileNameInput with the name of the file', function() { |
| 80 | + $(fileInput).triggerHandler({ |
| 81 | + type: 'change', |
| 82 | + target: { files: fileList } |
| 83 | + }) |
| 84 | + |
| 85 | + $timeout.flush() |
| 86 | + |
| 87 | + expect(fileNameInput.value).to.equal('test.zip') |
| 88 | + }) |
| 89 | + |
| 90 | + describe('with a valid file', function() { |
| 91 | + beforeEach(function() { |
| 92 | + $(fileInput).triggerHandler({ |
| 93 | + type: 'change', |
| 94 | + target: { files: fileList } |
| 95 | + }) |
| 96 | + $timeout.flush() |
| 97 | + }) |
| 98 | + |
| 99 | + it('calls setFileReference', function() { |
| 100 | + expect(mockSetFileReference).calledOnce |
| 101 | + }) |
| 102 | + |
| 103 | + it('has ng-valid-filesize class', function() { |
| 104 | + expect($(fileInput).hasClass('ng-valid-filesize')).to.be.true |
| 105 | + }) |
| 106 | + |
| 107 | + it('has ng-valid-required class', function() { |
| 108 | + expect($(fileInput).hasClass('ng-valid-required')).to.be.true |
| 109 | + }) |
| 110 | + |
| 111 | + it('works with Windows file type application/x-zip', function(){ |
| 112 | + fileList[0].type = 'application/x-zip' |
| 113 | + |
| 114 | + $(fileInput).triggerHandler({ |
| 115 | + type: 'change', |
| 116 | + target: { files: fileList } |
| 117 | + }) |
| 118 | + |
| 119 | + $timeout.flush() |
| 120 | + |
| 121 | + expect(mockSetFileReference).called |
| 122 | + expect($(fileInput).hasClass('ng-valid-filesize')).to.be.true |
| 123 | + expect($(fileInput).hasClass('ng-valid-required')).to.be.true |
| 124 | + }) |
| 125 | + |
| 126 | + it('works with Windows file type application/x-zip-compressed', function(){ |
| 127 | + fileList[0].type = 'application/x-zip-compressed' |
| 128 | + |
| 129 | + $(fileInput).triggerHandler({ |
| 130 | + type: 'change', |
| 131 | + target: { files: fileList } |
| 132 | + }) |
| 133 | + |
| 134 | + $timeout.flush() |
| 135 | + |
| 136 | + expect(mockSetFileReference).called |
| 137 | + expect($(fileInput).hasClass('ng-valid-filesize')).to.be.true |
| 138 | + expect($(fileInput).hasClass('ng-valid-required')).to.be.true |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + describe('with a file type that\'s not in the list of fileTypes given to the directive', function() { |
| 143 | + beforeEach(function() { |
| 144 | + fileList[0].type = 'image/png' |
| 145 | + |
| 146 | + $(fileInput).triggerHandler({ |
| 147 | + type: 'change', |
| 148 | + target: { files: fileList } |
| 149 | + }) |
| 150 | + |
| 151 | + $timeout.flush() |
| 152 | + }) |
| 153 | + |
| 154 | + it('does not call setFileReference', function() { |
| 155 | + expect(mockSetFileReference).not.calledOnce |
| 156 | + }) |
| 157 | + |
| 158 | + it('has ng-touched and ng-invalid-required classes', function() { |
| 159 | + expect($(fileInput).hasClass('ng-invalid-required')).to.be.true |
| 160 | + expect($(fileInput).hasClass('ng-touched')).to.be.true |
| 161 | + }) |
| 162 | + }) |
| 163 | + |
| 164 | + describe('with a file extension that is not in the list of fileTypes given to the directive', function() { |
| 165 | + beforeEach(function() { |
| 166 | + fileList[0].name = 'submission.zip.jpg' |
| 167 | + |
| 168 | + $(fileInput).triggerHandler({ |
| 169 | + type: 'change', |
| 170 | + target: { files: fileList } |
| 171 | + }) |
| 172 | + |
| 173 | + $timeout.flush() |
| 174 | + }) |
| 175 | + |
| 176 | + it('does not call setFileReference', function() { |
| 177 | + expect(mockSetFileReference).not.calledOnce |
| 178 | + }) |
| 179 | + |
| 180 | + it('has ng-touched and ng-invalid-required classes', function() { |
| 181 | + expect($(fileInput).hasClass('ng-invalid-required')).to.be.true |
| 182 | + expect($(fileInput).hasClass('ng-touched')).to.be.true |
| 183 | + }) |
| 184 | + }) |
| 185 | + |
| 186 | + describe('with a file that\'s greater than 500MB', function() { |
| 187 | + beforeEach(function() { |
| 188 | + fileList[0].size = 524288001 |
| 189 | + |
| 190 | + $(fileInput).triggerHandler({ |
| 191 | + type: 'change', |
| 192 | + target: { files: fileList } |
| 193 | + }) |
| 194 | + |
| 195 | + $timeout.flush() |
| 196 | + }) |
| 197 | + |
| 198 | + it('does not call setFileReference', function() { |
| 199 | + expect(mockSetFileReference).not.calledOnce |
| 200 | + }) |
| 201 | + |
| 202 | + it('has ng-touched and ng-invalid-filesize classes', function() { |
| 203 | + expect($(fileInput).hasClass('ng-invalid-filesize')).to.be.true |
| 204 | + expect($(fileInput).hasClass('ng-touched')).to.be.true |
| 205 | + }) |
| 206 | + }) |
| 207 | + }) |
| 208 | +}) |
0 commit comments