Skip to content

docs: add test case for #492 #495

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
Oct 14, 2024
Merged
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
48 changes: 48 additions & 0 deletions projects/testing-library/tests/issues/issue-492.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { AsyncPipe } from '@angular/common';
import { Component, inject, Injectable } from '@angular/core';
import { render, screen, waitFor } from '../../src/public_api';
import { Observable, BehaviorSubject, map } from 'rxjs';

test('displays username', async () => {
// stubbed user service using a Subject
const user = new BehaviorSubject({ name: 'username 1' });
const userServiceStub: Partial<UserService> = {
getName: () => user.asObservable().pipe(map((u) => u.name)),
};

// render the component with injection of the stubbed service
await render(UserComponent, {
componentProviders: [
{
provide: UserService,
useValue: userServiceStub,
},
],
});

// assert first username emitted is rendered
expect(await screen.findByRole('heading')).toHaveTextContent('username 1');

// emitting a second username
user.next({ name: 'username 2' });

// assert the second username is rendered
await waitFor(() => expect(screen.getByRole('heading')).toHaveTextContent('username 2'));
});

@Component({
selector: 'atl-user',
standalone: true,
template: `<h1>{{ username$ | async }}</h1>`,
imports: [AsyncPipe],
})
class UserComponent {
readonly username$: Observable<string> = inject(UserService).getName();
}

@Injectable()
class UserService {
getName(): Observable<string> {
throw new Error('Not implemented');
}
}