Repository navigation
test(docs-infra): add missing tests for observables and promises #34537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
aio/content/examples/comparing-observables/e2e/src/app.e2e-spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /* | ||
| * This example project is special in that it is not a cli app. To run tests appropriate for this | ||
| * project, the test command is overwritten in `aio/content/examples/observables/example-config.json`. | ||
| * | ||
| * This is an empty placeholder file to ensure that `aio/tools/examples/run-example-e2e.js` runs | ||
| * tests for this project. | ||
| * | ||
| * TODO: Fix our infrastructure/tooling, so that this hack is not necessary. | ||
| */ |
12 changes: 12 additions & 0 deletions
12
aio/content/examples/comparing-observables/example-config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "tests": [ | ||
| { | ||
| "cmd": "yarn", | ||
| "args": ["tsc", "--project", "tsconfig.spec.json", "--module", "commonjs"] | ||
| }, | ||
| { | ||
| "cmd": "yarn", | ||
| "args": ["jasmine", "out-tsc/**/*.spec.js"] | ||
| } | ||
| ] | ||
| } |
26 changes: 26 additions & 0 deletions
26
aio/content/examples/comparing-observables/src/observables.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| }); | ||
| }); | ||
| }); |
82 changes: 57 additions & 25 deletions
82
aio/content/examples/comparing-observables/src/observables.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)); | ||
|
sonukapoor marked this conversation as resolved.
Resolved
|
||
|
|
||
| // #enddocregion chain | ||
| // #enddocregion chain | ||
| return observable; | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
aio/content/examples/comparing-observables/src/promises.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()); | ||
| }); | ||
| }); |
69 changes: 44 additions & 25 deletions
69
aio/content/examples/comparing-observables/src/promises.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,44 @@ | ||
| // #docregion promise | ||
| // initiate execution | ||
| const promise = new Promise<number>((resolve, reject) => { | ||
| // Executer fn... | ||
| }); | ||
|
|
||
| promise.then(value => { | ||
| // handle result here | ||
| }); | ||
|
|
||
| // #enddocregion promise | ||
|
|
||
| // #docregion chain | ||
|
|
||
| promise.then(v => 2 * v); | ||
|
|
||
| // #enddocregion chain | ||
|
|
||
| // #docregion error | ||
|
|
||
| promise.then(() => { | ||
| throw Error('my error'); | ||
| }); | ||
|
|
||
| // #enddocregion error | ||
| // #docplaster | ||
|
|
||
| export function docRegionPromise(console: Console, inputValue: number) { | ||
| // #docregion promise | ||
|
gkalpak marked this conversation as resolved.
Resolved
|
||
| // 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 | ||
|
gkalpak marked this conversation as resolved.
Resolved
|
||
| promise.then(value => { | ||
| // handle result here | ||
| // #enddocregion promise | ||
| // The below is used in the unit tests. | ||
| console.log(value); | ||
| return value; | ||
|
gkalpak marked this conversation as resolved.
Resolved
|
||
| // #docregion promise | ||
| }); | ||
| // #enddocregion promise | ||
| promise = | ||
| // #docregion chain | ||
| promise.then(v => 2 * v); | ||
| // #enddocregion chain | ||
|
|
||
| return promise; | ||
| } | ||
|
|
||
| export function docRegionError() { | ||
| let promise = Promise.resolve(); | ||
| promise = | ||
| // #docregion error | ||
|
|
||
| promise.then(() => { | ||
| throw new Error('my error'); | ||
| }); | ||
|
|
||
| // #enddocregion error | ||
| return promise; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.