Skip to content

Commit

Permalink
test(): add initial test suite (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlewis92 authored and ihadeed committed Sep 7, 2016
1 parent d5513db commit 1facde3
Show file tree
Hide file tree
Showing 11 changed files with 735 additions and 8 deletions.
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies:

test:
override:
- echo "No tests are written at the moment. But we will attempt to build the library with the latest changes."
- npm test
- npm run build

deployment:
Expand Down
60 changes: 60 additions & 0 deletions karma.conf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const WATCH = process.argv.indexOf('--watch') > -1;

module.exports = config => {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'browserify'],

// list of files / patterns to load in the browser
files: [
'test/**/*.spec.ts'
],

// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/**/*.spec.ts': ['browserify']
},

browserify: {
plugin: [ 'tsify' ],
extensions: ['.js', '.ts']
},

phantomjsLauncher: {
// Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
exitOnResourceError: true
},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['dots'],

// web server port
port: 9876,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: WATCH,

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: !WATCH
});
};
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,35 @@
"decamelize": "^1.2.0",
"dgeni": "^0.4.2",
"dgeni-packages": "^0.10.18",
"es6-shim": "~0.35.1",
"glob": "^6.0.4",
"gulp": "^3.9.1",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.4",
"gulp-tslint": "^5.0.0",
"gulp-uglify": "^1.5.4",
"ionic-gulp-tslint": "^1.0.0",
"jasmine-core": "~2.5.0",
"karma": "~1.2.0",
"karma-browserify": "~5.1.0",
"karma-jasmine": "~1.0.2",
"karma-phantomjs-launcher": "~1.0.2",
"lodash": "3.10.1",
"minimist": "^1.1.3",
"mkdirp": "^0.5.1",
"node-html-encoder": "0.0.2",
"q": "1.4.1",
"semver": "^5.0.1",
"tsify": "~1.0.4",
"tslint": "^3.8.1",
"tslint-ionic-rules": "0.0.5",
"typescript": "^1.8.10"
"typescript": "^1.8.10",
"watchify": "~3.7.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "karma start",
"test:watch": "npm test -- --watch",
"start": "npm run test:watch",
"lint": "./node_modules/.bin/gulp lint",
"watch": "./node_modules/.bin/tsc -w",
"build": "npm run lint && npm run build:js && npm run build:bundle && npm run build:minify",
Expand Down
5 changes: 3 additions & 2 deletions src/ng1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ declare var window;
*/
export function initAngular1(plugins) {
if (window.angular) {
window.angular.module('ionic.native', []);

const ngModule = window.angular.module('ionic.native', []);

for (var name in plugins) {
let serviceName = '$cordova' + name;
let cls = plugins[name];

(function(serviceName, cls, name) {
window.angular.module('ionic.native').service(serviceName, [function() {
ngModule.service(serviceName, [function() {
var funcs = window.angular.copy(cls);
funcs.prototype['name'] = name;
return funcs;
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { Observable } from 'rxjs/Observable';

declare var window;
declare var Promise;
declare var $q;


/**
* @private
Expand Down
141 changes: 141 additions & 0 deletions test/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/// <reference path="./../typings/index.d.ts" />

import 'es6-shim';
import {Plugin, Cordova} from './../src/plugins/plugin';

declare const window: any;
window.plugins = {
test: {}
};

const testPluginMeta = {
plugin: 'cordova-plugin-test',
pluginRef: 'plugins.test',
repo: 'https://github.com/apache/cordova-plugin-test',
platforms: ['Android', 'iOS']
};

describe('plugin', () => {

it('sync method', () => {

window.plugins.test.syncMethod = () => {
return 'syncResult';
};

@Plugin(testPluginMeta)
class Test {

@Cordova({
sync: true
})
static syncMethod(arg: any): boolean { return; };

}

const spy = spyOn(window.plugins.test, 'syncMethod').and.callThrough();
const result = Test.syncMethod('foo');
expect(result).toEqual('syncResult');
expect(spy).toHaveBeenCalledWith('foo', undefined, undefined);

});

it('normal order callback', done => {

window.plugins.test.normalOrderCallback = (args, success, error) => {
success('normalOrderCallback');
};

@Plugin(testPluginMeta)
class Test {
@Cordova()
static normalOrderCallback(args: any): Promise<any> { return; }
}

const spy = spyOn(window.plugins.test, 'normalOrderCallback').and.callThrough();
Test.normalOrderCallback('foo').then(result => {
expect(result).toEqual('normalOrderCallback');
done();
});
expect(spy.calls.mostRecent().args[0]).toEqual('foo');

});

it('reverse order callback', done => {

window.plugins.test.reverseOrderCallback = (success, error, args) => {
success('reverseOrderCallback');
};

@Plugin(testPluginMeta)
class Test {

@Cordova({
callbackOrder: 'reverse'
})
static reverseOrderCallback(args: any): Promise<any> { return; }

}

const spy = spyOn(window.plugins.test, 'reverseOrderCallback').and.callThrough();
Test.reverseOrderCallback('foo').then(result => {
expect(result).toEqual('reverseOrderCallback');
done();
});
expect(spy.calls.mostRecent().args[2]).toEqual('foo');

});

it('node style callback', done => {

window.plugins.test.nodeStyleCallback = (args, done) => {
done(null, 'nodeStyleCallback');
};

@Plugin(testPluginMeta)
class Test {

@Cordova({
callbackStyle: 'node'
})
static nodeStyleCallback(args: any): Promise<any> { return; }

}

const spy = spyOn(window.plugins.test, 'nodeStyleCallback').and.callThrough();
Test.nodeStyleCallback('foo').then(result => {
expect(result).toEqual('nodeStyleCallback');
done();
});
expect(spy.calls.mostRecent().args[0]).toEqual('foo');

});

it('object style callback', done => {

window.plugins.test.objectStyleCallback = (args, {success}) => {
success('objectStyleCallback');
};

@Plugin(testPluginMeta)
class Test {

@Cordova({
callbackStyle: 'object',
successName: 'success',
errorName: 'error'
})
static objectStyleCallback(args: any): Promise<any> { return; }

}

const spy = spyOn(window.plugins.test, 'objectStyleCallback').and.callThrough();
Test.objectStyleCallback('foo').then(result => {
expect(result).toEqual('objectStyleCallback');
done();
});
expect(spy.calls.mostRecent().args[0]).toEqual('foo');

});

});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"sourceMap": true,
"declaration": true,
"experimentalDecorators": true,
"outDir": "dist"
"outDir": "dist",
"moduleResolution": "node"
},
"files": [
"typings/es6-shim/es6-shim.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions typings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"globalDevDependencies": {
"jasmine": "registry:dt/jasmine#2.2.0+20160621224255"
}
}
Loading

0 comments on commit 1facde3

Please sign in to comment.