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,40 +1,72 @@
import { map } from 'rxjs/operators';
// #docplaster
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
// #docregion observable
export function docRegionObservable(console: Console) {
// #docregion observable
// declare a publishing operation
const observable = new Observable<number>(observer => {
// Subscriber fn...
});
// declare a publishing operation
const observable = new Observable<number>(observer => {
// Subscriber fn...
// #enddocregion observable
// The below code is used for unit testing only
observer.next(2);
// #docregion observable
});
// initiate execution
observable.subscribe(() => {
// observer handles notifications
});
// initiate execution
observable.subscribe(value => {
// 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;
}
// #docregion unsubscribe
export function docRegionUnsubscribe() {
const observable = new Observable<number>(() => {
// Subscriber fn...
});
// #docregion unsubscribe
const subscription = observable.subscribe(() => {
// observer handles notifications
});
const subscription = observable.subscribe(() => {
// observer handles notifications
});
subscription.unsubscribe();
subscription.unsubscribe();
// #enddocregion unsubscribe
// #enddocregion unsubscribe
return subscription;
}
// #docregion error
export function docRegionError() {
const observable = new Observable<number>(() => {
// Subscriber fn...
});
observable.subscribe(() => {
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);
});
// #docregion chain
observable =
// #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 @@
// #docregion promise
// initiate execution
const promise = new Promise<number>((resolve, reject) => {
// Executer fn...
});
// #docplaster
promise.then(value => {
// handle result here
});
export function docRegionPromise(console: Console, inputValue: number) {
// #docregion promise
// initiate execution
let promise = new Promise<number>((resolve, reject) => {
// Executer fn...
// #enddocregion promise
// The below is used in the unit tests.
resolve(inputValue);
// #docregion promise
});
// #enddocregion promise
promise =
// #docregion promise
promise.then(value => {
// handle result here
// #enddocregion promise
// The below is used in the unit tests.
console.log(value);
return value;
// #docregion promise
});
// #enddocregion promise
promise =
// #docregion chain
promise.then(v => 2 * v);
// #enddocregion chain
// #enddocregion promise
return promise;
}
// #docregion chain
export function docRegionError() {
let promise = Promise.resolve();
promise =
// #docregion error
promise.then(v => 2 * v);
promise.then(() => {
throw new Error('my error');
});
// #enddocregion chain
// #docregion error
promise.then(() => {
throw Error('my error');
});
// #enddocregion error
// #enddocregion error
return promise;
}