File tree Expand file tree Collapse file tree
packages/fxa-settings/src/pages/Index Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
5- import { screen , waitFor } from '@testing-library/react' ;
5+ import { act , screen , waitFor } from '@testing-library/react' ;
66import { userEvent } from '@testing-library/user-event' ;
77import { createMockIndexOAuthNativeIntegration , Subject } from './mocks' ;
88import { renderWithLocalizationProvider } from 'fxa-react/lib/test-utils/localizationProvider' ;
@@ -252,4 +252,30 @@ describe('Index page', () => {
252252 ) . toHaveAttribute ( 'data-glean-id' , 'email_first_submit' ) ;
253253 } ) ;
254254 } ) ;
255+
256+ describe ( 'email input auto-focus' , ( ) => {
257+ beforeEach ( ( ) => {
258+ jest . useFakeTimers ( ) ;
259+ } ) ;
260+
261+ afterEach ( ( ) => {
262+ jest . useRealTimers ( ) ;
263+ } ) ;
264+
265+ it ( 'does not focus the email input until the defer timeout elapses' , ( ) => {
266+ renderWithLocalizationProvider ( < Subject /> ) ;
267+
268+ expect ( screen . getByLabelText ( 'Enter your email' ) ) . not . toHaveFocus ( ) ;
269+ } ) ;
270+
271+ it ( 'focuses the email input after the defer timeout elapses' , ( ) => {
272+ renderWithLocalizationProvider ( < Subject /> ) ;
273+
274+ act ( ( ) => {
275+ jest . advanceTimersByTime ( 10 ) ;
276+ } ) ;
277+
278+ expect ( screen . getByLabelText ( 'Enter your email' ) ) . toHaveFocus ( ) ;
279+ } ) ;
280+ } ) ;
255281} ) ;
Original file line number Diff line number Diff line change @@ -80,11 +80,22 @@ export const Index = ({
8080 const legalTerms = integration . getLegalTerms ( ) ;
8181
8282 const emailEngageEventEmitted = useRef ( false ) ;
83+ const emailInputRef = useRef < HTMLInputElement > ( null ) ;
8384
8485 useEffect ( ( ) => {
8586 GleanMetrics . emailFirst . view ( ) ;
8687 } , [ ] ) ;
8788
89+ // Defer auto-focus once. If the container redirects an already-signed-in user
90+ // away, intermittently the browser's email autocomplete dropdown can be shown on
91+ // the next page, presumably from a redirect mid-render. See FXA-14009
92+ useEffect ( ( ) => {
93+ const timer = setTimeout ( ( ) => {
94+ emailInputRef . current ?. focus ( ) ;
95+ } , 10 ) ;
96+ return ( ) => clearTimeout ( timer ) ;
97+ } , [ ] ) ;
98+
8899 const onSubmit = async ( { email } : IndexFormData ) => {
89100 setIsSubmitting ( true ) ;
90101 try {
@@ -199,7 +210,7 @@ export const Index = ({
199210 inputMode = "email"
200211 label = "Enter your email"
201212 inputRef = { register ( ) }
202- autoFocus
213+ inputRefDOM = { emailInputRef }
203214 errorText = { tooltipErrorMessage }
204215 onChange = { handleInputChange }
205216 autoCapitalize = "off"
You can’t perform that action at this time.
0 commit comments