Skip to content

Commit

Permalink
Merge 20bfd8e into c84c4a6
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll committed Jun 27, 2017
2 parents c84c4a6 + 20bfd8e commit f451670
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 190 deletions.
26 changes: 13 additions & 13 deletions spec/operators/every-spec.ts
Expand Up @@ -2,7 +2,7 @@ import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const { asDiagram };
declare const asDiagram: Function;
declare const hot: typeof marbleTestingSignature.hot;
declare const cold: typeof marbleTestingSignature.cold;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
Expand All @@ -12,11 +12,11 @@ const Observable = Rx.Observable;

/** @test {every} */
describe('Observable.prototype.every', () => {
function truePredicate(x) {
function truePredicate(x: never) {
return true;
}

function predicate(x) {
function predicate(x: number) {
return x % 5 === 0;
}

Expand Down Expand Up @@ -55,13 +55,13 @@ describe('Observable.prototype.every', () => {
observer.next(1);
observer.complete();
})
.every(function (value: number, index: number) {
.every(function (this: typeof thisArg, value: number, index: number) {
expect(this).to.deep.equal(thisArg);
}, thisArg).subscribe();
});

it('should emit true if source is empty', () => {
const source = hot('-----|');
const source = hot('-----|', <{ [index: string]: number; }>{});
const sourceSubs = '^ !';
const expected = '-----(x|)';

Expand All @@ -70,7 +70,7 @@ describe('Observable.prototype.every', () => {
});

it('should emit false if single source of element does not match with predicate', () => {
const source = hot('--a--|');
const source = hot('--a--|', <{ [index: string]: number; }>{});
const sourceSubs = '^ !';
const expected = '--(x|)';

Expand All @@ -79,7 +79,7 @@ describe('Observable.prototype.every', () => {
});

it('should emit false if none of element does not match with predicate', () => {
const source = hot('--a--b--c--d--e--|');
const source = hot('--a--b--c--d--e--|', { a: 1, b: 2, c: 3, d: 4, e: 5 });
const sourceSubs = '^ !';
const expected = '--(x|)';

Expand Down Expand Up @@ -115,9 +115,9 @@ describe('Observable.prototype.every', () => {
const unsub = ' ! ';

const result = source
.mergeMap((x: any) => Observable.of(x))
.mergeMap((x) => Observable.of(x))
.every(predicate)
.mergeMap((x: any) => Observable.of(x));
.mergeMap((x) => Observable.of(x));

expectObservable(result, unsub).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(sourceSubs);
Expand All @@ -128,7 +128,7 @@ describe('Observable.prototype.every', () => {
const sourceSubs = '^ !';
const expected = '--------#';

function faultyPredicate(x) {
function faultyPredicate(x: string) {
if (x === 'c') {
throw 'error';
} else {
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('Observable.prototype.every', () => {
const source = Observable.of(3);
const expected = '#';

function faultyPredicate(x) {
function faultyPredicate(x: number) {
throw 'error';
}

Expand All @@ -192,7 +192,7 @@ describe('Observable.prototype.every', () => {
const source = Observable.of(5, 10, 15, 20);
const expected = '#';

function faultyPredicate(x) {
function faultyPredicate(x: number) {
if (x === 15) {
throw 'error';
}
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('Observable.prototype.every', () => {
});

it('should emit true if source does not emit after subscription', () => {
const source = hot('--z--^-----|');
const source = hot('--z--^-----|', { z: 0 });
const sourceSubs = '^ !';
const expected = '------(x|)';

Expand Down
120 changes: 60 additions & 60 deletions spec/operators/filter-spec.ts
Expand Up @@ -2,7 +2,7 @@ import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const { asDiagram };
declare const asDiagram: Function;
declare const hot: typeof marbleTestingSignature.hot;
declare const cold: typeof marbleTestingSignature.cold;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
Expand All @@ -12,32 +12,32 @@ const Observable = Rx.Observable;

/** @test {filter} */
describe('Observable.prototype.filter', () => {
function oddFilter(x) {
return (+x) % 2 === 1;
function oddFilter(x: number) {
return x % 2 === 1;
}

function isPrime(i) {
function isPrime(i: number) {
if (+i <= 1) { return false; }
const max = Math.floor(Math.sqrt(+i));
const max = Math.floor(Math.sqrt(Number(i)));
for (let j = 2; j <= max; ++j) {
if (+i % j === 0) { return false; }
}
return true;
}

asDiagram('filter(x => x % 2 === 1)')('should filter out even values', () => {
const source = hot('--0--1--2--3--4--|');
const subs = '^ !';
const expected = '-----1-----3-----|';
const source = hot<number>('--0--1--2--3--4--|');
const subs = '^ !';
const expected = '-----1-----3-----|';

expectObservable(source.filter(oddFilter)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should filter in only prime numbers', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ !';
const expected = '--3---5----7-------|';
const source = hot<number>('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ !';
const expected = '--3---5----7-------|';

expectObservable(source.filter(isPrime)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
Expand All @@ -60,19 +60,19 @@ describe('Observable.prototype.filter', () => {
});

it('should filter in only prime numbers, source unsubscribes early', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ ! ';
const unsub = ' ! ';
const expected = '--3---5----7- ';
const source = hot<number>('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ ! ';
const unsub = ' ! ';
const expected = '--3---5----7- ';

expectObservable(source.filter(isPrime), unsub).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should filter in only prime numbers, source throws', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--#');
const subs = '^ !';
const expected = '--3---5----7-------#';
const source = hot<number>('-1--2--^-3-4-5-6--7-8--9--#');
const subs = '^ !';
const expected = '--3---5----7-------#';

expectObservable(source.filter(isPrime)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
Expand All @@ -92,7 +92,7 @@ describe('Observable.prototype.filter', () => {
return isPrime(x);
}

expectObservable((<any>source).filter(predicate)).toBe(expected);
expectObservable(source.filter(predicate)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

Expand All @@ -102,19 +102,19 @@ describe('Observable.prototype.filter', () => {
const expected = '--3--------7-------|';

function predicate(x: any, i: number) {
return isPrime((+x) + i * 10);
return isPrime(Number(x) + i * 10);
}

expectObservable((<any>source).filter(predicate)).toBe(expected);
expectObservable(source.filter(predicate)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should invoke predicate once for each checked value', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '--3---5----7-------|';
const source = hot<number>('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '--3---5----7-------|';

let invoked = 0;
const predicate = (x: any) => {
const predicate = (x: number) => {
invoked++;
return isPrime(x);
};
Expand All @@ -136,9 +136,9 @@ describe('Observable.prototype.filter', () => {
const expected = '--3--------7- ';

function predicate(x: any, i: number) {
return isPrime((+x) + i * 10);
return isPrime(Number(x) + i * 10);
}
expectObservable((<any>source).filter(predicate), unsub).toBe(expected);
expectObservable(source.filter(predicate), unsub).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

Expand All @@ -148,9 +148,9 @@ describe('Observable.prototype.filter', () => {
const expected = '--3--------7-------#';

function predicate(x: any, i: number) {
return isPrime((+x) + i * 10);
return isPrime(Number(x) + i * 10);
}
expectObservable((<any>source).filter(predicate)).toBe(expected);
expectObservable(source.filter(predicate)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

Expand All @@ -165,10 +165,10 @@ describe('Observable.prototype.filter', () => {
if (invoked === 4) {
throw 'error';
}
return isPrime((+x) + i * 10);
return isPrime(Number(x) + i * 10);
}

expectObservable((<any>source).filter(predicate)).toBe(expected);
expectObservable(source.filter(predicate)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

Expand All @@ -178,18 +178,18 @@ describe('Observable.prototype.filter', () => {

expectObservable(
source
.filter((x) => parseInt(x) % 2 === 0)
.filter((x) => parseInt(x) % 3 === 0)
.filter((x) => Number(x) % 2 === 0)
.filter((x) => Number(x) % 3 === 0)
).toBe(expected);
});

it('should be able to accept and use a thisArg', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '--------6----------|';

function Filterer() {
this.filter1 = (x: number) => x % 2 === 0;
this.filter2 = (x: number) => x % 3 === 0;
class Filterer {
filter1 = (x: string) => Number(x) % 2 === 0;
filter2 = (x: string) => Number(x) % 3 === 0;
}

const filterer = new Filterer();
Expand All @@ -203,60 +203,60 @@ describe('Observable.prototype.filter', () => {
});

it('should be able to use filter and map composed', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '----a---b----c-----|';
const source = hot<number>('-1--2--^-3-4-5-6--7-8--9--|');
const expected = '----a---b----c-----|';
const values = { a: 16, b: 36, c: 64 };

expectObservable(
source
.filter((x) => parseInt(x) % 2 === 0)
.map((x) => parseInt(x) * parseInt(x))
.filter((x) => Number(x) % 2 === 0)
.map((x) => Number(x) * Number(x))
).toBe(expected, values);
});

it('should propagate errors from the source', () => {
const source = hot('--0--1--2--3--4--#');
const subs = '^ !';
const expected = '-----1-----3-----#';
const source = hot<number>('--0--1--2--3--4--#');
const subs = '^ !';
const expected = '-----1-----3-----#';

expectObservable(source.filter(oddFilter)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should support Observable.empty', () => {
const source = cold('|');
const subs = '(^!)';
const expected = '|';
const source = cold<number>('|');
const subs = '(^!)';
const expected = '|';

expectObservable(source.filter(oddFilter)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should support Observable.never', () => {
const source = cold('-');
const subs = '^';
const expected = '-';
const source = cold<number>('-');
const subs = '^';
const expected = '-';

expectObservable(source.filter(oddFilter)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should support Observable.throw', () => {
const source = cold('#');
const subs = '(^!)';
const expected = '#';
const source = cold<number>('#');
const subs = '(^!)';
const expected = '#';

expectObservable(source.filter(oddFilter)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should send errors down the error path', (done: MochaDone) => {
it('should send errors down the error path', (done) => {
Observable.of(42).filter(<any>((x: number, index: number) => {
throw 'bad';
}))
.subscribe((x: number) => {
.subscribe((x) => {
done(new Error('should not be called'));
}, (err: any) => {
}, (err) => {
expect(err).to.equal('bad');
done();
}, () => {
Expand All @@ -265,15 +265,15 @@ describe('Observable.prototype.filter', () => {
});

it('should not break unsubscription chain when unsubscribed explicitly', () => {
const source = hot('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ ! ';
const unsub = ' ! ';
const expected = '--3---5----7- ';
const source = hot<number>('-1--2--^-3-4-5-6--7-8--9--|');
const subs = '^ ! ';
const unsub = ' ! ';
const expected = '--3---5----7- ';

const r = source
.mergeMap((x: any) => Observable.of(x))
.mergeMap((x) => Observable.of(x))
.filter(isPrime)
.mergeMap((x: any) => Observable.of(x));
.mergeMap((x) => Observable.of(x));

expectObservable(r, unsub).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
Expand Down

0 comments on commit f451670

Please sign in to comment.