1- import { act , fireEvent , render , screen } from "@testing-library/react" ;
1+ import "@testing-library/jest-dom" ;
2+ import {
3+ act ,
4+ fireEvent ,
5+ render ,
6+ screen ,
7+ waitFor ,
8+ } from "@testing-library/react" ;
29import userEvent from "@testing-library/user-event" ;
310import { createRef } from "react" ;
411import { Provider } from "react-redux" ;
@@ -11,13 +18,14 @@ import { newWritingSystem } from "types/writingSystem";
1118
1219jest . mock ( "components/DataEntry/utilities.ts" , ( ) => ( {
1320 ...jest . requireActual ( "components/DataEntry/utilities.ts" ) ,
14- focusInput : jest . fn ( ) ,
21+ focusInput : ( ) => mockFocusInput ( ) ,
1522} ) ) ;
1623jest . mock ( "components/Pronunciations/PronunciationsFrontend" , ( ) => jest . fn ( ) ) ;
1724
1825const mockAddNewAudio = jest . fn ( ) ;
1926const mockAddNewEntry = jest . fn ( ) ;
2027const mockDelNewAudio = jest . fn ( ) ;
28+ const mockFocusInput = jest . fn ( ) ;
2129const mockSetNewGloss = jest . fn ( ) ;
2230const mockSetNewNote = jest . fn ( ) ;
2331const mockSetNewVern = jest . fn ( ) ;
@@ -79,7 +87,7 @@ const fireEnterOnActiveElement = async (): Promise<void> => {
7987} ;
8088
8189beforeEach ( ( ) => {
82- jest . resetAllMocks ( ) ;
90+ jest . clearAllMocks ( ) ;
8391} ) ;
8492
8593afterEach ( ( ) => {
@@ -173,4 +181,31 @@ describe("NewEntry", () => {
173181 expect ( mockAddNewEntry ) . toHaveBeenCalledTimes ( 1 ) ;
174182 expect ( mockResetNewEntry ) . toHaveBeenCalledTimes ( 1 ) ;
175183 } ) ;
184+
185+ it ( "returns focus to gloss after closing note dialog" , async ( ) => {
186+ await renderNewEntry ( ) ;
187+ mockFocusInput . mockClear ( ) ;
188+
189+ // Click the note button to open the dialog
190+ await userEvent . click ( screen . getByTestId ( NewEntryId . ButtonNote ) ) ;
191+ expect ( mockFocusInput ) . not . toHaveBeenCalled ( ) ;
192+
193+ // Cancel and verify that focusInput was called after transition completes
194+ await userEvent . click ( screen . getByText ( new RegExp ( "cancel" ) ) ) ;
195+ await waitFor ( ( ) => expect ( mockFocusInput ) . toHaveBeenCalled ( ) ) ;
196+ } ) ;
197+
198+ it ( "returns focus to gloss after confirming note" , async ( ) => {
199+ await renderNewEntry ( ) ;
200+ mockFocusInput . mockClear ( ) ;
201+
202+ // Click the note button to open the dialog and type a note
203+ await userEvent . click ( screen . getByTestId ( NewEntryId . ButtonNote ) ) ;
204+ await userEvent . type ( document . activeElement ! , "note text" ) ;
205+ expect ( mockFocusInput ) . not . toHaveBeenCalled ( ) ;
206+
207+ // Confirm and verify that focusInput was called after transition completes
208+ await userEvent . click ( screen . getByText ( new RegExp ( "confirm" ) ) ) ;
209+ await waitFor ( ( ) => expect ( mockFocusInput ) . toHaveBeenCalled ( ) ) ;
210+ } ) ;
176211} ) ;
0 commit comments