-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from cvuorinen/fix-undefined-on-falsy-value
Set up unit tests with Karma & jasmine + fix undefined on falsy value
- Loading branch information
Showing
5 changed files
with
207 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |