@@ -1236,6 +1236,10 @@ describe("ChatView - Focus Grabbing Tests", () => {
12361236 />
12371237 </ ExtensionStateContextProvider > ,
12381238 )
1239+ // when chat view is hidden, this triggers a "blur" event
1240+ if ( textAreaElement ) {
1241+ fireEvent . blur ( textAreaElement )
1242+ }
12391243
12401244 expect ( mockFocus ) . toHaveBeenCalledTimes ( 1 )
12411245
@@ -1256,7 +1260,7 @@ describe("ChatView - Focus Grabbing Tests", () => {
12561260 } )
12571261
12581262 it ( "does not grab focus when unhidden and not previously focused" , async ( ) => {
1259- const { rerender } = render (
1263+ const { getByTestId , rerender } = render (
12601264 < ExtensionStateContextProvider >
12611265 < ChatView
12621266 isHidden = { false }
@@ -1267,6 +1271,7 @@ describe("ChatView - Focus Grabbing Tests", () => {
12671271 </ ExtensionStateContextProvider > ,
12681272 )
12691273
1274+ const textAreaElement = getByTestId ( "chat-textarea" ) . querySelector ( "input" )
12701275 rerender (
12711276 < ExtensionStateContextProvider >
12721277 < ChatView
@@ -1277,6 +1282,10 @@ describe("ChatView - Focus Grabbing Tests", () => {
12771282 />
12781283 </ ExtensionStateContextProvider > ,
12791284 )
1285+ // when chat view is hidden, this triggers a "blur" event
1286+ if ( textAreaElement ) {
1287+ fireEvent . blur ( textAreaElement )
1288+ }
12801289
12811290 rerender (
12821291 < ExtensionStateContextProvider >
@@ -1326,6 +1335,10 @@ describe("ChatView - Focus Grabbing Tests", () => {
13261335 expect ( mockFocus ) . toHaveBeenCalledTimes ( 1 )
13271336 } )
13281337
1338+ // when extension becomes invisible, this will trigger a blur event before the message is sent.
1339+ if ( textAreaElement ) {
1340+ fireEvent . blur ( textAreaElement )
1341+ }
13291342 await sendActionMessage ( "didBecomeInvisible" )
13301343 expect ( mockFocus ) . toHaveBeenCalledTimes ( 1 )
13311344
@@ -1339,7 +1352,7 @@ describe("ChatView - Focus Grabbing Tests", () => {
13391352 } )
13401353
13411354 it ( "does not grab focus when unhidden and not previously focused" , async ( ) => {
1342- render (
1355+ const { findByTestId } = render (
13431356 < ExtensionStateContextProvider >
13441357 < ChatView
13451358 isHidden = { false }
@@ -1350,6 +1363,12 @@ describe("ChatView - Focus Grabbing Tests", () => {
13501363 </ ExtensionStateContextProvider > ,
13511364 )
13521365
1366+ const textAreaElement = ( await findByTestId ( "chat-textarea" ) ) . querySelector ( "input" )
1367+
1368+ // when extension becomes invisible, this will trigger a blur event before the message is sent.
1369+ if ( textAreaElement ) {
1370+ fireEvent . blur ( textAreaElement )
1371+ }
13531372 await sendActionMessage ( "didBecomeInvisible" )
13541373 expect ( mockFocus ) . toHaveBeenCalledTimes ( 0 )
13551374
@@ -1389,9 +1408,12 @@ describe("ChatView - Focus Grabbing Tests", () => {
13891408 if ( textAreaElement ) {
13901409 fireEvent . blur ( textAreaElement )
13911410 }
1392-
13931411 expect ( mockFocus ) . toHaveBeenCalledTimes ( 1 )
13941412
1413+ // must wait for > 500msecs after blur before making panel invisible
1414+ // so that the blur is interpreted as a deliberate user action.
1415+ await sleep ( 550 )
1416+
13951417 await sendActionMessage ( "didBecomeInvisible" )
13961418
13971419 expect ( mockFocus ) . toHaveBeenCalledTimes ( 1 )
0 commit comments