Skip to content

docs: add async component example #171

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

Merged
merged 1 commit into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apps/example-app/app/examples/14-async-component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { fakeAsync, tick } from '@angular/core/testing';
import { render, screen, fireEvent } from '@testing-library/angular';

import { AsyncComponent } from './14-async-component';

test('can use fakeAsync utilities', fakeAsync(async () => {
await render(AsyncComponent);

const load = await screen.findByRole('button', { name: /load/i });
fireEvent.click(load);

tick(10_000);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use flush() if you just want to jump to the end.


const hello = await screen.findByText('Hello world');
expect(hello).toBeInTheDocument();
}));
27 changes: 27 additions & 0 deletions apps/example-app/app/examples/14-async-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ApplicationInitStatus, Component, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs';
import { delay, filter, mapTo } from 'rxjs/operators';

@Component({
selector: 'app-fixture',
template: `
<button (click)="load()">Load</button>
<div *ngIf="data$ | async as data">{{ data }}</div>
`,
})
export class AsyncComponent implements OnDestroy {
actions = new Subject<string>();
data$ = this.actions.pipe(
filter((x) => x === 'LOAD'),
mapTo('Hello world'),
delay(10_000),
);

load() {
this.actions.next('LOAD');
}

ngOnDestroy() {
this.actions.complete();
}
}