unit testing - Angular 2.0.0 - Mock File Uploader -


i using ng2-file-upload. how mock fileuploader class in unit testing?

import { fileuploader } 'ng2-file-upload/ng2-file-upload';  @component({   selector: 'my-component',   template: '...',   providers: [ myservice ] })  export class mycomponent {   public uploader: fileuploader = new fileuploader({url: '/my-app/api/upload',               authtoken: 'token'}); constructor() {   this.uploader.oncompleteitem = (item:any, response: any, headers: any) => {     console.log('how test here');   } } 

i having hard time mocking in spec. please help.

i going reply comment, think following may answer original question on how mock fileuploader class taken unit test file file-drop.directive.spec.ts:

import { component } '@angular/core'; import { inject, componentfixture, testbed } '@angular/core/testing';  import { fileuploader } './file-uploader.class'; import { fileuploadmodule } './file-upload.module';  @component({   selector: 'container',   template: `<input type="file" ng2fileselect [uploader]="uploader" />` }) export class containercomponent {   public uploader:fileuploader = new fileuploader({url: 'localhost:3000'}); }  describe('directive: fileselectdirective', () => {    beforeeach(() => {     testbed.configuretestingmodule({       imports: [fileuploadmodule],       declarations: [containercomponent],       providers: [containercomponent]     });   });    it('should fine', inject([containercomponent], (fixture:componentfixture<containercomponent>) => {     expect(fixture).not.tobenull();   })); }); 

where import { fileuploader } './file-uploader.class'; how import fileuploader test, containercomponent imported test itself.

further this, have created dummy file test on component, still writing it!

it('should accept file upload', () => {     var modifieddate = new date();     var file = new file([3555], 'test-file.jpg', {lastmodified : modifieddate, type: 'image/jpeg'});     fileuploadcomponent.upload(file); }); 

to test if works, have metadata model expect populated upon file being selected. can therefore make 2 assertions, both upload input box have file , metadata object populated.

in case of ng2-file-upload, beleive file list populated allowing check test file has been imported that.

good luck!


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -