Roo-Code/src/integrations/editor/__tests__/DiffViewProvider.spec.ts
Chris Estreich 62c3914034
Farewell jest (#4607)
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
2025-06-16 21:39:45 -07:00

96 lines
2.7 KiB
TypeScript

import { DiffViewProvider } from "../DiffViewProvider"
import * as vscode from "vscode"
// Mock vscode
vi.mock("vscode", () => ({
workspace: {
applyEdit: vi.fn(),
},
window: {
createTextEditorDecorationType: vi.fn(),
},
WorkspaceEdit: vi.fn().mockImplementation(() => ({
replace: vi.fn(),
delete: vi.fn(),
})),
Range: vi.fn(),
Position: vi.fn(),
Selection: vi.fn(),
TextEditorRevealType: {
InCenter: 2,
},
}))
// Mock DecorationController
vi.mock("../DecorationController", () => ({
DecorationController: vi.fn().mockImplementation(() => ({
setActiveLine: vi.fn(),
updateOverlayAfterLine: vi.fn(),
clear: vi.fn(),
})),
}))
describe("DiffViewProvider", () => {
let diffViewProvider: DiffViewProvider
const mockCwd = "/mock/cwd"
let mockWorkspaceEdit: { replace: any; delete: any }
beforeEach(() => {
vi.clearAllMocks()
mockWorkspaceEdit = {
replace: vi.fn(),
delete: vi.fn(),
}
vi.mocked(vscode.WorkspaceEdit).mockImplementation(() => mockWorkspaceEdit as any)
diffViewProvider = new DiffViewProvider(mockCwd)
// Mock the necessary properties and methods
;(diffViewProvider as any).relPath = "test.txt"
;(diffViewProvider as any).activeDiffEditor = {
document: {
uri: { fsPath: `${mockCwd}/test.txt` },
getText: vi.fn(),
lineCount: 10,
},
selection: {
active: { line: 0, character: 0 },
anchor: { line: 0, character: 0 },
},
edit: vi.fn().mockResolvedValue(true),
revealRange: vi.fn(),
}
;(diffViewProvider as any).activeLineController = { setActiveLine: vi.fn(), clear: vi.fn() }
;(diffViewProvider as any).fadedOverlayController = { updateOverlayAfterLine: vi.fn(), clear: vi.fn() }
})
describe("update method", () => {
it("should preserve empty last line when original content has one", async () => {
;(diffViewProvider as any).originalContent = "Original content\n"
await diffViewProvider.update("New content", true)
expect(mockWorkspaceEdit.replace).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
"New content\n",
)
})
it("should not add extra newline when accumulated content already ends with one", async () => {
;(diffViewProvider as any).originalContent = "Original content\n"
await diffViewProvider.update("New content\n", true)
expect(mockWorkspaceEdit.replace).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
"New content\n",
)
})
it("should not add newline when original content does not end with one", async () => {
;(diffViewProvider as any).originalContent = "Original content"
await diffViewProvider.update("New content", true)
expect(mockWorkspaceEdit.replace).toHaveBeenCalledWith(expect.anything(), expect.anything(), "New content")
})
})
})