@@ -5,52 +5,37 @@ import { signal } from '@angular/core';
55import Header from './header' ;
66import { Authentication } from '@sandbox-app/authentication/authentication' ;
77
8- const mockUser = signal < { isAuthenticated : boolean ; name : string | null } > ( {
9- isAuthenticated : true ,
10- name : 'Test User' ,
11- } ) ;
12- const mockAuthService = {
13- user : mockUser ,
14- } ;
15-
168it ( 'renders anonymous header when user is not authenticated' , async ( ) => {
17- mockUser . set ( { isAuthenticated : false , name : null } ) ;
18-
19- await render ( Header , {
20- providers : [ provideRouter ( [ ] ) , { provide : Authentication , useValue : mockAuthService } ] ,
21- } ) ;
9+ await setup ( { isAuthenticated : false , name : null } ) ;
2210
2311 expect ( screen . getByText ( 'Login' ) ) . toBeInTheDocument ( ) ;
2412 expect ( screen . getByText ( 'Customers' ) ) . toBeInTheDocument ( ) ;
2513} ) ;
2614
2715it ( 'renders authenticated header when user is authenticated' , async ( ) => {
28- mockUser . set ( { isAuthenticated : true , name : 'John Doe' } ) ;
29-
30- await render ( Header , {
31- providers : [ provideRouter ( [ ] ) , { provide : Authentication , useValue : mockAuthService } ] ,
32- } ) ;
16+ await setup ( { isAuthenticated : true , name : 'John Doe' } ) ;
3317
3418 expect ( screen . getByText ( '👋 Hello, John Doe' ) ) . toBeInTheDocument ( ) ;
3519 expect ( screen . getByText ( 'Logout' ) ) . toBeInTheDocument ( ) ;
3620 expect ( screen . getByText ( 'Customers' ) ) . toBeInTheDocument ( ) ;
3721} ) ;
3822
3923it ( 'has working navigation links' , async ( ) => {
40- mockUser . set ( { isAuthenticated : true , name : 'John Doe' } ) ;
41-
42- await render ( Header , {
43- providers : [
44- provideRouter ( [
45- { path : 'customers' , component : Header } ,
46- { path : 'user' , component : Header } ,
47- ] ) ,
48- ] ,
49- } ) ;
24+ await setup ( { isAuthenticated : true , name : 'John Doe' } ) ;
5025
5126 const customersLink = screen . getByRole ( 'link' , { name : 'Customers' } ) ;
5227 const userLink = screen . getByRole ( 'link' , { name : 'Current User' } ) ;
5328
5429 expect ( customersLink ) . toHaveAttribute ( 'href' , '/customers' ) ;
5530 expect ( userLink ) . toHaveAttribute ( 'href' , '/user' ) ;
5631} ) ;
32+
33+ function setup ( user : { isAuthenticated : boolean ; name : string | null } ) {
34+ const mockAuthService = {
35+ user : signal ( user ) ,
36+ } ;
37+
38+ return render ( Header , {
39+ providers : [ provideRouter ( [ ] ) , { provide : Authentication , useValue : mockAuthService } ] ,
40+ } ) ;
41+ }
0 commit comments