Skip to content

Commit

Permalink
test(ScalarObservable): add tests for fast-path methods
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Oct 14, 2015
1 parent 33053b1 commit 754d445
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"google-closure-compiler": "^20150920.0.0",
"http-server": "^0.8.0",
"istanbul": "^0.3.22",
"jasmine": "^2.3.1",
"jasmine": "^2.3.2",
"jasmine-core": "^2.2.0",
"lodash": "^3.5.0",
"platform": "^1.3.0",
Expand Down
129 changes: 129 additions & 0 deletions spec/observables/ScalarObservable-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
var Rx = require('../../dist/cjs/Rx');
var ScalarObservable = require('../../dist/cjs/observables/ScalarObservable');
var EmptyObservable = require('../../dist/cjs/observables/EmptyObservable');
var ErrorObservable = require('../../dist/cjs/observables/ErrorObservable');
var Observable = Rx.Observable;

describe('ScalarObservable', function () {
it('should create expose a value property', function () {
var s = new ScalarObservable(1);
expect(s.value).toBe(1);
});

describe('prototype.map()', function () {
it('should map to a new ScalarObservable', function () {
var s = new ScalarObservable(1);
var r = s.map(function (x) { return x + '!!!'; });
expect(r instanceof ScalarObservable).toBe(true);
expect(r.value).toBe('1!!!');
});

it('should return an ErrorObservable if map errors', function () {
var s = new ScalarObservable(1);
var r = s.map(function (x) { throw 'bad!'; });
expect(r instanceof ErrorObservable).toBe(true);
expect(r.error).toBe('bad!');
});
});

describe('prototype.count()', function () {
it('should map to a new ScalarObservable of 1', function () {
var s = new ScalarObservable(1);
var r = s.count();
expect(r instanceof ScalarObservable).toBe(true);
expect(r.value).toBe(1);
});

it('should map to a new ScalarObservable of 1 if predicate matches', function () {
var s = new ScalarObservable(1);
var r = s.count(function (x) { return x === 1; });
expect(r instanceof ScalarObservable).toBe(true);
expect(r.value).toBe(1);
});

it('should map to a new ScalarObservable of 0 if predicate does not match', function () {
var s = new ScalarObservable(1);
var r = s.count(function (x) { return x === 0; });
expect(r instanceof ScalarObservable).toBe(true);
expect(r.value).toBe(0);
});

it('should map to a new ErrorObservable if predicate errors', function () {
var s = new ScalarObservable(1);
var r = s.count(function () { throw 'bad!'; });
expect(r instanceof ErrorObservable).toBe(true);
expect(r.error).toBe('bad!');
});
});

describe('prototype.filter()', function () {
it('should return itself if the filter matches its value', function () {
var s = new ScalarObservable(1);
var r = s.filter(function (x) { return x === 1; });
expect(s).toBe(r);
});

it('should return EmptyObservable if filter does not match', function () {
var s = new ScalarObservable(1);
var r = s.filter(function (x) { return x === 0; });
expect(r instanceof EmptyObservable).toBe(true);
});

it('should map to a new ErrorObservable if predicate errors', function () {
var s = new ScalarObservable(1);
var r = s.filter(function () { throw 'bad!'; });
expect(r instanceof ErrorObservable).toBe(true);
expect(r.error).toBe('bad!');
});
});

describe('prototype.take()', function () {
it('should return itself if count > 0', function () {
var s = new ScalarObservable(1);
var r = s.take(1);
expect(s).toBe(r);
});

it('should return EmptyObservable if count === 0', function () {
var s = new ScalarObservable(1);
var r = s.take(0);
expect(r instanceof EmptyObservable).toBe(true);
});
});

describe('prototype.skip()', function () {
it('should return itself if count === 0', function () {
var s = new ScalarObservable(1);
var r = s.skip(0);
expect(s).toBe(r);
});

it('should return EmptyObservable if count > 0', function () {
var s = new ScalarObservable(1);
var r = s.skip(1);
expect(r instanceof EmptyObservable).toBe(true);
});
});

describe('prototype.reduce()', function () {
it('should return a ScalarObservable of the result if there is a seed', function () {
var s = new ScalarObservable(1);
var r = s.reduce(function (a, x) { return a + x; }, 1);
expect(r instanceof ScalarObservable).toBe(true);
expect(r.value).toBe(2);
});

it('should return itself if there is no seed', function () {
var s = new ScalarObservable(1);
var r = s.reduce(function (a, x) { return a + x; });
expect(r).toBe(s);
});
});
});

// If you uncomment this, jasmine breaks? WEIRD
// describe('reality', function () {
// it('should exist in this universe', function () {
// expect(true).toBe(true);
// });
// });
13 changes: 11 additions & 2 deletions src/observables/ScalarObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,17 @@ export default class ScalarObservable<T> extends Observable<T> {
return this.reduce(project, acc);
}

count(): Observable<number> {
return new ScalarObservable(1);
count(predicate?: (value: T, index: number, source: Observable<T>) => boolean, thisArg?: any): Observable<number> {
if (!predicate) {
return new ScalarObservable(1);
} else {
let result = tryCatch(predicate).call(thisArg || this, this.value, 0, this);
if (result === errorObject) {
return new ErrorObservable(errorObject.e);
} else {
return new ScalarObservable(result ? 1 : 0);
}
}
}

skip(count: number): Observable<T> {
Expand Down

0 comments on commit 754d445

Please sign in to comment.