-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb4fc74
commit fbbed20
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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'); | ||
} | ||
} |