test(): update tests

This commit is contained in:
Ibby Hadeed 2017-05-16 05:31:35 -04:00
parent 02cc615c21
commit 4f6ec0cfcd
2 changed files with 201 additions and 32 deletions

View File

@ -1,44 +1,91 @@
import { Plugin, Cordova, CordovaProperty } from './decorators';
import 'core-js';
import { Plugin, Cordova, CordovaProperty, CordovaCheck, CordovaInstance, InstanceProperty } from './decorators';
import { IonicNativePlugin } from './ionic-native-plugin';
import { ERR_CORDOVA_NOT_AVAILABLE, ERR_PLUGIN_NOT_INSTALLED } from './plugin';
declare const window: any;
window['myPlugin'] = {
class TestObject {
hello: () => 'world',
key: 'value'
constructor(public _objectInstance: any) {}
};
@InstanceProperty
name: string;
describe('Decorators', () => {
@CordovaInstance({ sync: true })
pingSync(): string { return; }
@Plugin({
pluginName: 'MyPlugin',
pluginRef: 'myPlugin',
@CordovaInstance()
ping(): Promise<any> { return; }
}
@Plugin({
pluginName: 'TestPlugin',
pluginRef: 'testPlugin',
repo: '',
plugin: 'cordova-plugin-my-plugin'
})
class MyPlugin extends IonicNativePlugin {
})
class TestPlugin extends IonicNativePlugin {
@CordovaProperty
key: string;
name: string;
@Cordova({ sync: true })
hello(): string { return; }
pingSync(): string { return; }
@Cordova()
ping(): Promise<string> { return; }
@CordovaCheck()
customPing(): Promise<string> {
return Promise.resolve('pong');
}
let instance: MyPlugin;
create(): TestObject {
return new TestObject(TestPlugin.getPlugin().create());
}
}
function definePlugin() {
(window as any).testPlugin = {
name: 'John Smith',
ping: (success: Function, error: Function) => success('pong'),
pingSync: () => 'pong',
create: function TestObject() {
this.pingSync = () => 'pong';
this.ping = (success: Function, error: Function) => success('pong');
this.name = 'John Smith';
return this;
}
};
}
describe('Regular Decorators', () => {
let plugin: TestPlugin;
beforeEach(() => {
instance = new MyPlugin();
plugin = new TestPlugin();
definePlugin();
});
describe('Plugin', () => {
it('should set class properties', () => {
expect(MyPlugin.getPluginName()).toEqual('MyPlugin');
expect(MyPlugin.getPluginRef()).toEqual('myPlugin');
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);
});
});
@ -46,7 +93,49 @@ describe('Decorators', () => {
describe('Cordova', () => {
it('should do a sync function', () => {
expect(instance.hello()).toEqual('world');
expect(plugin.pingSync()).toEqual('pong');
});
it('should do an async function', (done: Function) => {
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: Function) => {
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: Function) => {
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();
});
});
});
@ -54,14 +143,95 @@ describe('Decorators', () => {
describe('CordovaProperty', () => {
it('should return property value', () => {
expect(instance.key).toEqual('value');
expect(plugin.name).toEqual('John Smith');
});
it('should set property value', () => {
instance.key = 'value2';
expect(instance.key).toEqual('value2');
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('Instance Decorators', () => {
let instance: TestObject,
plugin: TestPlugin;
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');
});
});
});

View File

@ -10,6 +10,9 @@ checkReady();
// declare const window;
// declare var Promise;
export const ERR_CORDOVA_NOT_AVAILABLE = { error: 'cordova_not_available' };
export const ERR_PLUGIN_NOT_INSTALLED = { error: 'plugin_not_installed' };
/**
* Checks if plugin/cordova is available
@ -35,15 +38,11 @@ export function checkAvailability(plugin: any, methodName?: string, pluginName?:
if (!pluginInstance || (!!methodName && typeof pluginInstance[methodName] === 'undefined')) {
if (!window.cordova) {
cordovaWarn(pluginName, methodName);
return {
error: 'cordova_not_available'
};
return ERR_CORDOVA_NOT_AVAILABLE;
}
pluginWarn(pluginName, pluginPackage, methodName);
return {
error: 'plugin_not_installed'
};
return ERR_PLUGIN_NOT_INSTALLED;
}
return true;