Skip to content

Commit

Permalink
Chores(test): init
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Sharp committed Jan 16, 2015
1 parent 380e82c commit 98df613
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ results

npm-debug.log
node_modules
bower_components
build

.tern-project
.tern-project
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: node_js
node_js:
- "0.10"

before_script:
- npm install
- npm install -g gulp
- npm install -g bower
- bower install -F

script:
- gulp build
7 changes: 6 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
"license": "MIT",
"homepage": "https://github.com/kyjan/angular-sails",
"dependencies": {
"angular": ">=1.2.*"
"angular": ">=1.2.*",
"sails.io.js": "*"
},
"devDependencies":{
"angular-mocks": ">=1.2.*"
},
"ignore": [
"**/.*",
"gulpfile.js",
"package.json",
"node_modules",
"build"
"bower_components",
"app/bower_components",
"test",
Expand Down
55 changes: 55 additions & 0 deletions files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var pkg = require('./package.json');

var pkgFiles = {
angular: [
'bower_components/angular/angular.js'
],
karma: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'mock/socket-io.js',
'bower_components/sails.io.js/sails.io.js'
],
'karma-build': [
'@karma',
'build/' + pkg.name + '.js',
'@karma-tests'
],
'karma-min': [
'@karma',
'build/' + pkg.name + '.min.js',
'@karma-tests'
],
'karma-src': [
'@karma',
'@src',
'@karma-tests'
],
'karma-tests': [
'test/**/*.spec.js'
],
src: [
'src/**/*.js',
]
};

if (module.exports) {
module.exports.files = pkgFiles;
module.exports.mergeFilesFor = function() {
var files = [];

Array.prototype.slice.call(arguments, 0).forEach(function(filegroup) {
pkgFiles[filegroup].forEach(function(file) {
// replace @ref
var match = file.match(/^\@(.*)/);
if (match) {
files = files.concat(pkgFiles[match[1]]);
} else {
files.push(file);
}
});
});

return files;
};
}
40 changes: 33 additions & 7 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
'use strict';

