Skip to content

Commit

Permalink
refactor: refactor throwError operator to use factory function in uni…
Browse files Browse the repository at this point in the history
…t tests and .ts file (#6261)

* refactor: refactor throwError operator to use factory function in unit tests and .ts file

* test: add unit test for throwError factory support

Co-authored-by: Nitin Malave <nitin.malave@nvent.com>
  • Loading branch information
nitinmalave and Nitin Malave authored Apr 27, 2021
1 parent 1f7b9c5 commit 6ef2725
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 34 deletions.
15 changes: 13 additions & 2 deletions spec-dtslint/observables/throwError-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ import { throwError, animationFrameScheduler } from 'rxjs';
it('should accept any type and return never observable', () => {
const a = throwError(1); // $ExpectType Observable<never>
const b = throwError('a'); // $ExpectType Observable<never>
const c = throwError({a: 1}); // $ExpectType Observable<never>
const c = throwError({ a: 1 }); // $ExpectType Observable<never>
const d = throwError(() => ({ a: 2 })); // $ExpectType Observable<never>
});

it('should support scheduler', () => {
it('should support an error value and a scheduler', () => {
const a = throwError(1, animationFrameScheduler); // $ExpectType Observable<never>
});

it('should accept any type and return never observable with support of factory', () => {
const a = throwError(() => (1)); // $ExpectType Observable<never>
const b = throwError(() => ('a')); // $ExpectType Observable<never>
const c = throwError(() => ({ a: 1 })); // $ExpectType Observable<never>
const d = throwError(() => ({ a: 2 })); // $ExpectType Observable<never>
});

it('should support a factory and a scheduler', () => {
const a = throwError(() => 1, animationFrameScheduler); // $ExpectType Observable<never>
});
10 changes: 5 additions & 5 deletions spec/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('Observable', () => {
});

it('should reject promise when in error', (done) => {
throwError('bad')
throwError(() => ('bad'))
.forEach(() => {
done(new Error('should not be called'));
}, Promise)
Expand Down Expand Up @@ -460,7 +460,7 @@ describe('Observable', () => {
},
};

throwError('bad').subscribe(o);
throwError(() => ('bad')).subscribe(o);
}
);

Expand Down Expand Up @@ -618,7 +618,7 @@ describe('Observable', () => {
});

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

it('should rethrow if next handler throws', () => {
Expand Down Expand Up @@ -658,13 +658,13 @@ describe('Observable', () => {
it('should rethrow synchronous errors from flattened observables', () => {
expect(() => {
of(1)
.pipe(concatMap(() => throwError(new Error('Ahoy! An error!'))))
.pipe(concatMap(() => throwError(() => new Error('Ahoy! An error!'))))
.subscribe(console.log);
}).to.throw('Ahoy! An error!');

expect(() => {
of(1)
.pipe(switchMap(() => throwError(new Error('Avast! Thar be a new error!'))))
.pipe(switchMap(() => throwError(() => new Error('Avast! Thar be a new error!'))))
.subscribe(console.log);
}).to.throw('Avast! Thar be a new error!');
});
Expand Down
2 changes: 1 addition & 1 deletion spec/firstValueFrom-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('firstValueFrom', () => {
});

it('should error for errored observables', async () => {
const source = throwError(new Error('blorp!'));
const source = throwError(() => new Error('blorp!'));
let error: any = null;
try {
await firstValueFrom(source);
Expand Down
2 changes: 1 addition & 1 deletion spec/lastValueFrom-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('lastValueFrom', () => {
});

it('should error for errored observables', async () => {
const source = throwError(new Error('blorp!'));
const source = throwError(() => new Error('blorp!'));
let error: any = null;
try {
await lastValueFrom(source);
Expand Down
4 changes: 2 additions & 2 deletions spec/observables/throwError-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ describe('throwError', () => {
it('should create a cold observable that just emits an error', () => {
rxTest.run(({ expectObservable }) => {
const expected = '#';
const e1 = throwError('error');
const e1 = throwError(() => 'error');
expectObservable(e1).toBe(expected);
});
});

it('should emit one value', (done) => {
let calls = 0;
throwError('bad').subscribe(
throwError(() => 'bad').subscribe(
() => {
done(new Error('should not be called'));
},
Expand Down
4 changes: 2 additions & 2 deletions spec/operators/buffer-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ describe('Observable.prototype.buffer', () => {
it('should work with non-empty and throw selector', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = hot('---^--a--');
const b = throwError(new Error('too bad'));
const b = throwError(() => new Error('too bad'));
const expected = '#';
expectObservable(a.pipe(buffer(b))).toBe(expected, null, new Error('too bad'));
});
});

it('should work with throw and non-empty selector', () => {
testScheduler.run(({ hot, expectObservable }) => {
const a = throwError(new Error('too bad'));
const a = throwError(() => new Error('too bad'));
const b = hot('---^--a--');
const expected = '#';
expectObservable(a.pipe(buffer(b))).toBe(expected, null, new Error('too bad'));
Expand Down
2 changes: 1 addition & 1 deletion spec/operators/bufferTime-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ describe('bufferTime operator', () => {

it('should handle throw', () => {
testScheduler.run(({ time, expectObservable }) => {
const e1 = throwError(new Error('haha'));
const e1 = throwError(() => new Error('haha'));
const expected = '#';
const t = time('----------|');

Expand Down
8 changes: 4 additions & 4 deletions spec/operators/catchError-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('catchError operator', () => {
})
);

throwError(new Error('Some error')).pipe(
throwError(() => new Error('Some error')).pipe(
catchError(() => synchronousObservable),
takeWhile((x) => x != 2) // unsubscribe at the second side-effect
).subscribe(() => { /* noop */ });
Expand Down Expand Up @@ -339,7 +339,7 @@ describe('catchError operator', () => {
});

it('should pass the error as the first argument', (done) => {
throwError('bad').pipe(
throwError(() => ('bad')).pipe(
catchError((err: any) => {
expect(err).to.equal('bad');
return EMPTY;
Expand All @@ -358,7 +358,7 @@ describe('catchError operator', () => {

input$.pipe(
mergeMap(input =>
throwError('bad').pipe(catchError(err => input))
throwError(() => ('bad')).pipe(catchError(err => input))
)
).subscribe(x => {
expect(x).to.be.equal(42);
Expand Down Expand Up @@ -410,7 +410,7 @@ describe('catchError operator', () => {
const testError = new Error('BROKEN PROMISE');
from(Promise.reject(testError)).pipe(
catchError(err =>
throwError(thrownError)
throwError(() => (thrownError))
)
).subscribe(subscribeSpy, errorSpy);

Expand Down
2 changes: 1 addition & 1 deletion spec/operators/concatAll-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('concatAll operator', () => {

it('should throw if any child observable throws', () => {
testScheduler.run(({ expectObservable }) => {
const e1 = from([of('a'), throwError('error'), of('c')]).pipe(take(10));
const e1 = from([of('a'), throwError(() => ('error')), of('c')]).pipe(take(10));
const expected = '(a#)';

expectObservable(e1.pipe(concatAll())).toBe(expected);
Expand Down
2 changes: 1 addition & 1 deletion spec/operators/mergeAll-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('mergeAll', () => {
// prettier-ignore
const e1 = from([
of('a'),
throwError('error'),
throwError(() => ('error')),
of('c')
]);
const expected = '(a#)';
Expand Down
2 changes: 1 addition & 1 deletion spec/operators/mergeScan-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe('mergeScan', () => {
const e1subs = ' ^--!';
const expected = ' ---#';

const result = e1.pipe(mergeScan(() => throwError(new Error('bad!')), []));
const result = e1.pipe(mergeScan(() => throwError(() => new Error('bad!')), []));

expectObservable(result).toBe(expected, undefined, new Error('bad!'));
expectSubscriptions(e1.subscriptions).toBe(e1subs);
Expand Down
4 changes: 2 additions & 2 deletions spec/operators/onErrorResumeNext-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('onErrorResumeNext operator', () => {
}
});

throwError(new Error('Some error')).pipe(
throwError(() => new Error('Some error')).pipe(
onErrorResumeNext(synchronousObservable),
take(3),
).subscribe(() => { /* noop */ });
Expand All @@ -141,7 +141,7 @@ describe('onErrorResumeNext operator', () => {

it('should work with promise', (done) => {
const expected = [1, 2];
const source = concat(of(1), throwError('meh'));
const source = concat(of(1), throwError(() => ('meh')));

source.pipe(onErrorResumeNext(Promise.resolve(2)))
.subscribe(x => {
Expand Down
2 changes: 1 addition & 1 deletion spec/operators/publishReplay-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ describe('publishReplay operator', () => {

it('should emit error when the selector returns Observable.throw', () => {
const error = "It's broken";
const selector = () => throwError(error);
const selector = () => throwError(() => (error));
const source = cold('--1--2---3---|');
const sourceSubs = '(^!)';
const published = source.pipe(publishReplay(1, Infinity, selector));
Expand Down
2 changes: 1 addition & 1 deletion spec/operators/race-legacy-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('race operator', () => {
it('should ignore latter observables if a former one errors immediately', () => {
const onError = sinon.spy();
const onSubscribe = sinon.spy() as any;
const e1 = throwError('kaboom'); // Wins the race
const e1 = throwError(() => ('kaboom')); // Wins the race
const e2 = defer(onSubscribe); // Should be ignored

e1.pipe(race(e2)).subscribe({ error: onError });
Expand Down
2 changes: 1 addition & 1 deletion spec/operators/raceWith-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe('raceWith operator', () => {
it('should ignore latter observables if a former one errors immediately', () => {
const onError = sinon.spy();
const onSubscribe = sinon.spy() as any;
const e1 = throwError('kaboom'); // Wins the race
const e1 = throwError(() => ('kaboom')); // Wins the race
const e2 = defer(onSubscribe); // Should be ignored

e1.pipe(raceWith(e2)).subscribe({ error: onError });
Expand Down
6 changes: 3 additions & 3 deletions spec/operators/retry-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('retry operator', () => {
index++;
if (index === 1 || index === 3) {
errors++;
return throwError('bad');
return throwError(() => ('bad'));
} else {
return of(42);
}
Expand Down Expand Up @@ -145,7 +145,7 @@ describe('retry operator', () => {
index++;
if (index === 1 || index === 3) {
errors++;
return throwError('bad');
return throwError(() => ('bad'));
} else {
return of(42);
}
Expand Down Expand Up @@ -293,7 +293,7 @@ describe('retry operator', () => {
const expected = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3];

of(1, 2, 3).pipe(
concat(throwError('bad!')),
concat(throwError(() => ('bad!'))),
multicast(() => new Subject<number>()),
refCount(),
retry(4)
Expand Down
2 changes: 1 addition & 1 deletion spec/operators/retryWhen-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ describe('retryWhen operator', () => {
}
});
const subscription = source.pipe(retryWhen(errors$ => errors$.pipe(
mergeMap((err, i) => i < 3 ? of(true) : throwError(err))
mergeMap((err, i) => i < 3 ? of(true) : throwError(() => (err)))
))).subscribe({
next: value => results.push(value),
error: (err) => results.push(err)
Expand Down
4 changes: 2 additions & 2 deletions spec/operators/throwIfEmpty-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('throwIfEmpty', () => {
throwIfEmpty(() => error),
mergeMap((value) => {
if (value > 1) {
return throwError(new Error());
return throwError(() => new Error());
}

return of(value);
Expand Down Expand Up @@ -210,7 +210,7 @@ describe('throwIfEmpty', () => {
throwIfEmpty(),
mergeMap((value) => {
if (value > 1) {
return throwError(new Error());
return throwError(() => new Error());
}

return of(value);
Expand Down
2 changes: 1 addition & 1 deletion spec/operators/toPromise-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Observable.toPromise', () => {
});

it('should handle errors properly', (done) => {
throwError('bad')
throwError(() => 'bad')
.toPromise(Promise)
.then(
() => {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/Notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class Notification<T> {
: //
kind === 'E'
? // Error kind. Return an observable that emits the error.
throwError(error)
throwError(() => error)
: //
kind === 'C'
? // Completion kind. Kind is "C", return an observable that just completes.
Expand Down

0 comments on commit 6ef2725

Please sign in to comment.