Reputation: 2422
Basically i have to test this below function, where i'm reading from text file
$window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dir) {
var path = 'somefile.txt';
dir.getFile(path, { create: true }, function (file) {
file.file(function (file) {
var reader = new FileReader();
reader.onloadend = function () {
resolve(this.result);
}
reader.readAsText(file);
});
}, error);
}, error);
i'm stuck in writing the unit test cases for reading file
describe('get data from file', function () {
it('should read the files from the data', function () {
var syncFile = 'somefile.txt';
expect( ).toBe( );
});
});
How to write unit test for filereader for reading the file? PS : i'm new to unit testing using karma
Upvotes: 5
Views: 1770
Reputation: 1705
You should not use FileReader directly. Change that line to
var reader = new $window.FileReader();
In your test mock the $window and return a custom FileReader object. Then do the tests on that. Something like below.
describe('get data from file', function () {
var $window, fileReader;
beforeEach(function () {
inject(function (_$window_) {
$window = _$window_;
});
fileReader = function () {
return {};
};
spyOn($window, "FileReader").and.returnValue(fileReader);
});
it('should read the files from the data', function () {
var syncFile = 'somefile.txt';
expect($window.FileReader).toHaveBeenCalled();
});
});
Upvotes: 9