test(docs-infra): add missing tests for obserables and promises (#34537)

This commit adds missing tests for obserables and promises which
are both stand-alone mini-apps.

PR Close #34537
This commit is contained in:
Sonu Kapoor 2020-08-22 13:16:16 +03:00 committed by Joey Perrott
parent 618cb32407
commit fedcfec346
4 changed files with 145 additions and 45 deletions

View File

@ -0,0 +1,26 @@
import { docRegionChain, docRegionObservable, docRegionUnsubscribe } from './observables';
describe('observables', () => {
it('should print 2', (doneFn: DoneFn) => {
const consoleLogSpy = spyOn(console, 'log');
const observable = docRegionObservable(console);
observable.subscribe(() => {
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
expect(consoleLogSpy).toHaveBeenCalledWith(2);
doneFn();
});
});
it('should close the subscription', () => {
const subscription = docRegionUnsubscribe();
expect(subscription.closed).toBeTruthy();
});
it('should chain an observable', (doneFn: DoneFn) => {
const observable = docRegionChain();
observable.subscribe(value => {
expect(value).toBe(4);
doneFn();
});
});
});

View File

@ -1,20 +1,37 @@
import { map } from 'rxjs/operators'; // #docplaster
import { Observable } from 'rxjs';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
export function docRegionObservable(console: Console) {
// #docregion observable // #docregion observable
// declare a publishing operation // declare a publishing operation
const observable = new Observable<number>(observer => { const observable = new Observable<number>(observer => {
// Subscriber fn... // Subscriber fn...
// #enddocregion observable
// The below code is used for unit testing only
observer.next(2);
// #docregion observable
}); });
// initiate execution // initiate execution
observable.subscribe(() => { observable.subscribe(value => {
// observer handles notifications // observer handles notifications
// #enddocregion observable
// The below code is used for unit testing only
console.log(value);
// #docregion observable
}); });
// #enddocregion observable // #enddocregion observable
return observable;
}
export function docRegionUnsubscribe() {
const observable = new Observable<number>(() => {
// Subscriber fn...
});
// #docregion unsubscribe // #docregion unsubscribe
const subscription = observable.subscribe(() => { const subscription = observable.subscribe(() => {
@ -24,17 +41,32 @@ const subscription = observable.subscribe(() => {
subscription.unsubscribe(); subscription.unsubscribe();
// #enddocregion unsubscribe // #enddocregion unsubscribe
return subscription;
}
// #docregion error export function docRegionError() {
const observable = new Observable<number>(() => {
observable.subscribe(() => { // Subscriber fn...
throw Error('my error');
}); });
// #docregion error
observable.subscribe(() => {
throw new Error('my error');
});
// #enddocregion error // #enddocregion error
}
export function docRegionChain() {
let observable = new Observable<number>(observer => {
// Subscriber fn...
observer.next(2);
});
observable =
// #docregion chain // #docregion chain
observable.pipe(map(v => 2 * v)); observable.pipe(map(v => 2 * v));
// #enddocregion chain // #enddocregion chain
return observable;
}

View File

@ -0,0 +1,23 @@
import { docRegionError, docRegionPromise } from './promises';
describe('promises', () => {
it('should print 2', (doneFn: DoneFn) => {
const consoleLogSpy = spyOn(console, 'log');
const pr = docRegionPromise(console, 2);
pr.then((value) => {
expect(consoleLogSpy).toHaveBeenCalledTimes(1);
expect(consoleLogSpy).toHaveBeenCalledWith(2);
expect(value).toBe(4);
doneFn();
});
});
it('should throw an error', (doneFn: DoneFn) => {
const promise = docRegionError();
promise
.then(() => {
throw new Error('Promise should be rejected.');
},
() => doneFn());
});
});

View File

@ -1,25 +1,44 @@
// #docplaster
export function docRegionPromise(console: Console, inputValue: number) {
// #docregion promise // #docregion promise
// initiate execution // initiate execution
const promise = new Promise<number>((resolve, reject) => { let promise = new Promise<number>((resolve, reject) => {
// Executer fn... // Executer fn...
// #enddocregion promise
// The below is used in the unit tests.
resolve(inputValue);
// #docregion promise
}); });
// #enddocregion promise
promise =
// #docregion promise
promise.then(value => { promise.then(value => {
// handle result here // handle result here
});
// #enddocregion promise // #enddocregion promise
// The below is used in the unit tests.
console.log(value);
return value;
// #docregion promise
});
// #enddocregion promise
promise =
// #docregion chain // #docregion chain
promise.then(v => 2 * v); promise.then(v => 2 * v);
// #enddocregion chain // #enddocregion chain
return promise;
}
export function docRegionError() {
let promise = Promise.resolve();
promise =
// #docregion error // #docregion error
promise.then(() => { promise.then(() => {
throw Error('my error'); throw new Error('my error');
}); });
// #enddocregion error // #enddocregion error
return promise;
}