lundi 6 juillet 2015

How to write unit test for file upload in directive with anuglarjs

I have a directive as following, the function of it is to read a file:

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

Html to load the file is as following:

<input id="fileInput" name="fileInput" type="file" accept='.csv' on-read-file="showContent($fileContent)" />

I try to write unit test to see whether it can parse the content of file succefully, I wrote a little bit and then get stuck. Any one can help?

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 = '<div></div>';
        el = $compile(files)(scope);
        $rootScope.$digest();
        scope = el.isolateScope();
    }

    it('should load', function () {
        loadDirective();
        expect(el.length).to.equal(1);
    });

});

Aucun commentaire:

Enregistrer un commentaire