7
7
} from "testHelpers/websockets" ;
8
8
import { OneWayWebSocket } from "utils/OneWayWebSocket" ;
9
9
import { createUseAgentLogs } from "./useAgentLogs" ;
10
+ import { act } from "react" ;
10
11
11
12
const millisecondsInOneMinute = 60_000 ;
12
13
@@ -82,14 +83,15 @@ describe("useAgentLogs", () => {
82
83
it ( "Automatically sorts logs that are received out of order" , async ( ) => {
83
84
const { hookResult, publisherResult } = mountHook ( ) ;
84
85
const logs = generateMockLogs ( 10 , new Date ( "september 9, 1999" ) ) ;
85
- const reversed = logs . toReversed ( ) ;
86
86
87
- for ( const log of reversed ) {
88
- publisherResult . current . publishMessage (
89
- new MessageEvent < string > ( "message" , {
90
- data : JSON . stringify ( log ) ,
91
- } ) ,
92
- )
87
+ for ( const log of logs . toReversed ( ) ) {
88
+ act ( ( ) => {
89
+ publisherResult . current . publishMessage (
90
+ new MessageEvent < string > ( "message" , {
91
+ data : JSON . stringify ( [ log ] ) ,
92
+ } ) ,
93
+ )
94
+ } )
93
95
}
94
96
await waitFor ( ( ) => expect ( hookResult . current ) . toEqual ( logs ) ) ;
95
97
} ) ;
@@ -111,7 +113,7 @@ describe("useAgentLogs", () => {
111
113
const initialEvent = new MessageEvent < string > ( "message" , {
112
114
data : JSON . stringify ( initialLogs ) ,
113
115
} ) ;
114
- publisherResult . current . publishMessage ( initialEvent ) ;
116
+ act ( ( ) => publisherResult . current . publishMessage ( initialEvent ) ) ;
115
117
await waitFor ( ( ) => expect ( hookResult . current ) . toEqual ( initialLogs ) ) ;
116
118
117
119
// Disable the hook (and have the hook close the connection behind the
@@ -126,7 +128,7 @@ describe("useAgentLogs", () => {
126
128
const newEvent = new MessageEvent < string > ( "message" , {
127
129
data : JSON . stringify ( newLogs ) ,
128
130
} ) ;
129
- publisherResult . current . publishMessage ( newEvent ) ;
131
+ act ( ( ) => publisherResult . current . publishMessage ( newEvent ) ) ;
130
132
await waitFor ( ( ) => expect ( hookResult . current ) . toEqual ( newLogs ) ) ;
131
133
} ) ;
132
134
} ) ;
0 commit comments