@@ -5,52 +5,54 @@ 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-
16- it ( 'renders anonymous header when user is not authenticated' , async ( ) => {
17- mockUser . set ( { isAuthenticated : false , name : null } ) ;
8+ function setupWithRoutes ( user : { isAuthenticated : boolean ; name : string | null } ) {
9+ const mockUser = signal ( user ) ;
10+ const mockAuthService = {
11+ user : mockUser ,
12+ } ;
1813
19- await render ( Header , {
20- providers : [ provideRouter ( [ ] ) , { provide : Authentication , useValue : mockAuthService } ] ,
14+ return render ( Header , {
15+ providers : [
16+ provideRouter ( [
17+ { path : 'customers' , component : Header } ,
18+ { path : 'user' , component : Header } ,
19+ ] ) ,
20+ { provide : Authentication , useValue : mockAuthService } ,
21+ ] ,
2122 } ) ;
23+ }
24+
25+ it ( 'renders anonymous header when user is not authenticated' , async ( ) => {
26+ await setup ( { isAuthenticated : false , name : null } ) ;
2227
2328 expect ( screen . getByText ( 'Login' ) ) . toBeInTheDocument ( ) ;
2429 expect ( screen . getByText ( 'Customers' ) ) . toBeInTheDocument ( ) ;
2530} ) ;
2631
2732it ( '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- } ) ;
33+ await setup ( { isAuthenticated : true , name : 'John Doe' } ) ;
3334
3435 expect ( screen . getByText ( '👋 Hello, John Doe' ) ) . toBeInTheDocument ( ) ;
3536 expect ( screen . getByText ( 'Logout' ) ) . toBeInTheDocument ( ) ;
3637 expect ( screen . getByText ( 'Customers' ) ) . toBeInTheDocument ( ) ;
3738} ) ;
3839
3940it ( '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- } ) ;
41+ await setupWithRoutes ( { isAuthenticated : true , name : 'John Doe' } ) ;
5042
5143 const customersLink = screen . getByRole ( 'link' , { name : 'Customers' } ) ;
5244 const userLink = screen . getByRole ( 'link' , { name : 'Current User' } ) ;
5345
5446 expect ( customersLink ) . toHaveAttribute ( 'href' , '/customers' ) ;
5547 expect ( userLink ) . toHaveAttribute ( 'href' , '/user' ) ;
5648} ) ;
49+
50+ function setup ( user : { isAuthenticated : boolean ; name : string | null } ) {
51+ const mockAuthService = {
52+ user : signal ( user ) ,
53+ } ;
54+
55+ return render ( Header , {
56+ providers : [ provideRouter ( [ ] ) , { provide : Authentication , useValue : mockAuthService } ] ,
57+ } ) ;
58+ }
0 commit comments