@@ -13,8 +13,6 @@ const locators = {
1313 navLinksLocator : `[aria-label="${ englishLocale . components . containers . navBar . controls . toggle } "] + div` ,
1414 // Global UI controls
1515 languageDropdownName : englishLocale . components . common . languageDropdown . label ,
16- // Initially, the themeToggle's name will be the light mode i18n.
17- themeToggleName : englishLocale . components . common . themeToggle . loading ,
1816 themeToggleAriaLabels : {
1917 light : englishLocale . components . common . themeToggle . light ,
2018 dark : englishLocale . components . common . themeToggle . dark ,
@@ -30,6 +28,11 @@ const getTheme = (page: Page) =>
3028 ( ) => document . documentElement . dataset . theme as 'light' | 'dark'
3129 ) ;
3230
31+ const getCurrentAriaLabel = ( theme : string ) =>
32+ theme === 'dark'
33+ ? locators . themeToggleAriaLabels . light
34+ : locators . themeToggleAriaLabels . dark ;
35+
3336const openLanguageMenu = async ( page : Page ) => {
3437 const button = page . getByRole ( 'button' , {
3538 name : locators . languageDropdownName ,
@@ -71,30 +74,27 @@ test.describe('Node.js Website', () => {
7174 test . describe ( 'Theme' , ( ) => {
7275 test ( 'should toggle between light/dark themes' , async ( { page } ) => {
7376 const themeToggle = page . getByRole ( 'button' , {
74- name : locators . themeToggleName ,
77+ name : / S w i t c h t o ( L i g h t | D a r k ) M o d e / i ,
7578 } ) ;
76- await expect ( themeToggle ) . toBeVisible ( ) ;
7779
7880 const initialTheme = await getTheme ( page ) ;
79- const initialAriaLabel = await themeToggle . getAttribute ( 'aria-label' ) ;
80- expect ( initialAriaLabel ) . toBe (
81- locators . themeToggleAriaLabels [ initialTheme ]
82- ) ;
81+ const initialAriaLabel = getCurrentAriaLabel ( initialTheme ) ;
82+ let currentAriaLabel = await themeToggle . getAttribute ( 'aria-label' ) ;
83+ expect ( currentAriaLabel ) . toBe ( initialAriaLabel ) ;
8384
8485 await themeToggle . click ( ) ;
8586
8687 const newTheme = await getTheme ( page ) ;
87- const newAriaLabel = await themeToggle . getAttribute ( 'aria-label' ) ;
88+ const newAriaLabel = getCurrentAriaLabel ( newTheme ) ;
89+ currentAriaLabel = await themeToggle . getAttribute ( 'aria-label' ) ;
8890
8991 expect ( newTheme ) . not . toBe ( initialTheme ) ;
90- expect ( [ 'light' , 'dark' ] ) . toContain ( newTheme ) ;
91-
92- expect ( newAriaLabel ) . toBe ( locators . themeToggleAriaLabels [ newTheme ] ) ;
92+ expect ( currentAriaLabel ) . toBe ( newAriaLabel ) ;
9393 } ) ;
9494
9595 test ( 'should persist theme across page navigation' , async ( { page } ) => {
9696 const themeToggle = page . getByRole ( 'button' , {
97- name : locators . themeToggleName ,
97+ name : / S w i t c h t o ( L i g h t | D a r k ) M o d e / i ,
9898 } ) ;
9999 await themeToggle . click ( ) ;
100100 const selectedTheme = await getTheme ( page ) ;
0 commit comments