如何测试文件上传指令?

时间:2014-10-06 15:37:30

标签: angularjs testing file-upload directive

我该如何测试?

element.bind('change', function(evt) {
  var files = evt.target.files;

  if (!attrs.previewContainer) {
    attrs.previewContainer = "preview";
    var div = document.createElement("div");
    var li = document.createElement("li");
    li.appendChild(div);
    li.id = attrs.previewContainer;
    element.append(div);
  }

  if (typeof files !== "undefined") {
    if (attrs.multiple) {
      for (var i = 0, l = files.length; i < l; i++) {
        loadMetadata(files[i], attrs).then(function(result) {
          result.url = URL.createObjectURL(files[i]);
          scope.metadata.push(result);
        });
      }
    } else {
      loadMetadata(files[0], attrs).then(function(result) {
        result.url = URL.createObjectURL(files[0]);
        scope.metadata = result;
      });
    }
  }
});

文件HTML:

<input name='uploadVideo' type="file" metadata="uploadVideo" 
       preview-container="videoPreview" data-ng-model="uploadVideo.file">

我想在自定义指令上创建一个测试文件,用于处理上传视频和查看预览。所以我想知道如何创建一个以角度收听的事件: 或类似的

it('Test input file', inject(function() {

  fakeFile = {
    name: '/home/developer/mcd2/public/modules/uploads/store/videos/542e73cf054fbc3a1630d216/bear_movie.mp4'
  };

  var fileInput, evt;
  fileInput = element.find('input').trigger('input');
  evt = $.Event('change');

  evt.target = {
    files: [fakeFile]
  };

  angular.element(fileInput).triggerHandler(evt);
  fileInput.trigger(evt);
  expect(form.$modelValue.name).toBe(fakeFile.name);
  expect(form.$valid).toBeTruthy();
}));

0 个答案:

没有答案