Skip to content
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

use isAuth in app component #22

Merged
merged 1 commit into from
Feb 23, 2024
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
1 change: 1 addition & 0 deletions localServer/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ app.use(cors({
app.use(bodyParser.json())

app.route('/api/auth/login').post(authRoutes.login)
app.route('/api/auth/logout').post(authRoutes.logout)

const port = 9000
app.listen(port, () => {
Expand Down
15 changes: 11 additions & 4 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<div class="componentBorder">
<h1 data-id="welcomeMessage">{{ 'app.HELLO' | transloco:{value: title} }}</h1>
<button data-id="logoutButton" (click)="onLogout()">{{'app.LOGOUT_BUTTON' | transloco}}</button>
<p>Congratulations! Your app is running. 🎉</p>

@if(isAuth | async) {
<h1
data-id="welcomeMessage">
{{ 'app.HELLO' | transloco:{value: title} }}
</h1>
<button
data-id="logoutButton"
(click)="onLogout()">
{{'app.LOGOUT_BUTTON' | transloco}}
</button>
}
<router-outlet />
</div>

14 changes: 12 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ import {
import {
AuthService
} from './services/auth/auth.service'
import {
AsyncPipe
} from '@angular/common'
import {
Observable
} from 'rxjs'

const COMPONENT_TRANSLOCO_SCOPE = 'app'
@Component({
selector: 'app-root',
standalone: true,
imports: [
RouterOutlet,
TranslocoPipe
TranslocoPipe,
AsyncPipe
],
templateUrl: './app.component.html',
styleUrl: './app.component.css',
Expand All @@ -38,11 +45,14 @@ export class AppComponent {
}

public title = 'contracts-angular'
public isAuth: Observable<boolean>

public constructor(
private auth$: AuthService,
private router: Router
) {}
) {
this.isAuth = this.auth$.isAuth()
}

public onLogout(): void {
this.auth$.logout().subscribe({
Expand Down
4 changes: 4 additions & 0 deletions src/app/tests/app.component.harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ export class AppHarness extends ComponentHarness {
const text = await this.getH1(id)
return await text?.text() || ''
}

public async controlPresent(id: string): Promise<boolean> {
return !!(await this.locatorForOptional(`h1[data-id="${id}"],button[data-id="${id}"]`)())
}
}
26 changes: 22 additions & 4 deletions src/app/tests/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Router
} from '@angular/router'
import {
BehaviorSubject,
of
} from 'rxjs'
import {
Expand All @@ -28,14 +29,18 @@ import {
} from '@angular/cdk/testing/testbed'

describe('AppComponent', () => {
let isAuthMock: BehaviorSubject<boolean>
let appHarness: AppHarness
let router: Router

beforeEach(async () => {
const authServiceMock = jasmine.createSpyObj<AuthService>('authService', ['logout'])
authServiceMock.logout.and.callFake(() => {
return of(undefined)
})
isAuthMock = new BehaviorSubject(true)
const authServiceMock = jasmine.createSpyObj<AuthService>('authService', [
'logout',
'isAuth'
])
authServiceMock.isAuth.and.returnValue(isAuthMock)
authServiceMock.logout.and.returnValue(of(undefined))
await TestBed.configureTestingModule({
imports: [
AppComponent,
Expand Down Expand Up @@ -63,4 +68,17 @@ describe('AppComponent', () => {
await appHarness.clickButton('logoutButton')
expect(navigateSpy).toHaveBeenCalledWith(['/login'])
})

it('should display welcome message and logout button only to authenticated user', async () => {
expect(await appHarness.controlPresent('welcomeMessage')).toBe(true)
expect(await appHarness.controlPresent('logoutButton')).toBe(true)

isAuthMock.next(false)
expect(await appHarness.controlPresent('welcomeMessage')).toBe(false)
expect(await appHarness.controlPresent('logoutButton')).toBe(false)

isAuthMock.next(true)
expect(await appHarness.controlPresent('welcomeMessage')).toBe(true)
expect(await appHarness.controlPresent('logoutButton')).toBe(true)
})
})