@@ -17,16 +17,15 @@ jest.mock("@src/utils/auth/user-logout", () => ({
1717 userLogout : jest . fn ( ) ,
1818} ) ) ;
1919
20- const testHeader = ( expectedVisible : boolean ) => {
20+ const expectHeaderVisible = ( expectedVisible : boolean ) => {
2121 const serviceLink = screen . queryByRole ( "link" , {
2222 name : "Check and book an RSV vaccination" ,
2323 } ) ;
2424 const logoLink = screen . queryByRole ( "link" , { name : "NHS homepage" } ) ;
25+
2526 if ( expectedVisible ) {
2627 expect ( serviceLink ) . toBeVisible ( ) ;
27- expect ( serviceLink ?. getAttribute ( "href" ) ) . toEqual ( "/check-and-book-rsv" ) ;
2828 expect ( logoLink ) . toBeVisible ( ) ;
29- expect ( logoLink ?. getAttribute ( "href" ) ) . toEqual ( "/check-and-book-rsv" ) ;
3029 } else {
3130 expect ( serviceLink ) . toBeNull ( ) ;
3231 expect ( logoLink ) . toBeNull ( ) ;
@@ -46,28 +45,61 @@ describe("AppHeader", () => {
4645 } ) ;
4746 } ) ;
4847
49- it ( "shows the app header with logout link when authenticated" , async ( ) => {
50- mockSession = { status : "authenticated" } ;
51- render ( < AppHeader /> ) ;
52- testHeader ( true ) ;
53- const logoutLink = screen . getByRole ( "link" , { name : "Log out" } ) ;
54- expect ( logoutLink ) . toBeVisible ( ) ;
55- expect ( logoutLink ?. getAttribute ( "href" ) ) . toEqual ( "#" ) ;
56- } ) ;
48+ describe ( "when authenticated" , ( ) => {
49+ beforeEach ( ( ) => {
50+ mockSession = { status : "authenticated" } ;
51+ } ) ;
5752
58- it ( "shows the app header without logout link when unauthenticated" , async ( ) => {
59- mockSession = { status : "unauthenticated" } ;
60- render ( < AppHeader /> ) ;
61- testHeader ( true ) ;
62- const logoutLink = screen . queryByRole ( "link" , { name : "Log out" } ) ;
63- expect ( logoutLink ) . toBeNull ( ) ;
53+ it ( "shows the app header with logout link" , async ( ) => {
54+ render ( < AppHeader /> ) ;
55+ expectHeaderVisible ( true ) ;
56+ const logoutLink = screen . getByRole ( "link" , { name : "Log out" } ) ;
57+ expect ( logoutLink ) . toBeVisible ( ) ;
58+ expect ( logoutLink ?. getAttribute ( "href" ) ) . toEqual ( "#" ) ;
59+ } ) ;
60+
61+ it ( "logo and service name should link to service homepage" , async ( ) => {
62+ render ( < AppHeader /> ) ;
63+
64+ const logoLink = screen . queryByRole ( "link" , { name : "NHS homepage" } ) ;
65+ const serviceLink = screen . queryByRole ( "link" , {
66+ name : "Check and book an RSV vaccination" ,
67+ } ) ;
68+ expect ( serviceLink ?. getAttribute ( "href" ) ) . toEqual ( "/check-and-book-rsv" ) ;
69+ expect ( logoLink ?. getAttribute ( "href" ) ) . toEqual ( "/check-and-book-rsv" ) ;
70+ } ) ;
71+
72+ it ( "logs out on click" , async ( ) => {
73+ render ( < AppHeader /> ) ;
74+ screen . getByRole ( "link" , { name : "Log out" } ) . click ( ) ;
75+ expect ( userLogout ) . toHaveBeenCalled ( ) ;
76+ } ) ;
6477 } ) ;
6578
66- it ( "logs out on click" , async ( ) => {
67- mockSession = { status : "authenticated" } ;
68- render ( < AppHeader /> ) ;
69- screen . getByRole ( "link" , { name : "Log out" } ) . click ( ) ;
70- expect ( userLogout ) . toHaveBeenCalled ( ) ;
79+ describe ( "when unauthenticated" , ( ) => {
80+ beforeEach ( ( ) => {
81+ mockSession = { status : "unauthenticated" } ;
82+ } ) ;
83+
84+ it ( "shows the app header without logout link" , async ( ) => {
85+ mockSession = { status : "unauthenticated" } ;
86+ render ( < AppHeader /> ) ;
87+ expectHeaderVisible ( true ) ;
88+ const logoutLink = screen . queryByRole ( "link" , { name : "Log out" } ) ;
89+ expect ( logoutLink ) . toBeNull ( ) ;
90+ } ) ;
91+
92+ it ( "should not include link destination for logo and service name" , async ( ) => {
93+ mockSession = { status : "unauthenticated" } ;
94+ render ( < AppHeader /> ) ;
95+
96+ const logoLink = screen . queryByRole ( "link" , { name : "NHS homepage" } ) ;
97+ const serviceLink = screen . queryByRole ( "link" , {
98+ name : "Check and book an RSV vaccination" ,
99+ } ) ;
100+ expect ( serviceLink ?. getAttribute ( "href" ) ) . toEqual ( "#" ) ;
101+ expect ( logoLink ?. getAttribute ( "href" ) ) . toEqual ( "#" ) ;
102+ } ) ;
71103 } ) ;
72104 } ) ;
73105
@@ -81,7 +113,7 @@ describe("AppHeader", () => {
81113 } ) ;
82114
83115 it ( "hides the app header" , async ( ) => {
84- testHeader ( false ) ;
116+ expectHeaderVisible ( false ) ;
85117 } ) ;
86118 } ) ;
87119
@@ -95,7 +127,7 @@ describe("AppHeader", () => {
95127 } ) ;
96128
97129 it ( "hides the app header" , async ( ) => {
98- testHeader ( false ) ;
130+ expectHeaderVisible ( false ) ;
99131 } ) ;
100132 } ) ;
101133} ) ;
0 commit comments