title: How to write unit test for file upload in directive with angularjs
body: <p>I have a directive as following, the function of it is to read a file:</p>
  
  <pre><code>myapp.directive('onReadFile', function ($parse) {
      return {
          restrict: 'A',
          scope: false,
          link: function(scope, element, attrs) {
          var fn = $parse(attrs.onReadFile);
  
          element.on('change', function(onChangeEvent) {
              var reader = new FileReader();
  
              reader.onload = function(onLoadEvent) {
                  scope.$apply(function() {
                      fn(scope, {$fileContent:onLoadEvent.target.result});
                  });
              };
  
              reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]);
          });
      }
      };
  });
  </code></pre>
  
  <p>Html to load the file is as following:</p>
  
  <pre><code>&lt;input id="fileInput" name="fileInput" type="file" accept='.csv' on-read-file="showContent($fileContent)" /&gt;
  </code></pre>
  
  <p>I try to write unit test to see whether it can parse the content of file successfully, I wrote a little bit and then get stuck.</p>
  
  <pre><code>describe.only('On Read File Directive Test', function () {
      var el, scope, $rootScope, $compile, $parse;
  
      beforeEach(module('somemoudle'));
  
      beforeEach(function () {
          inject([
              '$compile',
              '$rootScope',
              '$parse',
              function (_$compile, _$rootScope, _$parse) {
                  $compile = _$compile;
                  $rootScope = _$rootScope;
                  scope = $rootScope.$new();
                  $parse = _$parse;
              }
          ]);
      });
  
      function loadDirective() {
          var files = '&lt;div&gt;&lt;/div&gt;';
          el = $compile(files)(scope);
          $rootScope.$digest();
          scope = el.isolateScope();
      }
  
      it('should load', function () {
          loadDirective();
          expect(el.length).to.equal(1);
      });
  
  });
  </code></pre>