lundi 6 juillet 2015

How to write unit test for read file directive

My directive is as following:

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]);
        });
    }
    };
});

I want to write a unit test to test whether it loading and read file in a right way. I do not know how to write it in a right way. The following code give me an error: ' SyntaxError: Parse error'

describe.only('On Read File Directive Test', function () {
    var el, scope, $rootScope, $compile, $parse;

    beforeEach(module('somemoudle'));

    beforeEach(function () {
        inject([
            '$compile',
            '$rootScope'
            function (_$compile, _$rootScope, _$parse) {
                $compile = _$compile;
                $rootScope = _$rootScope;
                scope = $rootScope.$new();
                $parse = _$parse;
            }
        ]);
    });

    function loadDirective() {
        var html = '<input onreadfile />';
        el = $compile(html)(scope);
        $rootScope.$digest();
        scope = el.isolateScope();
    }

    it('should load', function () {
        loadDirective();
    });

});

Aucun commentaire:

Enregistrer un commentaire