import 'core-js' ; import { Observable } from 'rxjs/Observable' ; import { Cordova , CordovaCheck , CordovaInstance , CordovaProperty , InstanceProperty , Plugin } from './decorators' ; import { IonicNativePlugin } from './ionic-native-plugin' ; import { ERR_CORDOVA_NOT_AVAILABLE , ERR_PLUGIN_NOT_INSTALLED } from './plugin' ; declare const window ; class TestObject { constructor ( public _objectInstance ) { } @ InstanceProperty name : string ; @ CordovaInstance ( { sync : true } ) pingSync ( ) { return ; } @ CordovaInstance ( ) ping ( ) { return ; } } @ Plugin ( { pluginName : 'TestPlugin' , pluginRef : 'testPlugin' , repo : '' , plugin : 'cordova-plugin-my-plugin' , platforms : [ 'Android' , 'iOS' ] } ) class TestPlugin extends IonicNativePlugin { @ CordovaProperty name : string ; @ Cordova ( { sync : true } ) pingSync ( ) { return ; } @ Cordova ( ) ping ( ) { return ; } @ CordovaCheck ( ) customPing ( ) { return Promise . resolve ( 'pong' ) ; } create ( ) { return new TestObject ( TestPlugin . getPlugin ( ) . create ( ) ) ; } @ Cordova ( { destruct : true } ) destructPromise ( ) { return ; } @ Cordova ( { destruct : true , observable : true } ) destructObservable ( ) { return ; } } function definePlugin ( ) { ( window as any ) . testPlugin = { name : 'John Smith' , ping : ( success , error ) => success ( 'pong' ) , pingSync : ( ) => 'pong' , create : function TestObject ( ) { this . pingSync = ( ) => 'pong' ; this . ping = ( success , error ) => success ( 'pong' ) ; this . name = 'John Smith' ; return this ; } , destructPromise : ( success ) => success ( 'hello' , 'world' ) , destructObservable : ( success ) => success ( 'hello' , 'world' ) } ; } describe ( 'Regular Decorators' , ( ) => { let plugin ; beforeEach ( ( ) => { plugin = new TestPlugin ( ) ; definePlugin ( ) ; } ) ; describe ( 'Plugin' , ( ) => { it ( 'should set pluginName' , ( ) => { expect ( TestPlugin . getPluginName ( ) ) . toEqual ( 'TestPlugin' ) ; } ) ; it ( 'should set pluginRef' , ( ) => { expect ( TestPlugin . getPluginRef ( ) ) . toEqual ( 'testPlugin' ) ; } ) ; it ( 'should return original plugin object' , ( ) => { expect ( TestPlugin . getPlugin ( ) ) . toEqual ( window . testPlugin ) ; } ) ; it ( 'should return supported platforms' , ( ) => { expect ( TestPlugin . getSupportedPlatforms ( ) ) . toEqual ( [ 'Android' , 'iOS' ] ) ; } ) ; } ) ; describe ( 'Cordova' , ( ) => { it ( 'should do a sync function' , ( ) => { expect ( plugin . pingSync ( ) ) . toEqual ( 'pong' ) ; } ) ; it ( 'should do an async function' , ( done ) => { plugin . ping ( ) . then ( res => { expect ( res ) . toEqual ( 'pong' ) ; done ( ) ; } ) . catch ( e => { expect ( e ) . toBeUndefined ( ) ; done ( 'Method should have resolved' ) ; } ) ; } ) ; it ( 'should throw plugin_not_installed error' , ( done ) => { delete window . testPlugin ; window . cordova = true ; expect ( < any > plugin . pingSync ( ) ) . toEqual ( ERR_PLUGIN_NOT_INSTALLED ) ; plugin . ping ( ) . catch ( e => { expect ( e ) . toEqual ( ERR_PLUGIN_NOT_INSTALLED . error ) ; delete window . cordova ; done ( ) ; } ) ; } ) ; it ( 'should throw cordova_not_available error' , ( done ) => { delete window . testPlugin ; expect ( < any > plugin . pingSync ( ) ) . toEqual ( ERR_CORDOVA_NOT_AVAILABLE ) ; plugin . ping ( ) . catch ( e => { expect ( e ) . toEqual ( ERR_CORDOVA_NOT_AVAILABLE . error ) ; done ( ) ; } ) ; } ) ; } ) ; describe ( 'CordovaProperty' , ( ) => { it ( 'should return property value' , ( ) => { expect ( plugin . name ) . toEqual ( 'John Smith' ) ; } ) ; it ( 'should set property value' , ( ) => { plugin . name = 'value2' ; expect ( plugin . name ) . toEqual ( 'value2' ) ; } ) ; } ) ; describe ( 'CordovaCheck' , ( ) => { it ( 'should run the method when plugin exists' , done => { plugin . customPing ( ) . then ( res => { expect ( res ) . toEqual ( 'pong' ) ; done ( ) ; } ) ; } ) ; it ( 'shouldnt run the method when plugin doesnt exist' , done => { delete window . testPlugin ; window . cordova = true ; plugin . customPing ( ) . catch ( e => { expect ( e ) . toEqual ( ERR_PLUGIN_NOT_INSTALLED . error ) ; done ( ) ; } ) ; } ) ; } ) ; describe ( 'CordovaOptions' , ( ) => { describe ( 'destruct' , ( ) => { it ( 'should destruct values returned by a Promise' , done => { plugin . destructPromise ( ) . then ( ( args ) => { expect ( args ) . toEqual ( [ 'hello' , 'world' ] ) ; done ( ) ; } ) ; } ) ; it ( 'should destruct values returned by an Observable' , done => { plugin . destructObservable ( ) . subscribe ( ( args ) => { expect ( args ) . toEqual ( [ 'hello' , 'world' ] ) ; done ( ) ; } ) ; } ) ; } ) ; } ) ; } ) ; describe ( 'Instance Decorators' , ( ) => { let instance , plugin ; beforeEach ( ( ) => { definePlugin ( ) ; plugin = new TestPlugin ( ) ; instance = plugin . create ( ) ; } ) ; describe ( 'Instance plugin' , ( ) => { } ) ; describe ( 'CordovaInstance' , ( ) => { it ( 'should call instance async method' , done => { instance . ping ( ) . then ( r => { expect ( r ) . toEqual ( 'pong' ) ; done ( ) ; } ) ; } ) ; it ( 'should call instance sync method' , ( ) => { expect ( instance . pingSync ( ) ) . toEqual ( 'pong' ) ; } ) ; it ( 'shouldnt call instance method when _objectInstance is undefined' , ( ) => { delete instance . _objectInstance ; instance . ping ( ) . then ( r => { expect ( r ) . toBeUndefined ( ) ; } ) . catch ( e => { expect ( e ) . toBeUndefined ( ) ; } ) ; } ) ; } ) ; describe ( 'InstanceProperty' , ( ) => { it ( 'should return property value' , ( ) => { expect ( instance . name ) . toEqual ( 'John Smith' ) ; } ) ; it ( 'should set property value' , ( ) => { instance . name = 'John Cena' ; expect ( instance . name ) . toEqual ( 'John Cena' ) ; } ) ; } ) ; } ) ;