Skip to content

Commit

Permalink
test(all): modernize all tests (#4665)
Browse files Browse the repository at this point in the history
* test(all): modernize all tests

- disables tests that relied on polluting the global  instance, thus causing all tests that were relying on legacy dot-chaining or patched creation methods to fail to build.
- updates all tests to use pipeable operators and appropriate creation functions.

resolves #4633

* test(publish): use pipeable refCount
  • Loading branch information
benlesh committed Mar 28, 2019
1 parent 23e7223 commit 4ea688c
Show file tree
Hide file tree
Showing 29 changed files with 237 additions and 235 deletions.
File renamed without changes.
94 changes: 54 additions & 40 deletions spec/Observable-spec.ts
Expand Up @@ -2,8 +2,8 @@ import { expect } from 'chai';
import * as sinon from 'sinon';
import { Observer, TeardownLogic } from '../src/internal/types';
import { cold, expectObservable, expectSubscriptions } from './helpers/marble-testing';
import { Observable, config, Subscription, noop, Subscriber, Operator, NEVER, Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { Observable, config, Subscription, noop, Subscriber, Operator, NEVER, Subject, of, throwError, empty } from 'rxjs';
import { map, multicast, refCount, filter, count, tap, combineLatest, concat, merge, race, zip } from 'rxjs/operators';

declare const asDiagram: any, rxTestScheduler: any;

Expand Down Expand Up @@ -57,7 +57,7 @@ describe('Observable', () => {
describe('forEach', () => {
it('should iterate and return a Promise', (done) => {
const expected = [1, 2, 3];
const result = Observable.of(1, 2, 3).forEach(function (x) {
const result = of(1, 2, 3).forEach(function (x) {
expect(x).to.equal(expected.shift());
}, Promise)
.then(() => {
Expand All @@ -68,7 +68,7 @@ describe('Observable', () => {
});

it('should reject promise when in error', (done) => {
Observable.throwError('bad').forEach((x) => {
throwError('bad').forEach((x) => {
done(new Error('should not be called'));
}, Promise).then(() => {
done(new Error('should not complete'));
Expand All @@ -86,7 +86,7 @@ describe('Observable', () => {
return new Promise<number>(callback);
} as any;

Observable.of(42).forEach((x) => {
of(42).forEach((x) => {
expect(x).to.equal(42);
}).then(() => {
expect(wasCalled).to.be.true;
Expand All @@ -97,7 +97,7 @@ describe('Observable', () => {
it('should reject promise if nextHandler throws', (done) => {
const results: number[] = [];

Observable.of(1, 2, 3).forEach((x) => {
of(1, 2, 3).forEach((x) => {
if (x === 3) {
throw new Error('NO THREES!');
}
Expand Down Expand Up @@ -223,7 +223,7 @@ describe('Observable', () => {

expect(unsubscribeCalled).to.be.false;

Observable.empty().subscribe();
empty().subscribe();

expect(unsubscribeCalled).to.be.false;
});
Expand All @@ -244,7 +244,7 @@ describe('Observable', () => {

expect(unsubscribeCalled).to.be.false;

Observable.empty().subscribe(observer);
empty().subscribe(observer);

expect(unsubscribeCalled).to.be.false;
});
Expand Down Expand Up @@ -326,7 +326,7 @@ describe('Observable', () => {
done();
};
})
.do(() => times += 1)
.pipe(tap(() => times += 1))
.subscribe(
function () {
if (times === 2) {
Expand Down Expand Up @@ -357,7 +357,7 @@ describe('Observable', () => {
done();
};
})
.do(() => times += 1)
.pipe(tap(() => times += 1))
.subscribe(
function () {
if (times === 2) {
Expand Down Expand Up @@ -388,7 +388,7 @@ describe('Observable', () => {
done();
};
})
.do(() => times += 1)
.pipe(tap(() => times += 1))
.subscribe(
function () {
if (times === 2) {
Expand All @@ -413,7 +413,7 @@ describe('Observable', () => {
}
};

Observable.of(1).subscribe(o);
of(1).subscribe(o);
});

it('should accept an anonymous observer with just an error function and call the error function in the context' +
Expand All @@ -428,7 +428,7 @@ describe('Observable', () => {
}
};

Observable.throwError('bad').subscribe(o);
throwError('bad').subscribe(o);
});

it('should accept an anonymous observer with just a complete function and call the complete function in the' +
Expand All @@ -442,12 +442,12 @@ describe('Observable', () => {
}
};

Observable.empty().subscribe(o);
empty().subscribe(o);
});

it('should accept an anonymous observer with no functions at all', () => {
expect(() => {
Observable.empty().subscribe(<any>{});
empty().subscribe(<any>{});
}).not.to.throw();
});

Expand All @@ -466,7 +466,7 @@ describe('Observable', () => {
done();
};
})
.do(() => times += 1)
.pipe(tap(() => times += 1))
.subscribe({
next() {
if (times === 2) {
Expand Down Expand Up @@ -495,7 +495,7 @@ describe('Observable', () => {
done();
};
})
.do(() => times += 1)
.pipe(tap(() => times += 1))
.subscribe({
next() {
if (times === 2) {
Expand Down Expand Up @@ -526,7 +526,7 @@ describe('Observable', () => {
done();
};
})
.do(() => times += 1)
.pipe(tap(() => times += 1))
.subscribe({
next() {
if (times === 2) {
Expand Down Expand Up @@ -574,7 +574,7 @@ describe('Observable', () => {
});

it('should throw synchronously', () => {
expect(() => Observable.throwError(new Error()).subscribe())
expect(() => throwError(new Error()).subscribe())
.to.throw();
});

Expand Down Expand Up @@ -603,12 +603,12 @@ describe('Observable', () => {

describe('pipe', () => {
it('should exist', () => {
const source = Observable.of('test');
const source = of('test');
expect(source.pipe).to.be.a('function');
});

it('should pipe multiple operations', (done) => {
Observable.of('test')
of('test')
.pipe(
map((x) => x + x),
map((x) => x + '!!!')
Expand All @@ -623,7 +623,7 @@ describe('Observable', () => {
});

it('should return the same observable if there are no arguments', () => {
const source = Observable.of('test');
const source = of('test');
const result = source.pipe();
expect(result).to.equal(source);
});
Expand Down Expand Up @@ -717,7 +717,7 @@ describe('Observable.lift', () => {
observer.next(2);
observer.next(3);
observer.complete();
}).map((x) => { return 10 * x; });
}).pipe(map((x) => { return 10 * x; }));

expect(result instanceof MyCustomObservable).to.be.true;

Expand All @@ -734,15 +734,16 @@ describe('Observable.lift', () => {
});

it('should compose through multicast and refCount', (done) => {
const result = new MyCustomObservable((observer) => {
const result = new MyCustomObservable<number>((observer) => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
})
.multicast(() => new Subject<number>())
.refCount()
.map((x) => { return 10 * x; });
}).pipe(
multicast(() => new Subject<number>()),
refCount(),
map(x => 10 * x),
);

expect(result instanceof MyCustomObservable).to.be.true;

Expand All @@ -759,13 +760,14 @@ describe('Observable.lift', () => {
});

it('should compose through multicast with selector function', (done) => {
const result = new MyCustomObservable((observer) => {
const result = new MyCustomObservable<number>((observer) => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.complete();
})
.multicast(() => new Subject<number>(), (shared) => shared.map((x) => { return 10 * x; }));
}).pipe(
multicast(() => new Subject<number>(), shared => shared.pipe(map(x => 10 * x)))
);

expect(result instanceof MyCustomObservable).to.be.true;

Expand All @@ -786,7 +788,9 @@ describe('Observable.lift', () => {
const e2 = cold('--1--2-3-4---| ');
const expected = '--A-BC-D-EF-G-H-|';

const result = MyCustomObservable.from(e1).combineLatest(e2, (a, b) => String(a) + String(b));
const result = MyCustomObservable.from(e1).pipe(
combineLatest(e2, (a, b) => String(a) + String(b))
);

expect(result instanceof MyCustomObservable).to.be.true;

Expand All @@ -800,7 +804,9 @@ describe('Observable.lift', () => {
const e2 = cold('--x---y--|');
const expected = '--a--b---x---y--|';

const result = MyCustomObservable.from(e1).concat(e2, rxTestScheduler);
const result = MyCustomObservable.from(e1).pipe(
concat(e2, rxTestScheduler)
);

expect(result instanceof MyCustomObservable).to.be.true;

Expand All @@ -812,7 +818,9 @@ describe('Observable.lift', () => {
const e2 = cold('--x--y-|');
const expected = '-ax-by-|';

const result = MyCustomObservable.from(e1).merge(e2, rxTestScheduler);
const result = MyCustomObservable.from(e1).pipe(
merge(e2, rxTestScheduler)
);

expect(result instanceof MyCustomObservable).to.be.true;

Expand All @@ -826,7 +834,10 @@ describe('Observable.lift', () => {
const e2subs = '^ !';
const expected = '---a-----b-----c----|';

const result = MyCustomObservable.from(e1).race(e2);
const result = MyCustomObservable.from(e1).pipe(
// TODO: remove after race typings are fixed.
race(e2) as any
);

expect(result instanceof MyCustomObservable).to.be.true;

Expand All @@ -840,7 +851,9 @@ describe('Observable.lift', () => {
const e2 = cold('--1--2-3-4---| ');
const expected = ('--A--B----C-D| ');

const result = MyCustomObservable.from(e1).zip(e2, (a, b) => String(a) + String(b));
const result = MyCustomObservable.from(e1).pipe(
zip(e2, (a, b) => String(a) + String(b))
);

expect(result instanceof MyCustomObservable).to.be.true;

Expand Down Expand Up @@ -889,10 +902,11 @@ describe('Observable.lift', () => {
observer.next(2);
observer.next(3);
observer.complete();
})
.map((x) => { return 10 * x; })
.filter((x) => { return x > 15; })
.count();
}).pipe(
map(x => 10 * x),
filter(x => x > 15),
count(),
);

expect(result instanceof LogObservable).to.be.true;

Expand Down
13 changes: 7 additions & 6 deletions spec/Subject-spec.ts
@@ -1,7 +1,8 @@
import { expect } from 'chai';
import { hot, expectObservable } from './helpers/marble-testing';
import { Subject, ObjectUnsubscribedError, Observable, AsyncSubject, Observer } from 'rxjs';
import { Subject, ObjectUnsubscribedError, Observable, AsyncSubject, Observer, of } from 'rxjs';
import { AnonymousSubject } from 'rxjs/internal/Subject';
import { delay } from 'rxjs/operators';

/** @test {Subject} */
describe('Subject', () => {
Expand Down Expand Up @@ -304,7 +305,7 @@ describe('Subject', () => {

it('should have a static create function that works', () => {
expect(Subject.create).to.be.a('function');
const source = Observable.of(1, 2, 3, 4, 5);
const source = of(1, 2, 3, 4, 5);
const nexts: number[] = [];
const output: number[] = [];

Expand Down Expand Up @@ -350,7 +351,7 @@ describe('Subject', () => {

it('should have a static create function that works also to raise errors', () => {
expect(Subject.create).to.be.a('function');
const source = Observable.of(1, 2, 3, 4, 5);
const source = of(1, 2, 3, 4, 5);
const nexts: number[] = [];
const output: number[] = [];

Expand Down Expand Up @@ -395,7 +396,7 @@ describe('Subject', () => {
});

it('should be an Observer which can be given to Observable.subscribe', (done: MochaDone) => {
const source = Observable.of(1, 2, 3, 4, 5);
const source = of(1, 2, 3, 4, 5);
const subject = new Subject();
const expected = [1, 2, 3, 4, 5];

Expand All @@ -412,7 +413,7 @@ describe('Subject', () => {
});

it('should be usable as an Observer of a finite delayed Observable', (done: MochaDone) => {
const source = Observable.of(1, 2, 3).delay(50);
const source = of(1, 2, 3).pipe(delay(50));
const subject = new Subject();

const expected = [1, 2, 3];
Expand Down Expand Up @@ -531,7 +532,7 @@ describe('AnonymousSubject', () => {

const subject = Subject.create(null, new Observable((observer: Observer<any>) => {
subscribed = true;
const subscription = Observable.of('x').subscribe(observer);
const subscription = of('x').subscribe(observer);
return () => {
subscription.unsubscribe();
};
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions spec/observables/IteratorObservable-spec.ts
Expand Up @@ -4,7 +4,7 @@ import { fromIterable } from 'rxjs/internal/observable/fromIterable';
import { iterator as symbolIterator } from 'rxjs/internal/symbol/iterator';
import { TestScheduler } from 'rxjs/testing';
import { Notification, queueScheduler, Subscriber } from 'rxjs';
import { observeOn, materialize, take } from 'rxjs/operators';
import { observeOn, materialize, take, toArray } from 'rxjs/operators';

declare const expectObservable: any;
declare const rxTestScheduler: TestScheduler;
Expand Down Expand Up @@ -43,8 +43,8 @@ describe('fromIterable', () => {
const e1 = fromIterable<number>(new Int32Array([10, 20]), undefined).pipe(observeOn(rxTestScheduler));

let v1, v2: Array<Notification<any>>;
e1.pipe(materialize()).toArray().subscribe((x) => v1 = x);
e1.pipe(materialize()).toArray().subscribe((x) => v2 = x);
e1.pipe(materialize(), toArray()).subscribe((x) => v1 = x);
e1.pipe(materialize(), toArray()).subscribe((x) => v2 = x);

rxTestScheduler.flush();
expect(v1).to.deep.equal(expected);
Expand Down
4 changes: 3 additions & 1 deletion spec/observables/concat-spec.ts
Expand Up @@ -58,7 +58,9 @@ describe('static concat', () => {
const expected = '--i-j-k-l---i-j-';
const unsub = ' !';

const innerWrapped = inner.mergeMap((x) => of(x));
const innerWrapped = inner.pipe(
mergeMap((x) => of(x))
);
const result = concat(innerWrapped, innerWrapped, innerWrapped, innerWrapped)
.pipe(mergeMap((x) => of(x)));

Expand Down

0 comments on commit 4ea688c

Please sign in to comment.