javascript - jasmine test case file error while reading file -


  • i trying write unit test case code.
  • i getting error @ line myreader.readasdataurl(file);
  • i debugged code if see value of file in readasdataurl method.
  • i see below value file {name: "untitled-3.txt", lastmodified: 1503251185773, lastmodifieddate: sun aug 20 2017 13:46:25 gmt-0400 (eastern daylight time), webkitrelativepath: "", size: 35693, …}
  • but not sure how pass in unit test case method.
  • so getting below error.. typeerror: failed execute 'readasdataurl' on 'filereader': parameter 1 not of type 'blob'.

  • can guys tell me how fix it.

  • providing code , test case below.
    testcase--->     it('read financial player', (done) => {                      console.log("read------????");              let callfirsttime : boolean = true;             let url=              spyon(dashboardcontractcomponent.financialplayers.nbcuservice,'getresponse').and.                 callfake(() => {                          if(callfirsttime){                             callfirsttime = false; // invoked detectchanges()                              return observable.of([{                                 "playerid": "100",                                 "playername": "http://localhost:3000/assets/js/actualairings.json",                                 "playertype": "title",                                 "playerdata": "ywzjyxjlz2vyamh2dmfyzwdoynzi",                                 "notes": "",                                 "notesid": "100",                                 "elfdocid": "100",                                 "url": "http://localhost:3000/upload",                                 "date": "06/27/2017",                                 "addedbyname": "kamal",                                 "userid": "206509786",                                 "operationtype": "create"                               }, {                                 "playerid": "101",                                 "playername": "uploadtest4.txt",                                 "playertype": "title",                                 "playerdata": "manish",                                 "notes": "",                                 "notesid": "101",                                 "elfdocid": "101",                                 "url": "http://localhost:3000/upload",                                 "date": "06/27/2017",                                 "addedbyname": "kamal",                                 "userid": "206509786",                                 "operationtype": "create"                               }]                         );                         }                  });                   //const args = ['p0', 'p1', 'p2'];                 //call_me.apply(this, args);                   //var fruits = ['apple', 'banana'];                  //var fruits1 = {"test": "1"};                    //console.log("dashboardcontractcomponent.financialplayers---->" +                    //      json.stringify(dashboardcontractcomponent.financialplayers.blanket.apply(this, args)));                   spyon(dashboardcontractcomponent.financialplayers.gridkendo,'enablesaveplayer').and.returnvalue(null);                  //dashboardcontractcomponent.financialplayers.fileselect = "text.txt";                  //dashboardcontractcomponent.financialplayers.blanket(fruits[0]);                 //dashboardcontractcomponent.financialplayers.blanket(fruits1.test);                 dashboardcontractcomponent.financialplayers.blanket({                     files: "untitled-2.txt"                 });                  var myreader: filereader = new filereader();                 var file;                 myreader.readasdataurl(file);                  //dashboardcontractcomponent.financialplayers.blanket( {0: file, length: 1});                  //console.log("dashboardcontractcomponent.financialplayers._datasource._data.length---->" + dashboardcontractcomponent.financialplayers._datasource._data.length);                  fixture.whenstable().then(() => {                done();                //expect(dashboardcontractcomponent.financialplayers._datasource._data.length).toequal(3);             });          });   code----------->         blanket(inputvalue: any): void {         var = this;         var file: file = inputvalue.files[0];          var myreader: filereader = new filereader();         myreader.onloadend = (e) => {           this.encodebase64 = myreader.result;           that.fileselect = $("#attachplayerbrowsebtn").val().replace(/^.*\\/, "");           if (that.fileselect == '') {             that.dragdrop = that.clearbtn;           } else {             that.dragdrop = "";             that.dragdrop = that.fileselect;           }         }         $('.addelfplayerform').show();         if (inputvalue.files.length > 0) {           var filesize = 0;            filesize = inputvalue.files[0].size / 1048576; //size in mb             if (filesize > 5) {             alert("the player size exceeds max limit of 5 mb");            }            myreader.readasdataurl(file);         }     } 

within browser file object represent local file , contains file's information. when stub file object, has information not actual reference local file.

i suggest stubbing readasdataurl , check if receives file object in tests. , returning pre-defined dataurl tests.


edit

i in tests,

spyon(window, 'filereader').andreturn({     readasdataurl(){}     onloadend(){} }); 

you can replace readasdataurl() , onloadend() jasmine stubs , assert if called proper arguments.


edit

const filereaderspy = jasmine.createspyobj('filereader', ['readasdataurl', 'onloadend']);  spyon(window, 'filereader').andreturn(filereaderspy);  expect(filereaderspy.readasdataurl). tohavebeencalledwith(/* file object */); expect(filereaderspy.onloadend). tohavebeencalledwith(/* callback function */); 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -