Skip to content

Commit

Permalink
Merge pull request #4 from cvuorinen/fix-undefined-on-falsy-value
Browse files Browse the repository at this point in the history
Set up unit tests with Karma & jasmine + fix undefined on falsy value
  • Loading branch information
cvuorinen authored Jun 15, 2016
2 parents e10f80a + 341f2bf commit bf2919e
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 3 deletions.
2 changes: 1 addition & 1 deletion async-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
}

return values[inputId] || undefined;
return values[inputId];
};

// Need a way to tell the input objects apart from each other (so we only subscribe to them once)
Expand Down
73 changes: 73 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Karma configuration
// Generated on Wed Jun 15 2016 23:16:37 GMT+0300 (EEST)

module.exports = function(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'],


// list of files / patterns to load in the browser
files: [
'node_modules/phantomjs-polyfill/bind-polyfill.js',
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'async-filter.js',
'test/**/*.spec.js'
],


// list of files to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},


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


// 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: true,


// 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: false,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@
},
"homepage": "https://github.com/cvuorinen/angular1-async-filter#readme",
"devDependencies": {
"angular": "^1.5.7",
"angular-mocks": "^1.5.7",
"babel-cli": "^6.5.1",
"babel-preset-es2015": "^6.5.0"
"babel-preset-es2015": "^6.5.0",
"jasmine": "^2.4.1",
"karma": "^0.13.22",
"karma-jasmine": "^1.0.2",
"karma-phantomjs-launcher": "^1.0.0",
"phantomjs-polyfill": "0.0.2",
"phantomjs-prebuilt": "^2.1.7"
}
}
2 changes: 1 addition & 1 deletion src/async-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
}
}

return values[inputId] || undefined;
return values[inputId];
};

// Need a way to tell the input objects apart from each other (so we only subscribe to them once)
Expand Down
123 changes: 123 additions & 0 deletions test/async-filter.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
describe('Filter: async', function () {
var asyncFilter,
unresolvedPromise = function() {
return { then: function() {} };
},
resolvedPromise = function(value) {
return { then: function(callback) { callback(value); } };
},
promiseMock = function() {
var callback;
return {
then: function(callbackFn) { callback = callbackFn; },
resolve: function (value) { callback(value); }
};
},
observableMock = function() {
var callback;
return {
subscribe: function(callbackFn) { callback = callbackFn; },
next: function (value) { callback(value); }
};
};

beforeEach(module('asyncFilter'));
beforeEach(inject(function ($filter) {
asyncFilter = $filter('async');
}));

it('should return null on null input', function() {
expect(asyncFilter(null)).toEqual(null);
});

it('should return input when it is not Promise or Observable', function() {
expect(asyncFilter("foo")).toEqual("foo");
});

it('should return undefined when promise is not resolved', function() {
expect(asyncFilter(unresolvedPromise())).toEqual(undefined);
});

it('should return promises resolved value', function() {
expect(asyncFilter(resolvedPromise(42))).toEqual(42);
});

it('should return undefined until promise resolved', function() {
var promise = promiseMock();

expect(asyncFilter(promise)).toEqual(undefined);

promise.resolve(42);

expect(asyncFilter(promise)).toEqual(42);
});

it('should return falsy value', function() {
expect(asyncFilter(resolvedPromise(0))).toEqual(0);
});

it('should return undefined until observable emits', function() {
var observable = observableMock();

expect(asyncFilter(observable)).toEqual(undefined);

observable.next(42);

expect(asyncFilter(observable)).toEqual(42);
});

it('should return observables latest value', function() {
var observable = observableMock();
asyncFilter(observable);

observable.next(42);

expect(asyncFilter(observable)).toEqual(42);

observable.next("foo");

expect(asyncFilter(observable)).toEqual("foo");
});

it('should only subscribe once', function() {
var observable = observableMock();
spyOn(observable, 'subscribe');

asyncFilter(observable);
asyncFilter(observable);
asyncFilter(observable);

expect(observable.subscribe).toHaveBeenCalled();
expect(observable.subscribe.calls.count()).toEqual(1);
});

it('should call $applyAsync on each value if scope provided', function() {
var observable = observableMock();
var scope = {
$applyAsync: jasmine.createSpy('scope.$applyAsync')
};

expect(asyncFilter(observable, scope)).toEqual(undefined);
expect(scope.$applyAsync).not.toHaveBeenCalled();

observable.next(42);

expect(scope.$applyAsync).toHaveBeenCalled();
expect(scope.$applyAsync.calls.count()).toEqual(1);

observable.next("foo");

expect(scope.$applyAsync.calls.count()).toEqual(2);
});

it('should listen for scope $destroy event if scope provided', function() {
var observable = observableMock();
var scope = {
$on: jasmine.createSpy('scope.$on')
};

asyncFilter(observable, scope);

expect(scope.$on).toHaveBeenCalledWith('$destroy', jasmine.any(Function));
});
});

0 comments on commit bf2919e

Please sign in to comment.