Skip to content

Commit 43d0ca8

Browse files
committed
refactor: update useAgentLogs tests as unit tests
1 parent d6e00c3 commit 43d0ca8

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed
Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
renderHook,
3-
type RenderHookResult,
4-
waitFor,
5-
} from "@testing-library/react";
1+
import { renderHook, waitFor } from "@testing-library/react";
62
import type { WorkspaceAgentLog } from "api/typesGenerated";
73
import { MockWorkspaceAgent } from "testHelpers/entities";
84
import { createUseAgentLogs } from "./useAgentLogs";
@@ -22,14 +18,28 @@ function generateMockLogs(count: number): WorkspaceAgentLog[] {
2218
}));
2319
}
2420

25-
type MountHookResult = Readonly<
26-
RenderHookResult<readonly WorkspaceAgentLog[], { enabled: boolean }> & {
27-
publisher: MockWebSocketPublisher;
28-
}
29-
>;
21+
// A mutable object holding the most recent mock WebSocket connection. This
22+
// value will change as the hook opens/closes new connections
23+
type PublisherResult = {
24+
current: MockWebSocketPublisher;
25+
};
26+
27+
type MountHookResult = Readonly<{
28+
// Note: the value of `current` should be readonly, but the `current`
29+
// property itself should be mutable
30+
hookResult: {
31+
current: readonly WorkspaceAgentLog[];
32+
};
33+
rerender: (props: { enabled: boolean }) => void;
34+
publisherResult: PublisherResult;
35+
}>;
3036

3137
function mountHook(): MountHookResult {
32-
let publisher!: MockWebSocketPublisher;
38+
// Have to cheat the types a little bit to avoid a chicken-and-the-egg
39+
// scenario. publisherResult will be initialized with an undefined current
40+
// value, but it'll be guaranteed not to be undefined by the time this
41+
// function returns.
42+
const publisherResult: Partial<PublisherResult> = { current: undefined };
3343
const useAgentLogs = createUseAgentLogs((agentId, params) => {
3444
return new OneWayWebSocket({
3545
apiRoute: `/api/v2/workspaceagents/${agentId}/logs`,
@@ -38,41 +48,45 @@ function mountHook(): MountHookResult {
3848
after: params?.after?.toString() || "0",
3949
}),
4050
websocketInit: (url) => {
41-
const [mockSocket, mockPub] = createMockWebSocket(url);
42-
publisher = mockPub;
51+
const [mockSocket, mockPublisher] = createMockWebSocket(url);
52+
publisherResult.current = mockPublisher;
4353
return mockSocket;
4454
},
4555
});
4656
});
4757

48-
const { result, rerender, unmount } = renderHook(
58+
const { result, rerender } = renderHook(
4959
({ enabled }) => useAgentLogs(MockWorkspaceAgent, enabled),
5060
{ initialProps: { enabled: true } },
5161
);
5262

53-
return { result, rerender, unmount, publisher };
63+
return {
64+
rerender,
65+
hookResult: result,
66+
publisherResult: publisherResult as PublisherResult,
67+
};
5468
}
5569

5670
describe("useAgentLogs", () => {
5771
it("clears logs when hook becomes disabled (protection to avoid duplicate logs when hook goes back to being re-enabled)", async () => {
58-
const { result, rerender, publisher } = mountHook();
72+
const { hookResult, publisherResult, rerender } = mountHook();
5973

6074
// Verify that logs can be received after mount
6175
const initialLogs = generateMockLogs(3);
6276
const initialEvent = new MessageEvent<string>("message", {
6377
data: JSON.stringify(initialLogs),
6478
});
65-
publisher.publishMessage(initialEvent);
79+
publisherResult.current.publishMessage(initialEvent);
6680
await waitFor(() => {
6781
// Using expect.arrayContaining to account for the fact that we're
6882
// not guaranteed to receive WebSocket events in order
69-
expect(result.current).toEqual(expect.arrayContaining(initialLogs));
83+
expect(hookResult.current).toEqual(expect.arrayContaining(initialLogs));
7084
});
7185

7286
// Disable the hook (and have the hook close the connection behind the
7387
// scenes)
7488
rerender({ enabled: false });
75-
await waitFor(() => expect(result.current).toHaveLength(0));
89+
await waitFor(() => expect(hookResult.current).toHaveLength(0));
7690

7791
// Re-enable the hook (creating an entirely new connection), and send
7892
// new logs
@@ -81,9 +95,9 @@ describe("useAgentLogs", () => {
8195
const newEvent = new MessageEvent<string>("message", {
8296
data: JSON.stringify(newLogs),
8397
});
84-
publisher.publishMessage(newEvent);
98+
publisherResult.current.publishMessage(newEvent);
8599
await waitFor(() => {
86-
expect(result.current).toEqual(expect.arrayContaining(newLogs));
100+
expect(hookResult.current).toEqual(expect.arrayContaining(newLogs));
87101
});
88102
});
89103
});

0 commit comments

Comments
 (0)