var gulp = require('gulp'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
header = require('gulp-header'),
footer = require('gulp-footer');
footer = require('gulp-footer'),
ngAnnotate = require('gulp-ng-annotate'),
gulpKarma = require('gulp-karma'),
pkg = require('./package.json'),
files = require('./files');

var karmaTestConfig = gulpKarma({configFile: 'karma.conf.js', action: 'run'});


gulp.task('build-js', function () {
gulp.src(['src/ngSails.js', 'src/**/*.js'])
.pipe(concat('angular-sails.js'))
return gulp.src(files.mergeFilesFor('src'))
.pipe(ngAnnotate())
.pipe(concat(pkg.name+'.js'))
.pipe(header('(function (angular, io) {\n\'use strict\''))
.pipe(footer('}(angular, io));'))
.pipe(gulp.dest('./dist/'))
.pipe(concat('angular-sails.min.js'))
.pipe(gulp.dest('./build/'))
.pipe(concat(pkg.name+'.min.js'))
.pipe(uglify())
.pipe(gulp.dest('./dist/'));
.pipe(gulp.dest('./build/'));
});

gulp.task('dist-js', ['build-js'], function () {
return gulp.src('./build/*.js')
.pipe(gulp.dest('./dist/'));
});

gulp.task('test', function () {
return gulp.src(files.mergeFilesFor('karma-src')).pipe(karmaTestConfig);
});

gulp.task('test-build', ['build-js'], function () {
return gulp.src(files.mergeFilesFor('karma-build')).pipe(karmaTestConfig);
});

gulp.task('test-min', ['build-js'], function () {
return gulp.src(files.mergeFilesFor('karma-min')).pipe(karmaTestConfig);
});

gulp.task('watch', function () {
gulp.watch('src/**/*.js', ['build-js']);
return gulp.watch('src/**/*.js', ['build-js']);
});

gulp.task('default', ['build-js', 'watch']);
38 changes: 38 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Karma configuration
// Generated on Thu Jan 09 2014 13:59:16 GMT-0500 (EST)

module.exports = function (config) {
config.set({
basePath: './',

frameworks: ['mocha', 'chai-sinon'],

exclude: [],

reporters: ['progress'],

port: 9876,

runnerPort: 9100,

colors: true,

logLevel: config.LOG_INFO,

autoWatch: true,

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary (has to be installed with `npm install karma-chrome-canary-launcher`)
// - Firefox (has to be installed with `npm install karma-firefox-launcher`)
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['PhantomJS'],

captureTimeout: 60000,

singleRun: false
});
};
49 changes: 49 additions & 0 deletions mock/socket-io.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var io = {
connect: createMockSocketObject
};

function createMockSocketObject () {

var socket = {
on: function (ev, fn) {
(this._listeners[ev] = this._listeners[ev] || []).push(fn);
},
once: function (ev, fn) {
(this._listeners[ev] = this._listeners[ev] || []).push(fn);
fn._once = true;
},
emit: function (ev, data) {
if (this._listeners[ev]) {
var args = arguments;
this._listeners[ev].forEach(function (listener) {
if (listener._once) {
this.removeListener(ev, listener);
}
listener.apply(null, Array.prototype.slice.call(args, 1));
}.bind(this));
}
},
_listeners: {},
removeListener: function (ev, fn) {
if (fn) {
var index = this._listeners[ev].indexOf(fn);
if (index > -1) {
this._listeners[ev].splice(index, 1);
}
} else {
delete this._listeners[ev];
}
},
removeAllListeners: function (ev) {
if (ev) {
delete this._listeners[ev];
} else {
this._listeners = {};
}
},
disconnect: function () {},
connect: function () {}
};

return socket;
}
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
"gulp-concat": "^2.2.0",
"gulp-uglify": "^0.2.1",
"gulp-header": "^1.0.2",
"gulp-footer": "^1.0.4"
"gulp-footer": "^1.0.4",
"gulp-karma": "0.0.4",
"gulp-ng-annotate": "^0.3.3",
"karma": "^0.12.21",
"karma-chai-sinon": "^0.1.4",
"karma-chrome-launcher": "~0.1.0",
"karma-firefox-launcher": "~0.1.0",
"karma-mocha": "~0.1",
"karma-phantomjs-launcher": "~0.1.0",
"karma-script-launcher": "~0.1.0",
"sinon": "^1.12.1",
"sinon-chai": "^2.6.0"
}
}
9 changes: 4 additions & 5 deletions src/service/angular-sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ angular.module('ngSails').provider('$sails', function () {
this.interceptors = [];
this.responseHandler = undefined;

this.$get = ['$q', '$timeout', function ($q, $timeout) {
// @ngInject
this.$get = function ($q, $timeout) {
var socket = io.connect(provider.url),
defer = function () {
var deferred = $q.defer(),
Expand All @@ -34,7 +35,6 @@ angular.module('ngSails').provider('$sails', function () {
resolveOrReject = this.responseHandler || function (deferred, response) {
var jwr = response;

// backward compatibility with older sails.io (no JWR)
if(!(response instanceof Object && response.constructor.name === "JWR")){
jwr = {
body: response,
Expand All @@ -43,10 +43,8 @@ angular.module('ngSails').provider('$sails', function () {
};
}

// angular $http returns the 'body' as 'data'.
jwr.data = jwr.body;

// angular $http returns the 'statusCode' as 'status'.
jwr.status = jwr.statusCode;

// TODO: map 'status'/'statusCode' to a 'statusText' to mimic angular $http
Expand Down Expand Up @@ -82,6 +80,7 @@ angular.module('ngSails').provider('$sails', function () {
socket[eventName] = function (event, cb) {
if (cb !== null && angular.isFunction(cb)) {
socket['legacy_' + eventName](event, function (result) {
console.log('!!!', result);
angularify(cb, result);
});
}
Expand All @@ -92,5 +91,5 @@ angular.module('ngSails').provider('$sails', function () {
angular.forEach(eventNames, wrapEvent);

return socket;
}];
};
});
85 changes: 85 additions & 0 deletions test/angular-sails.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
describe('Agnular Sails', function() {

var $scope,
$compile,
$timeout,
$sails,
mockIoSocket,
spy;

beforeEach(module('ngSails'));

beforeEach(inject(function(_$rootScope_, _$compile_, _$timeout_, _$sails_) {
$scope = _$rootScope_.$new();
$compile = _$compile_;
$timeout = _$timeout_;
$sails = _$sails_;
mockIoSocket = $sails;
spy = sinon.spy();
}));

describe('#on', function() {

it('should apply asynchronously', function () {
$sails.on('event', spy);
mockIoSocket.emit('event');

expect(spy).to.have.been.not.called;
$timeout.flush();

expect(spy).to.have.been.called;
});

});

describe('#disconnect', function () {

it('should call the underlying socket.disconnect', function () {
mockIoSocket.disconnect = spy;
$sails.disconnect();
expect(spy).to.have.been.called;
});

});

describe('#connect', function () {

it('should call the underlying socket.connect', function () {
mockIoSocket.connect = spy;
$sails.connect();
expect(spy).to.have.been.called;
});

});


describe('#once', function () {

it('should apply asynchronously', function () {
$sails.once('event', spy);

mockIoSocket.emit('event');

expect(spy).to.have.been.not.called;
$timeout.flush();

expect(spy).to.have.been.called;
});

it('should only run once', function () {
var counter = 0;
$sails.once('event', function () {
counter += 1;
});

mockIoSocket.emit('event');
mockIoSocket.emit('event');
$timeout.flush();

expect(counter).to.equal(1);
});

});


});

0 comments on commit 98df613

Please sign in to comment.