angular


Jasmine file input uploads an image, the image onload inside of FileReader is not fired in test


I'm new with Jasmine/Karma and I recently encountered a problem with input file. I want to test the width and height of the image I uploaded. If it doesn't fit the max or min values given, it will add class "ng-invalid-image-dimension" to input.
Now the code works but in test, the function image.onload is not tiggered. I don't know why and it stucked me for several days please help!
imageUpload.directive.js
(function() {
'use strict';
angular.module('components.imageUpload', [])
.directive('imageUpload', function(defaultImages, imageDimensions) {
return {
restrict: 'EA',
scope: {
name: '#',
maxWidth: '#?',
minWidth: '#',
maxHeight: '#?',
minHeight: '#'
},
require: '^form',
template: '<input type="file" id="upload" name="upload" ng-model="file" onchange="angular.element(this).scope().uploadFile(this.files,name)">',
link: function(scope, element, attrs, form) {
scope.maxWidth = scope.maxWidth || scope.minWidth;
scope.maxHeight = scope.maxHeight || scope.minHeight;
scope.form = form;
scope.uploadFile = function(files, imgName) {
scope.$apply(function() {
scope.imgName = imgName;
scope.file = files[0];
});
var r = new FileReader();
r.readAsDataURL(scope.file);
r.onload = function(file){
console.log("filereader onload triggered");
var validateW = false;
var validateH = false;
var image = new Image();
image.src = file.target.result;
image.onload = function() {
console.log("image onload triggered");
validateW = checkImageDimension(this.width, scope.minWidth, scope.maxWidth);
validateH = checkImageDimension(this.height, scope.minHeight, scope.maxHeight);
if(validateW && validateH) {
scope.$apply(function() {scope.form[scope.name].$setValidity("imageDimension", true);});
} else {
scope.$apply(function() { scope.form[scope.name].$setValidity("imageDimension", false);});
}
};
};
};
function checkImageDimension(val, min, max) {
return (val >= min) && (val <= max);
}
}
})
})();
imageUpload.directive.html
<div class="form-group">
<div class="col-sm-8">
<input type="file" id="{{name}}" name="{{name}}" ng-model="file" placeholder="{{name}}" onchange="angular.element(this).scope().uploadFile(this.files,name)">
</div>
<div class="col-sm-4">
<span class="help-inline" ng-show="form[name].$error.imageDimension">Image dimension is not valid !</span>
</div>
</div>
imageUpload_test.directive.js:
describe('components.imageUpload', function() {
var $compile, $rootScope;
beforeEach(angular.mock.module('components.imageUpload'));
beforeEach(module('js/components/imageUpload/imageUpload.directive.html'));
beforeEach(inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
describe('image-upload directive', function() {
var element, scope, input, files, inputScope;
beforeEach(function() {
scope = $rootScope.$new();
element = angular.element('<form name="testForm" novalidate><image-upload name="testImage" default="false" type="png" min-width="150" max-width="300" min-height="57"></image-upload></form>');
$compile(element)(scope);
scope.$digest();
input = element.find('input');
inputScope = angular.element(input).scope();
});
it('should have ng-invalid-image-dimension class when uploaded a wrong size file', function(done) {
var mockFileHex = ["iVBORw0KGgoAAAANSUhEUgAAADcAAAA3CAYAAACo29JGAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDpEQjE3N0M0MDMxMjA2ODExODIyQUI1MD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urxT3Tt9QD54sw1FSDiWEFZa+bs7b+t9bcaJh960HvCKsZmNmSNlP8fRLUQAiiUzb9u9ltB/ujWD+/Kpgv+6mdT0s9VuBLCxg7n2PXOPRKnrkfrAZzZ+ya0xSWgpDtiU7s1LufonsmToAsY3kmUqwvcrG4cIhi2pexilXEAPyKulcWOdintodmTnWShtMA2VWIlxejBiNgD0euksAQ3CzA7YtmDQqRrfJDicB0Q0HMYNyJKi3BNnwZINXbDJFbC8x5xFhj3Jqb52TuQfTcPLzQCmNxLHIdI0kwLs3pZDLL676lDiSDW143ZXUg2fHL8zDvjvNBVo+Z4HvMlRRupx8noG4z5/KvAAMAg2UEHy8DCrwAAAAASUVORK5CYII="];
var blob = new Blob(mockFileHex, {type:'image/png'});
blob.name = "logoTest";
files = {0: blob, target: {result: {}}};
inputScope.uploadFile(files, "logoTest");
expect(input.hasClass('ng-invalid-image-dimension')).toEqual(true);
});
})
})
As I mentioned above, "image onload triggered" neveer displayed.

Related Links

this.strategyService.getAllstrategys(…).subscribe is not a function
Angular 2 / Ionic 2 post request body
Angular 2 Http-Post is not working
Property 'subscribe' does not exist on type 'Promise'
Angular4- on change select box returns wrong value
AOT Compilation error in angular library
Angular, Ionic2 Configure #ViewChild every time it is initialized
Angular ViewContainerRef and its ViewRefs - dynamic versus declarative?
Setting one parameter to a certain value upon entering a different parameter - NgSwitch?
Angular 2 page transition while leaving page
Adding the Multiselect feature to an Angular4 dropdown?
Add lines of code depending on folder structure - Precompiler?
Angular2 Rxjs Observable catch method compilation error
Partial file loader for translations [on hold]
How to pass data from modal to component?
Angular code diference between source code and debugger code

Categories

HOME
angular
git
slickgrid
java-ee
google-maps-api-3
openerp
rx-java
composer-php
veins
web-crawler
keyboard-shortcuts
uiviewcontroller
google-cloud-endpoints
graphviz
vaadin-grid
rpres
javacard
codeception
postgis
slim
boost-intrusive
filenet-p8
tablesorter
jxmaps
subplot
atlassian-sourcetree
web-component
skybot
google-app-invites
workspace
indexof
ddos
pywavelets
induction
uisegmentedcontrol
swiftmailer
initializing
interactive
android-runonuithread
dataweave
marytts
fluent-nhibernate
interstitial
mapped-drive
qnx
clisp
canvas-lms
showdown
opscenter
sift
ucos
kubernetes-health-check
cpio
google-chartwrapper
itil
owner
tizen-sdb
grid-layout
wns
oracle-streams
ng-template
irs
sparklines
datamapper
max-heap
velocity.js
acl2
aloha-editor
apache-wink
xcode-organizer
sapb1
file-attributes
string-search
swift-protocols
automapper-3
teamsite
sine-wave
nsurlprotocol
iron
freetextbox
apiaxle
statamic
stackmob
tandem
panning
mysql-5.0
converters
customer-support
pasteboard
clean-language
incremental-linking

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile