@@ -289,6 +289,42 @@ describe("ChipModalField inputType='password'", () => {
289289 expect ( document . querySelector ( '[aria-label="Hide password"]' ) ) . not . toBeNull ( )
290290 } )
291291
292+ it ( 'keeps the toggle label honest when a keyboard moves focus to it' , ( ) => {
293+ mountPasswordField ( 'hunter2-secret' )
294+ act ( ( ) => passwordInput ( ) . focus ( ) )
295+
296+ // Tabbing to the toggle blurs the input, which re-masks — so by the time a
297+ // keyboard can activate the button it already reads "Show password", and
298+ // activating it reveals. The label must never disagree with what is on
299+ // screen, in either direction.
300+ const focusedToggle = document . querySelector < HTMLButtonElement > ( '[aria-label="Hide password"]' )
301+ if ( ! focusedToggle ) throw new Error ( 'Toggle should read "Hide password" while focused' )
302+ act ( ( ) => focusedToggle . focus ( ) )
303+ expect ( passwordInput ( ) . className ) . toContain ( MASK_CLASS )
304+
305+ const toggle = document . querySelector < HTMLButtonElement > ( '[aria-label="Show password"]' )
306+ if ( ! toggle ) throw new Error ( 'Toggle should read "Show password" once the input has blurred' )
307+
308+ // Enter/Space on a focused button dispatch a plain click, with no mousedown.
309+ act ( ( ) => toggle . click ( ) )
310+ expect ( passwordInput ( ) . className ) . not . toContain ( MASK_CLASS )
311+ expect ( document . querySelector ( '[aria-label="Hide password"]' ) ) . not . toBeNull ( )
312+ } )
313+
314+ it ( 'hides a revealed password on keyboard activation when the input is not focused' , ( ) => {
315+ mountPasswordField ( 'hunter2-secret' )
316+
317+ const reveal = document . querySelector < HTMLButtonElement > ( '[aria-label="Show password"]' )
318+ if ( ! reveal ) throw new Error ( 'Reveal toggle did not render' )
319+ act ( ( ) => reveal . click ( ) )
320+ expect ( passwordInput ( ) . className ) . not . toContain ( MASK_CLASS )
321+
322+ const hide = document . querySelector < HTMLButtonElement > ( '[aria-label="Hide password"]' )
323+ if ( ! hide ) throw new Error ( 'Hide toggle did not render' )
324+ act ( ( ) => hide . click ( ) )
325+ expect ( passwordInput ( ) . className ) . toContain ( MASK_CLASS )
326+ } )
327+
292328 it ( 'hides a focused password instead of re-revealing it' , ( ) => {
293329 mountPasswordField ( 'hunter2-secret' )
294330 act ( ( ) => passwordInput ( ) . focus ( ) )
0 commit comments