import { HttpClient } from '@angular/common/http' ; import { TestBed } from '@angular/core/testing' ; import { Observable , of , throwError } from 'rxjs' ; import * as fs from 'file-saver' ; import { LazyService } from '@delon/util/other' ; import { NzSafeAny } from 'ng-zorro-antd/core/types' ; import { ZipModule } from './zip.module' ; import { ZipService } from './zip.service' ; let isErrorRequest = false ; let isClassZIP = false ; let isErrorGenZip = false ; class MockLazyService { load ( ) { ( window as NzSafeAny ) . JSZip = isClassZIP ? class JSZip { file ( ) { } generateAsync ( ) { return isErrorGenZip ? Promise . reject ( '' ) : Promise . resolve ( ) ; } } : DEFAULTMOCKJSZIP ; return Promise . resolve ( ) ; } } const DEFAULTMOCKJSZIP = { loadAsync : ( ) => { return Promise . resolve ( ) ; } , write : ( ) => { } } ; class MockHttpClient { request ( ) { return isErrorRequest ? throwError ( ( ) => null ) : of ( null ) ; } } describe ( 'abc: zip' , ( ) => { let srv ; function genModule ( ) { TestBed . configureTestingModule ( { imports : [ ZipModule ] , providers : [ { provide : HttpClient , useClass : MockHttpClient } , { provide : LazyService , useClass : MockLazyService } ] } ) ; srv = TestBed . inject < ZipService > ( ZipService ) ; } beforeEach ( ( ) => { isErrorRequest = false ; isClassZIP = false ; isErrorGenZip = false ; } ) ; describe ( '#read' , ( ) => { it ( 'should be load zip via url' , ( done ) => { genModule ( ) ; srv . read ( '/1.zip' ) . then ( ( ) => { expect ( true ) . toBe ( true ) ; done ( ) ; } , ( ) => { expect ( false ) . toBe ( true ) ; done ( ) ; } ) ; } ) ; it ( 'should be reject when request error via url' , ( done ) => { isErrorRequest = true ; genModule ( ) ; srv . read ( '/1.zip' ) . then ( ( ) => { expect ( false ) . toBe ( true ) ; done ( ) ; } , ( ) => { expect ( true ) . toBe ( true ) ; done ( ) ; } ) ; } ) ; it ( 'should be load zip via file object' , ( done ) => { genModule ( ) ; srv . read ( new File ( [ ] , '1.zip' ) ) . then ( ( ) => { expect ( true ) . toBe ( true ) ; done ( ) ; } , ( ) => { expect ( false ) . toBe ( true ) ; done ( ) ; } ) ; } ) ; } ) ; it ( '#create' , ( done ) => { isClassZIP = true ; genModule ( ) ; srv . create ( ) . then ( ( ) => { expect ( true ) . toBe ( true ) ; done ( ) ; } , ( ) => { expect ( false ) . toBe ( true ) ; done ( ) ; } ) ; } ) ; describe ( '#pushUrl' , ( ) => { let zip ; beforeEach ( ( done ) => { isClassZIP = true ; genModule ( ) ; srv . create ( ) . then ( res => { zip = res ; done ( ) ; } ) ; } ) ; it ( 'should be save zip file' , ( done ) => { srv . pushUrl ( zip , '1.zip' , '1.zip' ) . then ( ( ) => { expect ( true ) . toBe ( true ) ; done ( ) ; } , ( ) => { expect ( false ) . toBe ( true ) ; done ( ) ; } ) ; } ) ; it ( 'should be reject when bad request' , ( done ) => { isErrorRequest = true ; srv . pushUrl ( zip , '1.zip' , '1.zip' ) . then ( ( ) => { expect ( false ) . toBe ( true ) ; done ( ) ; } , ( ) => { expect ( true ) . toBe ( true ) ; done ( ) ; } ) ; } ) ; } ) ; describe ( '#save' , ( ) => { let zip ; beforeEach ( ( done ) => { isClassZIP = true ; genModule ( ) ; srv . create ( ) . then ( res => { zip = res ; done ( ) ; } ) ; } ) ; it ( 'should be save zip file' , ( done ) => { spyOn ( fs , 'saveAs' ) ; srv . save ( zip , { filename : '123.zip' } ) . then ( ( ) => { expect ( fs . saveAs ) . toHaveBeenCalled ( ) ; expect ( true ) . toBe ( true ) ; done ( ) ; } , ( ) => { expect ( false ) . toBe ( true ) ; done ( ) ; } ) ; } ) ; it ( 'should be call callback' , ( done ) => { spyOn ( fs , 'saveAs' ) ; let count = 0 ; srv . save ( zip , { callback : ( ) => ++ count } ) . then ( ( ) => { expect ( count ) . toBe ( 1 ) ; expect ( fs . saveAs ) . toHaveBeenCalled ( ) ; done ( ) ; } , ( ) => { expect ( false ) . toBe ( true ) ; done ( ) ; } ) ; } ) ; it ( 'should be reject when generateAsync return error' , ( done ) => { isErrorGenZip = true ; spyOn ( fs , 'saveAs' ) ; srv . save ( zip ) . then ( ( ) => { expect ( false ) . toBe ( true ) ; done ( ) ; } , ( ) => { expect ( fs . saveAs ) . not . toHaveBeenCalled ( ) ; expect ( true ) . toBe ( true ) ; done ( ) ; } ) ; } ) ; it ( 'should be throw error when invalid zip' , ( ) => { zip = null ; expect ( ( ) => { srv . save ( zip ) ; } ) . toThrowError ( ) ; } ) ; } ) ; } ) ;