|
1 | | -import { render } from "@testing-library/react"; |
| 1 | +import { act, render, screen } from "@testing-library/react"; |
2 | 2 | import { QueryClientProvider } from "react-query"; |
3 | 3 | import { afterEach, describe, expect, it, vi } from "vitest"; |
4 | 4 | import * as apiModule from "#/api/api"; |
5 | 5 | import { API } from "#/api/api"; |
6 | 6 | import { workspaceByIdKey } from "#/api/queries/workspaces"; |
| 7 | +import type { ProvisionerJobLog } from "#/api/typesGenerated"; |
7 | 8 | import { |
8 | 9 | MockStoppedWorkspace, |
9 | 10 | MockStoppingWorkspace, |
| 11 | + MockWorkspace, |
| 12 | + MockWorkspaceBuildLogs, |
10 | 13 | } from "#/testHelpers/entities"; |
11 | 14 | import { createTestQueryClient } from "#/testHelpers/renderHelpers"; |
12 | 15 | import { createMockWebSocket } from "#/testHelpers/websockets"; |
@@ -49,4 +52,42 @@ describe("WorkspaceBuildLogSection", () => { |
49 | 52 | } |
50 | 53 | }, |
51 | 54 | ); |
| 55 | + |
| 56 | + it("does not scroll the transcript as the build log streams and completes", async () => { |
| 57 | + const scrollIntoView = vi.spyOn(HTMLElement.prototype, "scrollIntoView"); |
| 58 | + let publishLog: ((log: ProvisionerJobLog) => void) | undefined; |
| 59 | + vi.spyOn(apiModule, "watchBuildLogsByBuildId").mockImplementation( |
| 60 | + (_buildId, { onMessage }) => { |
| 61 | + publishLog = onMessage; |
| 62 | + return createMockWebSocket("ws://test")[0]; |
| 63 | + }, |
| 64 | + ); |
| 65 | + vi.spyOn(API, "getWorkspaceBuildLogs").mockResolvedValue( |
| 66 | + MockWorkspaceBuildLogs, |
| 67 | + ); |
| 68 | + const buildId = MockWorkspace.latest_build.id; |
| 69 | + const queryClient = createTestQueryClient(); |
| 70 | + const ui = ( |
| 71 | + props: React.ComponentProps<typeof WorkspaceBuildLogSection>, |
| 72 | + ) => ( |
| 73 | + <QueryClientProvider client={queryClient}> |
| 74 | + <ChatWorkspaceContext |
| 75 | + value={{ workspaceId: MockWorkspace.id, buildId }} |
| 76 | + > |
| 77 | + <WorkspaceBuildLogSection {...props} /> |
| 78 | + </ChatWorkspaceContext> |
| 79 | + </QueryClientProvider> |
| 80 | + ); |
| 81 | + |
| 82 | + const { rerender } = render(ui({ status: "running" })); |
| 83 | + act(() => publishLog?.(MockWorkspaceBuildLogs[0])); |
| 84 | + act(() => publishLog?.(MockWorkspaceBuildLogs[1])); |
| 85 | + |
| 86 | + // The completed call's fetched logs replace the streamed ones. |
| 87 | + rerender(ui({ status: "completed", buildId })); |
| 88 | + await screen.findByText(/Apply complete!/); |
| 89 | + |
| 90 | + // scrollIntoView also scrolls the chat transcript. |
| 91 | + expect(scrollIntoView).not.toHaveBeenCalled(); |
| 92 | + }); |
52 | 93 | }); |
0 commit comments